quiz for ch.2: recursion - tarleton state university · hamiltonian circuit (a.k.a. cycle) =...

43
QUIZ for Ch.2: Recursion We always start by thoroughly understanding the problem. The best way to do this is to try out a few particular cases: What is the number of ways when the staircase has: 0(?), 1, 2, 3, 4 steps?

Upload: others

Post on 28-Jun-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: QUIZ for Ch.2: Recursion - Tarleton State University · Hamiltonian Circuit (a.k.a. Cycle) = Circuit/cycle that passes ... • What is an Eulerian circuit? 3.5 Two important exhaustive

QUIZ for Ch.2: Recursion

We always start by thoroughly understanding the problem. The best way to do this is to try out a few particular cases:What is the number of ways when the staircase has: 0(?), 1, 2, 3, 4 steps?

Page 2: QUIZ for Ch.2: Recursion - Tarleton State University · Hamiltonian Circuit (a.k.a. Cycle) = Circuit/cycle that passes ... • What is an Eulerian circuit? 3.5 Two important exhaustive

QUIZ for Ch.2: Fibonacci numbers

Quickly calculate F(20).

Hint: Use Binet’s formula!

Page 3: QUIZ for Ch.2: Recursion - Tarleton State University · Hamiltonian Circuit (a.k.a. Cycle) = Circuit/cycle that passes ... • What is an Eulerian circuit? 3.5 Two important exhaustive

What is the number of ways when the staircase has: 1, 2, 3, 4 steps?

# of steps 0 1 2 3 4

# of ways 1 (?) 1 2 3 5

The On-Line Encyclopedia of Integer Sequences https://oeis.org/

Page 4: QUIZ for Ch.2: Recursion - Tarleton State University · Hamiltonian Circuit (a.k.a. Cycle) = Circuit/cycle that passes ... • What is an Eulerian circuit? 3.5 Two important exhaustive

# of steps 0 1 2 3 4

# of ways 1 (?) 1 2 3 5

How do we prove that this pattern holds for any n?Let us examine the general case:

Therefore S(n) = S(n-1) + S(n-2)

Page 5: QUIZ for Ch.2: Recursion - Tarleton State University · Hamiltonian Circuit (a.k.a. Cycle) = Circuit/cycle that passes ... • What is an Eulerian circuit? 3.5 Two important exhaustive

Ch.3Brute Force

Exhaustive Search

Page 6: QUIZ for Ch.2: Recursion - Tarleton State University · Hamiltonian Circuit (a.k.a. Cycle) = Circuit/cycle that passes ... • What is an Eulerian circuit? 3.5 Two important exhaustive

Brute Force vs Exhaustive Search

What is the difference?A better name is naïve or straightforward … but, yes, it usually involves much more effort (force) than other,

more sophisticated algorithms!

Page 7: QUIZ for Ch.2: Recursion - Tarleton State University · Hamiltonian Circuit (a.k.a. Cycle) = Circuit/cycle that passes ... • What is an Eulerian circuit? 3.5 Two important exhaustive

Example of Brute Force algorithmfrom Ch.1

Page 8: QUIZ for Ch.2: Recursion - Tarleton State University · Hamiltonian Circuit (a.k.a. Cycle) = Circuit/cycle that passes ... • What is an Eulerian circuit? 3.5 Two important exhaustive

3.1 Selection Sort and Bubble Sort

Use the links provided on our webpage to visualize the operation of this and other sorting algorithms!

Page 9: QUIZ for Ch.2: Recursion - Tarleton State University · Hamiltonian Circuit (a.k.a. Cycle) = Circuit/cycle that passes ... • What is an Eulerian circuit? 3.5 Two important exhaustive

Fill in the missing steps!

∈ Θ(n2)

Page 10: QUIZ for Ch.2: Recursion - Tarleton State University · Hamiltonian Circuit (a.k.a. Cycle) = Circuit/cycle that passes ... • What is an Eulerian circuit? 3.5 Two important exhaustive

The large elements

“bubble” through the array.

Page 11: QUIZ for Ch.2: Recursion - Tarleton State University · Hamiltonian Circuit (a.k.a. Cycle) = Circuit/cycle that passes ... • What is an Eulerian circuit? 3.5 Two important exhaustive

If we count the comparisons, we have exactly the same number as for Selection Sort:

However, the number of swaps is quite different!

Page 12: QUIZ for Ch.2: Recursion - Tarleton State University · Hamiltonian Circuit (a.k.a. Cycle) = Circuit/cycle that passes ... • What is an Eulerian circuit? 3.5 Two important exhaustive

