CS 350 Algorithms and Complexity - Computer Action...

128
CS 350 Algorithms and Complexity Lecture 6: Exhaustive Search Algorithms Andrew P. Black Department of Computer Science Portland State University Winter 2019

Transcript of CS 350 Algorithms and Complexity - Computer Action...

Page 1: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

CS 350 Algorithms and Complexity

Lecture 6: Exhaustive Search Algorithms

Andrew P. Black

Department of Computer Science

Portland State University

Winter 2019

Page 2: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Brute Force• A straightforward approach, usually based

directly on the problem’s statement and definitions of the concepts involved

• Examples:Computing an (a > 0, n a nonnegative integer) by repeated multiplicationComputing n! by repeated multiplicationMultiplying two matrices following the definitionSearching for a key in a list sequentially

2

Page 3: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Examples of Brute-Force String Matching

• Pattern: 001011 Text: 10010101101001100101111010

• Pattern: happy Text: It is never too late to have a happy childhood.

• 3

Page 4: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Examples of Brute-Force String Matching

• Pattern: 001011 Text: 10010101101001100101111010

• Pattern: happy Text: It is never too late to have a happy childhood.

• 3

Page 5: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Examples of Brute-Force String Matching

• Pattern: 001011 Text: 10010101101001100101111010

• Pattern: happy Text: It is never too late to have a happy childhood.

• 3

Page 6: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Examples of Brute-Force String Matching

• Pattern: 001011 Text: 10010101101001100101111010

• Pattern: happy Text: It is never too late to have a happy childhood.

• 3

Page 7: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Examples of Brute-Force String Matching

• Pattern: 001011 Text: 10010101101001100101111010

• Pattern: happy Text: It is never too late to have a happy childhood.

• 3

Page 8: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Examples of Brute-Force String Matching

• Pattern: 001011 Text: 10010101101001100101111010

• Pattern: happy Text: It is never too late to have a happy childhood.

• 3

Page 9: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Examples of Brute-Force String Matching

• Pattern: 001011 Text: 10010101101001100101111010

• Pattern: happy Text: It is never too late to have a happy childhood.

• 3

Page 10: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Examples of Brute-Force String Matching

• Pattern: 001011 Text: 10010101101001100101111010

• Pattern: happy Text: It is never too late to have a happy childhood.

• 3

Page 11: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Examples of Brute-Force String Matching

• Pattern: 001011 Text: 10010101101001100101111010

• Pattern: happy Text: It is never too late to have a happy childhood.

• 3

Page 12: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Examples of Brute-Force String Matching

• Pattern: 001011 Text: 10010101101001100101111010

• Pattern: happy Text: It is never too late to have a happy childhood.

• 3

Page 13: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Examples of Brute-Force String Matching

• Pattern: 001011 Text: 10010101101001100101111010

• Pattern: happy Text: It is never too late to have a happy childhood.

• 3

Page 14: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Pseudocode and Efficiency

4

Page 15: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Pseudocode and Efficiency

4

Page 16: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Pseudocode and Efficiency

Efficiency: A: O(n) B: O(m(n-m)) C: O(m) D: O(m2) 4

Page 17: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Brute-Force Polynomial Evaluation

5

Page 18: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Brute-Force Polynomial Evaluation• Problem: Find the value of polynomial

p(x) = anxn + an-1xn-1 +… + a1x1 + a0 at a point x = x0

5

Page 19: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Brute-Force Polynomial Evaluation• Problem: Find the value of polynomial

p(x) = anxn + an-1xn-1 +… + a1x1 + a0 at a point x = x0

• Brute-force algorithm p ← 0.0for i ← n downto 0 do power ← 1 for j ← 1 to i do  //compute xi power ← power ∗ x p ← p + a[i] ∗ power return p

5

Page 20: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Brute-Force Polynomial Evaluation• Problem: Find the value of polynomial

p(x) = anxn + an-1xn-1 +… + a1x1 + a0 at a point x = x0

• Brute-force algorithm

• Efficiency:

p ← 0.0for i ← n downto 0 do power ← 1 for j ← 1 to i do  //compute xi power ← power ∗ x p ← p + a[i] ∗ power return p

5

Page 21: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Brute-Force Polynomial Evaluation• Problem: Find the value of polynomial

p(x) = anxn + an-1xn-1 +… + a1x1 + a0 at a point x = x0

• Brute-force algorithm

• Efficiency:

p ← 0.0for i ← n downto 0 do power ← 1 for j ← 1 to i do  //compute xi power ← power ∗ x p ← p + a[i] ∗ power return p

5

A: O(n) B: O(n2) C: O(lg n) D: O(n3)

Page 22: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Polynomial Evaluation: Improvement

6

Page 23: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Polynomial Evaluation: Improvement

• We can do better by evaluating from right to left:

6

Page 24: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Polynomial Evaluation: Improvement

• We can do better by evaluating from right to left:

• Better brute-force algorithm:

6

Page 25: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Polynomial Evaluation: Improvement

• We can do better by evaluating from right to left:

• Better brute-force algorithm:

6

Page 26: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Polynomial Evaluation: Improvement

• We can do better by evaluating from right to left:

• Better brute-force algorithm:p ← a[0] power ← 1 for i ← 1 to n do power ← power ∗ x p ← p + a[i] ∗ power return p

6

Page 27: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Polynomial Evaluation: Improvement

• We can do better by evaluating from right to left:

• Better brute-force algorithm:p ← a[0] power ← 1 for i ← 1 to n do power ← power ∗ x p ← p + a[i] ∗ power return p

6

Page 28: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Polynomial Evaluation: Improvement

• We can do better by evaluating from right to left:

• Better brute-force algorithm:

• Efficiency:

p ← a[0] power ← 1 for i ← 1 to n do power ← power ∗ x p ← p + a[i] ∗ power return p

6

Page 29: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Polynomial Evaluation: Improvement

• We can do better by evaluating from right to left:

• Better brute-force algorithm:

• Efficiency:

p ← a[0] power ← 1 for i ← 1 to n do power ← power ∗ x p ← p + a[i] ∗ power return p

6

A: O(n) B: O(n2) C: O(lg n) D: O(n3)

Page 30: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Closest-Pair Problem

• Find the two closest points in a set of n points (in the two-dimensional Cartesian plane).

• Brute-force algorithm:‣ Compute the distance between every pair of

distinct points° and return the indices of the points for which the

distance is the smallest.

7

Page 31: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Closest-Pair Brute-Force Algorithm (cont.)

8

Page 32: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Closest-Pair Brute-Force Algorithm (cont.)

• Efficiency:

8

Page 33: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Closest-Pair Brute-Force Algorithm (cont.)

• Efficiency:

8

A: O(n) B: O(n2) C: O(lg n) D: O(n3)

Page 34: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Closest-Pair Brute-Force Algorithm (cont.)

• Efficiency:

• How to make it faster? 8

A: O(n) B: O(n2) C: O(lg n) D: O(n3)

Page 35: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Problem:

If sqrtis 10 x slower than × and +, by how much will BruteForceClosestPoints speed up when we take out the sqrt ?

A. ~ 10 timesB. ~ 100 timesC. ~ 1000 times

9

Page 36: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Problem:Can you design a more efficient algorithm than the one based on the brute-force strategy to solve the closest-pair problem for n points x1, … , xn on the real line?

10

x2x3 x1 x5 x4

Page 37: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Brute Force Closest Pair

• An Example of a particular kind of Brute Force Algorithm based on:Exhaustive search

