QUIZ for Ch.2: Recursion - Tarleton State University · Hamiltonian Circuit (a.k.a. Cycle) =...

Preview:

Citation preview

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?

QUIZ for Ch.2: Fibonacci numbers

Quickly calculate F(20).

Hint: Use Binet’s formula!

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/

# 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)

Ch.3Brute Force

Exhaustive Search

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!

Example of Brute Force algorithmfrom Ch.1

3.1 Selection Sort and Bubble Sort

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

Fill in the missing steps!

∈ Θ(n2)

The large elements

“bubble” through the array.

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

However, the number of swaps is quite different!

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

Best and worst cases for the numbers of swaps:

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

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 …

3.1 Individual work for next time (in notebook!)

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

3.2 Sequential SearchBrute-Force String Matching

Read and take notes for next time!

3.3 Closest-PairConvex Hull

SKIP!

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]

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?

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.

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?

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)

TSP Quiz

TSP Quiz

TSP Quiz

TSP Quiz

n = 16i.

iv. n = 21

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!

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.

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.

3.4 Individual work for next time (in notebook!)

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

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.

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.

Graph representations (Sec. 1.4 of text)

DFS

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

Compare? … see next slide

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

DFS efficiency

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

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

(undirected graph)

DFS

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

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

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)

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!

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!

BFS

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

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

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.

Recommended