Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 22: Elementary Graph Algorithms

Similar presentations


Presentation on theme: "Chapter 22: Elementary Graph Algorithms"— Presentation transcript:

1 Chapter 22: Elementary Graph Algorithms

2 Undirected Graph Information Technology
Example: A city map for pedestrians (edges are streets, nodes are intersections) A B C D E F G J I H K L M N

3 Directed Graph (Digraph)
Information Technology Example: A city map for cars (edges are one-way streets, nodes are intersections) A B C D E F G J I H K L M N

4 Directed & Undirected Graphs
Information Technology Node, vertex (plural: vertices) Edge Self-loop (edge from a node to itself) Adjacent nodes (connected by an edge) Degree (#edges from or to a node) In-degree (#edges to a node) Out-degree (#edges from a node) Path (sequence of adjacent nodes)

5 Representations of Graphs
Information Technology Adjacency-matrix representation: a 2-dimensional array A of 0/1 values, with A[i,j ] containing the number of arcs between nodes i and j (undirected graph), or from node i to node j (digraph) Adjacency-list representation: a 1-dimensional array Adj of adjacency lists, with Adj [i ] containing the list of nodes adjacent to node i

6 Information Technology
A B C D E F G H I J K L M N A: B: C: D: E: F: G: J: I: H: K: L: M: N: B F G H A 1 1 1 1 A C B D G J B 1 1 C E J C 1 1 1 1 D I N A H D 1 1 1 A C K Information Technology E 1 1 1 C D I K L M F 1 1 E J A F K G 1 1 1 G H J H 1 1 1 J Note: For our convenience, each adjacency list is alphabetically ordered. J N I 1 1 E M J 1 1 1 1 1 1B1 1 C 1 A D E K L 1 M 1 1 F G J I N 1 1 H K L M N

7 B1 1 Information Technology A C D E F G J I H K L M N A: B: C: D: E:
A B C D E F G H I J K L M N A: B: C: D: E: F: G: J: I: H: K: L: M: N: B F A 1 1 C G J B 1 C E J C 1 1 N H D 1 1 1 A K Information Technology E 1 I L M F 1 E A K G 1 1 G H J H 1 1 Note: For our convenience, each adjacency list is alphabetically ordered. N I 1 M J 1 1 1 B1 1 A C D E K 1 L M 1 F G J I N 1 H K L M N

8 Graph Traversals by breadth-first search (BFS)
Information Technology by breadth-first search (BFS) by depth-first search (DFS) BFS and DFS work on directed graphs as well as on undirected graphs (the examples below are for digraphs). BFS and DFS assume the given graph is represented using adjacency lists.

9 Graph Traversals Breadth-first search (BFS) Depth-first search (DFS)
Information Technology Breadth-first search (BFS) Depth-first search (DFS)

10 BFS Algorithm

11 Information Technology
BFS order (black) Queue (gray) A A: B: C: D: E: F: G: J: I: H: K: L: M: N: B F C Given a source node, A. Paint the source gray. Paint other nodes white. Add the source to the initially empty first-in first-out (FIFO) queue of gray nodes. G J C E J N H A K Information Technology I L M E A K G H J N M A B C D E F G J I K L M N

12 Information Technology
BFS order (black) Queue (gray) A BF A: B: C: D: E: F: G: J: Paint it black (all adjacent I: H: K: L: M: N: B F C Dequeue head node, A. Paint its undiscovered (=white) adjacent nodes gray and enqueue them. G J C E J N H A K Information Technology I L M E nodes are discovered). A K G H J N M A B C D E F G J I K L M N

13 Information Technology
BFS order (black) Queue (gray) A BF AB FC A: B: C: D: E: F: G: J: I: H: K: L: M: N: B F C G J C E J N H A K Information Technology I L M E A K G H J N M A B C D E F G J I K L M N

14 Information Technology
BFS order (black) Queue (gray) A BF AB FC ABF CH A: B: C: D: E: F: G: J: I: H: K: L: M: N: B F C G J C E J N H A K Information Technology I L M E A K G H J N M A B C D E F G J I K L M N

15 Information Technology
BFS order (black) Queue (gray) A BF AB FC ABF CH ABFC HGJ A: B: C: D: E: F: G: J: I: H: K: L: M: N: B F C G J C E J N H A K Information Technology I L M E A K G H J N M A B C D E F G J I K L M N

16 Information Technology
BFS order (black) Queue (gray) A BF AB FC ABF CH ABFC HGJ ABFCH GJK A: B: C: D: E: F: G: J: I: H: K: L: M: N: B F C G J C E J N H A K Information Technology I L M E A K G H J N M A B C D E F G J I K L M N

17 Information Technology
BFS order (black) Queue (gray) A BF AB FC ABF CH ABFC HGJ ABFCH GJK ABFCHG JK A: B: C: D: E: F: G: J: I: H: K: L: M: N: B F C G J C E J N H ( A ) ( K ) Information Technology I L M E A K G H J N M A B C D E F G J I K L M N

18 Information Technology
BFS order (black) Queue (gray) A BF AB FC ABF CH ABFC HGJ ABFCH GJK ABFCHG JK ABFCHGJ KILM A: B: C: D: E: F: G: J: I: H: K: L: M: N: B F C G J C E J N H A K Information Technology I L M E A K G H J N M A B C D E F G J I K L M N

19 Information Technology
BFS order (black) Queue (gray) A BF AB FC ABF CH ABFC HGJ ABFCH GJK ABFCHG JK ABFCHGJ KILM ABFCHGJK ILM A: B: C: D: E: F: G: J: I: H: K: L: M: N: B F C G J C E J N H A K Information Technology I L M E A K G ) ( H ( ) ( J ) and so on N M A B C D E F G J I K L M N

20 Information Technology
BFS order (black) Queue (gray) A BF AB FC ABF CH ABFC HGJ ABFCH GJK ABFCHG JK ABFCHGJ KILM ABFCHGJK ILM ABFCHGJKI LME ABFCHGJKIL ME ABFCHGJKILM EN ABFCHGJKILME N ABFCHGJKILMEN A: B F B: C C: G J D: E: N F: H G: A K J: I L M I: E H: K: C E J Information Technology G H J L: M: N: May restart from white source D N M A B C D E F G J I K L M N

21 Analysis of BFS for graph (V,E) (without any restarts)
Information Technology The initialisations take Θ(V) time. Each node is enqueued (in O(1) time) and dequeued (in O(1) time) at most once (because no node is ever repainted white), hence O(V) time for all queue operations. Each adjacency list is scanned at most once, and their total length is Θ(E), hence O(E) time for scanning adjacency lists.  Aggregate analysis: O(V+E) time. 20

22 Refinements of BFS (at no increase in time complexity)
Information Technology Store the predecessor (or parent) of each newly discovered node (initially NIL, as undiscovered = white): breadth-first tree, rooted at source s. Store the distance (in #edges) from the source s to each newly discovered node (by adding 1 to the distance of its parent, with s being at distance 0): lengths of shortest paths from s to all reachable nodes.

23 Breadth-first tree from source A (without restart from D):
Information Technology Breadth-first tree from source A (without restart from D): A B C E F G J I H K L M N

24 Shortest Path

25 Shortest Path

26 Graph Traversals Breadth-first search (BFS) Depth-first search (DFS)
Information Technology Breadth-first search (BFS) Depth-first search (DFS)

27 Depth first search Algorithm

28 Information Technology
Paint all nodes white. DFS finish order (black) Stack (gray) Rest (white) ABCDEFGHIJKLMN Information Technology A B C D E F G J I H K L M N

29 Information Technology
The first white node, A, is the first source node. DFS finish order (black) Stack (gray) Rest (white) A BCDEFGHIJKLMN Paint the next undiscovered (=white) adjacent node gray and push it onto a stack before continuing on it (recursive case); if no such adjacent node (or: “child”) is found, then pop the top node off the stack, paint it black (all adjacent nodes discovered), and stop (base case). Information Technology A B C D E F G J I H K L M N

30 Information Technology
Continue to A’s first undiscovered child, B. DFS finish order (black) Stack (gray) Rest (white) A BCDEFGHIJKLMN BA CDEFGHIJKLMN Information Technology A B C D E F G J I H K L M N

31 Information Technology
Continue to B’s first undiscovered child, C. DFS finish order (black) Stack (gray) Rest (white) A BCDEFGHIJKLMN BA CDEFGHIJKLMN CBA DEFGHIJKLMN Information Technology A B C D E F G J I H K L M N

32 Information Technology
Continue to C’s first undiscovered child, G. DFS finish order (black) Stack (gray) Rest (white) A BCDEFGHIJKLMN BA CDEFGHIJKLMN CBA DEFGHIJKLMN GCBA DEFHIJKLMN Information Technology A B C D E F G J I H K L M N

33 Information Technology
Start at A Continue to G’s first undiscovered child, K. DFS finish order (black) Stack (gray) Rest (white) A BCDEFGHIJKLMN BA CDEFGHIJKLMN CBA DEFGHIJKLMN GCBA DEFHIJKLMN KGCBA DEFHIJLMN Information Technology A B C D E F G J I H t K L M N

34 Information Technology
Continue to K’s first undiscovered child, H. DFS finish order (black) Stack (gray) Rest (white) A BCDEFGHIJKLMN BA CDEFGHIJKLMN CBA DEFGHIJKLMN GCBA DEFHIJKLMN KGCBA DEFHIJLMN HKGCBA DEFIJLMN Information Technology A B C D E F G J I H K L M N 30

35 Information Technology
H has no undiscovered child, backtrack until we find node K, with undiscovered child J. DFS finish order (black) Stack (gray) Rest (white) A BCDEFGHIJKLMN BA CDEFGHIJKLMN CBA DEFGHIJKLMN GCBA DEFHIJKLMN KGCBA DEFHIJLMN HKGCBA DEFIJLMN H JKGCBA DEFILMN Information Technology A B C D E F G J I H K L M N

36 Information Technology
Continue to J’s first undiscovered child, I. DFS finish order (black) Stack (gray) Rest (white) A BCDEFGHIJKLMN BA CDEFGHIJKLMN CBA DEFGHIJKLMN GCBA DEFHIJKLMN KGCBA DEFHIJLMN HKGCBA DEFIJLMN H JKGCBA DEFILMN IJKGCBA DEFLMN Information Technology A B C D E F G J I H K L M N

37 Information Technology
Continue to I’s first undiscovered child, E. DFS finish order (black) Stack (gray) Rest (white) A BCDEFGHIJKLMN BA CDEFGHIJKLMN CBA DEFGHIJKLMN GCBA DEFHIJKLMN KGCBA DEFHIJLMN HKGCBA DEFIJLMN H JKGCBA DEFILMN IJKGCBA DEFLMN EIJKGCBA DFLMN Information Technology A B C D E F G J I H K L M N

38 Information Technology
Continue to E’s first undiscovered child, N. DFS finish order (black) Stack (gray) Rest (white) A BCDEFGHIJKLMN BA CDEFGHIJKLMN CBA DEFGHIJKLMN GCBA DEFHIJKLMN KGCBA DEFHIJLMN HKGCBA DEFIJLMN H JKGCBA DEFILMN IJKGCBA DEFLMN EIJKGCBA DFLMN NEIJKGCBA DFLM Information Technology A B C D E F G J I H K L M N

39 Information Technology
Continue to N’s first undiscovered child, M. DFS finish order (black) Stack (gray) Rest (white) A BCDEFGHIJKLMN BA CDEFGHIJKLMN CBA DEFGHIJKLMN GCBA DEFHIJKLMN KGCBA DEFHIJLMN HKGCBA DEFIJLMN H JKGCBA DEFILMN IJKGCBA DEFLMN EIJKGCBA DFLMN NEIJKGCBA DFLM MNEIJKGCBA DFL Information Technology A B C D E F G J I H K L M N

40 Information Technology
M has no undiscovered child, backtrack until we find node J, with undiscovered child L. DFS finish order (black) Stack (gray) Rest (white) A BCDEFGHIJKLMN BA CDEFGHIJKLMN CBA DEFGHIJKLMN GCBA DEFHIJKLMN KGCBA DEFHIJLMN HKGCBA DEFIJLMN H JKGCBA DEFILMN IJKGCBA DEFLMN EIJKGCBA DFLMN NEIJKGCBA DFLM MNEIJKGCBA DFL HMNEI LJKGCBA DF Information Technology A B C D E F G J I H K L M N

41 Information Technology
L has no undiscovered child, backtrack until we find node A, with undiscovered child F. DFS finish order (black) Stack (gray) Rest (white) A BCDEFGHIJKLMN BA CDEFGHIJKLMN CBA DEFGHIJKLMN GCBA DEFHIJKLMN KGCBA DEFHIJLMN HKGCBA DEFIJLMN H JKGCBA DEFILMN IJKGCBA DEFLMN EIJKGCBA DFLMN NEIJKGCBA DFLM MNEIJKGCBA DFL HMNEI LJKGCBA DF HMNEILJKGCB FA D Information Technology A B C D E F G J I H K L M N

42 Information Technology
F has no undiscovered child; the backtrack clears the stack. DFS finish order (black) Stack (gray) Rest (white) A BCDEFGHIJKLMN BA CDEFGHIJKLMN CBA DEFGHIJKLMN GCBA DEFHIJKLMN KGCBA DEFHIJLMN HKGCBA DEFIJLMN H JKGCBA DEFILMN IJKGCBA DEFLMN EIJKGCBA DFLMN NEIJKGCBA DFLM MNEIJKGCBA DFL HMNEI LJKGCBA DF HMNEILJKGCB FA D HMNEILJKGCBFA Information Technology A B C D E F G J I H K L M N

43 Information Technology
Restart from next white node, D, as source. DFS finish order (black) Stack (gray) Rest (white) A BCDEFGHIJKLMN BA CDEFGHIJKLMN CBA DEFGHIJKLMN GCBA DEFHIJKLMN KGCBA DEFHIJLMN HKGCBA DEFIJLMN H JKGCBA DEFILMN IJKGCBA DEFLMN EIJKGCBA DFLMN NEIJKGCBA DFLM MNEIJKGCBA DFL HMNEI LJKGCBA DF HMNEILJKGCB FA D HMNEILJKGCBFA Information Technology A B C D E F G J I H K L M N

44 Information Technology
D has no undiscovered child; backtrack; done. DFS finish order (black) Stack (gray) Rest (white) A BCDEFGHIJKLMN BA CDEFGHIJKLMN CBA DEFGHIJKLMN GCBA DEFHIJKLMN KGCBA DEFHIJLMN HKGCBA DEFIJLMN H JKGCBA DEFILMN IJKGCBA DEFLMN EIJKGCBA DFLMN NEIJKGCBA DFLM MNEIJKGCBA DFL HMNEI LJKGCBA DF HMNEILJKGCB FA D HMNEILJKGCBFA HMNEILJKGCBFAD Information Technology A B C D E F G J I H K L M N 40

45 Information Technology
DFS order (of pushing): ABCGKHJIENMLFD DFS finish order (black) Stack (gray) Rest (white) A BCDEFGHIJKLMN BA CDEFGHIJKLMN CBA DEFGHIJKLMN GCBA DEFHIJKLMN KGCBA DEFHIJLMN HKGCBA DEFIJLMN H JKGCBA DEFILMN IJKGCBA DEFLMN EIJKGCBA DFLMN NEIJKGCBA DFLM MNEIJKGCBA DFL HMNEI LJKGCBA DF HMNEILJKGCB FA D HMNEILJKGCBFA HMNEILJKGCBFAD Information Technology A B C D E F G J I H K L M N 41

46 Analysis of DFS for graph (V,E) (with restarts)
Information Technology Initialisations and restarts take Θ(V) time. Each node is visited exactly once (because no node is ever repainted white). Each adjacency list is scanned exactly once, and their total length is Θ(E), hence Θ(E) time for scanning adjacency lists. Aggregate analysis: Θ(V+E) time. It takes time linear in the size of the graph.

47 Refinements of DFS (at no increase in time complexity)
Information Technology Store the predecessor (or parent) of each newly discovered node (initially NIL, as undiscovered = white): depth-first forest of depth-first trees that are rooted at the chosen source nodes. Store the timestamps of discovering (graying) and finishing (blackening) each node: useful in many graph algorithms (for example: topological sort, see below).

48 Information Technology
Depth-first forest (of depth-first trees) DFS finish order (black) Stack (gray) Rest (white) A BCDEFGHIJKLMN BA CDEFGHIJKLMN CBA DEFGHIJKLMN GCBA DEFHIJKLMN KGCBA DEFHIJLMN HKGCBA DEFIJLMN H JKGCBA DEFILMN IJKGCBA DEFLMN EIJKGCBA DFLMN NEIJKGCBA DFLM MNEIJKGCBA DFL HMNEI LJKGCBA DF HMNEILJKGCB FA D HMNEILJKGCBFA HMNEILJKGCBFAD Information Technology A B C D E F G J I H K L M N

49 Information Technology
Depth-first forest A B C F G H K D E J I L M N Information Technology Another depth-first forest, starting from D A B C D E F G J I H K L M N Starting from L, then M, then J, then K, then D A B C D E H K L M N

50 Strongly Connected Components of a Digraph
Information Technology Strongly connected component (SCC): maximal set of nodes where there is a path from each node to each other node. Many algorithms first divide a digraph into its SCCs, then process these SCCs separately, and finally combine the sub- solutions. (This is not divide-&-conquer!) An undirected graph can be decomposed into its connected components.

51 Strongly Connected Components of a Digraph
Strongly connected component (SCC): maximal set of nodes where there is a path from each node to each other node. Information Technology A B C D E F G J I H K L M N

52 Strongly Connected Components of a Digraph
Strongly connected component (SCC): maximal set of nodes where there is a path from each node to each other node. Information Technology A B C D E F G J I K L M N

53 Computing the Strongly Connected Components of a Digraph G
Information Technology Enumerate the nodes of G in DFS finish order, starting from any node Compute the transpose GT (that is, reverse all edges) Make a DFS in GT, considering nodes in reverse finish order from original DFS Each tree in this depth-first forest is a strongly connected component 49

54 GT G Information Technology A B C D E F G J I H K L M N HMNEILJKGCBFAD
DFS finish order of G: DAFBCGKJLIENMH Reverse DFS finish order of G: A B C D E GT F G J I H K L M N 50

55 Information Technology
DFS finish order (black) Stack (gray) Rest (white) D AFBCGKJLIENMH Information Technology A B C D E F G J I H K L M N

56 Information Technology
DFS finish order (black) Stack (gray) Rest (white) D AFBCGKJLIENMH D| A FBCGKJLIENMH Information Technology A B C D E F G J I H K L M N

57 Information Technology
DFS finish order (black) Stack (gray) Rest (white) D AFBCGKJLIENMH D| A FBCGKJLIENMH GA FBCKJLIENMH Information Technology A B C D E F G J I H K L M N

58 Information Technology
DFS finish order (black) Stack (gray) Rest (white) D AFBCGKJLIENMH D| A FBCGKJLIENMH GA FBCKJLIENMH CGA FBKJLIENMH Information Technology A B C D E F G J I H K L M N

59 Information Technology
DFS finish order (black) Stack (gray) Rest (white) D AFBCGKJLIENMH D| A FBCGKJLIENMH GA FBCKJLIENMH CGA FBKJLIENMH BCGA FKJLIENMH Information Technology A B C D E F G J I H K L M N

60 Information Technology
DFS finish order (black) Stack (gray) Rest (white) D AFBCGKJLIENMH D| A FBCGKJLIENMH GA FBCKJLIENMH CGA FBKJLIENMH BCGA FKJLIENMH D|BC KGA FJLIENMH Information Technology A B C D E F G J I H K L M N

61 Information Technology
DFS finish order (black) Stack (gray) Rest (white) D AFBCGKJLIENMH D| A FBCGKJLIENMH GA FBCKJLIENMH CGA FBKJLIENMH BCGA FKJLIENMH D|BC KGA FJLIENMH HKGA FJLIENM Information Technology A B C D E F G J I H K L M N

62 Information Technology
DFS finish order (black) Stack (gray) Rest (white) D AFBCGKJLIENMH D| A FBCGKJLIENMH GA FBCKJLIENMH CGA FBKJLIENMH BCGA FKJLIENMH D|BC KGA FJLIENMH HKGA FJLIENM FHKGA JLIENM Information Technology A B C D E F G J I H K L M N

63 Information Technology
DFS finish order (black) Stack (gray) Rest (white) D AFBCGKJLIENMH D| A FBCGKJLIENMH GA FBCKJLIENMH CGA FBKJLIENMH BCGA FKJLIENMH D|BC KGA FJLIENMH HKGA FJLIENM FHKGA JLIENM D|BCFHKGA| J LIENM Information Technology A B C D E F G J I H K L M N

64 Information Technology
DFS finish order (black) Stack (gray) Rest (white) D AFBCGKJLIENMH D| A FBCGKJLIENMH GA FBCKJLIENMH CGA FBKJLIENMH BCGA FKJLIENMH D|BC KGA FJLIENMH HKGA FJLIENM FHKGA JLIENM D|BCFHKGA| J LIENM D|BCFHKGA|J| L IENM Information Technology A B C D E F G J I H K L M N 60

65 Information Technology
DFS finish order (black) Stack (gray) Rest (white) D AFBCGKJLIENMH D| A FBCGKJLIENMH GA FBCKJLIENMH CGA FBKJLIENMH BCGA FKJLIENMH D|BC KGA FJLIENMH HKGA FJLIENM FHKGA JLIENM D|BCFHKGA| J LIENM D|BCFHKGA|J| L IENM D|BCFHKGA|J|L I ENM Information Technology A B C D E F G J I H K L M N

66 Information Technology
DFS finish order (black) Stack (gray) Rest (white) D AFBCGKJLIENMH D| A FBCGKJLIENMH GA FBCKJLIENMH CGA FBKJLIENMH BCGA FKJLIENMH D|BC KGA FJLIENMH HKGA FJLIENM FHKGA JLIENM D|BCFHKGA| J LIENM D|BCFHKGA|J L IENM D|BCFHKGA|J|L I ENM D|BCFHKGA|J|L|I E NM Information Technology A B C D E F G J I H K L M N

67 Information Technology
DFS finish order (black) Stack (gray) Rest (white) D AFBCGKJLIENMH D| A FBCGKJLIENMH GA FBCKJLIENMH CGA FBKJLIENMH BCGA FKJLIENMH D|BC KGA FJLIENMH HKGA FJLIENM FHKGA JLIENM D|BCFHKGA| J LIENM D|BCFHKGA|J L IENM D|BCFHKGA|J|L I ENM D|BCFHKGA|J|L|I E NM D|BCFHKGA|J|L|I|E N M Information Technology A B C D E F G J I H K L M N

68 Information Technology
DFS finish order (black) Stack (gray) Rest (white) D AFBCGKJLIENMH D| A FBCGKJLIENMH GA FBCKJLIENMH CGA FBKJLIENMH BCGA FKJLIENMH D|BC KGA FJLIENMH HKGA FJLIENM FHKGA JLIENM D|BCFHKGA| J LIENM D|BCFHKGA|J L IENM D|BCFHKGA|J|L I ENM D|BCFHKGA|J|L|I E NM D|BCFHKGA|J|L|I|E N M MN Information Technology A B C D E F G J I H K L M N

69 Information Technology
DFS finish order (black) Stack (gray) Rest (white) D AFBCGKJLIENMH D| A FBCGKJLIENMH GA FBCKJLIENMH CGA FBKJLIENMH BCGA FKJLIENMH D|BC KGA FJLIENMH HKGA FJLIENM FHKGA JLIENM D|BCFHKGA| J LIENM D|BCFHKGA|J L IENM D|BCFHKGA|J|L I ENM D|BCFHKGA|J|L|I E NM D|BCFHKGA|J|L|I|E N M MN D|BCFHKGA|J|L|I|E|MN Information Technology A B C D E F G J I H K L M N

70 Information Technology
DFS finish order (black) Stack (gray) Rest (white) D AFBCGKJLIENMH D| A FBCGKJLIENMH GA FBCKJLIENMH CGA FBKJLIENMH BCGA FKJLIENMH D|BC KGA FJLIENMH HKGA FJLIENM FHKGA JLIENM D|BCFHKGA| J LIENM D|BCFHKGA|J L IENM D|BCFHKGA|J|L I ENM D|BCFHKGA|J|L|I E NM D|BCFHKGA|J|L|I|E N M MN D|BCFHKGA|J|L|I|E|MN Information Technology A B C D E F G J I H K L M N

71 Information Technology
DFS finish order (black) Stack (gray) Rest (white) D AFBCGKJLIENMH D| A FBCGKJLIENMH GA FBCKJLIENMH CGA FBKJLIENMH BCGA FKJLIENMH D|BC KGA FJLIENMH HKGA FJLIENM FHKGA JLIENM D|BCFHKGA| J LIENM D|BCFHKGA|J L IENM D|BCFHKGA|J|L I ENM D|BCFHKGA|J|L|I E NM D|BCFHKGA|J|L|I|E N M MN D|BCFHKGA|J|L|I|E|MN Information Technology A B C D E F G J I K L M N

72 Information Technology
D E Information Technology A B C F G H K J I L M N A B C D E F G J I H K L M N

73 Analysis of decomposition into SCCs of digraph G=(V,E)
Information Technology Assume G is represented by adjacency lists: DFS in G: Θ(V+E) time, including reversal Compute transpose GT: Θ(V+E) time DFS in GT: Θ(V+E) time Output SCCs: Θ(V+E) time Total: Θ(V+E) time A strongly connected digraph has one SCC.

74 then ni appears before nj in the ordering.
TOPOLOGICAL SORT Information Technology A topological sort is a linear ordering of all nodes of a directed acyclic graph (DAG). If there is a path from node ni to node nj then ni appears before nj in the ordering. Note on DAGs: There is no cycle (path from node to itself) There is at least one node of in-degree 0 There is at least one node of out-degree 0

75 TOPOLOGICAL SORT Example:
Information Technology Example: We are given courses at a university and the prerequisite relations between. These can be modelled as a DAG. A topological sort of these courses gives a valid sequence of taking them.

76 TOPOLOGICAL SORT Information Technology DArk Dig Av Algo Intro IT PKD
AD2 AI

77 TOPOLOGICAL SORT Information Technology DArk Dig Av Algo Intro IT PKD
AD2 Node with in-degree 0 AI

78 TOPOLOGICAL SORT Information Technology DArk Dig Av Algo Intro IT PKD
AD2 Nodes with out-degree 0 AI

79 TOPOLOGICAL SORT Information Technology Select node with in-degree 0
DArk Dig Information Technology Av Algo Intro IT Select node with in-degree 0 Print it out Remove it Repeat PKD AD2 AI

80 TOPOLOGICAL SORT Information Technology Select IntroIT DArk Dig
Av Algo Intro IT Select IntroIT PKD AD2 AI

81 TOPOLOGICAL SORT Information Technology Select IntroIT Print IntroIT
DArk Dig Information Technology Av Algo Intro IT Select IntroIT Print IntroIT PKD AD2 AI IntroIT

82 TOPOLOGICAL SORT Information Technology Select IntroIT Print IntroIT
DArk Dig Information Technology Av Algo Select IntroIT Print IntroIT Remove it PKD AD2 AI IntroIT

83 TOPOLOGICAL SORT Information Technology Select PKD DArk Dig Av Algo
AD2 AI IntroIT

84 TOPOLOGICAL SORT Information Technology Select PKD Print PKD DArk Dig
Av Algo Select PKD Print PKD PKD AD2 AI IntroIT, PKD 80

85 TOPOLOGICAL SORT Information Technology Select PKD Print PKD Remove it
DArk Dig Information Technology Av Algo Select PKD Print PKD Remove it AD2 AI IntroIT, PKD

86 TOPOLOGICAL SORT Information Technology Select DArkDig DArk Dig
Av Algo Select DArkDig AD2 AI IntroIT, PKD

87 TOPOLOGICAL SORT Information Technology Select DArkDig Print DArkDig
Av Algo Select DArkDig Print DArkDig AD2 AI IntroIT, PKD, DArkDig

88 TOPOLOGICAL SORT Information Technology Select DArkDig Print DArkDig
Av Algo Select DArkDig Print DArkDig Remove it AD2 AI IntroIT, PKD, DArkDig

89 TOPOLOGICAL SORT Information Technology Select AI Av Algo AD2 AI
IntroIT, PKD, DArkDig

90 TOPOLOGICAL SORT Information Technology Select AI Print AI Av Algo AD2
IntroIT, PKD, DArkDig, AI

91 TOPOLOGICAL SORT Information Technology Select AI Print AI Remove it
Av Algo Select AI Print AI Remove it AD2 IntroIT, PKD, DArkDig, AI

92 TOPOLOGICAL SORT Information Technology Select AD2 Av Algo AD2
IntroIT, PKD, DArkDig, AI

93 TOPOLOGICAL SORT Information Technology Select AD2 Print AD2 Av Algo
IntroIT, PKD, DArkDig, AI, AD2

94 TOPOLOGICAL SORT Information Technology Select AD2 Print AD2 Remove it
Av Algo Select AD2 Print AD2 Remove it IntroIT, PKD, DArkDig, AI, AD2 90

95 TOPOLOGICAL SORT Information Technology Select AvAlgo Av Algo
IntroIT, PKD, DArkDig, AI, AD2

96 TOPOLOGICAL SORT Information Technology Select AvAlgo Print AvAlgo
IntroIT, PKD, DArkDig, AI, AD2, AvAlgo

97 TOPOLOGICAL SORT Information Technology Select AvAlgo Print AvAlgo
Remove it IntroIT, PKD, DArkDig, AI, AD2, AvAlgo

98 TOPOLOGICAL SORT A topological sort is not necessarily unique:
Information Technology A topological sort is not necessarily unique: At any step, we may have more than one node with in-degree 0, so we have to choose one among them. IntroIT, PKD, AI, DArkDig, AD2, AvAlgo is also a valid sequence.

99 Other Algorithm and Analysis of Topological Sort
Information Technology Algorithm: Compute DFS finish time of each node; as each node is finished (blackened), insert it onto the front of an initially empty linked list; return that list. The total running time is thus Θ(V+E).


Download ppt "Chapter 22: Elementary Graph Algorithms"

Similar presentations


Ads by Google