11

Page 38: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Exhaustive Search• A brute force solution to a problem involving

search for an element with a special property, usually among combinatorial objects such as permutations, combinations, or subsets of a set.

• Method:‣ generate a list of all potential solutions to the problem in a

systematic manner (see algorithms in Sec. 4.3)

‣ evaluate potential solutions one by one, disqualifying infeasible ones and, for an optimization problem, keeping track of the best one found so far

‣ when search ends, announce the solution(s) found

12

Page 39: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Example 1: Traveling Salesman Problem

• Given n cities with known distances between each pair, find the shortest tour that passes through all the cities exactly once before returning to the starting city

• Alternatively: find shortest Hamiltonian circuit in a weighted connected graph

• Example:

a b

c d

8

2

7

5 34

13

Page 40: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

TSP by Exhaustive Search Tour Cost a→b→c→d→a 2+3+7+5 = 17a→b→d→c→a 2+4+7+8 = 21a→c→b→d→a 8+3+4+5 = 20a→c→d→b→a 8+7+4+2 = 21a→d→b→c→a 5+4+3+8 = 20a→d→c→b→a 5+7+3+2 = 17

More tours?Less tours?Efficiency:

14

a b

c d

8

2

7

5 34

Page 41: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

TSP by Exhaustive Search Tour Cost a→b→c→d→a 2+3+7+5 = 17a→b→d→c→a 2+4+7+8 = 21a→c→b→d→a 8+3+4+5 = 20a→c→d→b→a 8+7+4+2 = 21a→d→b→c→a 5+4+3+8 = 20a→d→c→b→a 5+7+3+2 = 17

More tours?Less tours?Efficiency:

14

a b

c d

8

2

7

5 34

A: O(n)B: O(n2) C: O(n3)

D: O((n-1)!)E: O(n!)

Page 42: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Example 2: Knapsack Problem• Given n items:

‣ weights: w1 w2 … wn

‣ values: v1 v2 … vn

‣ a knapsack of capacity W

• Find most valuable subset of the items that fit into the knapsack

• Example: Knapsack capacity W=16

15

item      weight value

1. 2 $20

2. 5 $30

3. 10 $50

4. 5 $10

Page 43: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Knapsack Problem by Exhaustive SearchSubset Total weight Total value {1} 2 $20 {2} 5 $30 {3} 10 $50 {4} 5 $10 {1,2} 7 $50 {1,3} 12 $70 {1,4} 7 $30 {2,3} 15 $80 {2,4} 10 $40 {3,4} 15 $60{1,2,3} 17 infeasible{1,2,4} 12 $60{1,3,4} 17 infeasible{2,3,4} 20 infeasible

{1,2,3,4} 22 infeasible

16

item      weight value

1. 2 $20

2. 5 $30

3. 10 $50

4. 5 $10

Knapsack capacity W=16

Page 44: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Knapsack Problem by Exhaustive SearchSubset Total weight Total value {1} 2 $20 {2} 5 $30 {3} 10 $50 {4} 5 $10 {1,2} 7 $50 {1,3} 12 $70 {1,4} 7 $30 {2,3} 15 $80 {2,4} 10 $40 {3,4} 15 $60{1,2,3} 17 infeasible{1,2,4} 12 $60{1,3,4} 17 infeasible{2,3,4} 20 infeasible

{1,2,3,4} 22 infeasible

16

item      weight value

1. 2 $20

2. 5 $30

3. 10 $50

4. 5 $10

Knapsack capacity W=16

Page 45: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Knapsack Problem by Exhaustive SearchSubset Total weight Total value {1} 2 $20 {2} 5 $30 {3} 10 $50 {4} 5 $10 {1,2} 7 $50 {1,3} 12 $70 {1,4} 7 $30 {2,3} 15 $80 {2,4} 10 $40 {3,4} 15 $60{1,2,3} 17 infeasible{1,2,4} 12 $60{1,3,4} 17 infeasible{2,3,4} 20 infeasible

{1,2,3,4} 22 infeasible

16

item      weight value

1. 2 $20

2. 5 $30

3. 10 $50

4. 5 $10

Knapsack capacity W=16

• Efficiency? A: O(n2) B: O(2n) C: O(n!)D: O((n-1)!)

Page 46: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Example 3: The Assignment Problem

• There are n people who need to be assigned to n jobs, one person per job. The cost of assigning person p to job j isC[ i, j ]. Find an assignment that minimizes the total cost.         Job 1 Job 2 Job 3 Job 4

Person 1 9   2 7 8

Person 2 6 4 3 7

Person 3 5 8 1 8

Person 4 7 6 9 4

• Algorithmic Plan: Generate all legitimate assignments, compute their costs, and select the cheapest one.

• How many assignments are there …

17

Page 47: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Example 3: The Assignment Problem

• There are n people who need to be assigned to n jobs, one person per job. The cost of assigning person p to job j isC[ i, j ]. Find an assignment that minimizes the total cost.         Job 1 Job 2 Job 3 Job 4

Person 1 9   2 7 8

Person 2 6 4 3 7

Person 3 5 8 1 8

Person 4 7 6 9 4

• Algorithmic Plan: Generate all legitimate assignments, compute their costs, and select the cheapest one.

• How many assignments are there …

17

an assignment ‹a1, a2, a3, a4›

means that person igets job ai

Page 48: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Example 3: The Assignment Problem

• There are n people who need to be assigned to n jobs, one person per job. The cost of assigning person p to job j isC[ i, j ]. Find an assignment that minimizes the total cost.         Job 1 Job 2 Job 3 Job 4

Person 1 9   2 7 8

Person 2 6 4 3 7

Person 3 5 8 1 8

Person 4 7 6 9 4

• Algorithmic Plan: Generate all legitimate assignments, compute their costs, and select the cheapest one.

• How many assignments are there …

17

an assignment ‹a1, a2, a3, a4›

means that person igets job ai

‹a1, a2, a3,a4›

Page 49: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Example 3: The Assignment Problem

• There are n people who need to be assigned to n jobs, one person per job. The cost of assigning person p to job j isC[ i, j ]. Find an assignment that minimizes the total cost.         Job 1 Job 2 Job 3 Job 4

Person 1 9   2 7 8

Person 2 6 4 3 7

Person 3 5 8 1 8

Person 4 7 6 9 4

• Algorithmic Plan: Generate all legitimate assignments, compute their costs, and select the cheapest one.

• How many assignments are there …

17

an assignment ‹a1, a2, a3, a4›

means that person igets job ai

‹2, 4, 3,1›

Page 50: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Example 3: The Assignment Problem

• There are n people who need to be assigned to n jobs, one person per job. The cost of assigning person p to job j isC[ i, j ]. Find an assignment that minimizes the total cost.         Job 1 Job 2 Job 3 Job 4

Person 1 9   2 7 8

Person 2 6 4 3 7

Person 3 5 8 1 8

Person 4 7 6 9 4

• Algorithmic Plan: Generate all legitimate assignments, compute their costs, and select the cheapest one.

• How many assignments are there …

17

an assignment ‹a1, a2, a3, a4›

means that person igets job ai

‹2, 4, 3,1›

Page 51: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Example 3: The Assignment Problem

• There are n people who need to be assigned to n jobs, one person per job. The cost of assigning person p to job j isC[ i, j ]. Find an assignment that minimizes the total cost.         Job 1 Job 2 Job 3 Job 4

Person 1 9   2 7 8

Person 2 6 4 3 7

