CSCE 411 Lecture 18
Jump to navigation
Jump to search
« previous | Friday, October 5, 2012 | next »
Depth-First Search (DFS) Cont'd
Topological Sort
Given a directed acyclic graph (DAG), find a linear ordering of the nodes such that if is an edge, then precedes .
DAG edges indicate a precedence or dependency.
Perform DFS, and either
- insert vertices at front of list as recursive call finishes or
- sort all vertices by finish time
Example
Tasks that have to be done to eat breakfast:
get glass, pour juice, get bowl, pour cereal, pour milk, get spoon, eat.
Precedence:
eat breakfast |-- pour juice | `-- get glass |-- pour milk | `-- pour cereal | `-- get bowl `-- get spoon
DFS traversal has following discovery and finish times:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | | '--eat--' | | | | '--milk--' | | '--spoon--' | '---juice---' | | '---cereal----' | '-----glass-----' '------bowl--------'
When each node's recursive call finishes, insert it on the front of a list:
spoon, bowl, cereal, milk, glass, juice, eat
(or just sort by finish time)
Correctness
3 Cases for precedes :
- is finished when is discovered, then finishes before .
- is not yet discovered when
is discovered but not yet finishedwill not happen on an acyclic graph.