CS 350 Algorithms and Complexity - Computer Action...

Post on 01-Apr-2021

2 views 0 download

transcript

CS 350 Algorithms and Complexity

Lecture 6: Exhaustive Search Algorithms

Andrew P. Black

Department of Computer Science

Portland State University

Winter 2019

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

Examples of Brute-Force String Matching

• Pattern: 001011 Text: 10010101101001100101111010

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

• 3

Examples of Brute-Force String Matching

• Pattern: 001011 Text: 10010101101001100101111010

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

• 3

Examples of Brute-Force String Matching

• Pattern: 001011 Text: 10010101101001100101111010

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

• 3

Examples of Brute-Force String Matching

• Pattern: 001011 Text: 10010101101001100101111010

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

• 3

Examples of Brute-Force String Matching

• Pattern: 001011 Text: 10010101101001100101111010

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

• 3

Examples of Brute-Force String Matching

• Pattern: 001011 Text: 10010101101001100101111010

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

• 3

Examples of Brute-Force String Matching

• Pattern: 001011 Text: 10010101101001100101111010

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

• 3

Examples of Brute-Force String Matching

• Pattern: 001011 Text: 10010101101001100101111010

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

• 3

Examples of Brute-Force String Matching

• Pattern: 001011 Text: 10010101101001100101111010

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

• 3

Examples of Brute-Force String Matching

• Pattern: 001011 Text: 10010101101001100101111010

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

• 3

Examples of Brute-Force String Matching

• Pattern: 001011 Text: 10010101101001100101111010

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

• 3

Pseudocode and Efficiency

4

Pseudocode and Efficiency

4

Pseudocode and Efficiency

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

Brute-Force Polynomial Evaluation

5

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

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

5

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

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

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)

Polynomial Evaluation: Improvement

6

Polynomial Evaluation: Improvement

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

6

Polynomial Evaluation: Improvement

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

• Better brute-force algorithm:

6

Polynomial Evaluation: Improvement

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

• Better brute-force algorithm:

6

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

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

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

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)

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

Closest-Pair Brute-Force Algorithm (cont.)

8

Closest-Pair Brute-Force Algorithm (cont.)

• Efficiency:

8

Closest-Pair Brute-Force Algorithm (cont.)

• Efficiency:

8

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

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)

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

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

Brute Force Closest Pair

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

11

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

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

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

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

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

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

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

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

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

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

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›

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›

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›

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›

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›

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›

Assignment Problem by Exhaustive Search

18

•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

•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

•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

•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

•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

•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

•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

•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

•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!)

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

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

Convex Hulls

20

• What is a Convex Set?

Convex Hulls

20

• What is a Convex Set?

Convex Hulls

20

• What is a Convex Set?

Convex Hulls

20

• What is a Convex Set?

Convex Hulls

20

• What is a Convex Set?

Convex Hulls

20

• What is a Convex Set?

Convex Hulls

20

• What is a Convex Set?

Convex Hulls

20

• What is a Convex Set?

Convex Hulls

20

• What is a Convex Set?

Convex Hulls

20

• What is a Convex Set?

Convex Hulls

20

• What is a Convex Set?

Convex Hulls

20

• What is a Convex Set?

Convex Hulls

20

• What is a Convex Set?

Convex Hulls

20

• What is a Convex Set?

Convex Hulls

20

• What is a Convex Set?

Convex Hulls

20

• What is a Convex Set?

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

21

Convex Hulls

22

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

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

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

Applications of Convex Hull• Collision-detection in video games

• Robot motion planning

23

Applications of Convex Hull• Collision-detection in video games

• Robot motion planning

23

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

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

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

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

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

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)

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)

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)

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

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

Searching in Graphs

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

‣ Breadth-first search

28

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].

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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

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

Two choices of data structure:

• Adjacency Matrix: Θ( V 2 )

• Adjacency List: Θ( V + E )

48

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

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

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

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

Applications

• Checking for connectivity‣ How?

• Checking for Cycles‣ How?

50