Figure out the best and worst case for the numbers of swaps in each algorithm!

Page 13: QUIZ for Ch.2: Recursion - Tarleton State University · Hamiltonian Circuit (a.k.a. Cycle) = Circuit/cycle that passes ... • What is an Eulerian circuit? 3.5 Two important exhaustive

Best and worst cases for the numbers of swaps:

Best = Worst = n-1∈ Θ(n) Best (already sorted) = 0 Worst (reverse sorted) =

Page 14: QUIZ for Ch.2: Recursion - Tarleton State University · Hamiltonian Circuit (a.k.a. Cycle) = Circuit/cycle that passes ... • What is an Eulerian circuit? 3.5 Two important exhaustive

Both sorting algs. presented are brute-force – they implement straightforward, unsophisticated strategies of “introducing order” in the array.

Often, a brute-force alg. can be improved very easily.Example: In Bubble Sort, if no swaps were made during an execution of the inner loop, then …

Page 15: QUIZ for Ch.2: Recursion - Tarleton State University · Hamiltonian Circuit (a.k.a. Cycle) = Circuit/cycle that passes ... • What is an Eulerian circuit? 3.5 Two important exhaustive

3.1 Individual work for next time (in notebook!)

End-of-section exercises 8, 9, 11, 13.

Page 16: QUIZ for Ch.2: Recursion - Tarleton State University · Hamiltonian Circuit (a.k.a. Cycle) = Circuit/cycle that passes ... • What is an Eulerian circuit? 3.5 Two important exhaustive

3.2 Sequential SearchBrute-Force String Matching

Read and take notes for next time!

3.3 Closest-PairConvex Hull

SKIP!

Page 17: QUIZ for Ch.2: Recursion - Tarleton State University · Hamiltonian Circuit (a.k.a. Cycle) = Circuit/cycle that passes ... • What is an Eulerian circuit? 3.5 Two important exhaustive

What is the number of comparisons performed by this “super-brute-force” Bubble Sort algorithm?

How does it compare with the one we studied previously (p.100 of our text)?

What is its “theta”? How does it compare with the one we studied previously (p.100 of our text)?

QUIZ: Bubble Sort

for i ←0 to n − 2 dofor j ←0 to n − 2 do

if A[j + 1]<A[j ] swap A[j ] and A[j + 1]

Page 18: QUIZ for Ch.2: Recursion - Tarleton State University · Hamiltonian Circuit (a.k.a. Cycle) = Circuit/cycle that passes ... • What is an Eulerian circuit? 3.5 Two important exhaustive

QUIZ: Finding the order of growth of an unknown

sorting alg.

What is the number of comparisons performed by this unknown sorting algorithm?What is theta?

Page 19: QUIZ for Ch.2: Recursion - Tarleton State University · Hamiltonian Circuit (a.k.a. Cycle) = Circuit/cycle that passes ... • What is an Eulerian circuit? 3.5 Two important exhaustive

3.4 Exhaustive Search

Note that although the idea of exhaustive search is quite straightforward, its implementation typically requires an(other) algorithm for generating certain combinatorial objects. We delay a discussion of such algorithms until the next chapter and assume here that they exist.

Page 20: QUIZ for Ch.2: Recursion - Tarleton State University · Hamiltonian Circuit (a.k.a. Cycle) = Circuit/cycle that passes ... • What is an Eulerian circuit? 3.5 Two important exhaustive

Traveling Salesman Problem (TSP)Shortest Hamiltonian Circuit

Find the shortest path through a given set of n cities that visits each city exactly once before returning to the city where it started.Hamiltonian Circuit (a.k.a. Cycle) = Circuit/cycle that passes through all the vertices of the graph exactly once.Shortest H. Circuit solves TSP!

Exhaustive search algorithm: Generate all permutations of the edges of the graph, and then examine each one in turn …

Need a separate algorithm to generate all permutations!

Are the graph edges uni- or bidirectional?

Page 21: QUIZ for Ch.2: Recursion - Tarleton State University · Hamiltonian Circuit (a.k.a. Cycle) = Circuit/cycle that passes ... • What is an Eulerian circuit? 3.5 Two important exhaustive

Traveling Salesman Problem (TSP)Shortest Hamiltonian Circuit

Exhaustive search algorithm: Generate all permutations of the edges of the graph, and then examine each one in turn.

Size of input is nr. of cities, n.Because it is a cycle, we can start with an arbitrary city, which leaves only the remaining n-1 P(n) = (n-1)!

