Data Structures in Java Lecture 17: Traversing Graphs. Shortest - - PowerPoint PPT Presentation

data structures in java
SMART_READER_LITE
LIVE PREVIEW

Data Structures in Java Lecture 17: Traversing Graphs. Shortest - - PowerPoint PPT Presentation

Data Structures in Java Lecture 17: Traversing Graphs. Shortest Paths. 10/18/2015 Daniel Bauer 1 Today: Graph Traversals Depth First Search (a generalization of pre-order traversal on trees to graphs, users a Stack) Breadth First


slide-1
SLIDE 1

Data Structures in Java

Lecture 17: Traversing Graphs. Shortest Paths.

10/18/2015 Daniel Bauer

1

slide-2
SLIDE 2

Today: Graph Traversals

  • Depth First Search (a generalization of pre-order

traversal on trees to graphs, users a Stack)

  • Breadth First Search (uses a Queue)
  • Dijkstra’s algorithm to find weighted shortest paths

(uses a Priority Queue)

  • Topological sort for Directed Acyclic Graphs.
  • Application: Shortest Project Completion Time.

2

slide-3
SLIDE 3
  • A Graph is a pair of two sets G=(V,E):
  • V: the set of vertices (or nodes)
  • E: the set of edges.
  • each edge is a pair (v,w) where

v,w ∈ V


Graphs

v1 v3 v4 v5 v2 v6

V = {v1, v2, v3, v4, v5, v6 } E = {(v1, v2), (v1, v3), (v2, v3),(v2, v5),(v3, v4),
 (v3, v6),(v4, v5), (v4, v6), (v5, v6)}

3

slide-4
SLIDE 4

Edges

  • Graphs may be directed or undirected.
  • In directed graphs, the edge pairs are ordered.
  • Edges often have some weight or cost associated

with them (weighted graphs).

v1 v3 v4 v5 v2 v6

E = {(v1, v3), (v2, v1),(v2, v3), (v3, v4), 
 (v3, v5), (v4, v6), (v5, v6)} V = {v1, v2, v3, v4, v5, v6 } directed and weighted graph

1 3 5 6 2 3 1

4

slide-5
SLIDE 5

Paths

v1 v3

v4

v5

v2

v6

1 3 5 6 2 3 1

Path from v1 to v6, length 3, cost 8 (v1, v3), (v3, v5), (v5, v6)

  • length of a path: 


k-1 = number of edges on path

  • cost of a path:


Sum of all edge costs. 


5

  • Vertex w is adjacent to vertex v iff (w,v) ∈ E.
  • A path is a sequence of vertices w1, w2, …, wk


such that (wi, wi+1) ∈ E.

slide-6
SLIDE 6
  • Represent graph G = (E,V), option 2: Adjacency Lists
  • For each vertex, keep a list of all adjacent vertices.

Representing Graphs

v0 v2 v3 v4 v1 v5

1 2 3 5 4 4 3

v0 v1 v2 v3 v4 v5 v0:1 v2:3 v2:2 v3:3 v4:4 v5:3 v5:4

6

slide-7
SLIDE 7
  • Represent graph G = (E,V), option 2: Adjacency Lists
  • For each vertex, keep a list of all adjacent vertices.

Representing Graphs

v0 v2 v3 v4 v1 v5

1 2 3 5 4 4 3

v0 v1 v2 v3 v4 v5 v0:1 v2:3 v2:2 v3:3 v4:4 v5:3 v5:4 Space requirement:

6

slide-8
SLIDE 8

Graph Search

7

slide-9
SLIDE 9

Graph Search

8

slide-10
SLIDE 10

Graph Search

G

Letters indicate junctions where a decision must be made.

H

9

slide-11
SLIDE 11

Graph Search

A B C D E G

H

H F

10

slide-12
SLIDE 12

Graph Search

A B C D E G

H

H F

11

slide-13
SLIDE 13

Graph Search: Depth First Search (DFS)

  • Goal: Systematically explore the graph, starting at vertex s

(source) touching all edges.

  • Graph Traversals are the core ingredient of most graph

algorithms.

v1 v2 v3 v4 v5 v6 v7

  • Push s to the stack.
  • While the stack is not empty:
  • u <- stack.pop()
  • push all vertices adjacent to u


to the stack.

v1 Use a stack.

12

slide-14
SLIDE 14

Graph Search: Depth First Search (DFS)

  • Goal: Systematically explore the graph, starting at vertex s

(source) touching all edges.

  • Graph Traversals are the core ingredient of most graph

algorithms.

v1

v2

v3

v4

v5 v6 v7

v2 v4

  • Push s to the stack.
  • While the stack is not empty:
  • u <- stack.pop()
  • push all vertices adjacent to u


to the stack.

Use a stack.

13

slide-15
SLIDE 15

Graph Search: Depth First Search (DFS)

  • Goal: Systematically explore the graph, starting at vertex s

(source) touching all edges.

  • Graph Traversals are the core ingredient of most graph

algorithms.

v1 v2

v3

v4

v5 v6 v7

v2 v3 v6 v5 v7

  • Push s to the stack.
  • While the stack is not empty:
  • u <- stack.pop()
  • push all vertices adjacent to u


to the stack.

Use a stack.

14

slide-16
SLIDE 16
  • Push s to the stack.
  • While the stack is not empty:
  • u <- stack.pop()
  • push all vertices adjacent to u


to the stack.

Use a stack. Use a stack.

Graph Search: Depth First Search (DFS)

  • Goal: Systematically explore the graph, starting at vertex s

(source) touching all edges.

  • Graph Traversals are the core ingredient of most graph

algorithms.

v1

v2 v3 v4 v5

v6

v7

v2 v6 v5 v7

15

slide-17
SLIDE 17
  • Push s to the stack.
  • While the stack is not empty:
  • u <- stack.pop()
  • push all vertices adjacent to u


to the stack.

Use a stack. Use a stack.

Graph Search: Depth First Search (DFS)

  • Goal: Systematically explore the graph, starting at vertex s

(source) touching all edges.

  • Graph Traversals are the core ingredient of most graph

algorithms.

v1

v2 v3 v4 v5

v6

v7

v2 v6 v5 v7

Problem: This Graph contains cycles!

15

slide-18
SLIDE 18

Depth First Search (DFS) with Visited Set

v1 v2 v3 v4 v5 v6 v7

v1

  • Push s to the stack.
  • While the stack is not empty:
  • u <- stack.pop()
  • if u is not in visited:
  • add u to visited.
  • push all vertices adjacent to u


to the stack.

Use a stack and a set visited. visited {}

16

slide-19
SLIDE 19

Depth First Search (DFS) with Visited Set

v1

v2

v3

v4

v5 v6 v7

v2 Visited: {v1} v4

  • Push s to the stack.
  • While the stack is not empty:
  • u <- stack.pop()
  • if u is not in visited:
  • add u to visited.
  • push all vertices adjacent to u


to the stack.

Use a stack and a set visited.

17

slide-20
SLIDE 20

Depth First Search (DFS) with Visited Set

v1 v2

v3

v4

v5 v6 v7

v2 Visited: {v1,v4} v5 v7 v6 v3

  • Push s to the stack.
  • While the stack is not empty:
  • u <- stack.pop()
  • if u is not in visited:
  • add u to visited.
  • push all vertices adjacent to u


to the stack.

Use a stack and a set visited.

18

slide-21
SLIDE 21

Depth First Search (DFS) with Visited Set

v1

v2 v3 v4 v5

v6

v7

v2 Visited: {v1,v4,v3} v5 v7 v6

  • Push s to the stack.
  • While the stack is not empty:
  • u <- stack.pop()
  • if u is not in visited:
  • add u to visited.
  • push all vertices adjacent to u


to the stack.

Use a stack and a set visited. v6 v1

19

slide-22
SLIDE 22

Depth First Search (DFS) with Visited Set

v1 v2 v3 v4 v5 v6 v7

