depth-first search directed graph algorithms f h k...directed graph algorithms cse 373 2/22/2006 cse...

16
1 Directed Graph Algorithms CSE 373 2/22/2006 CSE 373 Wi 06 - Digraph Algorithms 2 discovery edge cross edge back edge forward edge Depth-First Search A C B D F H G E Stack (before): Stack (after): A I J L K unexplored edge 2/22/2006 CSE 373 Wi 06 - Digraph Algorithms 3 Depth-First Search A C B D F H G E Stack (before): A Stack (after): C I J L K discovery edge cross edge back edge forward edge unexplored edge 2/22/2006 CSE 373 Wi 06 - Digraph Algorithms 4 Depth-First Search A C B D F H G E Stack (before): C Stack (after): D, E I J L K discovery edge cross edge back edge forward edge unexplored edge 2/22/2006 CSE 373 Wi 06 - Digraph Algorithms 5 Depth-First Search A C B D F H G E Stack (before): D (from C), E Stack (after): D (from C), D (from E) I J L K discovery edge cross edge back edge forward edge unexplored edge 2/22/2006 CSE 373 Wi 06 - Digraph Algorithms 6 Depth-First Search A C B D F H G E Stack (before): D (from C), D (from E) Stack (after): D (from C) I J L K discovery edge cross edge back edge forward edge unexplored edge

Upload: others

Post on 29-Jun-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Depth-First Search Directed Graph Algorithms F H K...Directed Graph Algorithms CSE 373 2/22/2006 CSE 373 Wi 06 - Digraph Algorithms 2 discovery edge ... Algorithms 40 Only acyclic

1

Directed Graph Algorithms

CSE 373

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

2

discovery edge

cross edge

back edge

forward edge

Depth-First Search

A

C

B

D

F H

G

E

Stack (before): Stack (after): A

IJ

L

K

unexplored edge

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

3

Depth-First Search

A

C

B

D

F H

G

E

Stack (before): AStack (after): C

IJ

L

K

discovery edge

cross edge

back edge

forward edge

unexplored edge

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

4

Depth-First Search

A

C

B

D

F H

G

E

Stack (before): CStack (after): D, E

IJ

L

K

discovery edge

cross edge

back edge

forward edge

unexplored edge

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

5

Depth-First Search

A

C

B

D

F H

G

E

Stack (before): D (from C), EStack (after): D (from C), D (from E)

IJ

L

K

discovery edge

cross edge

back edge

forward edge

unexplored edge

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

6

Depth-First Search

A

C

B

D

F H

G

E

Stack (before): D (from C), D (from E)Stack (after): D (from C)

IJ

L

K

discovery edge

cross edge

back edge

forward edge

unexplored edge

Page 2: Depth-First Search Directed Graph Algorithms F H K...Directed Graph Algorithms CSE 373 2/22/2006 CSE 373 Wi 06 - Digraph Algorithms 2 discovery edge ... Algorithms 40 Only acyclic

2

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

7

Depth-First Search

A

C

B

D

F H

G

E

Stack (before): D (from C)Stack (after):

IJ

L

K

discovery edge

cross edge

back edge

forward edge

unexplored edge

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

8

Depth-First Search

A

C

B

D

F H

G

E

Stack (before): Stack (after): B

IJ

L

K

discovery edge

cross edge

back edge

forward edge

unexplored edge

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

9

Depth-First Search

A

C

B

D

F H

G

E

Stack (before): B Stack (after):

IJ

L

K

discovery edge

cross edge

back edge

forward edge

unexplored edge

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

10

Depth-First Search

A

C

B

D

F H

G

E

Stack (before): Stack (after): F

IJ

L

K

discovery edge

cross edge

back edge

forward edge

unexplored edge

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

11

Depth-First Search

A

C

B

D

F H

G

E

Stack (before): F Stack (after): H

IJ

L

K

discovery edge

cross edge

back edge

forward edge

unexplored edge

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

12

Depth-First Search

A

C

B

D

F H

G

E

Stack (before): H Stack (after): G

IJ

L

K

discovery edge

cross edge

back edge

forward edge

unexplored edge