If we then realize that each tour has two possible directions, we have P(n) = (n-1)!/2. (Continues in end-of-section problem below)

Page 22: QUIZ for Ch.2: Recursion - Tarleton State University · Hamiltonian Circuit (a.k.a. Cycle) = Circuit/cycle that passes ... • What is an Eulerian circuit? 3.5 Two important exhaustive

TSP Quiz

Page 23: QUIZ for Ch.2: Recursion - Tarleton State University · Hamiltonian Circuit (a.k.a. Cycle) = Circuit/cycle that passes ... • What is an Eulerian circuit? 3.5 Two important exhaustive

TSP Quiz

Page 24: QUIZ for Ch.2: Recursion - Tarleton State University · Hamiltonian Circuit (a.k.a. Cycle) = Circuit/cycle that passes ... • What is an Eulerian circuit? 3.5 Two important exhaustive

TSP Quiz

Page 25: QUIZ for Ch.2: Recursion - Tarleton State University · Hamiltonian Circuit (a.k.a. Cycle) = Circuit/cycle that passes ... • What is an Eulerian circuit? 3.5 Two important exhaustive

TSP Quiz

n = 16i.

iv. n = 21

Page 26: QUIZ for Ch.2: Recursion - Tarleton State University · Hamiltonian Circuit (a.k.a. Cycle) = Circuit/cycle that passes ... • What is an Eulerian circuit? 3.5 Two important exhaustive

Knapsack Problem

Given n items of known weights w1, w2, . . . , wn and values v1, v2, . . . , vn and a knapsack of capacity W, find the most valuable subset of the items that fit into the knapsack.

Exhaustive search algorithm: Generate all subsets of n items, and then find the one with the highest value.

Need a separate algorithm to generate all subsets!

Page 27: QUIZ for Ch.2: Recursion - Tarleton State University · Hamiltonian Circuit (a.k.a. Cycle) = Circuit/cycle that passes ... • What is an Eulerian circuit? 3.5 Two important exhaustive

Assignment Problem

There are n people who need to be assigned to execute n jobs, one person per job. The cost that would accrue if the ith person is assigned to the jth job is a known quantity C[i, j ] for each pair i, j = 1, 2, . . . , n. C is called the cost matrix.Find an assignment with the minimum total cost.

Page 28: QUIZ for Ch.2: Recursion - Tarleton State University · Hamiltonian Circuit (a.k.a. Cycle) = Circuit/cycle that passes ... • What is an Eulerian circuit? 3.5 Two important exhaustive

Assignment Problem

Find an assignment with the minimum total cost.

Exhaustive search algorithm: Generate all permutations of n indices. For each permutation (j1, …, ji, …, jn) pick for each row (person) i the job ji and add up the costs. Select the permutation with the minimum cost.

Page 29: QUIZ for Ch.2: Recursion - Tarleton State University · Hamiltonian Circuit (a.k.a. Cycle) = Circuit/cycle that passes ... • What is an Eulerian circuit? 3.5 Two important exhaustive

3.4 Individual work for next time (in notebook!)

End-of-section exercise 3.• What is an Eulerian circuit?

Page 30: QUIZ for Ch.2: Recursion - Tarleton State University · Hamiltonian Circuit (a.k.a. Cycle) = Circuit/cycle that passes ... • What is an Eulerian circuit? 3.5 Two important exhaustive

3.5 Two important exhaustive search algs. for graphs: DFS and BFS

Note: Starting vertex can be chosen arbitrarily, although in practice there will be an “entry point” to the graph.

Page 31: QUIZ for Ch.2: Recursion - Tarleton State University · Hamiltonian Circuit (a.k.a. Cycle) = Circuit/cycle that passes ... • What is an Eulerian circuit? 3.5 Two important exhaustive

DFS

The size of a graph is a little tricky to measure, because there are two important parameters: |V| and |E|. Depending how the graph is represented, they can play different roles.

Page 32: QUIZ for Ch.2: Recursion - Tarleton State University · Hamiltonian Circuit (a.k.a. Cycle) = Circuit/cycle that passes ... • What is an Eulerian circuit? 3.5 Two important exhaustive

Graph representations (Sec. 1.4 of text)

Page 33: QUIZ for Ch.2: Recursion - Tarleton State University · Hamiltonian Circuit (a.k.a. Cycle) = Circuit/cycle that passes ... • What is an Eulerian circuit? 3.5 Two important exhaustive

DFS