v2 Visited: {v1,v4,v3} v5 v7 v6

  • Push s to the stack.
  • While the stack is not empty:
  • u <- stack.pop()
  • if u is not in visited:
  • add u to visited.
  • push all vertices adjacent to u


to the stack.

Use a stack and a set visited. v6

20

slide-23
SLIDE 23

Depth First Search (DFS) with Visited Set

v1 v2 v3 v4 v5 v6 v7

v2 Visited: {v1,v4,v3,v6} v5 v7 v6

  • Push s to the stack.
  • While the stack is not empty:
  • u <- stack.pop()
  • if u is not in visited:
  • add u to visited.
  • push all vertices adjacent to u


to the stack.

Use a stack and a set visited.

21

slide-24
SLIDE 24

Depth First Search (DFS) with Visited Set

v1 v2 v3 v4 v5 v6 v7

v2 Visited: v5 v7

  • Push s to the stack.
  • While the stack is not empty:
  • u <- stack.pop()
  • if u is not in visited:
  • add u to visited.
  • push all vertices adjacent to u


to the stack.

Use a stack and a set visited. {v1,v4,v3,v6}

22

slide-25
SLIDE 25

Depth First Search (DFS) with Visited Set

v1 v2 v3 v4 v5

v6

v7

v2 Visited: v5

  • Push s to the stack.
  • While the stack is not empty:
  • u <- stack.pop()
  • if u is not in visited:
  • add u to visited.
  • push all vertices adjacent to u


to the stack.

Use a stack and a set visited. {v1,v4,v3,v6,v7}

23

v6

slide-26
SLIDE 26

Depth First Search (DFS) with Visited Set

v1 v2 v3 v4 v5

v6

v7

v2 Visited: v5

  • Push s to the stack.
  • While the stack is not empty:
  • u <- stack.pop()
  • if u is not in visited:
  • add u to visited.
  • push all vertices adjacent to u


to the stack.

Use a stack and a set visited. {v1,v4,v3,v6,v7}

24

slide-27
SLIDE 27

Depth First Search (DFS) with Visited Set

v1 v2 v3 v4 v5 v6

v7

v2 Visited:

  • Push s to the stack.
  • While the stack is not empty:
  • u <- stack.pop()
  • if u is not in visited:
  • add u to visited.
  • push all vertices adjacent to u


to the stack.

Use a stack and a set visited. {v1,v4,v3,v6,v7,v5}

25

v7

slide-28
SLIDE 28

Depth First Search (DFS) with Visited Set

v1 v2 v3 v4 v5 v6

v7

v2 Visited:

  • Push s to the stack.
  • While the stack is not empty:
  • u <- stack.pop()
  • if u is not in visited:
  • add u to visited.
  • push all vertices adjacent to u


to the stack.

Use a stack and a set visited. {v1,v4,v3,v6,v7,v5}

26

slide-29
SLIDE 29

Depth First Search (DFS) with Visited Set

v1 v2 v3 v4 v5 v6 v7

Visited:

  • Push s to the stack.
  • While the stack is not empty:
  • u <- stack.pop()
  • if u is not in visited:
  • add u to visited.
  • push all vertices adjacent to u


to the stack.

Use a stack and a set visited. {v1,v4,v3,v6,v7,v5,v2}

27

slide-30
SLIDE 30

Depth First Search (DFS) with Visited Set

v1 v2 v3 v4 v5 v6 v7

Visited:

  • Push s to the stack.
  • While the stack is not empty:
  • u <- stack.pop()
  • if u is not in visited:
  • add u to visited.
  • push all vertices adjacent to u


to the stack.

Use a stack and a set visited. {v1,v4,v3,v6,v7,v5,v2}

Running time: O(|E|)

27

slide-31
SLIDE 31

Recursive DFS (with visited marker kept on vertex objects)

v1

v2 v3 v4 v5 v6 v7

28

void dfs( Vertex v ) { v.visited = true; for each Vertex w adjacent to v if( !w.visited ) dfs( w ); }

DFS Spanning Tree v1

slide-32
SLIDE 32

Recursive DFS (with visited marker kept on vertex objects)

v1

v2 v3

v4

v5 v6 v7

29

void dfs( Vertex v ) { v.visited = true; for each Vertex w adjacent to v if( !w.visited ) dfs( w ); }

DFS Spanning Tree v1 v4

slide-33
SLIDE 33

Recursive DFS (with visited marker kept on vertex objects)

v1

v2

v3 v4

v5 v6 v7

30

void dfs( Vertex v ) { v.visited = true; for each Vertex w adjacent to v if( !w.visited ) dfs( w ); }

DFS Spanning Tree v1 v4 v3

slide-34
SLIDE 34

Recursive DFS (with visited marker kept on vertex objects)

v1

v2

v3 v4

v5

v6

v7

31

void dfs( Vertex v ) { v.visited = true; for each Vertex w adjacent to v if( !w.visited ) dfs( w ); }

DFS Spanning Tree v1 v4 v3 v6

slide-35
SLIDE 35

Recursive DFS (with visited marker kept on vertex objects)

v1

v2

v3 v4

v5

v6 v7

32

void dfs( Vertex v ) { v.visited = true; for each Vertex w adjacent to v if( !w.visited ) dfs( w ); }

DFS Spanning Tree v1 v4 v3 v6 v7

slide-36
SLIDE 36

Recursive DFS (with visited marker kept on vertex objects)

v1

v2

v3 v4 v5 v6 v7

33

void dfs( Vertex v ) { v.visited = true; for each Vertex w adjacent to v if( !w.visited ) dfs( w ); }

DFS Spanning Tree v1 v4 v3 v6 v7 v5

slide-37
SLIDE 37

Recursive DFS (with visited marker kept on vertex objects)

v1 v2 v3 v4 v5 v6 v7

34

void dfs( Vertex v ) { v.visited = true; for each Vertex w adjacent to v if( !w.visited ) dfs( w ); }

DFS Spanning Tree v1 v4 v3 v6 v7 v5 v2

slide-38
SLIDE 38

DFS on the Entire Graph

v1

v2 v3 v4 v5 v6 v7

35

  • It is possible that not all vertices are reachable from a

designated start vertex.

slide-39
SLIDE 39

v1 v2

v3 v4 v5 v6 v7

36

  • It is possible that not all vertices are reachable from a

designated start vertex.

DFS on the Entire Graph

slide-40
SLIDE 40

v1 v2

v3

v4

v5 v6 v7

37

  • It is possible that not all vertices are reachable from a

designated start vertex.

DFS on the Entire Graph

slide-41
SLIDE 41

v1 v2

v3

v4 v5

v6 v7

38

  • It is possible that not all vertices are reachable from a

designated start vertex. v1: v2, v4 v2: v4, v5
 v3: v1, v6
 v4: v5
 v5: 
 v6: v7
 v7: v5

DFS on the Entire Graph

slide-42
SLIDE 42

v1 v2 v3 v4 v5

v6 v7

39

  • If stack is empty or we reach top of recursion, scan through

adjacency list until we find an unseen starting node.

  • It is possible that not all vertices are reachable from a

designated start vertex. v1: v2, v4 v2: v4, v5
 v3: v1, v6
 v4: v5
 v5: 
 v6: v7
 v7: v5

DFS on the Entire Graph

slide-43
SLIDE 43

v1 v2 v3 v4 v5

v6 v7

39

  • If stack is empty or we reach top of recursion, scan through

adjacency list until we find an unseen starting node.

  • It is possible that not all vertices are reachable from a

designated start vertex. v1: v2, v4 v2: v4, v5
 v3: v1, v6
 v4: v5
 v5: 
 v6: v7
 v7: v5

Running time for complete DFS traversal: O(|V|+|E|)

DFS on the Entire Graph

slide-44
SLIDE 44

Breadth-First Search (BFS)

v1 v2 v3 v4 v5 v6 v7

  • Enqueue s
  • Add s to visited
  • While the queue is not empty:
  • u <- dequeue()
  • for each vertex v that is 


adjacent to u:

  • if v is not in visited:
  • Add v to visited.
  • enqueue(v).