Page 3: Depth-First Search Directed Graph Algorithms F H K...Directed Graph Algorithms CSE 373 2/22/2006 CSE 373 Wi 06 - Digraph Algorithms 2 discovery edge ... Algorithms 40 Only acyclic

3

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

13

Depth-First Search

A

C

B

D

F H

G

E

Stack (before): G Stack (after): I, J, L

IJ

L

K

discovery edge

cross edge

back edge

forward edge

unexplored edge

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

14

Depth-First Search

A

C

B

D

F H

G

E

Stack (before): I, J, L Stack (after): I, J

IJ

L

K

discovery edge

cross edge

back edge

forward edge

unexplored edge

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

15

Depth-First Search

A

C

B

D

F H

G

E

Stack (before): I, J Stack (after): I, K

IJ

L

K

discovery edge

cross edge

back edge

forward edge

unexplored edge

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

16

Depth-First Search

A

C

B

D

F H

G

E

Stack (before): I, K Stack (after): I

IJ

L

K

discovery edge

cross edge

back edge

forward edge

unexplored edge

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

17

Depth-First Search

A

C

B

D

F H

G

E

Stack (before): I Stack (after):

IJ

L

K

discovery edge

cross edge

back edge

forward edge

unexplored edge

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

18

Breadth-First Search

A

C

B

D

F H

G

E

Queue (before): Queue (after): A

IJ

L

K

discovery edge

cross edge

back edge

forward edge

unexplored edge

Page 4: Depth-First Search Directed Graph Algorithms F H K...Directed Graph Algorithms CSE 373 2/22/2006 CSE 373 Wi 06 - Digraph Algorithms 2 discovery edge ... Algorithms 40 Only acyclic

4

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

19

Breadth-First Search

A

C

B

D

F H

G

E

Queue (before): AQueue (after): C

IJ

L

K

discovery edge

cross edge

back edge

forward edge

unexplored edge

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

20

Breadth-First Search

A

C

B

D

F H

G

E

Queue (before): CQueue (after): D, E

IJ

L

K

discovery edge

cross edge

back edge

forward edge

unexplored edge

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

21

Breadth-First Search

A

C

B

D

F H

G

E

Queue (before): D, EQueue (after): E

IJ

L

K

discovery edge

cross edge

back edge

forward edge

unexplored edge

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

22

Breadth-First Search

A

C

B

D

F H

G

E

Queue (before): EQueue (after):

IJ

L

K

discovery edge

cross edge

back edge

forward edge

unexplored edge

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

23

Breadth-First Search

A

C

B

D

F H

G

E

Queue (before): Queue (after): B

IJ

L

K

discovery edge

cross edge

back edge

forward edge

unexplored edge

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

24

Breadth-First Search

A

C

B

D

F H

G

E

Queue (before): BQueue (after):

IJ

L

K

discovery edge

cross edge

back edge

forward edge

unexplored edge

Page 5: Depth-First Search Directed Graph Algorithms F H K...Directed Graph Algorithms CSE 373 2/22/2006 CSE 373 Wi 06 - Digraph Algorithms 2 discovery edge ... Algorithms 40 Only acyclic

5

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

25

Breadth-First Search

A

C

B

D

F H

G

E

Queue (before): BQueue (after):

IJ

L

K

discovery edge

cross edge

back edge

forward edge

unexplored edge

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

26

Breadth-First Search

A

C

B

D

F H

G

E

Queue (before): Queue (after): F

IJ

L

K

discovery edge

cross edge

back edge

forward edge

unexplored edge

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

27

Breadth-First Search

A

C

B

D

F H

G

E

Queue (before): F Queue (after): H

IJ

L

K

discovery edge

cross edge

back edge

forward edge

unexplored edge

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

28

Breadth-First Search

A

C

B

D

F H

G

E

Queue (before): H Queue (after): G

IJ

L

K

discovery edge

cross edge

back edge

forward edge

unexplored edge

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

29

Breadth-First Search

A

C

B

D

F H

G

E

Queue (before): G Queue (after): I, J, L

IJ

L

K

discovery edge

cross edge

back edge

forward edge

