![Page 1: Topological sort: example · Topological sort: example Given a DAG of prerequisites for courses, a topological sort can be used to determine an order in which to take the courses](https://reader030.vdocument.in/reader030/viewer/2022040621/5f3671880b61b772f26aea7b/html5/thumbnails/1.jpg)
05/12/2015 DFR - DSA - Graphs 2 1
Topological sort: example
Given a DAG of prerequisites for courses, a topological sort can be used to determine an order in which to take the courses
(TS: DAG => sequence) (modified dfs) prints reverse topological order of a DAG from v
tsort(v) {mark v visited
for each w adjacent to v if w unvisited tsort(w)display(v)}
![Page 2: Topological sort: example · Topological sort: example Given a DAG of prerequisites for courses, a topological sort can be used to determine an order in which to take the courses](https://reader030.vdocument.in/reader030/viewer/2022040621/5f3671880b61b772f26aea7b/html5/thumbnails/2.jpg)
05/12/2015 DFR - DSA - Graphs 2 2
Topological sort: example
start: A
tsort(A) => G K H D E C A Breverse => B A C E D H K G
KE
GC
HB
DA
ABCDE
HG
CEDG HH
KK
E
![Page 3: Topological sort: example · Topological sort: example Given a DAG of prerequisites for courses, a topological sort can be used to determine an order in which to take the courses](https://reader030.vdocument.in/reader030/viewer/2022040621/5f3671880b61b772f26aea7b/html5/thumbnails/3.jpg)
Topological Sort example
tsort(v) {Amark v visited
for each w adjacent to v if w unvisited tsort(w)display(v)}
path: A output:reverse:
05/12/2015 DFR - DSA - Graphs 2 3
ABCDE
HG
CEDG HH
KK
E
KE
GC
HB
DA
![Page 4: Topological sort: example · Topological sort: example Given a DAG of prerequisites for courses, a topological sort can be used to determine an order in which to take the courses](https://reader030.vdocument.in/reader030/viewer/2022040621/5f3671880b61b772f26aea7b/html5/thumbnails/4.jpg)
Topological Sort example
tsort(v) {mark v visited
A for each w adjacent to v if w unvisited tsort(w)display(v)}
path: A Coutput:reverse:
05/12/2015 DFR - DSA - Graphs 2 4
ABCDE
HG
CEDG HH
KK
E
KE
GC
HB
DA
![Page 5: Topological sort: example · Topological sort: example Given a DAG of prerequisites for courses, a topological sort can be used to determine an order in which to take the courses](https://reader030.vdocument.in/reader030/viewer/2022040621/5f3671880b61b772f26aea7b/html5/thumbnails/5.jpg)
Topological Sort example
tsort(v) {Cmark v visited
for each w adjacent to v if w unvisited tsort(w)display(v)}
path: A C output:reverse:
05/12/2015 DFR - DSA - Graphs 2 5
ABCDE
HG
CEDG HH
KK
E
KE
GC
HB
DA
![Page 6: Topological sort: example · Topological sort: example Given a DAG of prerequisites for courses, a topological sort can be used to determine an order in which to take the courses](https://reader030.vdocument.in/reader030/viewer/2022040621/5f3671880b61b772f26aea7b/html5/thumbnails/6.jpg)
Topological Sort example
tsort(v) {mark v visited
Cfor each w adjacent to v if w unvisited tsort(w)display(v)}
path: A C Doutput:reverse:
05/12/2015 DFR - DSA - Graphs 2 6
ABCDE
HG
CEDG HH
KK
E
KE
GC
HB
DA
![Page 7: Topological sort: example · Topological sort: example Given a DAG of prerequisites for courses, a topological sort can be used to determine an order in which to take the courses](https://reader030.vdocument.in/reader030/viewer/2022040621/5f3671880b61b772f26aea7b/html5/thumbnails/7.jpg)
Topological Sort example
tsort(v) {D mark v visited
for each w adjacent to v if w unvisited tsort(w)display(v)}
path: A C D output:reverse:
05/12/2015 DFR - DSA - Graphs 2 7
ABCDE
HG
CEDG HH
KK
E
KE
GC
HB
DA
![Page 8: Topological sort: example · Topological sort: example Given a DAG of prerequisites for courses, a topological sort can be used to determine an order in which to take the courses](https://reader030.vdocument.in/reader030/viewer/2022040621/5f3671880b61b772f26aea7b/html5/thumbnails/8.jpg)
Topological Sort example
tsort(v) {mark v visited
Dfor each w adjacent to v if w unvisited tsort(w)display(v)}
path: A C D Goutput:reverse:
05/12/2015 DFR - DSA - Graphs 2 8
ABCDE
HG
CEDG HH
KK
E
KE
GC
HB
DA
![Page 9: Topological sort: example · Topological sort: example Given a DAG of prerequisites for courses, a topological sort can be used to determine an order in which to take the courses](https://reader030.vdocument.in/reader030/viewer/2022040621/5f3671880b61b772f26aea7b/html5/thumbnails/9.jpg)
Topological Sort example
tsort(v) {Gmark v visited
for each w adjacent to v if w unvisited tsort(w)display(v)}
path: A C D Goutput:reverse:
05/12/2015 DFR - DSA - Graphs 2 9
ABCDE
HG
CEDG HH
KK
E
KE
GC
HB
DA
![Page 10: Topological sort: example · Topological sort: example Given a DAG of prerequisites for courses, a topological sort can be used to determine an order in which to take the courses](https://reader030.vdocument.in/reader030/viewer/2022040621/5f3671880b61b772f26aea7b/html5/thumbnails/10.jpg)
Topological Sort example
tsort(v) {mark v visited
G for each w adjacent to v if w unvisited tsort(w)display(v)}
path: A C D Goutput:reverse:
05/12/2015 DFR - DSA - Graphs 2 10
ABCDE
HG
CEDG HH
KK
E
KE
GC
HB
DA
![Page 11: Topological sort: example · Topological sort: example Given a DAG of prerequisites for courses, a topological sort can be used to determine an order in which to take the courses](https://reader030.vdocument.in/reader030/viewer/2022040621/5f3671880b61b772f26aea7b/html5/thumbnails/11.jpg)
Topological Sort example
tsort(v) {mark v visitedfor each w adjacent to v if w unvisited tsort(w)
Gdisplay(v)}
path: A C D Goutput: Greverse:
05/12/2015 DFR - DSA - Graphs 2 11
ABCDE
HG
CEDG HH
KK
E
KE
GC
HB
DA
![Page 12: Topological sort: example · Topological sort: example Given a DAG of prerequisites for courses, a topological sort can be used to determine an order in which to take the courses](https://reader030.vdocument.in/reader030/viewer/2022040621/5f3671880b61b772f26aea7b/html5/thumbnails/12.jpg)
Topological Sort example
tsort(v) {mark v visited
D for each w adjacent to v if w unvisited tsort(w)display(v)}
path: A C D output: Greverse:
05/12/2015 DFR - DSA - Graphs 2 12
ABCDE
HG
CEDG HH
KK
E
KE
GC
HB
DA
![Page 13: Topological sort: example · Topological sort: example Given a DAG of prerequisites for courses, a topological sort can be used to determine an order in which to take the courses](https://reader030.vdocument.in/reader030/viewer/2022040621/5f3671880b61b772f26aea7b/html5/thumbnails/13.jpg)
Topological Sort example
tsort(v) {mark v visited
D for each w adjacent to v if w unvisited tsort(w)display(v)}
path: A C D Houtput: Greverse:
05/12/2015 DFR - DSA - Graphs 2 13
ABCDE
HG
CEDG HH
KK
E
KE
GC
HB
DA
![Page 14: Topological sort: example · Topological sort: example Given a DAG of prerequisites for courses, a topological sort can be used to determine an order in which to take the courses](https://reader030.vdocument.in/reader030/viewer/2022040621/5f3671880b61b772f26aea7b/html5/thumbnails/14.jpg)
Topological Sort example
tsort(v) {H mark v visited
for each w adjacent to v if w unvisited tsort(w)display(v)}
path: A C D Houtput: Greverse:
05/12/2015 DFR - DSA - Graphs 2 14
ABCDE
HG
CEDG HH
KK
E
KE
GC
HB
DA
![Page 15: Topological sort: example · Topological sort: example Given a DAG of prerequisites for courses, a topological sort can be used to determine an order in which to take the courses](https://reader030.vdocument.in/reader030/viewer/2022040621/5f3671880b61b772f26aea7b/html5/thumbnails/15.jpg)
Topological Sort example
tsort(v) {mark v visited
H for each w adjacent to v if w unvisited tsort(w)display(v)}
path: A C D H Koutput: Greverse:
05/12/2015 DFR - DSA - Graphs 2 15
ABCDE
HG
CEDG HH
KK
E
KE
GC
HB
DA
![Page 16: Topological sort: example · Topological sort: example Given a DAG of prerequisites for courses, a topological sort can be used to determine an order in which to take the courses](https://reader030.vdocument.in/reader030/viewer/2022040621/5f3671880b61b772f26aea7b/html5/thumbnails/16.jpg)
Topological Sort example
tsort(v) {Kmark v visited
for each w adjacent to v if w unvisited tsort(w)display(v)}
path: A C D H Koutput: Greverse:
05/12/2015 DFR - DSA - Graphs 2 16
ABCDE
HG
CEDG HH
KK
E
KE
GC
HB
DA
![Page 17: Topological sort: example · Topological sort: example Given a DAG of prerequisites for courses, a topological sort can be used to determine an order in which to take the courses](https://reader030.vdocument.in/reader030/viewer/2022040621/5f3671880b61b772f26aea7b/html5/thumbnails/17.jpg)
Topological Sort example
tsort(v) {mark v visited
K for each w adjacent to v if w unvisited tsort(w)display(v)}
path: A C D H Koutput: Greverse:
05/12/2015 DFR - DSA - Graphs 2 17
ABCDE
HG
CEDG HH
KK
E
KE
GC
HB
DA
![Page 18: Topological sort: example · Topological sort: example Given a DAG of prerequisites for courses, a topological sort can be used to determine an order in which to take the courses](https://reader030.vdocument.in/reader030/viewer/2022040621/5f3671880b61b772f26aea7b/html5/thumbnails/18.jpg)
Topological Sort example
tsort(v) {mark v visitedfor each w adjacent to v if w unvisited tsort(w)
Kdisplay(v)}
path: A C D H Koutput: G Kreverse:
05/12/2015 DFR - DSA - Graphs 2 18
ABCDE
HG
CEDG HH
KK
E
KE
GC
HB
DA
![Page 19: Topological sort: example · Topological sort: example Given a DAG of prerequisites for courses, a topological sort can be used to determine an order in which to take the courses](https://reader030.vdocument.in/reader030/viewer/2022040621/5f3671880b61b772f26aea7b/html5/thumbnails/19.jpg)
Topological Sort example
tsort(v) {mark v visited
H for each w adjacent to v if w unvisited tsort(w)display(v)}
path: A C D Houtput: G Kreverse:
05/12/2015 DFR - DSA - Graphs 2 19
ABCDE
HG
CEDG HH
KK
E
KE
GC
HB
DA
![Page 20: Topological sort: example · Topological sort: example Given a DAG of prerequisites for courses, a topological sort can be used to determine an order in which to take the courses](https://reader030.vdocument.in/reader030/viewer/2022040621/5f3671880b61b772f26aea7b/html5/thumbnails/20.jpg)
Topological Sort example
tsort(v) {mark v visitedfor each w adjacent to v if w unvisited tsort(w)
Hdisplay(v)}
path: A C D Houtput: G K Hreverse:
05/12/2015 DFR - DSA - Graphs 2 20
ABCDE
HG
CEDG HH
KK
E
KE
GC
HB
DA
![Page 21: Topological sort: example · Topological sort: example Given a DAG of prerequisites for courses, a topological sort can be used to determine an order in which to take the courses](https://reader030.vdocument.in/reader030/viewer/2022040621/5f3671880b61b772f26aea7b/html5/thumbnails/21.jpg)
Topological Sort example
tsort(v) {mark v visited
D for each w adjacent to v if w unvisited tsort(w)display(v)}
path: A C D output: G K Hreverse:
05/12/2015 DFR - DSA - Graphs 2 21
ABCDE
HG
CEDG HH
KK
E
KE
GC
HB
DA
![Page 22: Topological sort: example · Topological sort: example Given a DAG of prerequisites for courses, a topological sort can be used to determine an order in which to take the courses](https://reader030.vdocument.in/reader030/viewer/2022040621/5f3671880b61b772f26aea7b/html5/thumbnails/22.jpg)
Topological Sort example
tsort(v) {mark v visitedfor each w adjacent to v if w unvisited tsort(w)
Ddisplay(v)}
path: A C D output: G K H Dreverse:
05/12/2015 DFR - DSA - Graphs 2 22
ABCDE
HG
CEDG HH
KK
E
KE
GC
HB
DA
![Page 23: Topological sort: example · Topological sort: example Given a DAG of prerequisites for courses, a topological sort can be used to determine an order in which to take the courses](https://reader030.vdocument.in/reader030/viewer/2022040621/5f3671880b61b772f26aea7b/html5/thumbnails/23.jpg)
Topological Sort example
tsort(v) {mark v visited
C for each w adjacent to v if w unvisited tsort(w)display(v)}
path: A C output: G K H Dreverse:
05/12/2015 DFR - DSA - Graphs 2 23
ABCDE
HG
CEDG HH
KK
E
KE
GC
HB
DA
![Page 24: Topological sort: example · Topological sort: example Given a DAG of prerequisites for courses, a topological sort can be used to determine an order in which to take the courses](https://reader030.vdocument.in/reader030/viewer/2022040621/5f3671880b61b772f26aea7b/html5/thumbnails/24.jpg)
Topological Sort example
tsort(v) {mark v visited
C for each w adjacent to v if w unvisited tsort(w)display(v)}
path: A C E output: G K H Dreverse:
05/12/2015 DFR - DSA - Graphs 2 24
ABCDE
HG
CEDG HH
KK
E
KE
GC
HB
DA
![Page 25: Topological sort: example · Topological sort: example Given a DAG of prerequisites for courses, a topological sort can be used to determine an order in which to take the courses](https://reader030.vdocument.in/reader030/viewer/2022040621/5f3671880b61b772f26aea7b/html5/thumbnails/25.jpg)
Topological Sort example
tsort(v) {Emark v visited
for each w adjacent to v if w unvisited tsort(w)display(v)}
path: A C E output: G K H Dreverse:
05/12/2015 DFR - DSA - Graphs 2 25
ABCDE
HG
CEDG HH
KK
E
KE
GC
HB
DA
![Page 26: Topological sort: example · Topological sort: example Given a DAG of prerequisites for courses, a topological sort can be used to determine an order in which to take the courses](https://reader030.vdocument.in/reader030/viewer/2022040621/5f3671880b61b772f26aea7b/html5/thumbnails/26.jpg)
Topological Sort example
tsort(v) {mark v visited
E for each w adjacent to v if w unvisited tsort(w)display(v)}
path: A C E output: G K H Dreverse:
05/12/2015 DFR - DSA - Graphs 2 26
ABCDE
HG
CEDG HH
KK
E
KE
GC
HB
DA
![Page 27: Topological sort: example · Topological sort: example Given a DAG of prerequisites for courses, a topological sort can be used to determine an order in which to take the courses](https://reader030.vdocument.in/reader030/viewer/2022040621/5f3671880b61b772f26aea7b/html5/thumbnails/27.jpg)
Topological Sort example
tsort(v) {mark v visitedfor each w adjacent to v if w unvisited tsort(w)
E display(v)}
path: A C E output: G K H D Ereverse:
05/12/2015 DFR - DSA - Graphs 2 27
ABCDE
HG
CEDG HH
KK
E
KE
GC
HB
DA
![Page 28: Topological sort: example · Topological sort: example Given a DAG of prerequisites for courses, a topological sort can be used to determine an order in which to take the courses](https://reader030.vdocument.in/reader030/viewer/2022040621/5f3671880b61b772f26aea7b/html5/thumbnails/28.jpg)
Topological Sort example
tsort(v) {mark v visited
C for each w adjacent to v if w unvisited tsort(w)display(v)}
path: A Coutput: G K H D Ereverse:
05/12/2015 DFR - DSA - Graphs 2 28
ABCDE
HG
CEDG HH
KK
E
KE
GC
HB
DA
![Page 29: Topological sort: example · Topological sort: example Given a DAG of prerequisites for courses, a topological sort can be used to determine an order in which to take the courses](https://reader030.vdocument.in/reader030/viewer/2022040621/5f3671880b61b772f26aea7b/html5/thumbnails/29.jpg)
Topological Sort example
tsort(v) {mark v visitedfor each w adjacent to v if w unvisited tsort(w)
Cdisplay(v)}
path: A C output: G K H D E Creverse:
05/12/2015 DFR - DSA - Graphs 2 29
ABCDE
HG
CEDG HH
KK
E
KE
GC
HB
DA
![Page 30: Topological sort: example · Topological sort: example Given a DAG of prerequisites for courses, a topological sort can be used to determine an order in which to take the courses](https://reader030.vdocument.in/reader030/viewer/2022040621/5f3671880b61b772f26aea7b/html5/thumbnails/30.jpg)
Topological Sort example
tsort(v) {mark v visited
A for each w adjacent to v if w unvisited tsort(w)display(v)}
path: Aoutput: G K H D E Creverse:
05/12/2015 DFR - DSA - Graphs 2 30
ABCDE
HG
CEDG HH
KK
E
KE
GC
HB
DA
![Page 31: Topological sort: example · Topological sort: example Given a DAG of prerequisites for courses, a topological sort can be used to determine an order in which to take the courses](https://reader030.vdocument.in/reader030/viewer/2022040621/5f3671880b61b772f26aea7b/html5/thumbnails/31.jpg)
Topological Sort example
tsort(v) {mark v visitedfor each w adjacent to v if w unvisited tsort(w)
Adisplay(v)}
path: A output: G K H D E C Areverse:
05/12/2015 DFR - DSA - Graphs 2 31
ABCDE
HG
CEDG HH
KK
E
KE
GC
HB
DA
![Page 32: Topological sort: example · Topological sort: example Given a DAG of prerequisites for courses, a topological sort can be used to determine an order in which to take the courses](https://reader030.vdocument.in/reader030/viewer/2022040621/5f3671880b61b772f26aea7b/html5/thumbnails/32.jpg)
Topological Sort example
tsort(v) {mark v visitedfor each w adjacent to v if w unvisited tsort(w)display(v)}
path:output: G K H D E C Areverse:
05/12/2015 DFR - DSA - Graphs 2 32
ABCDE
HG
CEDG HH
KK
E
KE
GC
HB
DA
![Page 33: Topological sort: example · Topological sort: example Given a DAG of prerequisites for courses, a topological sort can be used to determine an order in which to take the courses](https://reader030.vdocument.in/reader030/viewer/2022040621/5f3671880b61b772f26aea7b/html5/thumbnails/33.jpg)
Topological Sort example
tsort(v) {Bmark v visited
for each w adjacent to v if w unvisited tsort(w)display(v)}
path: B output: G K H D E C Areverse:
05/12/2015 DFR - DSA - Graphs 2 33
ABCDE
HG
CEDG HH
KK
E
KE
GC
HB
DA
![Page 34: Topological sort: example · Topological sort: example Given a DAG of prerequisites for courses, a topological sort can be used to determine an order in which to take the courses](https://reader030.vdocument.in/reader030/viewer/2022040621/5f3671880b61b772f26aea7b/html5/thumbnails/34.jpg)
Topological Sort example
tsort(v) {mark v visited
Bfor each w adjacent to v if w unvisited tsort(w)display(v)}
path: B output: G K H D E C Areverse:
05/12/2015 DFR - DSA - Graphs 2 34
ABCDE
HG
CEDG HH
KK
E
KE
GC
HB
DA
![Page 35: Topological sort: example · Topological sort: example Given a DAG of prerequisites for courses, a topological sort can be used to determine an order in which to take the courses](https://reader030.vdocument.in/reader030/viewer/2022040621/5f3671880b61b772f26aea7b/html5/thumbnails/35.jpg)
Topological Sort example
tsort(v) {mark v visitedfor each w adjacent to v if w unvisited tsort(w)
Bdisplay(v)}
path: Boutput: G K H D E C A B reverse:
05/12/2015 DFR - DSA - Graphs 2 35
ABCDE
HG
CEDG HH
KK
E
KE
GC
HB
DA
![Page 36: Topological sort: example · Topological sort: example Given a DAG of prerequisites for courses, a topological sort can be used to determine an order in which to take the courses](https://reader030.vdocument.in/reader030/viewer/2022040621/5f3671880b61b772f26aea7b/html5/thumbnails/36.jpg)
Topological Sort example
tsort(v) {mark v visitedfor each w adjacent to v if w unvisited tsort(w)display(v)}
path:output: G K H D E C A B reverse: B A C E D H K G
05/12/2015 DFR - DSA - Graphs 2 36
ABCDE
HG
CEDG HH
KK
E
KE
GC
HB
DA