Use a queue and a set visited. Visited: {v1} Queue v1

40

slide-45
SLIDE 45

Breadth-First Search (BFS)

v1 v2 v3 v4 v5 v6 v7

  • Enqueue s
  • Add s to visited
  • While the queue is not empty:
  • u <- dequeue()
  • for each vertex v that is 


adjacent to u:

  • if v is not in visited:
  • Add v to visited.
  • enqueue(v).

Use a queue and a set visited. Visited: {v1,v2,v4} Queue v2 v4

41

slide-46
SLIDE 46

Breadth-First Search (BFS)

v1 v2 v3 v4 v5 v6 v7

  • Enqueue s
  • Add s to visited
  • While the queue is not empty:
  • u <- dequeue()
  • for each vertex v that is 


adjacent to u:

  • if v is not in visited:
  • Add v to visited.
  • enqueue(v).

Use a queue and a set visited. Visited: {v1,v2,v4,v5} Queue v4 v5

42

slide-47
SLIDE 47

Breadth-First Search (BFS)

v1 v2 v3 v4 v5 v6 v7

  • Enqueue s
  • Add s to visited
  • While the queue is not empty:
  • u <- dequeue()
  • for each vertex v that is 


adjacent to u:

  • if v is not in visited:
  • Add v to visited.
  • enqueue(v).

Use a queue and a set visited. Visited: {v1,v2,v4,v5,v7,v6,v3} Queue v5 v7 v6 v3

43

slide-48
SLIDE 48

Breadth-First Search (BFS)

v1 v2 v3 v4 v5 v6 v7

  • Enqueue s
  • Add s to visited
  • While the queue is not empty:
  • u <- dequeue()
  • for each vertex v that is 


adjacent to u:

  • if v is not in visited:
  • Add v to visited.
  • enqueue(v).

Use a queue and a set visited. Visited: {v1,v2,v4,v5,v7,v6,v3} Queue v7 v6 v3

44

slide-49
SLIDE 49

Breadth-First Search (BFS)

v1 v2 v3 v4 v5 v6 v7

  • Enqueue s
  • Add s to visited
  • While the queue is not empty:
  • u <- dequeue()
  • for each vertex v that is 


adjacent to u:

  • if v is not in visited:
  • Add v to visited.
  • enqueue(v).

Use a queue and a set visited. Visited: {v1,v2,v4,v5,v7,v6,v3} Queue v6 v3

45

slide-50
SLIDE 50

Breadth-First Search (BFS)

v1 v2 v3 v4 v5 v6 v7

  • Enqueue s
  • Add s to visited
  • While the queue is not empty:
  • u <- dequeue()
  • for each vertex v that is 


adjacent to u:

  • if v is not in visited:
  • Add v to visited.
  • enqueue(v).

Use a queue and a set visited. Visited: {v1,v2,v4,v5,v7,v6,v3} Queue v3

46

slide-51
SLIDE 51

Breadth-First Search (BFS)

v1 v2 v3 v4 v5 v6 v7

  • Enqueue s
  • Add s to visited
  • While the queue is not empty:
  • u <- dequeue()
  • for each vertex v that is 


adjacent to u:

  • if v is not in visited:
  • Add v to visited.
  • enqueue(v).

Use a queue and a set visited. Visited: {v1,v2,v4,v5,v7,v6,v3} Queue

47

slide-52
SLIDE 52

Breadth-First Search (BFS)

v1 v2 v3 v4 v5 v6 v7

  • Enqueue s
  • Add s to visited
  • While the queue is not empty:
  • u <- dequeue()
  • for each vertex v that is 


adjacent to u:

  • if v is not in visited:
  • Add v to visited.
  • enqueue(v).

Use a queue and a set visited. Visited: {v1,v2,v4,v5,v7,v6,v3} Queue

Running time (to traverse the entire graph): O(|V|+|E|)

47

slide-53
SLIDE 53

Breadth-First Search (BFS)

v1 v2 v3 v4 v5 v6 v7

  • Enqueue s
  • Add s to visited
  • While the queue is not empty:
  • u <- dequeue()
  • for each vertex v that is 


adjacent to u:

  • if v is not in visited:
  • Add v to visited.
  • enqueue(v).

Use a queue and a set visited. Visited: {v1,v2,v4,v5,v7,v6,v3} Queue

Running time (to traverse the entire graph): O(|V|+|E|) BFS will traverse the entire graph even without a visited set. DFS can get stuck in a loop.

47

slide-54
SLIDE 54

Finding Shortest Paths

  • Goal: Find the shortest path between two vertices s and t.

v1 v2 v3 v4 v5 v6 v7

What is the shortest path between v3 and v7?

48

slide-55
SLIDE 55

Finding Shortest Paths

  • Goal: Find the shortest path between two vertices s and t.

v1 v2 v3 v4 v5 v6 v7

What is the shortest path between v3 and v7? length 3

v1 v4 v7 v3

49

slide-56
SLIDE 56

Finding Shortest Paths

  • It turns out that finding the shortest path between s and

ALL other vertices is just as easy. This problem is called single-source shortest paths.

v1 v2

v3

v4 v5 v6 v7

  • Goal: Find the shortest path between two vertices s and t.

50

slide-57
SLIDE 57

Finding Shortest Paths

  • It turns out that finding the shortest path between s and

ALL other vertices is just as easy. This problem is called single-source shortest paths.

v1 v2

v3

v4 v5 v6 v7

  • Goal: Find the shortest path between two vertices s and t.

1 1 2 2 3 3

50

slide-58
SLIDE 58

Finding Shortest Paths

  • It turns out that finding the shortest path between s and

ALL other vertices is just as easy. This problem is called single-source shortest paths.

v1 v2

v3

v4 v5 v6 v7

  • Goal: Find the shortest path between two vertices s and t.

1 1 2 2 3 3

50

slide-59
SLIDE 59

Finding Shortest Paths with BFS

v1 v2 v4 v5 v6 v7

∞ ∞ ∞ ∞ ∞ ∞

  • s.distance = 0
  • for all v ∈ V set v.distance = ∞
  • enqueue s
  • While the queue is not empty:
  • u <- dequeue()
  • for each vertex v that is adjacent 


to u:

  • if v.distance == ∞
  • v.distance = u.distance + 1
  • enqueue(v)

Queue v3

v3

51

slide-60
SLIDE 60

Finding Shortest Paths with BFS

v1 v2

v3

v4 v5 v6 v7

1

∞ ∞ ∞ ∞

1

Queue v1 v6

  • s.distance = 0
  • for all v ∈ V set v.distance = ∞
  • enqueue s
  • While the queue is not empty:
  • u <- dequeue()
  • for each vertex v that is adjacent 


to u:

  • if v.distance == ∞
  • v.distance = u.distance + 1
  • enqueue(v)

52

slide-61
SLIDE 61

Finding Shortest Paths with BFS

v1

v2

v3

v4 v5 v6 v7

1 2

∞ ∞

2 1

Queue v6 v2 v4

  • s.distance = 0
  • for all v ∈ V set v.distance = ∞
  • enqueue s
  • While the queue is not empty:
  • u <- dequeue()
  • for each vertex v that is adjacent 


to u:

  • if v.distance == ∞
  • v.distance = u.distance + 1
  • enqueue(v)

53

slide-62
SLIDE 62

Finding Shortest Paths with BFS

v1

v2

v3

v4 v5

v6

v7

1 2

∞ ∞

2 1

Queue v2 v4

  • s.distance = 0
  • for all v ∈ V set v.distance = ∞
  • enqueue s
  • While the queue is not empty:
  • u <- dequeue()
  • for each vertex v that is adjacent 


to u:

  • if v.distance == ∞
  • v.distance = u.distance + 1
  • enqueue(v)

54

slide-63
SLIDE 63

Finding Shortest Paths with BFS

v1 v2 v3

v4 v5

v6

v7

1 2

3 2 1