Adjacency matrix: Θ(|V|2)Adjacency list: Θ(|V| + |E|)

Compare? … see next slide

Determining efficiency:The basic operation is “visiting a node”.

Page 34: QUIZ for Ch.2: Recursion - Tarleton State University · Hamiltonian Circuit (a.k.a. Cycle) = Circuit/cycle that passes ... • What is an Eulerian circuit? 3.5 Two important exhaustive

DFS efficiency

Adjacency matrix: Θ(|V|2)Adjacency list: Θ(|V| + |E|)

What is |E| as a function of |V| in the worst case?

(undirected graph)

Page 35: QUIZ for Ch.2: Recursion - Tarleton State University · Hamiltonian Circuit (a.k.a. Cycle) = Circuit/cycle that passes ... • What is an Eulerian circuit? 3.5 Two important exhaustive

DFS

Important applications of DFS:• Checking is a graph is connected• Checking if a graph has cycles• Finding articulation points in a graph

Page 36: QUIZ for Ch.2: Recursion - Tarleton State University · Hamiltonian Circuit (a.k.a. Cycle) = Circuit/cycle that passes ... • What is an Eulerian circuit? 3.5 Two important exhaustive

BFSFor implementing BFS, the most natural data structure is a queue.

Page 37: QUIZ for Ch.2: Recursion - Tarleton State University · Hamiltonian Circuit (a.k.a. Cycle) = Circuit/cycle that passes ... • What is an Eulerian circuit? 3.5 Two important exhaustive

Fine point: Four types of edges in DFS and BFS

Not in this chapter, but required!

• Tree edge• Forward edge (to a

descendant)• Backward edge (to an

ancestor)• Cross edge (54)

Page 38: QUIZ for Ch.2: Recursion - Tarleton State University · Hamiltonian Circuit (a.k.a. Cycle) = Circuit/cycle that passes ... • What is an Eulerian circuit? 3.5 Two important exhaustive

Fine point: Four types of edges in DFS and BFS • Tree edge

• Forward edge (to a descendant)

• Backward edge (to an ancestor)

• Cross edge (54)

For undirected graphs:• DFS never generates fwd.

or cross edges (Why?)• BFS never generates fwd.

or bkd. edges (Why?)

Not in this chapter, but required!

Page 39: QUIZ for Ch.2: Recursion - Tarleton State University · Hamiltonian Circuit (a.k.a. Cycle) = Circuit/cycle that passes ... • What is an Eulerian circuit? 3.5 Two important exhaustive

Proof:• Cross edge cannot be forward: Assume by contradiction that,

say, g to i is fwd. …

I.e. no forward or backward!

Then i should have been listed next to h and j, because it is directly connected to g – contradiction!

Find a similar proof for backward edges!

Page 40: QUIZ for Ch.2: Recursion - Tarleton State University · Hamiltonian Circuit (a.k.a. Cycle) = Circuit/cycle that passes ... • What is an Eulerian circuit? 3.5 Two important exhaustive

BFS

Efficiency is the same as DFS:Adjacency matrix: Θ(|V|2)Adjacency list: Θ(|V| + |E|)

Page 41: QUIZ for Ch.2: Recursion - Tarleton State University · Hamiltonian Circuit (a.k.a. Cycle) = Circuit/cycle that passes ... • What is an Eulerian circuit? 3.5 Two important exhaustive

BFS

Important applications of BFS:• Checking is a graph is connected• Checking if a graph has cycles• Finding shortest paths in a(n unweighted) graph

Counting number of

hops

Page 42: QUIZ for Ch.2: Recursion - Tarleton State University · Hamiltonian Circuit (a.k.a. Cycle) = Circuit/cycle that passes ... • What is an Eulerian circuit? 3.5 Two important exhaustive
Page 43: QUIZ for Ch.2: Recursion - Tarleton State University · Hamiltonian Circuit (a.k.a. Cycle) = Circuit/cycle that passes ... • What is an Eulerian circuit? 3.5 Two important exhaustive

Homework for Ch.3- due next Wed., Feb. 12 -

• Sec. 3.1 (p.102): 4, 10– Note for 4: Count only multiplications!– Note for 10: In a linked list, we can only access the head of the list and

the next element in the list!

• Sec. 3.4 (p.120): 4– Note: The number of permutations of 4 jobs is 4! = 24.

• Sec. 3.5 (p.128): 1, 4, 10– Note for 10: Draw a graph whose vertices are specific places in the

maze! There should not be more than 25 vertices.