unexplored edge

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

30

Breadth-First Search

A

C

B

D

F H

G

E

Queue (before): I, J, L Queue (after): J (from G), L, J (from I)

IJ

L

K

discovery edge

cross edge

back edge

forward edge

unexplored edge

Page 6: Depth-First Search Directed Graph Algorithms F H K...Directed Graph Algorithms CSE 373 2/22/2006 CSE 373 Wi 06 - Digraph Algorithms 2 discovery edge ... Algorithms 40 Only acyclic

6

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

31

Breadth-First Search

A

C

B

D

F H

G

E

Queue (before): J (from G), L, J (from I)Queue (after): L, J (from I), K

IJ

L

K

discovery edge

cross edge

back edge

forward edge

unexplored edge

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

32

Breadth-First Search

A

C

B

D

F H

G

E

Queue (before): L, J (from I), KQueue (after): J (from I), K

IJ

L

K

discovery edge

cross edge

back edge

forward edge

unexplored edge

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

33

Breadth-First Search

A

C

B

D

F H

G

E

Queue (before): J (from I), KQueue (after): K

IJ

L

K

discovery edge

cross edge

back edge

forward edge

unexplored edge

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

34

Breadth-First Search

A

C

B

D

F H

G

E

Queue (before): KQueue (after):

IJ

L

K

discovery edge

cross edge

back edge

forward edge

unexplored edge

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

35

Topological Sort

321143322

326341370

378

401

421

Problem: Find an order inwhich all these courses can be taken.

Example: 142 143 378370 321 341 322326 421 401

In order to take a course, you must take all of its prerequisites first

142

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

36

Given a digraph G = (V, E), find a total ordering of its vertices such that:

for any edge (v, w) in E, v precedes w in the ordering

A

BC

F

D E

Topological Sort

Page 7: Depth-First Search Directed Graph Algorithms F H K...Directed Graph Algorithms CSE 373 2/22/2006 CSE 373 Wi 06 - Digraph Algorithms 2 discovery edge ... Algorithms 40 Only acyclic

7

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

37

A

BC

F

D E

EA DFB C

Any total ordering in whichall the arrows go to the rightis a valid solution

Topo sort - good example

Note that F can go anywhere in this list because it is not connected.Also the solution is not unique.

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

38

A

BC

F

D E

DA EFB C

Any ordering in whichan arrow goes to the leftis not a valid solution

Topo sort - bad example

NO!

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

39

Paths and Cycles

• Given a digraph G = (V,E), a path is a sequence of vertices v1,v2, …,vk such that:› (vi,vi+1) in E for 1 < i < k› path length = number of edges in the path› path cost = sum of costs of each edge

• A path is a cycle if :› k > 1; v1 = vk

• G is acyclic if it has no cycles.

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

40

Only acyclic graphs can be topologically sorted

• A directed graph with a cycle cannot be topologically sorted.

A

BC

F

D E

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

41

Step 1: Identify vertices that have no incoming edges• The “in-degree” of these vertices is zero

A

BC

F

D E

Topological sort algorithm: 1

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

42

Step 1: Identify vertices that have no incoming edges• If no such vertices, graph has only cycle(s) (cyclic graph)• Topological sort not possible – Halt.

A

BC

DExample of a cyclic graph

Topo sort algorithm: 1a

Page 8: Depth-First Search Directed Graph Algorithms F H K...Directed Graph Algorithms CSE 373 2/22/2006 CSE 373 Wi 06 - Digraph Algorithms 2 discovery edge ... Algorithms 40 Only acyclic

8

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

43

Step 1: Identify vertices that have no incoming edges• Select one such vertex

A

BC

F

D E

Select

Topo sort algorithm:1b

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

44

A

BC

F

D E

Step 2: Delete this vertex of in-degree 0 and all its outgoing edges from the graph. Place it in the output.

Topo sort algorithm: 2

A

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

45

A

BC

F

D E

Repeat Step 1 and Step 2 until graph is empty

Select

Continue until done

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

46

A

BC

F

D E

B

Select B. Copy to sorted list. Delete B and its edges.

B

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

47

A

C