Queue v4 v5

  • s.distance = 0
  • for all v ∈ V set v.distance = ∞
  • enqueue s
  • While the queue is not empty:
  • u <- dequeue()
  • for each vertex v that is adjacent 


to u:

  • if v.distance == ∞
  • v.distance = u.distance + 1
  • enqueue(v)

55

slide-64
SLIDE 64

Finding Shortest Paths with BFS

v1 v2 v3 v4

v5

v6

v7

1 2 3 3 2 1

Queue v5 v7

  • s.distance = 0
  • for all v ∈ V set v.distance = ∞
  • enqueue s
  • While the queue is not empty:
  • u <- dequeue()
  • for each vertex v that is adjacent 


to u:

  • if v.distance == ∞
  • v.distance = u.distance + 1
  • enqueue(v)

56

slide-65
SLIDE 65

Finding Shortest Paths with BFS

v1 v2 v3 v4 v5 v6

v7

1 2 3 3 2 1

Queue v7

  • s.distance = 0
  • for all v ∈ V set v.distance = ∞
  • enqueue s
  • While the queue is not empty:
  • u <- dequeue()
  • for each vertex v that is adjacent 


to u:

  • if v.distance == ∞
  • v.distance = u.distance + 1
  • enqueue(v)

57

slide-66
SLIDE 66
  • s.distance = 0
  • for all v ∈ V set v.distance = ∞
  • enqueue s
  • While the queue is not empty:
  • u <- dequeue()
  • for each vertex v that is adjacent 


to u:

  • if v.distance == ∞
  • v.distance = u.distance + 1
  • enqueue(u)

Finding Shortest Paths with BFS

v1 v2 v3 v4 v5 v6 v7

1 2 3 3 2 1

Queue

58

slide-67
SLIDE 67
  • s.distance = 0
  • for all v ∈ V set v.distance = ∞
  • enqueue s
  • While the queue is not empty:
  • u <- dequeue()
  • for each vertex v that is adjacent 


to u:

  • if v.distance == ∞
  • v.distance = u.distance + 1
  • enqueue(u)

Finding Shortest Paths with BFS

v1 v2 v3 v4 v5 v6 v7

1 2 3 3 2 1

Queue

This is just BFS. Running time: O(|V|+|E|)

58

slide-68
SLIDE 68

Finding Shortest Paths with BFS - Back pointers

v1 v2

v3

v4 v5 v6 v7

1

∞ ∞ ∞ ∞

1

Queue v1 v6 Maintain pointers to the previous node on the shortest path.

  • s.distance = 0
  • for all v ∈ V set v.distance = ∞
  • enqueue s
  • While the queue is not empty:
  • u <- dequeue()
  • for each vertex v that is adjacent 


to u:

  • if v.distance == ∞
  • v.prev = u
  • v.distance = u.distance + 1
  • enqueue(v)

59

slide-69
SLIDE 69

Finding Shortest Paths with BFS - Back pointers

v1 v2

v3

v4 v5 v6 v7

1

2 ∞ ∞ 2

1

Queue Maintain pointers to the previous node on the shortest path.

  • s.distance = 0
  • for all v ∈ V set v.distance = ∞
  • enqueue s
  • While the queue is not empty:
  • u <- dequeue()
  • for each vertex v that is adjacent 


to u:

  • if v.distance == ∞
  • v.prev = u
  • v.distance = u.distance + 1
  • enqueue(v)

v6 v2 v4

60

slide-70
SLIDE 70

Weighted Shortest Paths

  • Goal: Find the shortest path between two vertices s and t.

v1 v2 v3 v4 v5 v6 v7

What is the shortest path between v2 and v6?

2 4 2 1 3 10 2 4 6 1 5 8

61

slide-71
SLIDE 71

Weighted Shortest Paths

  • Goal: Find the shortest path between two vertices s and t.

v1

v2

v3

v4

v5

v6

v7

What is the shortest path between v2 and v6?

2 4 2 1 3 10 2 4 6 1 5 8

length 2 
 cost 11

  • Normal BFS will find this path.

62

slide-72
SLIDE 72

Weighted Shortest Paths

  • Goal: Find the shortest path between two vertices s and t.

v1

v2

v3

v4

v5

v6 v7

What is the shortest path between v2 and v6?

2 4 2 1 3 10 2 4 6 1 5 8

length 3 
 cost 8

  • This path is shorter.

63

slide-73
SLIDE 73

Negative Weights

  • We normally expect the shortest path to be simple.
  • Edges with Negative Weights can lead to negative cycles.
  • The concept of “shortest path” is then not clearly defined.

v1 v2 v3 v4

v5

v6 v7

What is the shortest path between v2 and v6?

2 4 2

  • 7

3 10 2 4 6 1 5 8

64

slide-74
SLIDE 74

Dijkstra’s Algorithm for Weighted Shortest Path

65

slide-75
SLIDE 75

Dijkstra’s Algorithm for Weighted Shortest Path

  • Cost annotations for each vertex reflect the lowest

cost using only vertices visited so far.

65

slide-76
SLIDE 76

Dijkstra’s Algorithm for Weighted Shortest Path

  • Cost annotations for each vertex reflect the lowest

cost using only vertices visited so far.

  • That means there might be a lower-cost path

through other vertices that have not been seen yet.

65

slide-77
SLIDE 77

Dijkstra’s Algorithm for Weighted Shortest Path

  • Cost annotations for each vertex reflect the lowest

cost using only vertices visited so far.

  • That means there might be a lower-cost path

through other vertices that have not been seen yet.

  • Keep nodes on a priority queue and always

expand the vertex with the lowest cost annotation first!

  • Intuitively, this means we will never
  • verestimate the cost and miss lower-cost path.

65

slide-78
SLIDE 78

Dijkstra’s Algorithm for Weighted Shortest Path

  • Cost annotations for each vertex reflect the lowest

cost using only vertices visited so far.

  • That means there might be a lower-cost path

through other vertices that have not been seen yet.

  • Keep nodes on a priority queue and always

expand the vertex with the lowest cost annotation first!

  • Intuitively, this means we will never
  • verestimate the cost and miss lower-cost path.

65

← This is a greedy algorithm

slide-79
SLIDE 79

Dijkstra’s Algorithm

v2 v3 v4 v5 v6 v7

2 4 2 1 3 10 2 4 6 1 5 8

Use a Priority Queue q

  • for all v ∈ V 


set v.cost = ∞, set v.visited = false

  • s.cost = 0, s.visited = true;
  • q.insert(s)

  • While q is not empty:
  • u <- q.deleteMin()
  • u.visited = true
  • for each edge (u,v):
  • if not v.visited:
  • if (u.cost + cost(u,v) < v.cost)
  • v.cost = u.cost + cost(u,v)
  • v.prev = u
  • q.insert(v)

∞ ∞ ∞ ∞ ∞ ∞

v1

66

slide-80
SLIDE 80

Use a Priority Queue q

  • for all v ∈ V 


set v.cost = ∞, set v.visited = false

  • s.cost = 0, s.visited = true;
  • q.insert(s)

  • While q is not empty:
  • u <- q.deleteMin()
  • u.visited = true
  • for each edge (u,v):
  • if not v.visited:
  • if (u.cost + cost(u,v) < v.cost)
  • v.cost = u.cost + cost(u,v)
  • v.prev = u
  • q.insert(v)

Dijkstra’s Algorithm

v1

v2 v3 v4 v5 v6 v7

2 4 2 1 3 10 2 4 6 1 5 8

2 1 ∞ ∞ ∞ ∞

67

slide-81
SLIDE 81

Use a Priority Queue q

  • for all v ∈ V 


set v.cost = ∞, set v.visited = false

  • s.cost = 0, s.visited = true;
  • q.insert(s)

  • While q is not empty:
  • u <- q.deleteMin()
  • u.visited = true
  • for each edge (u,v):
  • if not v.visited:
  • if (u.cost + cost(u,v) < v.cost)
  • v.cost = u.cost + cost(u,v)
  • v.prev = u
  • q.insert(v)

