ds lect 27 -graphs - iii (topological sort)
TRANSCRIPT
![Page 1: Ds lect 27 -graphs - iii (topological sort)](https://reader033.vdocuments.us/reader033/viewer/2022052912/55a0d3311a28ab59108b47ce/html5/thumbnails/1.jpg)
Graphs
Topological Sort
Suggested Readings: Weiss Ch. 9
![Page 2: Ds lect 27 -graphs - iii (topological sort)](https://reader033.vdocuments.us/reader033/viewer/2022052912/55a0d3311a28ab59108b47ce/html5/thumbnails/2.jpg)
Directed Graphs Usage
Directed graphs are often used to represent order-
dependent tasks
That is we cannot start a task before another task
finishes
We can model this task dependent constraint using arcs
An arc (i,j) means task j cannot start until task i is finished
Clearly, for the system not to hang, the graph must be
acyclic.
i j
Task j cannot start
until task i is finished
![Page 3: Ds lect 27 -graphs - iii (topological sort)](https://reader033.vdocuments.us/reader033/viewer/2022052912/55a0d3311a28ab59108b47ce/html5/thumbnails/3.jpg)
University Example
CS departments course structure
171151 180
211 251
272
271 252M132M111
231
201
221
361
362
381303
327336
341
343
342
332
334
104
How many indeg(171)?
How many outdeg(171)?
Any directed cycles?
![Page 4: Ds lect 27 -graphs - iii (topological sort)](https://reader033.vdocuments.us/reader033/viewer/2022052912/55a0d3311a28ab59108b47ce/html5/thumbnails/4.jpg)
Topological Sort
Topological sort is an algorithm for a directed acyclic
graph
It can be thought of as a way to linearly order the
vertices so that the linear order respects the ordering
relations implied by the arcs
0
12
3
45
6
7
8
9
For example:
0, 1, 2, 5, 9
0, 4, 5, 9
0, 6, 3, 7 ?
![Page 5: Ds lect 27 -graphs - iii (topological sort)](https://reader033.vdocuments.us/reader033/viewer/2022052912/55a0d3311a28ab59108b47ce/html5/thumbnails/5.jpg)
Topological Sort Idea:
Starting point must have zero indegree!
If it doesn’t exist, the graph would not be acyclic
1. A vertex with zero indegree is a task that can start right away. So we can output it first in the linear order
2. If a vertex i is output, then its outgoing arcs (i, j) are no longer useful, since tasks j does not need to wait for i anymore- so remove all i’s outgoing arcs
3. With vertex i removed, the new graph is still a directed acyclic graph. So, repeat step 1-2 until no vertex is left.
![Page 6: Ds lect 27 -graphs - iii (topological sort)](https://reader033.vdocuments.us/reader033/viewer/2022052912/55a0d3311a28ab59108b47ce/html5/thumbnails/6.jpg)
Topological Sort
Find all starting points
Reduce indegree(w)
Place new start
vertices on the Q
![Page 7: Ds lect 27 -graphs - iii (topological sort)](https://reader033.vdocuments.us/reader033/viewer/2022052912/55a0d3311a28ab59108b47ce/html5/thumbnails/7.jpg)
Example
0
12
3
45
6
7
8
9
0
1
2
3
4
5
6
7
8
9
2
6 1 4
7 5
8
5
3 2
8
9
9
0
1
2
3
4
5
6
7
8
9
0
1
2
1
1
2
1
1
2
2
Indegree
start
Q = { 0 }
OUTPUT: 0
![Page 8: Ds lect 27 -graphs - iii (topological sort)](https://reader033.vdocuments.us/reader033/viewer/2022052912/55a0d3311a28ab59108b47ce/html5/thumbnails/8.jpg)
Example
0
12
3
45
6
7
8
9
0
1
2
3
4
5
6
7
8
9
2
6 1 4
7 5
8
5
3 2
8
9
9
0
1
2
3
4
5
6
7
8
9
0
1
2
1
1
2
1
1
2
2
Indegree
Dequeue 0 Q = { }
-> remove 0’s arcs – adjust
indegrees of neighbors
OUTPUT:
Decrement 0’s
neighbors
-1
-1
-1
![Page 9: Ds lect 27 -graphs - iii (topological sort)](https://reader033.vdocuments.us/reader033/viewer/2022052912/55a0d3311a28ab59108b47ce/html5/thumbnails/9.jpg)
Example
0
12
3
45
6
7
8
9
0
1
2
3
4
5
6
7
8
9
2
6 1 4
7 5
8
5
3 2
8
9
9
0
1
2
3
4
5
6
7
8
9
0
0
2
1
0
2
0
1
2
2
Indegree
Dequeue 0 Q = { 6, 1, 4 }
Enqueue all starting points
OUTPUT: 0
Enqueue all
new start points
![Page 10: Ds lect 27 -graphs - iii (topological sort)](https://reader033.vdocuments.us/reader033/viewer/2022052912/55a0d3311a28ab59108b47ce/html5/thumbnails/10.jpg)
Example
12
3
45
6
7
8
9
0
1
2
3
4
5
6
7
8
9
2
6 1 4
7 5
8
5
3 2
8
9
9
0
1
2
3
4
5
6
7
8
9
0
0
2
1
0
2
0
1
2
2
Indegree
Dequeue 6 Q = { 1, 4 }
Remove arcs .. Adjust indegrees
of neighbors
OUTPUT: 0 6
Adjust neighbors
indegree
-1
-1
![Page 11: Ds lect 27 -graphs - iii (topological sort)](https://reader033.vdocuments.us/reader033/viewer/2022052912/55a0d3311a28ab59108b47ce/html5/thumbnails/11.jpg)
Example
12
3
45
7
8
9
0
1
2
3
4
5
6
7
8
9
2
6 1 4
7 5
8
5
3 2
8
9
9
0
1
2
3
4
5
6
7
8
9
0
0
1
0
0
2
0
1
2
2
Indegree
Dequeue 6 Q = { 1, 4, 3 }
Enqueue 3
OUTPUT: 0 6
Enqueue new
start
![Page 12: Ds lect 27 -graphs - iii (topological sort)](https://reader033.vdocuments.us/reader033/viewer/2022052912/55a0d3311a28ab59108b47ce/html5/thumbnails/12.jpg)
Example
12
3
45
7
8
9
0
1
2
3
4
5
6
7
8
9
2
6 1 4
7 5
8
5
3 2
8
9
9
0
1
2
3
4
5
6
7
8
9
0
0
1
0
0
2
0
1
2
2
Indegree
Dequeue 1 Q = { 4, 3 }
Adjust indegrees of neighbors
OUTPUT: 0 6 1
Adjust neighbors
of 1
-1
![Page 13: Ds lect 27 -graphs - iii (topological sort)](https://reader033.vdocuments.us/reader033/viewer/2022052912/55a0d3311a28ab59108b47ce/html5/thumbnails/13.jpg)
Example
2
3
45
7
8
9
0
1
2
3
4
5
6
7
8
9
2
6 1 4
7 5
8
5
3 2
8
9
9
0
1
2
3
4
5
6
7
8
9
0
0
0
0
0
2
0
1
2
2
Indegree
Dequeue 1 Q = { 4, 3, 2 }
Enqueue 2
OUTPUT: 0 6 1
Enqueue new
starting points
![Page 14: Ds lect 27 -graphs - iii (topological sort)](https://reader033.vdocuments.us/reader033/viewer/2022052912/55a0d3311a28ab59108b47ce/html5/thumbnails/14.jpg)
Example
2
3
45
7
8
9
0
1
2
3
4
5
6
7
8
9
2
6 1 4
7 5
8
5
3 2
8
9
9
0
1
2
3
4
5
6
7
8
9
0
0
0
0
0
2
0
1
2
2
Indegree
Dequeue 4 Q = { 3, 2 }
Adjust indegrees of neighbors
OUTPUT: 0 6 1 4
Adjust 4’s
neighbors
-1
![Page 15: Ds lect 27 -graphs - iii (topological sort)](https://reader033.vdocuments.us/reader033/viewer/2022052912/55a0d3311a28ab59108b47ce/html5/thumbnails/15.jpg)
Example
2
3
5
7
8
9
0
1
2
3
4
5
6
7
8
9
2
6 1 4
7 5
8
5
3 2
8
9
9
0
1
2
3
4
5
6
7
8
9
0
0
0
0
0
1
0
1
2
2
Indegree
Dequeue 4 Q = { 3, 2 }
No new start points found
OUTPUT: 0 6 1 4
NO new start
points
![Page 16: Ds lect 27 -graphs - iii (topological sort)](https://reader033.vdocuments.us/reader033/viewer/2022052912/55a0d3311a28ab59108b47ce/html5/thumbnails/16.jpg)
Example
2
3
5
7
8
9
0
1
2
3
4
5
6
7
8
9
2
6 1 4
7 5
8
5
3 2
8
9
9
0
1
2
3
4
5
6
7
8
9
0
0
0
0
0
1
0
1
2
2
Indegree
Dequeue 3 Q = { 2 }
Adjust 3’s neighbors
OUTPUT: 0 6 1 4 3
-1
![Page 17: Ds lect 27 -graphs - iii (topological sort)](https://reader033.vdocuments.us/reader033/viewer/2022052912/55a0d3311a28ab59108b47ce/html5/thumbnails/17.jpg)
Example
2
5
7
8
9
0
1
2
3
4
5
6
7
8
9
2
6 1 4
7 5
8
5
3 2
8
9
9
0
1
2
3
4
5
6
7
8
9
0
0
0
0
0
1
0
1
1
2
Indegree
Dequeue 3 Q = { 2 }
No new start points found
OUTPUT: 0 6 1 4 3
![Page 18: Ds lect 27 -graphs - iii (topological sort)](https://reader033.vdocuments.us/reader033/viewer/2022052912/55a0d3311a28ab59108b47ce/html5/thumbnails/18.jpg)
Example
2
5
7
8
9
0
1
2
3
4
5
6
7
8
9
2
6 1 4
7 5
8
5
3 2
8
9
9
0
1
2
3
4
5
6
7
8
9
0
0
0
0
0
1
0
1
1
2
Indegree
Dequeue 2 Q = { }
Adjust 2’s neighbors
OUTPUT: 0 6 1 4 3 2
-1
-1
![Page 19: Ds lect 27 -graphs - iii (topological sort)](https://reader033.vdocuments.us/reader033/viewer/2022052912/55a0d3311a28ab59108b47ce/html5/thumbnails/19.jpg)
Example
5
7
8
9
0
1
2
3
4
5
6
7
8
9
2
6 1 4
7 5
8
5
3 2
8
9
9
0
1
2
3
4
5
6
7
8
9
0
0
0
0
0
0
0
0
1
2
Indegree
Dequeue 2 Q = { 5, 7 }
Enqueue 5, 7
OUTPUT: 0 6 1 4 3 2
![Page 20: Ds lect 27 -graphs - iii (topological sort)](https://reader033.vdocuments.us/reader033/viewer/2022052912/55a0d3311a28ab59108b47ce/html5/thumbnails/20.jpg)
Example
5
7
8
9
0
1
2
3
4
5
6
7
8
9
2
6 1 4
7 5
8
5
3 2
8
9
9
0
1
2
3
4
5
6
7
8
9
0
0
0
0
0
0
0
0
1
2
Indegree
Dequeue 5 Q = { 7 }
Adjust neighbors
OUTPUT: 0 6 1 4 3 2 5
-1
![Page 21: Ds lect 27 -graphs - iii (topological sort)](https://reader033.vdocuments.us/reader033/viewer/2022052912/55a0d3311a28ab59108b47ce/html5/thumbnails/21.jpg)
Example
7
8
9
0
1
2
3
4
5
6
7
8
9
2
6 1 4
7 5
8
5
3 2
8
9
9
0
1
2
3
4
5
6
7
8
9
0
0
0
0
0
0
0
0
1
1
Indegree
Dequeue 5 Q = { 7 }
No new starts
OUTPUT: 0 6 1 4 3 2 5
![Page 22: Ds lect 27 -graphs - iii (topological sort)](https://reader033.vdocuments.us/reader033/viewer/2022052912/55a0d3311a28ab59108b47ce/html5/thumbnails/22.jpg)
Example
7
8
9
0
1
2
3
4
5
6
7
8
9
2
6 1 4
7 5
8
5
3 2
8
9
9
0
1
2
3
4
5
6
7
8
9
0
0
0
0
0
0
0
0
1
1
Indegree
Dequeue 7 Q = { }
Adjust neighbors
OUTPUT: 0 6 1 4 3 2 5 7
-1
![Page 23: Ds lect 27 -graphs - iii (topological sort)](https://reader033.vdocuments.us/reader033/viewer/2022052912/55a0d3311a28ab59108b47ce/html5/thumbnails/23.jpg)
Example
8
9
0
1
2
3
4
5
6
7
8
9
2
6 1 4
7 5
8
5
3 2
8
9
9
0
1
2
3
4
5
6
7
8
9
0
0
0
0
0
0
0
0
0
1
Indegree
Dequeue 7 Q = { 8 }
Enqueue 8
OUTPUT: 0 6 1 4 3 2 5 7
![Page 24: Ds lect 27 -graphs - iii (topological sort)](https://reader033.vdocuments.us/reader033/viewer/2022052912/55a0d3311a28ab59108b47ce/html5/thumbnails/24.jpg)
Example
8
9
0
1
2
3
4
5
6
7
8
9
2
6 1 4
7 5
8
5
3 2
8
9
9
0
1
2
3
4
5
6
7
8
9
0
0
0
0
0
0
0
0
0
1
Indegree
Dequeue 8 Q = { }
Adjust indegrees of neighbors
OUTPUT: 0 6 1 4 3 2 5 7 8
-1
![Page 25: Ds lect 27 -graphs - iii (topological sort)](https://reader033.vdocuments.us/reader033/viewer/2022052912/55a0d3311a28ab59108b47ce/html5/thumbnails/25.jpg)
Example
9
0
1
2
3
4
5
6
7
8
9
2
6 1 4
7 5
8
5
3 2
8
9
9
0
1
2
3
4
5
6
7
8
9
0
0
0
0
0
0
0
0
0
0
Indegree
Dequeue 8 Q = { 9 }
Enqueue 9
Dequeue 9 Q = { }
STOP – no neighbors
OUTPUT: 0 6 1 4 3 2 5 7 8 9
![Page 26: Ds lect 27 -graphs - iii (topological sort)](https://reader033.vdocuments.us/reader033/viewer/2022052912/55a0d3311a28ab59108b47ce/html5/thumbnails/26.jpg)
Example
OUTPUT: 0 6 1 4 3 2 5 7 8 9
0
12
3
45
6
7
8
9
Is output topologically correct?
![Page 27: Ds lect 27 -graphs - iii (topological sort)](https://reader033.vdocuments.us/reader033/viewer/2022052912/55a0d3311a28ab59108b47ce/html5/thumbnails/27.jpg)
Topological Sort: Complexity
We never visited a vertex more than one time
For each vertex, we had to examine all outgoing
edges
Σ outdegree(v) = m
This is summed over all vertices, not per vertex
So, our running time is exactly
O(n + m)