F

D E

B C

Select C. Copy to sorted list. Delete C and its edges.

C

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

48

AF

D E

B C D

Select D. Copy to sorted list. Delete D and its edges.

D

Page 9: Depth-First Search Directed Graph Algorithms F H K...Directed Graph Algorithms CSE 373 2/22/2006 CSE 373 Wi 06 - Digraph Algorithms 2 discovery edge ... Algorithms 40 Only acyclic

9

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

49

AF

E

B C D E F

Select E. Copy to sorted list. Delete E and its edges.Select F. Copy to sorted list. Delete F and its edges.

E, F

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

50

A B C D E F

Done

A

BC

F

D E

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

51

A

BC

F

D E

2 4

5

54

3

1

2

3

4

5

6

Assume adjacency listrepresentation

Implementation

A B C D E F1 2 3 4 5 6Translation

arrayvalue next

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

52

0

1

0

2

2

1In-Degree array; or add a field to array A

Calculate In-degrees

2 4

5

54

3

1

2

3

4

5

6

AD

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

53

Calculate In-degrees

for i = 1 to n do D[i] := 0; endforfor i = 1 to n do

x := A[i];while x ≠ null doD[x.value] := D[x.value] + 1;x := x.next;

endwhileendfor

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

54

Key idea: Initialize and maintain a queue (or stack)of vertices with In-Degree 0

1Queue 6

12 3

6

4 5

Maintaining Degree 0 Vertices

0

1

0

2

2

1

2 4

5

54

3

1

2

3

4

5

6

AD

Page 10: Depth-First Search Directed Graph Algorithms F H K...Directed Graph Algorithms CSE 373 2/22/2006 CSE 373 Wi 06 - Digraph Algorithms 2 discovery edge ... Algorithms 40 Only acyclic

10

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

55

After each vertex is output, when updating In-Degree array, enqueue any vertex whose In-Degree becomes zero

1

Queue 6

Output

2dequeue enqueue

12 3

6

4 5

Topo Sort using a Queue (breadth-first)

0

0

0

1

2

1

2 4

5

54

3

1

2

3

4

5

6

AD

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

56

After each vertex is output, when updating In-Degree array, push any vertex whose In-Degree becomes zero

1

Stack 2

Output

6pop push

12 3

6

4 5

Topo Sort using a Stack (depth-first)

0

0

0

1

2

1

2 4

5

54

3

1

2

3

4

5

6

AD

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

57

Topological Sort Algorithm1. Store each vertex’s In-Degree in an array D2. Initialize queue with all “in-degree=0” vertices3. While there are vertices remaining in the

queue:(a) Dequeue and output a vertex(b) Reduce In-Degree of all vertices adjacent to it by 1(c) Enqueue any of these vertices whose In-Degree

became zero4. If all vertices are output then success,

otherwise there is a cycle.2/22/2006 CSE 373 Wi 06 - Digraph

Algorithms58

Some Detail

Main Loopwhile notEmpty(Q) do

x := Dequeue(Q)Output(x)y := A[x];while y ≠ null doD[y.value] := D[y.value] – 1;if D[y.value] = 0 then Enqueue(Q,y.value);y := y.next;

endwhileendwhile

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

59

12 3 6

4 5

0

1

2

1 0

2

Queue (before): Queue (after): 1, 6

Answer:

Topo Sort w/ queue

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

60

12 3 6

4 5

0

0

1

1 0

2

Queue (before): 1, 6Queue (after): 6, 2

Answer: 1

Topo Sort w/ queue

Page 11: Depth-First Search Directed Graph Algorithms F H K...Directed Graph Algorithms CSE 373 2/22/2006 CSE 373 Wi 06 - Digraph Algorithms 2 discovery edge ... Algorithms 40 Only acyclic

11

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

61

12 3 6

4 5

0

0

1

1 0

2

Queue (before): 6, 2Queue (after): 2

Answer: 1, 6

Topo Sort w/ queue

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

62

12 3 6

4 5

0

0

1

0 0

2

Queue (before): 2Queue (after): 3

Answer: 1, 6, 2

Topo Sort w/ queue

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