Dijkstra’s Algorithm

v1

v2 v3

v4

v5 v6 v7

2 4 2 1 3 10 2 4 6 1 5 8

2 1 3 9 5 3

68

slide-82
SLIDE 82

Use a Priority Queue q

  • for all v ∈ V 


set v.cost = ∞, set v.visited = false

  • s.cost = 0, s.visited = true;
  • q.insert(s)

  • While q is not empty:
  • u <- q.deleteMin()
  • u.visited = true
  • for each edge (u,v):
  • if not v.visited:
  • if (u.cost + cost(u,v) < v.cost)
  • v.cost = u.cost + cost(u,v)
  • v.prev = u
  • q.insert(v)

Dijkstra’s Algorithm

v1 v2

v3

v4

v5 v6 v7

2 4 2 1 3 10 2 4 6 1 5 8

2 1 3 9 5 3

69

slide-83
SLIDE 83

Use a Priority Queue q

  • for all v ∈ V 


set v.cost = ∞, set v.visited = false

  • s.cost = 0, s.visited = true;
  • q.insert(s)

  • While q is not empty:
  • u <- q.deleteMin()
  • u.visited = true
  • for each edge (u,v):
  • if not v.visited:
  • if (u.cost + cost(u,v) < v.cost)
  • v.cost = u.cost + cost(u,v)
  • v.prev = u
  • q.insert(v)

Dijkstra’s Algorithm

v1 v2

v3

v4 v5

v6 v7

2 4 2 1 3 10 2 4 6 1 5 8

2 1 3 9 5 3

70

slide-84
SLIDE 84

Use a Priority Queue q

  • for all v ∈ V 


set v.cost = ∞, set v.visited = false

  • s.cost = 0, s.visited = true;
  • q.insert(s)

  • While q is not empty:
  • u <- q.deleteMin()
  • u.visited = true
  • for each edge (u,v):
  • if not v.visited:
  • if (u.cost + cost(u,v) < v.cost)
  • v.cost = u.cost + cost(u,v)
  • v.prev = u
  • q.insert(v)

Dijkstra’s Algorithm

v1 v2 v3 v4 v5

v6 v7

2 4 2 1 3 10 2 4 6 1 5 8

2 1 3 9 5 3

71

slide-85
SLIDE 85

Use a Priority Queue q

  • for all v ∈ V 


set v.cost = ∞, set v.visited = false

  • s.cost = 0, s.visited = true;
  • q.insert(s)

  • While q is not empty:
  • u <- q.deleteMin()
  • u.visited = true
  • for each edge (u,v):
  • if not v.visited:
  • if (u.cost + cost(u,v) < v.cost)
  • v.cost = u.cost + cost(u,v)
  • v.prev = u
  • q.insert(v)

Dijkstra’s Algorithm

v1 v2 v3 v4 v5

v6

v7

2 4 2 1 3 10 2 4 6 1 5 8

2 1 3 6 5 3

72

slide-86
SLIDE 86

Use a Priority Queue q

  • for all v ∈ V 


set v.cost = ∞, set v.visited = false

  • s.cost = 0, s.visited = true;
  • q.insert(s)

  • While q is not empty:
  • u <- q.deleteMin()
  • u.visited = true
  • for each edge (u,v):
  • if not v.visited:
  • if (u.cost + cost(u,v) < v.cost)
  • v.cost = u.cost + cost(u,v)
  • v.prev = u
  • q.insert(v)

Dijkstra’s Algorithm

v1 v2 v3 v4 v5 v6 v7

2 4 2 1 3 10 2 4 6 1 5 8

2 1 3 6 5 3

73

slide-87
SLIDE 87
  • While q is not empty:
  • u <- q.deleteMin()
  • u.visited = true
  • for each edge (u,v):
  • if not v.visited:
  • if (u.cost + cost(u,v) < v.cost)
  • v.cost = u.cost + cost(u,v)
  • v.prev = u
  • q.insert(v)

Dijkstra’s Algorithm - a subtle bug

v1 v2 v3 v4 v5

v6

v7

2 4 2 1 3 10 2 4 6 1 5 8

2 1 3 9 5 3

74

v7.cost +cost(v6,v7) = 6

slide-88
SLIDE 88
  • While q is not empty:
  • u <- q.deleteMin()
  • u.visited = true
  • for each edge (u,v):
  • if not v.visited:
  • if (u.cost + cost(u,v) < v.cost)
  • v.cost = u.cost + cost(u,v)
  • v.prev = u
  • q.insert(v)

Dijkstra’s Algorithm - a subtle bug

v1 v2 v3 v4 v5

v6

v7

2 4 2 1 3 10 2 4 6 1 5 8

2 1 3 9 5 3

74

v7.cost +cost(v6,v7) = 6

  • v7 is already in q, and has not been visited.
  • does insert(v7) create a new entry in the q or update the existing one?
  • if q is a heap, updating the cost will change v7 everywhere in the heap

and might make the heap invalid.

slide-89
SLIDE 89

Dijkstra’s Algorithm - Fixed

v1 v2 v3 v4 v5

v6

v7

2 4 2 1 3 10 2 4 6 1 5 8

2 1 3 9 5 3

75

v7.cost +cost(v6,v7) = 6

Use a Priority Queue q

  • for all v ∈ V 


set v.cost = ∞, set v.visited = false

  • s.cost = 0
  • q.insert((0, s))

  • While q is not empty:
  • (costu, u) <- q.deleteMin()
  • if not u.visited:
  • u.visited = true
  • for each edge (u,v):
  • if not v.visited:
  • if (costu + cost(u,v) < v.cost)
  • v.cost = u.cost + cost(u,v)
  • v.prev = u
  • q.insert((v.cost, v))
  • Keep a separate cost
  • bject in the queue

that isn’t updated.

  • Ignore duplicate

entries for vertices.

slide-90
SLIDE 90

Dijkstra’s Running Time

  • There are |E| insert and

deleteMin operations. 


  • The maximum size of

the priority queue is O(|E|). Each insert takes O(log |E|) 


O(|E| log |E|)

76

Use a Priority Queue q

  • for all v ∈ V 


set v.cost = ∞, set v.visited = false

  • s.cost = 0
  • q.insert((0, s))

  • While q is not empty:
  • (costu, u) <- q.deleteMin()
  • if not u.visited:
  • u.visited = true
  • for each edge (u,v):
  • if not v.visited:
  • if (ucost + cost(u,v) < v.cost)
  • v.cost = u.cost + cost(u,v)
  • v.prev = u
  • q.insert((v.cost, v))
slide-91
SLIDE 91

Dijkstra’s Running Time

  • There are |E| insert and

deleteMin operations. 


  • The maximum size of

the priority queue is O(|E|). Each insert takes O(log |E|) 


O(|E| log |E|)

76

Use a Priority Queue q

  • for all v ∈ V 


set v.cost = ∞, set v.visited = false

  • s.cost = 0
  • q.insert((0, s))

  • While q is not empty:
  • (costu, u) <- q.deleteMin()
  • if not u.visited:
  • u.visited = true
  • for each edge (u,v):
  • if not v.visited:
  • if (ucost + cost(u,v) < v.cost)
  • v.cost = u.cost + cost(u,v)
  • v.prev = u
  • q.insert((v.cost, v))

because |E| ≤ |V|2, 
 and therefore log |E| ≤ 2 log |V|

=O(|E| log |V|)

slide-92
SLIDE 92

W1004 W3134 W1007 W3137 W3157 W3203 W3261 W4111 W4115 W4156 W4701

A topological sort of a DAG is an ordering of its vertices
 such that if there is a path from u to w, u appears before w in the ordering.

77

Topological Sort in DAGs

slide-93
SLIDE 93

Topological Sort in DAGs

W1004 W3134 W1007 W3137 W3157 W3203 W3261 W4111 W4115 W4156 W4701

A topological sort of a DAG is an ordering of its vertices
 such that if there is a path from u to w, u appears before w in the ordering.

W1004 W1007

78