Person 3 5 8 1 8

Person 4 7 6 9 4

• Algorithmic Plan: Generate all legitimate assignments, compute their costs, and select the cheapest one.

• How many assignments are there …

17

an assignment ‹a1, a2, a3, a4›

means that person igets job ai

‹2, 4, 3,1›

Page 52: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Example 3: The Assignment Problem

• There are n people who need to be assigned to n jobs, one person per job. The cost of assigning person p to job j isC[ i, j ]. Find an assignment that minimizes the total cost.         Job 1 Job 2 Job 3 Job 4

Person 1 9   2 7 8

Person 2 6 4 3 7

Person 3 5 8 1 8

Person 4 7 6 9 4

• Algorithmic Plan: Generate all legitimate assignments, compute their costs, and select the cheapest one.

• How many assignments are there …

17

an assignment ‹a1, a2, a3, a4›

means that person igets job ai

‹2, 4, 3,1›

Page 53: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Example 3: The Assignment Problem

• There are n people who need to be assigned to n jobs, one person per job. The cost of assigning person p to job j isC[ i, j ]. Find an assignment that minimizes the total cost.         Job 1 Job 2 Job 3 Job 4

Person 1 9   2 7 8

Person 2 6 4 3 7

Person 3 5 8 1 8

Person 4 7 6 9 4

• Algorithmic Plan: Generate all legitimate assignments, compute their costs, and select the cheapest one.

• How many assignments are there …

17

an assignment ‹a1, a2, a3, a4›

means that person igets job ai

‹2, 4, 3,1›

Page 54: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Assignment Problem by Exhaustive Search

18

Page 55: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

•Consider the problem in terms of the Cost Matrix C

Assignment Problem by Exhaustive Search

18

C =

2

664

9 2 7 86 4 3 75 8 1 87 6 9 4

3

775

Page 56: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