63

12 3 6

4 5

0

0

0

0 0

1

Queue (before): 3Queue (after): 4

Answer: 1, 6, 2, 3

Topo Sort w/ queue

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

64

12 3 6

4 5

0

0

0

0 0

0

Queue (before): 4Queue (after): 5

Answer: 1, 6, 2, 3, 4

Topo Sort w/ queue

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

65

12 3 6

4 5

0

0

0

0 0

0

Queue (before): 5Queue (after):

Answer: 1, 6, 2, 3, 4, 5

Topo Sort w/ queue

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

66

12 3 6

4 5

0

1

3

2 0

2

Stack (before): Stack (after): 1, 6

Answer:

7

8

Topo Sort w/ stack

1

1

Page 12: Depth-First Search Directed Graph Algorithms F H K...Directed Graph Algorithms CSE 373 2/22/2006 CSE 373 Wi 06 - Digraph Algorithms 2 discovery edge ... Algorithms 40 Only acyclic

12

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

67

12 3 6

4 5

0

1

3

2 0

2

Stack (before): 1, 6Stack (after): 1, 7, 8

Answer: 6

7

80

0

Topo Sort w/ stack

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

68

12 3 6

4 5

0

1

3

2 0

2

Stack (before): 1, 7, 8Stack (after): 1, 7

Answer: 6, 8

7

80

0

Topo Sort w/ stack

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

69

12 3 6

4 5

0

1

3

2 0

2

Stack (before): 1, 7Stack (after): 1

Answer: 6, 8, 7

7

80

0

Topo Sort w/ stack

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

70

12 3 6

4 5

0

0

2

1 0

2

Stack (before): 1Stack (after): 2

Answer: 6, 8, 7, 1

7

80

0

Topo Sort w/ stack

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

71

12 3 6

4 5

0

0

1

0 0

2

Stack (before): 2Stack (after): 3

Answer: 6, 8, 7, 1, 2

7

80

0

Topo Sort w/ stack

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

72

12 3 6

4 5

0

0

0

0 0

1

Stack (before): 3Stack (after): 4

Answer: 6, 8, 7, 1, 2, 3

7

80

0

Topo Sort w/ stack

Page 13: Depth-First Search Directed Graph Algorithms F H K...Directed Graph Algorithms CSE 373 2/22/2006 CSE 373 Wi 06 - Digraph Algorithms 2 discovery edge ... Algorithms 40 Only acyclic

13

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

73

12 3 6

4 5

0

0

0

0 0

0

Stack (before): 4Stack (after): 5

Answer: 6, 8, 7, 1, 2, 3, 4

7

80

0

Topo Sort w/ stack

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

74

12 3 6

4 5

0

0

0

0 0

0

Stack (before): 5Stack (after):

Answer: 6, 8, 7, 1, 2, 3, 4, 5

7

80

0

Topo Sort w/ stack

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

75

12 3

4 5

0

1

2

2

1

Queue (before): Queue (after): 1

Answer:

TopoSort Fails (cycle)

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

76

12 3

4 5

0

0

1

2

1

Queue (before): 1Queue (after): 2

Answer: 1

TopoSort Fails (cycle)

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

77

12 3

4 5

0

0

1

1

1

Queue (before): 2Queue (after):

Answer: 1, 2

TopoSort Fails (cycle)

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

78

Topological Sort Analysis

• Initialize In-Degree array: O(|V| + |E|)• Initialize Queue with In-Degree 0 vertices: O(|V|)• Dequeue and output vertex:

› |V| vertices, each takes only O(1) to dequeue and output: O(|V|)

• Reduce In-Degree of all vertices adjacent to a vertex and Enqueue any In-Degree 0 vertices:› O(|E|)

• For input graph G=(V,E) run time = O(|V| + |E|)› Linear time!

Page 14: Depth-First Search Directed Graph Algorithms F H K...Directed Graph Algorithms CSE 373 2/22/2006 CSE 373 Wi 06 - Digraph Algorithms 2 discovery edge ... Algorithms 40 Only acyclic

14

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

79

Recall Path cost ,Path length• Path cost: the sum of the costs of each edge• Path length: the number of edges in the path