slide-94
SLIDE 94

Topological Sort in DAGs

W1004 W3134 W1007 W3137 W3157 W3203 W3261 W4111 W4115 W4156 W4701

A topological sort of a DAG is an ordering of its vertices
 such that if there is a path from u to w, u appears before w in the ordering.

W3134 W3137 W3157 W1004 W3203 W1007

79

slide-95
SLIDE 95

Topological Sort in DAGs

W1004 W3134 W1007 W3137 W3157 W3203 W3261 W4111 W4115 W4156 W4701

A topological sort of a DAG is an ordering of its vertices
 such that if there is a path from u to w, u appears before w in the ordering.

W4111 W4701 W3261 W3134 W3137 W3157 W1004 W3203 W1007

80

slide-96
SLIDE 96

Topological Sort in DAGs

W1004 W3134 W1007 W3137 W3157 W3203 W3261 W4111 W4115 W4156 W4701

A topological sort of a DAG is an ordering of its vertices
 such that if there is a path from u to w, u appears before w in the ordering.

W4115 W4111 W4701 W4156 W3261 W3134 W3137 W3157 W1004 W3203 W1007

81

slide-97
SLIDE 97

Application: Critical Path Analysis

  • An Event-Node Graph is a DAG in which
  • Edges represent tasks, weight represents the

time it takes to complete the task.

  • Vertices represent the event of completing a set
  • f tasks.

write
 articles c

  • m

m i s i

  • n


 p h

  • t
  • g

r a p h s edit proofread layout process 
 photographs 
 printing distribution w e b l a y

  • u

t u p l

  • a

d 


  • n

l i n e v e r s i

  • n

3 2 1 1 1 3 2 3 6 1

82

slide-98
SLIDE 98

Application: Critical Path Analysis

  • We are interested in the earliest completion time.


(Earliest time we can reach the final event).

  • This is equivalent to finding the longest path

through the DAG (why does this not work with cycles?).

write
 articles c

  • m

m i s i

  • n


 p h

  • t
  • g

r a p h s edit proofread layout process 
 photographs 
 printing distribution w e b l a y

  • u

t u p l

  • a

d 


  • n

l i n e v e r s i

  • n

3 2 1 1 1 3 2 3 6 1

83

slide-99
SLIDE 99

Application: Critical Path Analysis

  • If an event has more than one incoming event, all

tasks have to be finished before other tasks can proceed.

write
 articles c

  • m

m i s i

  • n


 p h

  • t
  • g

r a p h s edit proofread layout process 
 photographs 
 printing distribution w e b l a y

  • u

t u p l

  • a

d 


  • n

l i n e v e r s i

  • n

3 2 1 1 1 3 2 3 6 1

84

slide-100
SLIDE 100

Application: Critical Path Analysis

  • Basic idea: Compute the earliest completion time

for each event.

  • Can use Dijkstra’s algorithm O(|E| log |V|).
  • We now try to find the longest path.

write
 articles c

  • m

m i s i

  • n


 p h

  • t
  • g

r a p h s edit proofread layout process 
 photographs 
 printing distribution w e b l a y

  • u

t u p l

  • a

d 


  • n

l i n e v e r s i

  • n

3 2 1 1 1 3 2 3 6 1

85

slide-101
SLIDE 101

Application: Critical Path Analysis

  • Basic idea: Compute the earliest completion time

for each event.

  • Can use Dijkstra’s algorithm O(|E| log |V|).
  • We now try to find the longest path.

3 2 write
 articles c

  • m

m i s i

  • n


 p h

  • t
  • g

r a p h s edit proofread layout process 
 photographs 
 printing distribution w e b l a y

  • u

t u p l

  • a

d 


  • n

l i n e v e r s i

  • n

3 2 1 1 1 3 2 3 6 1

86

slide-102
SLIDE 102

Application: Critical Path Analysis

  • Basic idea: Compute the earliest completion time

for each event.

  • Can use Dijkstra’s algorithm O(|E| log |V|).
  • We now try to find the longest path.

3 4 2 write
 articles c

  • m

m i s i

  • n


 p h

  • t
  • g

r a p h s edit proofread layout process 
 photographs 
 printing distribution w e b l a y

  • u

t u p l

  • a

d 


  • n

l i n e v e r s i

  • n

3 2 1 1 1 3 2 3 6 1

87

slide-103
SLIDE 103

Application: Critical Path Analysis

  • Basic idea: Compute the earliest completion time

for each event.

  • Can use Dijkstra’s algorithm O(|E| log |V|).
  • We now try to find the longest path.

3 4 5 2 write
 articles c

  • m

m i s i

  • n


 p h

  • t
  • g

r a p h s edit proofread layout process 
 photographs 
 printing distribution w e b l a y

  • u

t u p l

  • a

d 


  • n

l i n e v e r s i

  • n

3 2 1 1 1 3 2 3 6 1

88

slide-104
SLIDE 104

Application: Critical Path Analysis

  • Basic idea: Compute the earliest completion time

for each event.

  • Can use Dijkstra’s algorithm O(|E| log |V|).
  • We now try to find the longest path.

3 4 5 2 8 write
 articles c

  • m

m i s i

  • n


 p h

  • t
  • g

r a p h s edit proofread layout process 
 photographs 
 printing distribution w e b l a y

  • u

t 7 u p l

  • a

d 


  • n

l i n e v e r s i

  • n

3 2 1 1 1 3 2 3 6 1

89

slide-105
SLIDE 105

Application: Critical Path Analysis

  • Basic idea: Compute the earliest completion time

for each event.

  • Can use Dijkstra’s algorithm O(|E| log |V|).
  • We now try to find the longest path.

3 4 5 2 8 write
 articles c

  • m

m i s i

  • n


 p h

  • t
  • g

r a p h s edit proofread layout process 
 photographs 
 11 printing distribution w e b l a y

  • u

t 7 u p l

  • a

d 


  • n

l i n e v e r s i

  • n

3 2 1 1 1 3 2 3 6 1

90

slide-106
SLIDE 106

Application: Critical Path Analysis

  • Basic idea: Compute the earliest completion time

for each event.

  • Can use Dijkstra’s algorithm O(|E| log |V|).
  • We now try to find the longest path.

3 4 5 2 8 write
 articles c

  • m

m i s i

  • n


 p h

  • t
  • g

r a p h s edit proofread layout process 
 photographs 
 11 printing distribution 17 w e b l a y

  • u

t 7 u p l

  • a

d 


  • n

l i n e v e r s i

  • n

3 2 1 1 1 3 2 3 6 1

91

slide-107
SLIDE 107

Application: Critical Path Analysis

  • Basic idea: Compute the earliest completion time

for each event.

  • Can use Dijkstra’s algorithm O(|E| log |V|).
  • We now try to find the longest path.

3 4 5 2 8 write
 articles c

  • m

m i s i

  • n


 p h

  • t
  • g

r a p h s edit proofread layout process 
 photographs 
 11 printing distribution 17 w e b l a y

  • u

t 7 u p l

  • a

d 


  • n

l i n e v e r s i

  • n

3 2 1 1 1 3 2 3 6 1

92

slide-108
SLIDE 108

Application: Critical Path Analysis

  • Basic idea: Compute the earliest completion time

for each event.

  • Can use Dijkstra’s algorithm O(|E| log |V|).
  • We now try to find the longest path.

3 4 5 2 8 write
 articles c

  • m

m i s i

  • n


 p h

  • t
  • g

r a p h s edit proofread layout process 
 photographs 
 11 printing distribution 17 w e b l a y

  • u

t 7 u p l

  • a

d 


  • n

l i n e v e r s i

  • n

3 2 1 1 1 3 2 3 6 1

93

slide-109
SLIDE 109

Application: Critical Path Analysis

  • Basic idea: Compute the earliest completion time

for each event.

  • Can use Dijkstra’s algorithm O(|E| log |V|).
  • We now try to find the longest path.

3 4 5 2 8 write
 articles c

  • m

m i s i

  • n


 p h

  • t
  • g