•Consider the problem in terms of the Cost Matrix C Assignment (col.#s) Total Cost 1, 2, 3, 4 9+4+1+4=18 1, 2, 4, 3 9+4+8+9=30

Assignment Problem by Exhaustive Search

18

C =

2

664

9 2 7 86 4 3 75 8 1 87 6 9 4

3

775

Page 57: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

•Consider the problem in terms of the Cost Matrix C Assignment (col.#s) Total Cost 1, 2, 3, 4 9+4+1+4=18 1, 2, 4, 3 9+4+8+9=30 1, 3, 2, 4 9+3+8+4=24

Assignment Problem by Exhaustive Search

18

C =

2

664

9 2 7 86 4 3 75 8 1 87 6 9 4

3

775

Page 58: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

•Consider the problem in terms of the Cost Matrix C Assignment (col.#s) Total Cost 1, 2, 3, 4 9+4+1+4=18 1, 2, 4, 3 9+4+8+9=30 1, 3, 2, 4 9+3+8+4=24 1, 3, 4, 2 9+3+8+6=26

Assignment Problem by Exhaustive Search

18

C =

2

664

9 2 7 86 4 3 75 8 1 87 6 9 4

3

775

Page 59: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

•Consider the problem in terms of the Cost Matrix C Assignment (col.#s) Total Cost 1, 2, 3, 4 9+4+1+4=18 1, 2, 4, 3 9+4+8+9=30 1, 3, 2, 4 9+3+8+4=24 1, 3, 4, 2 9+3+8+6=26 1, 4, 2, 3 9+7+8+9=33

Assignment Problem by Exhaustive Search

18

C =

2

664

9 2 7 86 4 3 75 8 1 87 6 9 4

3

775

Page 60: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

•Consider the problem in terms of the Cost Matrix C Assignment (col.#s) Total Cost 1, 2, 3, 4 9+4+1+4=18 1, 2, 4, 3 9+4+8+9=30 1, 3, 2, 4 9+3+8+4=24 1, 3, 4, 2 9+3+8+6=26 1, 4, 2, 3 9+7+8+9=33 1, 4, 3, 2 9+7+1+6=23

Assignment Problem by Exhaustive Search

18

C =

2

664

9 2 7 86 4 3 75 8 1 87 6 9 4

3

775

Page 61: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

•Consider the problem in terms of the Cost Matrix C Assignment (col.#s) Total Cost 1, 2, 3, 4 9+4+1+4=18 1, 2, 4, 3 9+4+8+9=30 1, 3, 2, 4 9+3+8+4=24 1, 3, 4, 2 9+3+8+6=26 1, 4, 2, 3 9+7+8+9=33 1, 4, 3, 2 9+7+1+6=23

etc.

Assignment Problem by Exhaustive Search

18

C =

2

664

9 2 7 86 4 3 75 8 1 87 6 9 4

3

775

Page 62: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

•Consider the problem in terms of the Cost Matrix C Assignment (col.#s) Total Cost 1, 2, 3, 4 9+4+1+4=18 1, 2, 4, 3 9+4+8+9=30 1, 3, 2, 4 9+3+8+4=24 1, 3, 4, 2 9+3+8+6=26 1, 4, 2, 3 9+7+8+9=33 1, 4, 3, 2 9+7+1+6=23

etc.How many assignments are there?

Assignment Problem by Exhaustive Search

18

C =

2

664

9 2 7 86 4 3 75 8 1 87 6 9 4

3

775

Page 63: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

•Consider the problem in terms of the Cost Matrix C Assignment (col.#s) Total Cost 1, 2, 3, 4 9+4+1+4=18 1, 2, 4, 3 9+4+8+9=30 1, 3, 2, 4 9+3+8+4=24 1, 3, 4, 2 9+3+8+6=26 1, 4, 2, 3 9+7+8+9=33 1, 4, 3, 2 9+7+1+6=23

etc.How many assignments are there?

Assignment Problem by Exhaustive Search

18

C =

2

664

9 2 7 86 4 3 75 8 1 87 6 9 4

3

775

A: O(n) B: O(n2) C: O(n3) D: O(n!)

Page 64: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Convex Hulls• What is a Convex Hull?

A. A bad design for a boatB. A good design for a boatC. A set of points without any concavitiesD. None of the above

Page 65: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Convex Hulls• What is a Convex Set?

A. A bad design for a boatB. A good design for a boatC. A set of points without any concavitiesD. None of the above

Page 66: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Convex Hulls

20

• What is a Convex Set?

Page 67: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Convex Hulls

20

• What is a Convex Set?

Page 68: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Convex Hulls

20

• What is a Convex Set?

Page 69: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Convex Hulls

20

• What is a Convex Set?

Page 70: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Convex Hulls

20

• What is a Convex Set?

Page 71: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Convex Hulls

20

• What is a Convex Set?

Page 72: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Convex Hulls

20

• What is a Convex Set?

Page 73: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Convex Hulls

20

• What is a Convex Set?

Page 74: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Convex Hulls

20

• What is a Convex Set?

Page 75: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Convex Hulls

20

• What is a Convex Set?

Page 76: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Convex Hulls

20

• What is a Convex Set?

Page 77: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Convex Hulls

20

• What is a Convex Set?

Page 78: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Convex Hulls

20

• What is a Convex Set?

Page 79: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Convex Hulls

20

• What is a Convex Set?

Page 80: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Convex Hulls

20

• What is a Convex Set?

Page 81: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Convex Hulls

20

• What is a Convex Set?

Page 82: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Convex HullsA set of points C is convex iff ∀ a, b ∈ C, all points on the line segment ab are entirely in C

21

Page 83: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Convex Hulls

22

Page 84: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Convex Hulls• Given an arbitrary set of points S, the

convex hull of S is the smallest convex set that contain all the points in S.

22

Page 85: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Convex Hulls• Given an arbitrary set of points S, the

convex hull of S is the smallest convex set that contain all the points in S.‣ Barricading sleeping tigers

22

Page 86: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Convex Hulls• Given an arbitrary set of points S, the

convex hull of S is the smallest convex set that contain all the points in S.‣ Barricading sleeping tigers

‣ Rubber-band around nails

22

Page 87: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Applications of Convex Hull• Collision-detection in video games

• Robot motion planning

23

Page 88: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Applications of Convex Hull• Collision-detection in video games

• Robot motion planning

23

Page 89: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Theorems about Convex Hulls• The convex hull of a set S is a convex

polygon all of whose vertices are at some of the points of S.

• A line segment ab is part of the boundary of the convex hull of S iff all the points of S lie on the same side of ab (or on ab)

24

Page 90: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Theorems about Convex Hulls• The convex hull of a set S is a convex

polygon all of whose vertices are at some of the points of S.

• A line segment ab is part of the boundary of the convex hull of S iff all the points of S lie on the same side of ab (or on ab)

24

a

b

Page 91: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Theorems about Convex Hulls• The convex hull of a set S is a convex

polygon all of whose vertices are at some of the points of S.

• A line segment ab is part of the boundary of the convex hull of S iff all the points of S lie on the same side of ab (or on ab)

24

Page 92: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Theorems about Convex Hulls• The convex hull of a set S is a convex

polygon all of whose vertices are at some of the points of S.

• A line segment ab is part of the boundary of the convex hull of S iff all the points of S lie on the same side of ab (or on ab)

24

a

b

Page 93: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Brute-Force Algorithm for Convex Hull

• write it down!‣ Assume that you have a method for

ascertaining if a point r is on a line pq, on the −ve side of line pq, or on the +ve side of pq

25

ab

+ r p

q

Page 94: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Brute-Force Algorithm for Convex Hull

• write it down!‣ Assume that you have a method for

ascertaining if a point r is on a line pq, on the −ve side of line pq, or on the +ve side of pq

25

ab

+ r p

q

r.whichSideOfLine(pq)

Page 95: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Brute-Force Algorithm for Convex Hull

• write it down!‣ Assume that you have a method for

ascertaining if a point r is on a line pq, on the −ve side of line pq, or on the +ve side of pq

25

ab

ax + by = c

+ r p

q

r.whichSideOfLine(pq)

Page 96: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Brute-Force Algorithm for Convex Hull

• write it down!‣ Assume that you have a method for

ascertaining if a point r is on a line pq, on the −ve side of line pq, or on the +ve side of pq

25

ab

ax + by = c

+ r p

q

c = px qy − qx py

r.whichSideOfLine(pq)

Page 97: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Brute-Force Algorithm for Convex Hull

edgeSet ← { }P: for p in S do:

Q: for q in S, q ≠ p do:goodSide ← 0R: for r in S, r≠p ∧ r≠q do:

side ← r.whichSideOfLine(pq)if side ≠ 0 then

if goodSide = 0 then goodSide ← side if goodSide ≠ side then exit Q.

edgeSet ← edgeSet ∪ {pq}

26

Page 98: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Final Comments on Exhaustive Search

• Exhaustive-search algorithms run in a realistic amount of time only on very small instances

• In some cases, there are much better alternatives! ‣ Euler circuits

‣ shortest paths

‣ minimum spanning tree

‣ assignment problem

• However, in many cases, exhaustive search (or a variation) is the only known way to find an exact solution

27

Page 99: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Searching in Graphs

Exhaustively search a graph, by traversing the edges, visiting every node onceTwo approaches:‣ Depth-first search and

‣ Breadth-first search

28

Page 100: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

29

3.5 Depth-First Search and Breadth-First Search 123

g

a

d

e

b

c f

j

h

h

i

j

ga

c

d f

b

ei

(a) (b) (c)

d3, 1c2, 5a1, 6

e 6, 2b 5, 3f4, 4

j10,7i9, 8h 8, 9

g 7,10

FIGURE 3.10 Example of a DFS traversal. (a) Graph. (b) Traversal’s stack (the firstsubscript number indicates the order in which a vertex is visited, i.e.,pushed onto the stack; the second one indicates the order in which itbecomes a dead-end, i.e., popped off the stack). (c) DFS forest with thetree and back edges shown with solid and dashed lines, respectively.

visit of the vertex starts), and we pop a vertex off the stack when it becomes adead end (i.e., the visit of the vertex ends).

It is also very useful to accompany a depth-first search traversal by construct-ing the so-called depth-first search forest. The starting vertex of the traversalserves as the root of the first tree in such a forest. Whenever a new unvisited vertexis reached for the first time, it is attached as a child to the vertex from which it isbeing reached. Such an edge is called a tree edge because the set of all such edgesforms a forest. The algorithm may also encounter an edge leading to a previouslyvisited vertex other than its immediate predecessor (i.e., its parent in the tree).Such an edge is called a back edge because it connects a vertex to its ancestor,other than the parent, in the depth-first search forest. Figure 3.10 provides an ex-ample of a depth-first search traversal, with the traversal stack and correspondingdepth-first search forest shown as well.

Here is pseudocode of the depth-first search.

ALGORITHM DFS(G)

//Implements a depth-first search traversal of a given graph//Input: Graph G = ⟨V, E⟩//Output: Graph G with its vertices marked with consecutive integers// in the order they are first encountered by the DFS traversalmark each vertex in V with 0 as a mark of being “unvisited”count ← 0for each vertex v in V do

if v is marked with 0dfs(v)124 Brute Force and Exhaustive Search

dfs(v)

//visits recursively all the unvisited vertices connected to vertex v

//by a path and numbers them in the order they are encountered//via global variable count

count ← count + 1; mark v with count

for each vertex w in V adjacent to v doif w is marked with 0

dfs(w)

The brevity of the DFS pseudocode and the ease with which it can be per-formed by hand may create a wrong impression about the level of sophisticationof this algorithm. To appreciate its true power and depth, you should trace thealgorithm’s action by looking not at a graph’s diagram but at its adjacency matrixor adjacency lists. (Try it for the graph in Figure 3.10 or a smaller example.)

How efficient is depth-first search? It is not difficult to see that this algorithmis, in fact, quite efficient since it takes just the time proportional to the size of thedata structure used for representing the graph in question. Thus, for the adjacencymatrix representation, the traversal time is in !(|V |2), and for the adjacency listrepresentation, it is in !(|V | + |E|) where |V | and |E| are the number of thegraph’s vertices and edges, respectively.

A DFS forest, which is obtained as a by-product of a DFS traversal, deserves afew comments, too. To begin with, it is not actually a forest. Rather, we can look atit as the given graph with its edges classified by the DFS traversal into two disjointclasses: tree edges and back edges. (No other types are possible for a DFS forestof an undirected graph.) Again, tree edges are edges used by the DFS traversal toreach previously unvisited vertices. If we consider only the edges in this class, wewill indeed get a forest. Back edges connect vertices to previously visited verticesother than their immediate predecessors in the traversal. They connect vertices totheir ancestors in the forest other than their parents.

A DFS traversal itself and the forest-like representation of the graph it pro-vides have proved to be extremely helpful for the development of efficient al-gorithms for checking many important properties of graphs.3 Note that the DFSyields two orderings of vertices: the order in which the vertices are reached for thefirst time (pushed onto the stack) and the order in which the vertices become deadends (popped off the stack). These orders are qualitatively different, and variousapplications can take advantage of either of them.

Important elementary applications of DFS include checking connectivity andchecking acyclicity of a graph. Since dfs halts after visiting all the vertices con-

3. The discovery of several such applications was an important breakthrough achieved by the twoAmerican computer scientists John Hopcroft and Robert Tarjan in the 1970s. For this and othercontributions, they were given the Turing Award—the most prestigious prize in the computing field[Hop87, Tar87].

Page 101: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

30

Example

9

1

Graph Searching

CSE 373Data Structures

Lecture 20

12/2/02 Graph Searching - Lecture 20 2

Graph Searching• Find Properties of Graphs

› Connected components› Bipartite structure› Biconnected components

• Applications› Alternating paths for matching› Garbage collection – used in Java run time system› Finding dead code › Finding the web graph – used by Google and

others

12/2/02 Graph Searching - Lecture 20 3

Depth First Search Algorithm

• Recursive marking algorithm• Initially every vertex is unmarked

DFS(i: vertex)mark i;for each j adjacent to i do

if j is unmarked then DFS(j)end{DFS}

Marks all vertices reachable from i

12/2/02 Graph Searching - Lecture 20 4

Example of Depth First Search

12

3

4

5

6

7

DFS(1)

12/2/02 Graph Searching - Lecture 20 5

Example Step 2

12

3

4

5

6

7

DFS(1)DFS(2)

12/2/02 Graph Searching - Lecture 20 6

Example Step 3

12

3

4

5

6

7

DFS(1)DFS(2)DFS(7)

8

dfs(1)

Page 102: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

31

Example

9

1

Graph Searching

CSE 373Data Structures

Lecture 20

12/2/02 Graph Searching - Lecture 20 2

Graph Searching• Find Properties of Graphs

› Connected components› Bipartite structure› Biconnected components

• Applications› Alternating paths for matching› Garbage collection – used in Java run time system› Finding dead code › Finding the web graph – used by Google and

others

12/2/02 Graph Searching - Lecture 20 3

Depth First Search Algorithm

• Recursive marking algorithm• Initially every vertex is unmarked

DFS(i: vertex)mark i;for each j adjacent to i do

if j is unmarked then DFS(j)end{DFS}

Marks all vertices reachable from i

12/2/02 Graph Searching - Lecture 20 4

Example of Depth First Search

12

3

4

5

6

7

DFS(1)

12/2/02 Graph Searching - Lecture 20 5

Example Step 2

12

3

4

5

6

7

DFS(1)DFS(2)

12/2/02 Graph Searching - Lecture 20 6

Example Step 3

12

3

4

5

6

7

DFS(1)DFS(2)DFS(7)

8

dfs(1) dfs(2)

Page 103: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

32

Example

9

1

Graph Searching

CSE 373Data Structures

Lecture 20

12/2/02 Graph Searching - Lecture 20 2

Graph Searching• Find Properties of Graphs

› Connected components› Bipartite structure› Biconnected components

• Applications› Alternating paths for matching› Garbage collection – used in Java run time system› Finding dead code › Finding the web graph – used by Google and

others

12/2/02 Graph Searching - Lecture 20 3

Depth First Search Algorithm

• Recursive marking algorithm• Initially every vertex is unmarked

DFS(i: vertex)mark i;for each j adjacent to i do

if j is unmarked then DFS(j)end{DFS}

Marks all vertices reachable from i

12/2/02 Graph Searching - Lecture 20 4

Example of Depth First Search

12

3

4

5

6

7

DFS(1)

12/2/02 Graph Searching - Lecture 20 5

Example Step 2

12

3

4

5

6

7

DFS(1)DFS(2)

12/2/02 Graph Searching - Lecture 20 6

Example Step 3

12

3

4

5

6

7

DFS(1)DFS(2)DFS(7)

8

dfs(1) dfs(2) dfs(7)

Page 104: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

33

Example

9

1

Graph Searching

CSE 373Data Structures

Lecture 20

12/2/02 Graph Searching - Lecture 20 2

Graph Searching• Find Properties of Graphs

› Connected components› Bipartite structure› Biconnected components

• Applications› Alternating paths for matching› Garbage collection – used in Java run time system› Finding dead code › Finding the web graph – used by Google and

others

12/2/02 Graph Searching - Lecture 20 3

Depth First Search Algorithm

• Recursive marking algorithm• Initially every vertex is unmarked

DFS(i: vertex)mark i;for each j adjacent to i do

if j is unmarked then DFS(j)end{DFS}

Marks all vertices reachable from i

12/2/02 Graph Searching - Lecture 20 4

Example of Depth First Search

12

3

4

5

6

7

DFS(1)

12/2/02 Graph Searching - Lecture 20 5

Example Step 2

12

3

4

5

6

7

DFS(1)DFS(2)

12/2/02 Graph Searching - Lecture 20 6

Example Step 3

12

3

4

5

6

7

DFS(1)DFS(2)DFS(7)

8

dfs(1) dfs(2) dfs(7) dfs(5)

Page 105: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

34

Example

9

1

Graph Searching

CSE 373Data Structures

Lecture 20

12/2/02 Graph Searching - Lecture 20 2

Graph Searching• Find Properties of Graphs

› Connected components› Bipartite structure› Biconnected components

• Applications› Alternating paths for matching› Garbage collection – used in Java run time system› Finding dead code › Finding the web graph – used by Google and

others

12/2/02 Graph Searching - Lecture 20 3

Depth First Search Algorithm

• Recursive marking algorithm• Initially every vertex is unmarked

DFS(i: vertex)mark i;for each j adjacent to i do

if j is unmarked then DFS(j)end{DFS}

Marks all vertices reachable from i

12/2/02 Graph Searching - Lecture 20 4

Example of Depth First Search

12

3

4

5

6

7

DFS(1)

12/2/02 Graph Searching - Lecture 20 5

Example Step 2

12

3

4

5

6

7

DFS(1)DFS(2)

12/2/02 Graph Searching - Lecture 20 6

Example Step 3

12

3

4

5

6

7

DFS(1)DFS(2)DFS(7)

8

dfs(1) dfs(2) dfs(7) dfs(5) dfs(4)

Page 106: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

35

Example

9

1

Graph Searching

CSE 373Data Structures

Lecture 20

12/2/02 Graph Searching - Lecture 20 2

Graph Searching• Find Properties of Graphs

› Connected components› Bipartite structure› Biconnected components

• Applications› Alternating paths for matching› Garbage collection – used in Java run time system› Finding dead code › Finding the web graph – used by Google and

others

12/2/02 Graph Searching - Lecture 20 3

Depth First Search Algorithm

• Recursive marking algorithm• Initially every vertex is unmarked

DFS(i: vertex)mark i;for each j adjacent to i do

if j is unmarked then DFS(j)end{DFS}

Marks all vertices reachable from i

12/2/02 Graph Searching - Lecture 20 4

Example of Depth First Search

12

3

4

5

6

7

DFS(1)

12/2/02 Graph Searching - Lecture 20 5

Example Step 2

12

3

4

5

6

7

DFS(1)DFS(2)

12/2/02 Graph Searching - Lecture 20 6

Example Step 3

12

3

4

5

6

7

DFS(1)DFS(2)DFS(7)

8

dfs(1) dfs(2) dfs(7) dfs(5) dfs(4) dfs(3)

Page 107: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

36

Example

9

1

Graph Searching

CSE 373Data Structures

Lecture 20

12/2/02 Graph Searching - Lecture 20 2

Graph Searching• Find Properties of Graphs

› Connected components› Bipartite structure› Biconnected components

• Applications› Alternating paths for matching› Garbage collection – used in Java run time system› Finding dead code › Finding the web graph – used by Google and

others

12/2/02 Graph Searching - Lecture 20 3

Depth First Search Algorithm

• Recursive marking algorithm• Initially every vertex is unmarked

DFS(i: vertex)mark i;for each j adjacent to i do

if j is unmarked then DFS(j)end{DFS}

Marks all vertices reachable from i

12/2/02 Graph Searching - Lecture 20 4

Example of Depth First Search

12

3

4

5

6

7

DFS(1)

12/2/02 Graph Searching - Lecture 20 5

Example Step 2

12

3

4

5

6

7

DFS(1)DFS(2)

12/2/02 Graph Searching - Lecture 20 6

Example Step 3

12

3

4

5

6

7

DFS(1)DFS(2)DFS(7)

8

dfs(1) dfs(2) dfs(7) dfs(5) dfs(4)

Page 108: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

36

Example

9

1

Graph Searching

CSE 373Data Structures

Lecture 20

12/2/02 Graph Searching - Lecture 20 2

Graph Searching• Find Properties of Graphs

› Connected components› Bipartite structure› Biconnected components

• Applications› Alternating paths for matching› Garbage collection – used in Java run time system› Finding dead code › Finding the web graph – used by Google and

others

12/2/02 Graph Searching - Lecture 20 3

Depth First Search Algorithm

• Recursive marking algorithm• Initially every vertex is unmarked

DFS(i: vertex)mark i;for each j adjacent to i do

if j is unmarked then DFS(j)end{DFS}

Marks all vertices reachable from i

12/2/02 Graph Searching - Lecture 20 4

Example of Depth First Search

12

3

4

5

6

7

DFS(1)

12/2/02 Graph Searching - Lecture 20 5

Example Step 2

12

3

4

5

6

7

DFS(1)DFS(2)

12/2/02 Graph Searching - Lecture 20 6

Example Step 3

12

3

4

5

6

7

DFS(1)DFS(2)DFS(7)

8

dfs(1) dfs(2) dfs(7) dfs(5) dfs(4)

Page 109: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

36

Example

9

1

Graph Searching

CSE 373Data Structures

Lecture 20

12/2/02 Graph Searching - Lecture 20 2

Graph Searching• Find Properties of Graphs

› Connected components› Bipartite structure› Biconnected components

• Applications› Alternating paths for matching› Garbage collection – used in Java run time system› Finding dead code › Finding the web graph – used by Google and

others

12/2/02 Graph Searching - Lecture 20 3

Depth First Search Algorithm

• Recursive marking algorithm• Initially every vertex is unmarked

DFS(i: vertex)mark i;for each j adjacent to i do

if j is unmarked then DFS(j)end{DFS}

Marks all vertices reachable from i

12/2/02 Graph Searching - Lecture 20 4

Example of Depth First Search

12

3

4

5

6

7

DFS(1)

12/2/02 Graph Searching - Lecture 20 5

Example Step 2

12

3

4

5

6

7

DFS(1)DFS(2)

12/2/02 Graph Searching - Lecture 20 6

Example Step 3

12

3

4

5

6

7

DFS(1)DFS(2)DFS(7)

8

dfs(1) dfs(2) dfs(7) dfs(5) dfs(4)

Page 110: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

37

Example

9

1

Graph Searching

CSE 373Data Structures

Lecture 20

12/2/02 Graph Searching - Lecture 20 2

Graph Searching• Find Properties of Graphs

› Connected components› Bipartite structure› Biconnected components

• Applications› Alternating paths for matching› Garbage collection – used in Java run time system› Finding dead code › Finding the web graph – used by Google and

others

12/2/02 Graph Searching - Lecture 20 3

Depth First Search Algorithm

• Recursive marking algorithm• Initially every vertex is unmarked

DFS(i: vertex)mark i;for each j adjacent to i do

if j is unmarked then DFS(j)end{DFS}

Marks all vertices reachable from i

12/2/02 Graph Searching - Lecture 20 4

Example of Depth First Search

12

3

4

5

6

7

DFS(1)

12/2/02 Graph Searching - Lecture 20 5

Example Step 2

12

3

4

5

6

7

DFS(1)DFS(2)

12/2/02 Graph Searching - Lecture 20 6

Example Step 3

12

3

4

5

6

7

DFS(1)DFS(2)DFS(7)

8

dfs(1) dfs(2) dfs(7) dfs(5)

Page 111: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

37

Example

9

1

Graph Searching

CSE 373Data Structures

Lecture 20

12/2/02 Graph Searching - Lecture 20 2

Graph Searching• Find Properties of Graphs

› Connected components› Bipartite structure› Biconnected components

• Applications› Alternating paths for matching› Garbage collection – used in Java run time system› Finding dead code › Finding the web graph – used by Google and

others

12/2/02 Graph Searching - Lecture 20 3

Depth First Search Algorithm

• Recursive marking algorithm• Initially every vertex is unmarked

DFS(i: vertex)mark i;for each j adjacent to i do

if j is unmarked then DFS(j)end{DFS}

Marks all vertices reachable from i

12/2/02 Graph Searching - Lecture 20 4

Example of Depth First Search

12

3

4

5

6

7

DFS(1)

12/2/02 Graph Searching - Lecture 20 5

Example Step 2

12

3

4

5

6

7

DFS(1)DFS(2)

12/2/02 Graph Searching - Lecture 20 6

Example Step 3

12

3

4

5

6

7

DFS(1)DFS(2)DFS(7)

8

dfs(1) dfs(2) dfs(7) dfs(5)

Page 112: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

38

Example

9

1

Graph Searching

CSE 373Data Structures

Lecture 20

12/2/02 Graph Searching - Lecture 20 2

Graph Searching• Find Properties of Graphs

› Connected components› Bipartite structure› Biconnected components

• Applications› Alternating paths for matching› Garbage collection – used in Java run time system› Finding dead code › Finding the web graph – used by Google and

others

12/2/02 Graph Searching - Lecture 20 3

Depth First Search Algorithm

• Recursive marking algorithm• Initially every vertex is unmarked

DFS(i: vertex)mark i;for each j adjacent to i do

if j is unmarked then DFS(j)end{DFS}

Marks all vertices reachable from i

12/2/02 Graph Searching - Lecture 20 4

Example of Depth First Search

12

3

4

5

6

7

DFS(1)

12/2/02 Graph Searching - Lecture 20 5

Example Step 2

12

3

4

5

6

7

DFS(1)DFS(2)

12/2/02 Graph Searching - Lecture 20 6

Example Step 3

12

3

4

5

6

7

DFS(1)DFS(2)DFS(7)

8

dfs(1) dfs(2) dfs(7) dfs(5) dfs(6)

Page 113: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

38

Example

9

1

Graph Searching

CSE 373Data Structures

Lecture 20

12/2/02 Graph Searching - Lecture 20 2

Graph Searching• Find Properties of Graphs

› Connected components› Bipartite structure› Biconnected components

• Applications› Alternating paths for matching› Garbage collection – used in Java run time system› Finding dead code › Finding the web graph – used by Google and

others

12/2/02 Graph Searching - Lecture 20 3

Depth First Search Algorithm

• Recursive marking algorithm• Initially every vertex is unmarked

DFS(i: vertex)mark i;for each j adjacent to i do

if j is unmarked then DFS(j)end{DFS}

Marks all vertices reachable from i

12/2/02 Graph Searching - Lecture 20 4

Example of Depth First Search

12

3

4

5

6

7

DFS(1)

12/2/02 Graph Searching - Lecture 20 5

Example Step 2

12

3

4

5

6

7

DFS(1)DFS(2)

12/2/02 Graph Searching - Lecture 20 6

Example Step 3

12

3

4

5

6

7

DFS(1)DFS(2)DFS(7)

8

dfs(1) dfs(2) dfs(7) dfs(5) dfs(6)

Page 114: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

39

Example

9

1

Graph Searching

CSE 373Data Structures

Lecture 20

12/2/02 Graph Searching - Lecture 20 2

Graph Searching• Find Properties of Graphs

› Connected components› Bipartite structure› Biconnected components

• Applications› Alternating paths for matching› Garbage collection – used in Java run time system› Finding dead code › Finding the web graph – used by Google and

others

12/2/02 Graph Searching - Lecture 20 3

Depth First Search Algorithm

• Recursive marking algorithm• Initially every vertex is unmarked

DFS(i: vertex)mark i;for each j adjacent to i do

if j is unmarked then DFS(j)end{DFS}

Marks all vertices reachable from i

12/2/02 Graph Searching - Lecture 20 4

Example of Depth First Search

12

3

4

5

6

7

DFS(1)

12/2/02 Graph Searching - Lecture 20 5

Example Step 2

12

3

4

5

6

7

DFS(1)DFS(2)

12/2/02 Graph Searching - Lecture 20 6

Example Step 3

12

3

4

5

6

7

DFS(1)DFS(2)DFS(7)

8

dfs(1) dfs(2) dfs(7) dfs(5)

Page 115: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

40

Example

9

1

Graph Searching

CSE 373Data Structures

Lecture 20

12/2/02 Graph Searching - Lecture 20 2

Graph Searching• Find Properties of Graphs

› Connected components› Bipartite structure› Biconnected components

• Applications› Alternating paths for matching› Garbage collection – used in Java run time system› Finding dead code › Finding the web graph – used by Google and

others

12/2/02 Graph Searching - Lecture 20 3

Depth First Search Algorithm

• Recursive marking algorithm• Initially every vertex is unmarked

DFS(i: vertex)mark i;for each j adjacent to i do

if j is unmarked then DFS(j)end{DFS}

Marks all vertices reachable from i

12/2/02 Graph Searching - Lecture 20 4

Example of Depth First Search

12

3

4

5

6

7

DFS(1)

12/2/02 Graph Searching - Lecture 20 5

Example Step 2

12

3

4

5

6

7

DFS(1)DFS(2)

12/2/02 Graph Searching - Lecture 20 6

Example Step 3

12

3

4

5

6

7

DFS(1)DFS(2)DFS(7)

8

dfs(1) dfs(2) dfs(7)

Page 116: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

41

Example

9

1

Graph Searching

CSE 373Data Structures

Lecture 20

12/2/02 Graph Searching - Lecture 20 2

Graph Searching• Find Properties of Graphs

› Connected components› Bipartite structure› Biconnected components

• Applications› Alternating paths for matching› Garbage collection – used in Java run time system› Finding dead code › Finding the web graph – used by Google and

others

12/2/02 Graph Searching - Lecture 20 3

Depth First Search Algorithm

• Recursive marking algorithm• Initially every vertex is unmarked

DFS(i: vertex)mark i;for each j adjacent to i do

if j is unmarked then DFS(j)end{DFS}

Marks all vertices reachable from i

12/2/02 Graph Searching - Lecture 20 4

Example of Depth First Search

12

3

4

5

6

7

DFS(1)

12/2/02 Graph Searching - Lecture 20 5

Example Step 2

12

3

4

5

6

7

DFS(1)DFS(2)

12/2/02 Graph Searching - Lecture 20 6

Example Step 3

12

3

4

5

6

7

DFS(1)DFS(2)DFS(7)

8

dfs(1) dfs(2)

Page 117: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

42

Example

9

1

Graph Searching

CSE 373Data Structures

Lecture 20

12/2/02 Graph Searching - Lecture 20 2

Graph Searching• Find Properties of Graphs

› Connected components› Bipartite structure› Biconnected components

• Applications› Alternating paths for matching› Garbage collection – used in Java run time system› Finding dead code › Finding the web graph – used by Google and

others

12/2/02 Graph Searching - Lecture 20 3

Depth First Search Algorithm

• Recursive marking algorithm• Initially every vertex is unmarked

DFS(i: vertex)mark i;for each j adjacent to i do

if j is unmarked then DFS(j)end{DFS}

Marks all vertices reachable from i

12/2/02 Graph Searching - Lecture 20 4

Example of Depth First Search

12

3

4

5

6

7

DFS(1)

12/2/02 Graph Searching - Lecture 20 5

Example Step 2

12

3

4

5

6

7

DFS(1)DFS(2)

12/2/02 Graph Searching - Lecture 20 6

Example Step 3

12

3

4

5

6

7

DFS(1)DFS(2)DFS(7)

8

dfs(1)

Page 118: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

43

Example

9

1

Graph Searching

CSE 373Data Structures

Lecture 20

12/2/02 Graph Searching - Lecture 20 2

Graph Searching• Find Properties of Graphs

› Connected components› Bipartite structure› Biconnected components

• Applications› Alternating paths for matching› Garbage collection – used in Java run time system› Finding dead code › Finding the web graph – used by Google and

others

12/2/02 Graph Searching - Lecture 20 3

Depth First Search Algorithm

• Recursive marking algorithm• Initially every vertex is unmarked

DFS(i: vertex)mark i;for each j adjacent to i do

if j is unmarked then DFS(j)end{DFS}

Marks all vertices reachable from i

12/2/02 Graph Searching - Lecture 20 4

Example of Depth First Search

12

3

4

5

6

7

DFS(1)

12/2/02 Graph Searching - Lecture 20 5

Example Step 2

12

3

4

5

6

7

DFS(1)DFS(2)

12/2/02 Graph Searching - Lecture 20 6

Example Step 3

12

3

4

5

6

7

DFS(1)DFS(2)DFS(7)

8

dfs(8)

Page 119: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

44

Example

9

1

Graph Searching

CSE 373Data Structures

Lecture 20

12/2/02 Graph Searching - Lecture 20 2

Graph Searching• Find Properties of Graphs

› Connected components› Bipartite structure› Biconnected components

• Applications› Alternating paths for matching› Garbage collection – used in Java run time system› Finding dead code › Finding the web graph – used by Google and

others

12/2/02 Graph Searching - Lecture 20 3

Depth First Search Algorithm

• Recursive marking algorithm• Initially every vertex is unmarked

DFS(i: vertex)mark i;for each j adjacent to i do

if j is unmarked then DFS(j)end{DFS}

Marks all vertices reachable from i

12/2/02 Graph Searching - Lecture 20 4

Example of Depth First Search

12

3

4

5

6

7

DFS(1)

12/2/02 Graph Searching - Lecture 20 5

Example Step 2

12

3

4

5

6

7

DFS(1)DFS(2)

12/2/02 Graph Searching - Lecture 20 6

Example Step 3

12

3

4

5

6

7

DFS(1)DFS(2)DFS(7)

8

dfs(8) dfs(9)

Page 120: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

45

Example

9

1

Graph Searching

CSE 373Data Structures

Lecture 20

12/2/02 Graph Searching - Lecture 20 2

Graph Searching• Find Properties of Graphs

› Connected components› Bipartite structure› Biconnected components

• Applications› Alternating paths for matching› Garbage collection – used in Java run time system› Finding dead code › Finding the web graph – used by Google and

others

12/2/02 Graph Searching - Lecture 20 3

Depth First Search Algorithm

• Recursive marking algorithm• Initially every vertex is unmarked

DFS(i: vertex)mark i;for each j adjacent to i do

if j is unmarked then DFS(j)end{DFS}

Marks all vertices reachable from i

12/2/02 Graph Searching - Lecture 20 4

Example of Depth First Search

12

3

4

5

6

7

DFS(1)

12/2/02 Graph Searching - Lecture 20 5

Example Step 2

12

3

4

5

6

7

DFS(1)DFS(2)

12/2/02 Graph Searching - Lecture 20 6

Example Step 3

12

3

4

5

6

7

DFS(1)DFS(2)DFS(7)

8

dfs(8)

Page 121: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

46

Example

9

1

Graph Searching

CSE 373Data Structures

Lecture 20

12/2/02 Graph Searching - Lecture 20 2

Graph Searching• Find Properties of Graphs

› Connected components› Bipartite structure› Biconnected components

• Applications› Alternating paths for matching› Garbage collection – used in Java run time system› Finding dead code › Finding the web graph – used by Google and

others

12/2/02 Graph Searching - Lecture 20 3

Depth First Search Algorithm

• Recursive marking algorithm• Initially every vertex is unmarked

DFS(i: vertex)mark i;for each j adjacent to i do

if j is unmarked then DFS(j)end{DFS}

Marks all vertices reachable from i

12/2/02 Graph Searching - Lecture 20 4

Example of Depth First Search

12

3

4

5

6

7

DFS(1)

12/2/02 Graph Searching - Lecture 20 5

Example Step 2

12

3

4

5

6

7

DFS(1)DFS(2)

12/2/02 Graph Searching - Lecture 20 6

Example Step 3

12

3

4

5

6

7

DFS(1)DFS(2)DFS(7)

8

Page 122: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Complexity?

• What's the basic operation?‣ finding all the Vertices in the graph?

‣ making a mark?

‣ checking a mark?

‣ finding all the neighbors of a node?

• Cost depends on the data structure used to represent the graph

47

Page 123: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Two choices of data structure:

• Adjacency Matrix: Θ( V 2 )

• Adjacency List: Θ( V + E )

48

Page 124: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

49

One Last look at the Example

9

1

Graph Searching

CSE 373Data Structures

Lecture 20

12/2/02 Graph Searching - Lecture 20 2

Graph Searching• Find Properties of Graphs

› Connected components› Bipartite structure› Biconnected components

• Applications› Alternating paths for matching› Garbage collection – used in Java run time system› Finding dead code › Finding the web graph – used by Google and

others

12/2/02 Graph Searching - Lecture 20 3

Depth First Search Algorithm

• Recursive marking algorithm• Initially every vertex is unmarked

DFS(i: vertex)mark i;for each j adjacent to i do

if j is unmarked then DFS(j)end{DFS}

Marks all vertices reachable from i

12/2/02 Graph Searching - Lecture 20 4

Example of Depth First Search

12

3

4

5

6

7

DFS(1)

12/2/02 Graph Searching - Lecture 20 5

Example Step 2

12

3

4

5

6

7

DFS(1)DFS(2)

12/2/02 Graph Searching - Lecture 20 6

Example Step 3

12

3

4

5

6

7

DFS(1)DFS(2)DFS(7)

8

Page 125: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

49

One Last look at the Example

9

1

Graph Searching

CSE 373Data Structures

Lecture 20

12/2/02 Graph Searching - Lecture 20 2

Graph Searching• Find Properties of Graphs

› Connected components› Bipartite structure› Biconnected components

• Applications› Alternating paths for matching› Garbage collection – used in Java run time system› Finding dead code › Finding the web graph – used by Google and

others

12/2/02 Graph Searching - Lecture 20 3

Depth First Search Algorithm

• Recursive marking algorithm• Initially every vertex is unmarked

DFS(i: vertex)mark i;for each j adjacent to i do

if j is unmarked then DFS(j)end{DFS}

Marks all vertices reachable from i

12/2/02 Graph Searching - Lecture 20 4

Example of Depth First Search

12

3

4

5

6

7

DFS(1)

12/2/02 Graph Searching - Lecture 20 5

Example Step 2

12

3

4

5

6

7

DFS(1)DFS(2)

12/2/02 Graph Searching - Lecture 20 6

Example Step 3

12

3

4

5

6

7

DFS(1)DFS(2)DFS(7)

8

Page 126: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

49

One Last look at the Example

9

1

Graph Searching

CSE 373Data Structures

Lecture 20

12/2/02 Graph Searching - Lecture 20 2

Graph Searching• Find Properties of Graphs

› Connected components› Bipartite structure› Biconnected components

• Applications› Alternating paths for matching› Garbage collection – used in Java run time system› Finding dead code › Finding the web graph – used by Google and

others

12/2/02 Graph Searching - Lecture 20 3

Depth First Search Algorithm

• Recursive marking algorithm• Initially every vertex is unmarked

DFS(i: vertex)mark i;for each j adjacent to i do

if j is unmarked then DFS(j)end{DFS}

Marks all vertices reachable from i

12/2/02 Graph Searching - Lecture 20 4

Example of Depth First Search

12

3

4

5

6

7

DFS(1)

12/2/02 Graph Searching - Lecture 20 5

Example Step 2

12

3

4

5

6

7

DFS(1)DFS(2)

12/2/02 Graph Searching - Lecture 20 6

Example Step 3

12

3

4

5

6

7

DFS(1)DFS(2)DFS(7)

8

Blue edges form a spanning three

Page 127: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

49

One Last look at the Example

9

1

Graph Searching

CSE 373Data Structures

Lecture 20

12/2/02 Graph Searching - Lecture 20 2

Graph Searching• Find Properties of Graphs

› Connected components› Bipartite structure› Biconnected components

• Applications› Alternating paths for matching› Garbage collection – used in Java run time system› Finding dead code › Finding the web graph – used by Google and

others

12/2/02 Graph Searching - Lecture 20 3

Depth First Search Algorithm

• Recursive marking algorithm• Initially every vertex is unmarked

DFS(i: vertex)mark i;for each j adjacent to i do

if j is unmarked then DFS(j)end{DFS}

Marks all vertices reachable from i

12/2/02 Graph Searching - Lecture 20 4

Example of Depth First Search

12

3

4

5

6

7

DFS(1)

12/2/02 Graph Searching - Lecture 20 5

Example Step 2

12

3

4

5

6

7

DFS(1)DFS(2)

12/2/02 Graph Searching - Lecture 20 6

Example Step 3

12

3

4

5

6

7

DFS(1)DFS(2)DFS(7)

8

Blue edges form a spanning three

Black edges lead back to an already-visited node

Page 128: CS 350 Algorithms and Complexity - Computer Action Teamweb.cecs.pdx.edu/~black/cs350/Lectures/lec06-Exhaustive Search.pdfExample 2: Knapsack Problem • Given n items: ‣ weights:

Applications

• Checking for connectivity‣ How?

• Checking for Cycles‣ How?

50