Presentation is loading. Please wait.

Presentation is loading. Please wait.

© 2004 Goodrich, Tamasia Recall: Digraphs A digraph is a graph whose edges are all directed Short for “directed graph” Applications one-way streets flights.

Similar presentations


Presentation on theme: "© 2004 Goodrich, Tamasia Recall: Digraphs A digraph is a graph whose edges are all directed Short for “directed graph” Applications one-way streets flights."— Presentation transcript:

1 © 2004 Goodrich, Tamasia Recall: Digraphs A digraph is a graph whose edges are all directed Short for “directed graph” Applications one-way streets flights task scheduling A C E B D

2 © 2004 Goodrich, Tamasia Digraph Application Scheduling: edge (a,b) means task a must be completed before b can be started The good life ics141 ics131 ics121 ics53 ics52 ics51 ics23ics22ics21 ics161 ics151 ics171

3 © 2004 Goodrich, Tamasia DFS In digraphs we traverse edges only along their direction In the directed DFS algorithm, we can distinguish four types of edges discovery edges back edges forward edges cross edges A directed DFS starting a a vertex s determines the vertices reachable from s A C E B D

4 © 2004 Goodrich, Tamasia Reachability DFS tree rooted at v: vertices reachable from v via directed paths A C E B D F A C ED A C E B D F

5 © 2004 Goodrich, Tamasia DAGs and Topological Ordering A directed acyclic graph (DAG) is a digraph that has no directed cycles A topological ordering of a digraph is a numbering v 1, …, v n of the vertices such that for every edge (v i, v j ), we have i  j Example: in a task scheduling digraph, a topological ordering a task sequence that satisfies the precedence constraints Theorem A digraph admits a topological ordering if and only if it is a DAG B A D C E DAG G B A D C E Topological ordering of G v1v1 v2v2 v3v3 v4v4 v5v5

6 © 2004 Goodrich, Tamasia write c.s. program play Topological Sorting Number vertices, so that (u,v) in E implies u < v wake up eat nap study computer sci. more c.s. work out sleep dream about graphs A typical student day 1 2 3 4 5 6 7 8 9 10 11 make cookies for professors

7 © 2004 Goodrich, Tamasia Running time: ? Algorithm for Topological Sorting Method TopologicalSort(G) H  G// Temporary copy of G n  G.numVertices() while H is not empty do Let v be a vertex with no outgoing edges Label v  n n  n - 1 Remove v from H

8 © 2004 Goodrich, Tamasia Topological Sorting Algorithm using DFS Run DFS Topological sort = Reverse order of Finish Order List O(n+m) time.

9 © 2004 Goodrich, Tamasia Topological Sorting Example

10 © 2004 Goodrich, Tamasia Topological Sorting Example 9

11 © 2004 Goodrich, Tamasia Topological Sorting Example 8 9

12 © 2004 Goodrich, Tamasia Topological Sorting Example 7 8 9

13 © 2004 Goodrich, Tamasia Topological Sorting Example 7 8 6 9

14 © 2004 Goodrich, Tamasia Topological Sorting Example 7 8 5 6 9

15 © 2004 Goodrich, Tamasia Topological Sorting Example 7 4 8 5 6 9

16 © 2004 Goodrich, Tamasia Topological Sorting Example 7 4 8 5 6 3 9

17 © 2004 Goodrich, Tamasia Topological Sorting Example 2 7 4 8 5 6 3 9

18 © 2004 Goodrich, Tamasia Topological Sorting Example 2 7 4 8 5 6 1 3 9


Download ppt "© 2004 Goodrich, Tamasia Recall: Digraphs A digraph is a graph whose edges are all directed Short for “directed graph” Applications one-way streets flights."

Similar presentations


Ads by Google