› Path length is the unweighted path cost

Seattle

San FranciscoDallas

Chicago

Salt Lake City

4

2 2

23

2 23

length(p) = 5cost(p) = 11

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

80

Shortest Path Problems• Given a graph G = (V, E) and a “source” vertex s

in V, find the minimum cost paths from s to every vertex in V

• Many variations:› unweighted vs. weighted› cyclic vs. acyclic› pos. weights only vs. pos. and neg. weights › etc

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

81

Why study shortest path problems?

• Traveling on a budget: What is the cheapest airline schedule from Seattle to city X?

• Optimizing routing of packets on the internet:› Vertices are routers and edges are network links with

different delays. What is the routing path with smallest total delay?

• Shipping: Find which highways and roads to take to minimize total delay due to traffic

• etc.2/22/2006 CSE 373 Wi 06 - Digraph

Algorithms82

Unweighted Shortest PathProblem: Given a “source” vertex s in an unweighted

directed graph G = (V,E), find the shortest path from s to all vertices in

G

A

C

B

D

F H

G

E

Source

Only interested

in path lengths

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

83

Breadth-First Search Solution

• Basic Idea: Starting at node s, find vertices that can be reached using 0, 1, 2, 3, …, N-1 edges (works even for cyclic graphs!)

A

C

B

D

F H

G

E

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

84

Breadth-First Search Alg.• Uses a queue to track vertices that are “nearby”• source vertex is s

Distance[s] := 0Enqueue(Q,s); Mark(s)//After a vertex is marked once

// it won’t be enqueued againwhile queue is not empty do

X := Dequeue(Q);for each vertex Y adjacent to X do

if Y is unmarked thenDistance[Y] := Distance[X] + 1;Previous[Y] := X;//if we want to record pathsEnqueue(Q,Y); Mark(Y);

• Running time = O(|V| + |E|)

Page 15: Depth-First Search Directed Graph Algorithms F H K...Directed Graph Algorithms CSE 373 2/22/2006 CSE 373 Wi 06 - Digraph Algorithms 2 discovery edge ... Algorithms 40 Only acyclic

15

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

85

Example: Shortest Path length

A

C

B

D

F H

G

E

0

Queue Q = C

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

86

Example (ct’d)

A

C

B

D

F H

G

E

0

Queue Q = A D E

1

1

1

Previouspointer

Indicates the vertex is marked

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

87

Example (ct’d)

A

C

B

D

F H

G

E

0

Q = D E B

1

1

1

2

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

88

Example (ct’d)

A

C

B

D

F H

G

E

0

Q = B G

1

1

1

2

2

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

89

Example (ct’d)

A

C

B

D

F H

G

E

0

Q = F

1

1

1

2

2

3 4

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

90

Example (ct’d)

A

C

B

D

F H

G

E

0

Q = H

1

1

1

2

2

3

Page 16: Depth-First Search Directed Graph Algorithms F H K...Directed Graph Algorithms CSE 373 2/22/2006 CSE 373 Wi 06 - Digraph Algorithms 2 discovery edge ... Algorithms 40 Only acyclic

16

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

91

What if edges have weights?

• Breadth First Search does not work anymore › minimum cost path may have more edges than

minimum length path

A

C

B

D

F H

G

E

2 3

2 1

1

42

11

93

8

3

Shortest path (length)from C to A:C A (cost = 9)

Minimum Cost Path = C E D A(cost = 8)

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

92

Dijkstra’s Algorithm for Weighted Shortest Path

• Classic algorithm for solving shortest path in weighted graphs (without negative weights)

• A greedy algorithm (irrevocably makes decisions without considering future consequences)

• Each vertex has a cost for path from initial vertex

2/22/2006 CSE 373 Wi 06 - Digraph Algorithms

93

Basic Idea of Dijkstra’sAlgorithm

• Find the vertex with smallest cost that has not been “marked” yet.

• Mark it and compute the cost of its neighbors.• Do this until all vertices are marked.• Note that each step of the algorithm we are

marking one vertex and we won’t change our decision: hence the term “greedy” algorithm