r a p h s edit proofread layout process 
 photographs 
 11 printing distribution 17 w e b l a y

  • u

t 7 u p l

  • a

d 


  • n

l i n e v e r s i

  • n

3 2 1 1 1 3 2 3 6 1

94

slide-110
SLIDE 110

Application: Critical Path Analysis

  • For DAGs we can improve on Dijkstra’s 


O(|E| log |V|) bound.

  • Use topological sort.

write
 articles c

  • m

m i s i

  • n


 p h

  • t
  • g

r a p h s edit proofread layout process 
 photographs 
 printing distribution w e b l a y

  • u

t u p l

  • a

d 


  • n

l i n e v e r s i

  • n

3 2 1 1 1 3 2 3 6 1

95

slide-111
SLIDE 111

Application: Critical Path Analysis

  • Basic idea: Compute the earliest completion time

for each event.

  • Process events in topological order.

3 4 5 2 write
 articles c

  • m

m i s i

  • n


 p h

  • t
  • g

r a p h s edit proofread layout process 
 photographs 
 printing distribution w e b l a y

  • u

t u p l

  • a

d 


  • n

l i n e v e r s i

  • n

3 2 1 1 1 3 2 3 6 1 Need at least 5 time steps to get here

96

slide-112
SLIDE 112

Computing Topological Order

  • Basic idea: Use BFS!
  • To compute topological order, we need to find all

incoming edges to a node first before visiting the node.

write
 articles c

  • m

m i s i

  • n


 p h

  • t
  • g

r a p h s edit proofread layout process 
 photographs 
 printing distribution w e b l a y

  • u

t u p l

  • a

d 


  • n

l i n e v e r s i

  • n

3 2 1 1 1 3 2 3 6 1

97

slide-113
SLIDE 113

Computing Topological Order

  • Example Application: Computing earliest

completion time.

  • First annotate each vertex with the number of

incoming edges (the indegree).

v1 v2 v4 v5 v3 v6 write
 articles c

  • m

m i s i

  • n


 p h

  • t
  • g

r a p h s edit proofread layout process 
 photographs 
 v7 printing distribution v8 w e b l a y

  • u

t v9 u p l

  • a

d 


  • n

l i n e v e r s i

  • n

3 2 1 1 1 3 2 3 6 1 1 1 1 2 1 1 2 1

98

slide-114
SLIDE 114

Computing Topological Order

v1 v2 v4 v5 v3 v6 write
 articles c

  • m

m i s i

  • n


 p h

  • t
  • g

r a p h s edit proofread layout process 
 photographs 
 v7 printing distribution v8 w e b l a y

  • u

t v9 u p l

  • a

d 


  • n

l i n e v e r s i

  • n

3 2 1 1 1 3 2 3 6 1 1 1 1 2 1 1 2 1

  • While the queue is not empty, dequeue a vertex, print it and 


decrement the indegree of its adjacent nodes.

  • If the indegree of any new vertex becomes 0, enqueue it.

Queue: Output: v1

99

slide-115
SLIDE 115

Computing Topological Order

v1 v2 v4 v5 v3 v6 write
 articles c

  • m

m i s i

  • n


 p h

  • t
  • g

r a p h s edit proofread layout process 
 photographs 
 v7 printing distribution v8 w e b l a y

  • u

t v9 u p l

  • a

d 


  • n

l i n e v e r s i

  • n

3 2 1 1 1 3 2 3 6 1 1 2 1 1 2 1

  • While the queue is not empty, dequeue a vertex, print it and 


decrement the indegree of its adjacent nodes.

  • If the indegree of any new vertex becomes 0, enqueue it.

Queue: Output: v1 v2 v3

100

slide-116
SLIDE 116

Computing Topological Order

v1 v2 v4 v5 v3 v6 write
 articles c

  • m

m i s i

  • n


 p h

  • t
  • g

r a p h s edit proofread layout process 
 photographs 
 v7 printing distribution v8 w e b l a y

  • u

t v9 u p l

  • a

d 


  • n

l i n e v e r s i

  • n

3 2 1 1 1 3 2 3 6 1 2 1 1 2 1

  • While the queue is not empty, dequeue a vertex, print it and 


decrement the indegree of its adjacent nodes.

  • If the indegree of any new vertex becomes 0, enqueue it.

Queue: Output: v1 v3 v2 v4

101

slide-117
SLIDE 117

Computing Topological Order

v1 v2 v4 v5 v3 v6 write
 articles c

  • m

m i s i

  • n


 p h

  • t
  • g

r a p h s edit proofread layout process 
 photographs 
 v7 printing distribution v8 w e b l a y

  • u

t v9 u p l

  • a

d 


  • n

l i n e v e r s i

  • n

3 2 1 1 1 3 2 3 6 1 1 1 1 2 1

  • While the queue is not empty, dequeue a vertex, print it and 


decrement the indegree of its adjacent nodes.

  • If the indegree of any new vertex becomes 0, enqueue it.

Queue: Output: v1 v4 v2

102

v3

slide-118
SLIDE 118

Computing Topological Order

v1 v2 v4 v5 v3 v6 write
 articles c

  • m

m i s i

  • n


 p h

  • t
  • g

r a p h s edit proofread layout process 
 photographs 
 v7 printing distribution v8 w e b l a y

  • u

t v9 u p l

  • a

d 


  • n

l i n e v e r s i

  • n

3 2 1 1 1 3 2 3 6 1 1 1 2 1

  • While the queue is not empty, dequeue a vertex, print it and 


decrement the indegree of its adjacent nodes.

  • If the indegree of any new vertex becomes 0, enqueue it.

Queue: Output: v1 v5 v2 v4

103

v3

slide-119
SLIDE 119

Computing Topological Order

v1 v2 v4 v5 v3 v6 write
 articles c

  • m

m i s i

  • n


 p h

  • t
  • g

r a p h s edit proofread layout process 
 photographs 
 v7 printing distribution v8 w e b l a y

  • u

t v9 u p l

  • a

d 


  • n

l i n e v e r s i

  • n

3 2 1 1 1 3 2 3 6 1 1 2

  • While the queue is not empty, dequeue a vertex, print it and 


decrement the indegree of its adjacent nodes.

  • If the indegree of any new vertex becomes 0, enqueue it.

Queue: Output: v6 v9 v5

104

v1 v2 v4 v3

slide-120
SLIDE 120

Computing Topological Order

v1 v2 v4 v5 v3 v6 write
 articles c

  • m

m i s i

  • n


 p h

  • t
  • g

r a p h s edit proofread layout process 
 photographs 
 v7 printing distribution v8 w e b l a y

  • u

t v9 u p l

  • a

d 


  • n

l i n e v e r s i

  • n

3 2 1 1 1 3 2 3 6 1 2

  • While the queue is not empty, dequeue a vertex, print it and 


decrement the indegree of its adjacent nodes.

  • If the indegree of any new vertex becomes 0, enqueue it.

Queue: Output: v9 v7 v5 v6

105

v1 v2 v4 v3

slide-121
SLIDE 121

Computing Topological Order

v1 v2 v4 v5 v3 v6 write
 articles c

  • m

m i s i

  • n


 p h

  • t
  • g

r a p h s edit proofread layout process 
 photographs 
 v7 printing distribution v8 w e b l a y

  • u

t v9 u p l

  • a

d 


  • n

l i n e v e r s i

  • n

3 2 1 1 1 3 2 3 6 1 1

  • While the queue is not empty, dequeue a vertex, print it and 


decrement the indegree of its adjacent nodes.

  • If the indegree of any new vertex becomes 0, enqueue it.

Queue: Output: v7 v5 v6 v9

106

v1 v2 v4 v3

slide-122
SLIDE 122

Computing Topological Order

v1 v2 v4 v5 v3 v6 write
 articles c

  • m

m i s i

  • n


 p h

  • t
  • g

r a p h s edit proofread layout process 
 photographs 
 v7 printing distribution v8 w e b l a y

  • u

t v9 u p l

  • a

