Topological sort: example · Topological sort: example Given a DAG of prerequisites for courses, a...

36
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) }

Transcript of Topological sort: example · Topological sort: example Given a DAG of prerequisites for courses, a...

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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