Data Structures in Java
Lecture 17: Traversing Graphs. Shortest Paths.
10/18/2015 Daniel Bauer
1
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
Lecture 17: Traversing Graphs. Shortest Paths.
10/18/2015 Daniel Bauer
1
traversal on trees to graphs, users a Stack)
(uses a Priority Queue)
2
v,w ∈ V
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
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
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)
k-1 = number of edges on path
Sum of all edge costs.
5
such that (wi, wi+1) ∈ E.
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
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
7
8
G
Letters indicate junctions where a decision must be made.
H
9
A B C D E G
H
H F
10
A B C D E G
H
H F
11
(source) touching all edges.
algorithms.
v1 v2 v3 v4 v5 v6 v7
to the stack.
v1 Use a stack.
12
(source) touching all edges.
algorithms.
v1
v2
v3
v4
v5 v6 v7
v2 v4
to the stack.
Use a stack.
13
(source) touching all edges.
algorithms.
v1 v2
v3
v4
v5 v6 v7
v2 v3 v6 v5 v7
to the stack.
Use a stack.
14
to the stack.
Use a stack. Use a stack.
(source) touching all edges.
algorithms.
v1
v2 v3 v4 v5
v6
v7
v2 v6 v5 v7
15
to the stack.
Use a stack. Use a stack.
(source) touching all edges.
algorithms.
v1
v2 v3 v4 v5
v6
v7
v2 v6 v5 v7
15
v1 v2 v3 v4 v5 v6 v7
v1
to the stack.
Use a stack and a set visited. visited {}
16
v1
v2
v3
v4
v5 v6 v7
v2 Visited: {v1} v4
to the stack.
Use a stack and a set visited.
17
v1 v2
v3
v4
v5 v6 v7
v2 Visited: {v1,v4} v5 v7 v6 v3
to the stack.
Use a stack and a set visited.
18
v1
v2 v3 v4 v5
v6
v7
v2 Visited: {v1,v4,v3} v5 v7 v6
to the stack.
Use a stack and a set visited. v6 v1
19
v1 v2 v3 v4 v5 v6 v7
v2 Visited: {v1,v4,v3} v5 v7 v6
to the stack.
Use a stack and a set visited. v6
20
v1 v2 v3 v4 v5 v6 v7
v2 Visited: {v1,v4,v3,v6} v5 v7 v6
to the stack.
Use a stack and a set visited.
21
v1 v2 v3 v4 v5 v6 v7
v2 Visited: v5 v7
to the stack.
Use a stack and a set visited. {v1,v4,v3,v6}
22
v1 v2 v3 v4 v5
v6
v7
v2 Visited: v5
to the stack.
Use a stack and a set visited. {v1,v4,v3,v6,v7}
23
v6
v1 v2 v3 v4 v5
v6
v7
v2 Visited: v5
to the stack.
Use a stack and a set visited. {v1,v4,v3,v6,v7}
24
v1 v2 v3 v4 v5 v6
v7
v2 Visited:
to the stack.
Use a stack and a set visited. {v1,v4,v3,v6,v7,v5}
25
v7
v1 v2 v3 v4 v5 v6
v7
v2 Visited:
to the stack.
Use a stack and a set visited. {v1,v4,v3,v6,v7,v5}
26
v1 v2 v3 v4 v5 v6 v7
Visited:
to the stack.
Use a stack and a set visited. {v1,v4,v3,v6,v7,v5,v2}
27
v1 v2 v3 v4 v5 v6 v7
Visited:
to the stack.
Use a stack and a set visited. {v1,v4,v3,v6,v7,v5,v2}
Running time: O(|E|)
27
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
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
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
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
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
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
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
v1
v2 v3 v4 v5 v6 v7
35
designated start vertex.
v1 v2
v3 v4 v5 v6 v7
36
designated start vertex.
v1 v2
v3
v4
v5 v6 v7
37
designated start vertex.
v1 v2
v3
v4 v5
v6 v7
38
designated start vertex. v1: v2, v4 v2: v4, v5 v3: v1, v6 v4: v5 v5: v6: v7 v7: v5
v1 v2 v3 v4 v5
v6 v7
39
adjacency list until we find an unseen starting node.
designated start vertex. v1: v2, v4 v2: v4, v5 v3: v1, v6 v4: v5 v5: v6: v7 v7: v5
v1 v2 v3 v4 v5
v6 v7
39
adjacency list until we find an unseen starting node.
designated start vertex. v1: v2, v4 v2: v4, v5 v3: v1, v6 v4: v5 v5: v6: v7 v7: v5
v1 v2 v3 v4 v5 v6 v7
adjacent to u:
Use a queue and a set visited. Visited: {v1} Queue v1
40
v1 v2 v3 v4 v5 v6 v7
adjacent to u:
Use a queue and a set visited. Visited: {v1,v2,v4} Queue v2 v4
41
v1 v2 v3 v4 v5 v6 v7
adjacent to u:
Use a queue and a set visited. Visited: {v1,v2,v4,v5} Queue v4 v5
42
v1 v2 v3 v4 v5 v6 v7
adjacent to u:
Use a queue and a set visited. Visited: {v1,v2,v4,v5,v7,v6,v3} Queue v5 v7 v6 v3
43
v1 v2 v3 v4 v5 v6 v7
adjacent to u:
Use a queue and a set visited. Visited: {v1,v2,v4,v5,v7,v6,v3} Queue v7 v6 v3
44
v1 v2 v3 v4 v5 v6 v7
adjacent to u:
Use a queue and a set visited. Visited: {v1,v2,v4,v5,v7,v6,v3} Queue v6 v3
45
v1 v2 v3 v4 v5 v6 v7
adjacent to u:
Use a queue and a set visited. Visited: {v1,v2,v4,v5,v7,v6,v3} Queue v3
46
v1 v2 v3 v4 v5 v6 v7
adjacent to u:
Use a queue and a set visited. Visited: {v1,v2,v4,v5,v7,v6,v3} Queue
47
v1 v2 v3 v4 v5 v6 v7
adjacent to u:
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
v1 v2 v3 v4 v5 v6 v7
adjacent to u:
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
v1 v2 v3 v4 v5 v6 v7
What is the shortest path between v3 and v7?
48
v1 v2 v3 v4 v5 v6 v7
What is the shortest path between v3 and v7? length 3
v1 v4 v7 v3
49
ALL other vertices is just as easy. This problem is called single-source shortest paths.
v1 v2
v3
v4 v5 v6 v7
50
ALL other vertices is just as easy. This problem is called single-source shortest paths.
v1 v2
v3
v4 v5 v6 v7
1 1 2 2 3 3
50
ALL other vertices is just as easy. This problem is called single-source shortest paths.
v1 v2
v3
v4 v5 v6 v7
1 1 2 2 3 3
50
v1 v2 v4 v5 v6 v7
∞ ∞ ∞ ∞ ∞ ∞
to u:
Queue v3
v3
51
v1 v2
v3
v4 v5 v6 v7
1
∞ ∞ ∞ ∞
1
Queue v1 v6
to u:
52
v1
v2
v3
v4 v5 v6 v7
1 2
∞ ∞
2 1
Queue v6 v2 v4
to u:
53
v1
v2
v3
v4 v5
v6
v7
1 2
∞ ∞
2 1
Queue v2 v4
to u:
54
v1 v2 v3
v4 v5
v6
v7
1 2
∞
3 2 1
Queue v4 v5
to u:
55
v1 v2 v3 v4
v5
v6
v7
1 2 3 3 2 1
Queue v5 v7
to u:
56
v1 v2 v3 v4 v5 v6
v7
1 2 3 3 2 1
Queue v7
to u:
57
to u:
v1 v2 v3 v4 v5 v6 v7
1 2 3 3 2 1
Queue
58
to u:
v1 v2 v3 v4 v5 v6 v7
1 2 3 3 2 1
Queue
This is just BFS. Running time: O(|V|+|E|)
58
v1 v2
v3
v4 v5 v6 v7
1
∞ ∞ ∞ ∞
1
Queue v1 v6 Maintain pointers to the previous node on the shortest path.
to u:
59
v1 v2
v3
v4 v5 v6 v7
1
2 ∞ ∞ 2
1
Queue Maintain pointers to the previous node on the shortest path.
to u:
v6 v2 v4
60
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
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
62
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
63
v1 v2 v3 v4
v5
v6 v7
What is the shortest path between v2 and v6?
2 4 2
3 10 2 4 6 1 5 8
64
65
cost using only vertices visited so far.
65
cost using only vertices visited so far.
through other vertices that have not been seen yet.
65
cost using only vertices visited so far.
through other vertices that have not been seen yet.
expand the vertex with the lowest cost annotation first!
65
cost using only vertices visited so far.
through other vertices that have not been seen yet.
expand the vertex with the lowest cost annotation first!
65
← This is a greedy algorithm
v2 v3 v4 v5 v6 v7
2 4 2 1 3 10 2 4 6 1 5 8
Use a Priority Queue q
set v.cost = ∞, set v.visited = false
∞ ∞ ∞ ∞ ∞ ∞
v1
66
Use a Priority Queue q
set v.cost = ∞, set v.visited = false
v1
v2 v3 v4 v5 v6 v7
2 4 2 1 3 10 2 4 6 1 5 8
2 1 ∞ ∞ ∞ ∞
67
Use a Priority Queue q
set v.cost = ∞, set v.visited = false
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
Use a Priority Queue q
set v.cost = ∞, set v.visited = false
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
Use a Priority Queue q
set v.cost = ∞, set v.visited = false
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
Use a Priority Queue q
set v.cost = ∞, set v.visited = false
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
Use a Priority Queue q
set v.cost = ∞, set v.visited = false
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
Use a Priority Queue q
set v.cost = ∞, set v.visited = false
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
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
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
and might make the heap invalid.
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
set v.cost = ∞, set v.visited = false
that isn’t updated.
entries for vertices.
deleteMin operations.
the priority queue is O(|E|). Each insert takes O(log |E|)
O(|E| log |E|)
76
Use a Priority Queue q
set v.cost = ∞, set v.visited = false
deleteMin operations.
the priority queue is O(|E|). Each insert takes O(log |E|)
O(|E| log |E|)
76
Use a Priority Queue q
set v.cost = ∞, set v.visited = false
because |E| ≤ |V|2, and therefore log |E| ≤ 2 log |V|
=O(|E| log |V|)
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
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
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
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
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
time it takes to complete the task.
write articles c
m i s i
p h
r a p h s edit proofread layout process photographs printing distribution w e b l a y
t u p l
d
l i n e v e r s i
3 2 1 1 1 3 2 3 6 1
82
(Earliest time we can reach the final event).
through the DAG (why does this not work with cycles?).
write articles c
m i s i
p h
r a p h s edit proofread layout process photographs printing distribution w e b l a y
t u p l
d
l i n e v e r s i
3 2 1 1 1 3 2 3 6 1
83
tasks have to be finished before other tasks can proceed.
write articles c
m i s i
p h
r a p h s edit proofread layout process photographs printing distribution w e b l a y
t u p l
d
l i n e v e r s i
3 2 1 1 1 3 2 3 6 1
84
for each event.
write articles c
m i s i
p h
r a p h s edit proofread layout process photographs printing distribution w e b l a y
t u p l
d
l i n e v e r s i
3 2 1 1 1 3 2 3 6 1
85
for each event.
3 2 write articles c
m i s i
p h
r a p h s edit proofread layout process photographs printing distribution w e b l a y
t u p l
d
l i n e v e r s i
3 2 1 1 1 3 2 3 6 1
86
for each event.
3 4 2 write articles c
m i s i
p h
r a p h s edit proofread layout process photographs printing distribution w e b l a y
t u p l
d
l i n e v e r s i
3 2 1 1 1 3 2 3 6 1
87
for each event.
3 4 5 2 write articles c
m i s i
p h
r a p h s edit proofread layout process photographs printing distribution w e b l a y
t u p l
d
l i n e v e r s i
3 2 1 1 1 3 2 3 6 1
88
for each event.
3 4 5 2 8 write articles c
m i s i
p h
r a p h s edit proofread layout process photographs printing distribution w e b l a y
t 7 u p l
d
l i n e v e r s i
3 2 1 1 1 3 2 3 6 1
89
for each event.
3 4 5 2 8 write articles c
m i s i
p h
r a p h s edit proofread layout process photographs 11 printing distribution w e b l a y
t 7 u p l
d
l i n e v e r s i
3 2 1 1 1 3 2 3 6 1
90
for each event.
3 4 5 2 8 write articles c
m i s i
p h
r a p h s edit proofread layout process photographs 11 printing distribution 17 w e b l a y
t 7 u p l
d
l i n e v e r s i
3 2 1 1 1 3 2 3 6 1
91
for each event.
3 4 5 2 8 write articles c
m i s i
p h
r a p h s edit proofread layout process photographs 11 printing distribution 17 w e b l a y
t 7 u p l
d
l i n e v e r s i
3 2 1 1 1 3 2 3 6 1
92
for each event.
3 4 5 2 8 write articles c
m i s i
p h
r a p h s edit proofread layout process photographs 11 printing distribution 17 w e b l a y
t 7 u p l
d
l i n e v e r s i
3 2 1 1 1 3 2 3 6 1
93
for each event.
3 4 5 2 8 write articles c
m i s i
p h
r a p h s edit proofread layout process photographs 11 printing distribution 17 w e b l a y
t 7 u p l
d
l i n e v e r s i
3 2 1 1 1 3 2 3 6 1
94
O(|E| log |V|) bound.
write articles c
m i s i
p h
r a p h s edit proofread layout process photographs printing distribution w e b l a y
t u p l
d
l i n e v e r s i
3 2 1 1 1 3 2 3 6 1
95
for each event.
3 4 5 2 write articles c
m i s i
p h
r a p h s edit proofread layout process photographs printing distribution w e b l a y
t u p l
d
l i n e v e r s i
3 2 1 1 1 3 2 3 6 1 Need at least 5 time steps to get here
96
incoming edges to a node first before visiting the node.
write articles c
m i s i
p h
r a p h s edit proofread layout process photographs printing distribution w e b l a y
t u p l
d
l i n e v e r s i
3 2 1 1 1 3 2 3 6 1
97
completion time.
incoming edges (the indegree).
v1 v2 v4 v5 v3 v6 write articles c
m i s i
p h
r a p h s edit proofread layout process photographs v7 printing distribution v8 w e b l a y
t v9 u p l
d
l i n e v e r s i
3 2 1 1 1 3 2 3 6 1 1 1 1 2 1 1 2 1
98
v1 v2 v4 v5 v3 v6 write articles c
m i s i
p h
r a p h s edit proofread layout process photographs v7 printing distribution v8 w e b l a y
t v9 u p l
d
l i n e v e r s i
3 2 1 1 1 3 2 3 6 1 1 1 1 2 1 1 2 1
decrement the indegree of its adjacent nodes.
Queue: Output: v1
99
v1 v2 v4 v5 v3 v6 write articles c
m i s i
p h
r a p h s edit proofread layout process photographs v7 printing distribution v8 w e b l a y
t v9 u p l
d
l i n e v e r s i
3 2 1 1 1 3 2 3 6 1 1 2 1 1 2 1
decrement the indegree of its adjacent nodes.
Queue: Output: v1 v2 v3
100
v1 v2 v4 v5 v3 v6 write articles c
m i s i
p h
r a p h s edit proofread layout process photographs v7 printing distribution v8 w e b l a y
t v9 u p l
d
l i n e v e r s i
3 2 1 1 1 3 2 3 6 1 2 1 1 2 1
decrement the indegree of its adjacent nodes.
Queue: Output: v1 v3 v2 v4
101
v1 v2 v4 v5 v3 v6 write articles c
m i s i
p h
r a p h s edit proofread layout process photographs v7 printing distribution v8 w e b l a y
t v9 u p l
d
l i n e v e r s i
3 2 1 1 1 3 2 3 6 1 1 1 1 2 1
decrement the indegree of its adjacent nodes.
Queue: Output: v1 v4 v2
102
v3
v1 v2 v4 v5 v3 v6 write articles c
m i s i
p h
r a p h s edit proofread layout process photographs v7 printing distribution v8 w e b l a y
t v9 u p l
d
l i n e v e r s i
3 2 1 1 1 3 2 3 6 1 1 1 2 1
decrement the indegree of its adjacent nodes.
Queue: Output: v1 v5 v2 v4
103
v3
v1 v2 v4 v5 v3 v6 write articles c
m i s i
p h
r a p h s edit proofread layout process photographs v7 printing distribution v8 w e b l a y
t v9 u p l
d
l i n e v e r s i
3 2 1 1 1 3 2 3 6 1 1 2
decrement the indegree of its adjacent nodes.
Queue: Output: v6 v9 v5
104
v1 v2 v4 v3
v1 v2 v4 v5 v3 v6 write articles c
m i s i
p h
r a p h s edit proofread layout process photographs v7 printing distribution v8 w e b l a y
t v9 u p l
d
l i n e v e r s i
3 2 1 1 1 3 2 3 6 1 2
decrement the indegree of its adjacent nodes.
Queue: Output: v9 v7 v5 v6
105
v1 v2 v4 v3
v1 v2 v4 v5 v3 v6 write articles c
m i s i
p h
r a p h s edit proofread layout process photographs v7 printing distribution v8 w e b l a y
t v9 u p l
d
l i n e v e r s i
3 2 1 1 1 3 2 3 6 1 1
decrement the indegree of its adjacent nodes.
Queue: Output: v7 v5 v6 v9
106
v1 v2 v4 v3
v1 v2 v4 v5 v3 v6 write articles c
m i s i
p h
r a p h s edit proofread layout process photographs v7 printing distribution v8 w e b l a y
t v9 u p l
d
l i n e v e r s i
3 2 1 1 1 3 2 3 6 1
decrement the indegree of its adjacent nodes.
Queue: Output: v8 v5 v6 v9 v7
107
v1 v2 v4 v3
v1 v2 v4 v5 v3 v6 write articles c
m i s i
p h
r a p h s edit proofread layout process photographs v7 printing distribution v8 w e b l a y
t v9 u p l
d
l i n e v e r s i
3 2 1 1 1 3 2 3 6 1
decrement the indegree of its adjacent nodes.
Queue: Output: v5 v6 v9 v7 v8
108
v1 v2 v4 v3
decrement the indegree of its adjacent nodes.
109
decrement the indegree of its adjacent nodes.
This is just BFS. Running time: O(|V|+|E|)
109
v2 v4 v5 v3 v6 write articles c
m i s i
p h
r a p h s edit proofread layout process photographs v7 printing distribution v8 w e b l a y
t v9 u p l
d
l i n e v e r s i
3 2 1 1 1 3 2 3 6 1 1 1 1 2 1 1 2 1
decrement the indegree of its adjacent nodes. Update earliest completion time for each adjacent node.
Queue: Output: v1
110
3 v4 v5 2 v6 write articles c
m i s i
p h
r a p h s edit proofread layout process photographs v7 printing distribution v8 w e b l a y
t v9 u p l
d
l i n e v e r s i
3 2 1 1 1 3 2 3 6 1 1 2 1 1 2 1
Queue: Output: v1 v2 v3
decrement the indegree of its adjacent nodes. Update earliest completion time for each adjacent node.
111
3 4 v5 2 v6 write articles c
m i s i
p h
r a p h s edit proofread layout process photographs v7 printing distribution v8 w e b l a y
t v9 u p l
d
l i n e v e r s i
3 2 1 1 1 3 2 3 6 1 2 1 1 2 1
Queue: Output: v1 v3 v2 v4
decrement the indegree of its adjacent nodes. Update earliest completion time for each adjacent node.
112
3 4 v5 2 v6 write articles c
m i s i
p h
r a p h s edit proofread layout process photographs v7 printing distribution v8 w e b l a y
t v9 u p l
d
l i n e v e r s i
3 2 1 1 1 3 2 3 6 1 1 1 1 2 1
Queue: Output: v1 v4 v2
decrement the indegree of its adjacent nodes. Update earliest completion time for each adjacent node.
113
v3
3 4 5 2 v6 write articles c
m i s i
p h
r a p h s edit proofread layout process photographs v7 printing distribution v8 w e b l a y
t v9 u p l
d
l i n e v e r s i
3 2 1 1 1 3 2 3 6 1 1 1 2 1
Queue: Output: v5 v4
decrement the indegree of its adjacent nodes. Update earliest completion time for each adjacent node.
114
v1 v2 v3
3 4 5 2 8 write articles c
m i s i
p h
r a p h s edit proofread layout process photographs v7 printing distribution v8 w e b l a y
t 7 u p l
d
l i n e v e r s i
3 2 1 1 1 3 2 3 6 1 1 2
Queue: Output: v6 v9 v5
decrement the indegree of its adjacent nodes. Update earliest completion time for each adjacent node.
115
v4 v1 v2 v3
3 4 5 2 8 write articles c
m i s i
p h
r a p h s edit proofread layout process photographs 11 printing distribution v8 w e b l a y
t 7 u p l
d
l i n e v e r s i
3 2 1 1 1 3 2 3 6 1 2
Queue: Output: v9 v7 v5 v6
decrement the indegree of its adjacent nodes. Update earliest completion time for each adjacent node.
116
v4 v1 v2 v3
3 4 5 2 8 write articles c
m i s i
p h
r a p h s edit proofread layout process photographs 11 printing distribution 8 w e b l a y
t 7 u p l
d
l i n e v e r s i
3 2 1 1 1 3 2 3 6 1 1
Queue: Output: v7 v5 v6 v9
decrement the indegree of its adjacent nodes. Update earliest completion time for each adjacent node.
117
v4 v1 v2 v3
3 4 5 2 8 write articles c
m i s i
p h
r a p h s edit proofread layout process photographs 11 printing distribution 17 w e b l a y
t 7 u p l
d
l i n e v e r s i
3 2 1 1 1 3 2 3 6 1
Queue: Output: v8 v5 v6 v9 v7
decrement the indegree of its adjacent nodes. Update earliest completion time for each adjacent node.
118
v4 v1 v2 v3