d 


  • n

l i n e v e r s i

  • n

3 2 1 1 1 3 2 3 6 1

  • While the queue is not empty, dequeue a vertex, print it and 


decrement the indegree of its adjacent nodes.

  • If the indegree of any new vertex becomes 0, enqueue it.

Queue: Output: v8 v5 v6 v9 v7

107

v1 v2 v4 v3

slide-123
SLIDE 123

Computing Topological Order

v1 v2 v4 v5 v3 v6 write
 articles c

  • m

m i s i

  • n


 p h

  • t
  • g

r a p h s edit proofread layout process 
 photographs 
 v7 printing distribution v8 w e b l a y

  • u

t v9 u p l

  • a

d 


  • n

l i n e v e r s i

  • n

3 2 1 1 1 3 2 3 6 1

  • While the queue is not empty, dequeue a vertex, print it and 


decrement the indegree of its adjacent nodes.

  • If the indegree of any new vertex becomes 0, enqueue it.

Queue: Output: v5 v6 v9 v7 v8

108

v1 v2 v4 v3

slide-124
SLIDE 124

Topological Sort - Running Time

  • First annotate each vertex with its indegree.
  • While the queue is not empty, dequeue a vertex, print it and 


decrement the indegree of its adjacent nodes.

  • If the indegree of any new vertex becomes 0, enqueue it.

109

slide-125
SLIDE 125

Topological Sort - Running Time

  • First annotate each vertex with its indegree.
  • While the queue is not empty, dequeue a vertex, print it and 


decrement the indegree of its adjacent nodes.

  • If the indegree of any new vertex becomes 0, enqueue it.

This is just BFS. Running time: O(|V|+|E|)

109

slide-126
SLIDE 126

Earliest Completion Time

v2 v4 v5 v3 v6 write
 articles c

  • m

m i s i

  • n


 p h

  • t
  • g

r a p h s edit proofread layout process 
 photographs 
 v7 printing distribution v8 w e b l a y

  • u

t v9 u p l

  • a

d 


  • n

l i n e v e r s i

  • n

3 2 1 1 1 3 2 3 6 1 1 1 1 2 1 1 2 1

  • While the queue is not empty, dequeue a vertex, print it and 


decrement the indegree of its adjacent nodes. Update earliest
 completion time for each adjacent node.

  • If the indegree of any new vertex becomes 0, enqueue it.

Queue: Output: v1

110

slide-127
SLIDE 127

Earliest Completion Time

3 v4 v5 2 v6 write
 articles c

  • m

m i s i

  • n


 p h

  • t
  • g

r a p h s edit proofread layout process 
 photographs 
 v7 printing distribution v8 w e b l a y

  • u

t v9 u p l

  • a

d 


  • n

l i n e v e r s i

  • n

3 2 1 1 1 3 2 3 6 1 1 2 1 1 2 1

Queue: Output: v1 v2 v3

  • While the queue is not empty, dequeue a vertex, print it and 


decrement the indegree of its adjacent nodes. Update earliest
 completion time for each adjacent node.

  • If the indegree of any new vertex becomes 0, enqueue it.

111

slide-128
SLIDE 128

Earliest Completion Time

3 4 v5 2 v6 write
 articles c

  • m

m i s i

  • n


 p h

  • t
  • g

r a p h s edit proofread layout process 
 photographs 
 v7 printing distribution v8 w e b l a y

  • u

t v9 u p l

  • a

d 


  • n

l i n e v e r s i

  • n

3 2 1 1 1 3 2 3 6 1 2 1 1 2 1

Queue: Output: v1 v3 v2 v4

  • While the queue is not empty, dequeue a vertex, print it and 


decrement the indegree of its adjacent nodes. Update earliest
 completion time for each adjacent node.

  • If the indegree of any new vertex becomes 0, enqueue it.

112

slide-129
SLIDE 129

Earliest Completion Time

3 4 v5 2 v6 write
 articles c

  • m

m i s i

  • n


 p h

  • t
  • g

r a p h s edit proofread layout process 
 photographs 
 v7 printing distribution v8 w e b l a y

  • u

t v9 u p l

  • a

d 


  • n

l i n e v e r s i

  • n

3 2 1 1 1 3 2 3 6 1 1 1 1 2 1

Queue: Output: v1 v4 v2

  • While the queue is not empty, dequeue a vertex, print it and 


decrement the indegree of its adjacent nodes. Update earliest
 completion time for each adjacent node.

  • If the indegree of any new vertex becomes 0, enqueue it.

113

v3

slide-130
SLIDE 130

Earliest Completion Time

3 4 5 2 v6 write
 articles c

  • m

m i s i

  • n


 p h

  • t
  • g

r a p h s edit proofread layout process 
 photographs 
 v7 printing distribution v8 w e b l a y

  • u

t v9 u p l

  • a

d 


  • n

l i n e v e r s i

  • n

3 2 1 1 1 3 2 3 6 1 1 1 2 1

Queue: Output: v5 v4

  • While the queue is not empty, dequeue a vertex, print it and 


decrement the indegree of its adjacent nodes. Update earliest
 completion time for each adjacent node.

  • If the indegree of any new vertex becomes 0, enqueue it.

114

v1 v2 v3

slide-131
SLIDE 131

Earliest Completion Time

3 4 5 2 8 write
 articles c

  • m

m i s i

  • n


 p h

  • t
  • g

r a p h s edit proofread layout process 
 photographs 
 v7 printing distribution v8 w e b l a y

  • u

t 7 u p l

  • a

d 


  • n

l i n e v e r s i

  • n

3 2 1 1 1 3 2 3 6 1 1 2

Queue: Output: v6 v9 v5

  • While the queue is not empty, dequeue a vertex, print it and 


decrement the indegree of its adjacent nodes. Update earliest
 completion time for each adjacent node.

  • If the indegree of any new vertex becomes 0, enqueue it.

115

v4 v1 v2 v3

slide-132
SLIDE 132

Earliest Completion Time

3 4 5 2 8 write
 articles c

  • m

m i s i

  • n


 p h

  • t
  • g

r a p h s edit proofread layout process 
 photographs 
 11 printing distribution v8 w e b l a y

  • u

t 7 u p l

  • a

d 


  • n

l i n e v e r s i

  • n

3 2 1 1 1 3 2 3 6 1 2

Queue: Output: v9 v7 v5 v6

  • While the queue is not empty, dequeue a vertex, print it and 


decrement the indegree of its adjacent nodes. Update earliest
 completion time for each adjacent node.

  • If the indegree of any new vertex becomes 0, enqueue it.

116

v4 v1 v2 v3

slide-133
SLIDE 133

Earliest Completion Time

3 4 5 2 8 write
 articles c

  • m

m i s i

  • n


 p h

  • t
  • g

r a p h s edit proofread layout process 
 photographs 
 11 printing distribution 8 w e b l a y

  • u

t 7 u p l

  • a

d 


  • n

l i n e v e r s i

  • n

3 2 1 1 1 3 2 3 6 1 1

Queue: Output: v7 v5 v6 v9

  • While the queue is not empty, dequeue a vertex, print it and 


decrement the indegree of its adjacent nodes. Update earliest
 completion time for each adjacent node.

  • If the indegree of any new vertex becomes 0, enqueue it.

117

v4 v1 v2 v3

slide-134
SLIDE 134

Earliest Completion Time

3 4 5 2 8 write
 articles c

  • m

m i s i

  • n


 p h

  • t
  • g

r a p h s edit proofread layout process 
 photographs 
 11 printing distribution 17 w e b l a y

  • u

t 7 u p l

  • a

d 


  • n

l i n e v e r s i

  • n

3 2 1 1 1 3 2 3 6 1

Queue: Output: v8 v5 v6 v9 v7

  • While the queue is not empty, dequeue a vertex, print it and 


decrement the indegree of its adjacent nodes. Update earliest
 completion time for each adjacent node.

  • If the indegree of any new vertex becomes 0, enqueue it.

118

v4 v1 v2 v3