990 likes | 1.06k Views
C h a p ter 22: E l eme n tary G ra p h A lgo r i t h ms. U ndi rected G ra p h. Information T echnology. E x ample: 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. D i rected Grap h (Digrap h ).
E N D
Undirected Graph InformationTechnology 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
Directed Graph (Digraph) InformationTechnology 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
Directed& Undirected Graphs InformationTechnology • 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)
Representations of Graphs InformationTechnology • 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 nodej (digraph) • Adjacency-list representation: • a 1-dimensional array Adj of adjacency lists, with Adj [i ] containing the list of nodes • adjacent to node i
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: A 1 1 1 1 B D G J B 1 1 C 1 1 1 1 D 1 1 1 A C K InformationTechnology E 1 1 1 C D I K L M F 1 1 A F K G 1 1 1 H 1 1 1 J Note:Forourconvenience, eachadjacency list is alphabeticallyordered. 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
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 C A 1 1 G J B 1 C 1 1 N H D 1 1 1 A K InformationTechnology E 1 I L M E F 1 A K G 1 1 H 1 1 Note:Forourconvenience, eachadjacency list is alphabeticallyordered. 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
Graph Traversals InformationTechnology • 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.
Graph Traversals InformationTechnology • Breadth-firstsearch(BFS) • Depth-first search (DFS)
A: B: C: D: E: F: G: J: I: H: K: L: M: N: B F C G J Givena source node, A.Paint the source gray. Paint other nodes white. Add the source to the initially empty first-in first-out (FIFO) queue ofgray nodes. N H A K InformationTechnology I L M E A K N M A B C D E F G J I K L M N
A: B: C: D: E: F: G: J: Paint itblack (alladjacentI: H: K: L: M: N: B F C G J Dequeue headnode, A.Paint itsundiscovered(=white) adjacent nodesgray andenqueue them. N H A K InformationTechnology I L M E nodes are discovered). A K N M A B C D E F G J I K L M N
A: B: C: D: E: F: G: J: I: H: K: L: M: N: B F C G J N H A K InformationTechnology I L M E A K N M A B C D E F G J I K L M N
A: B: C: D: E: F: G: J: I: H: K: L: M: N: B F C G J N H A K InformationTechnology I L M E A K N M A B C D E F G J I K L M N
A: B: C: D: E: F: G: J: I: H: K: L: M: N: B F C G J N H A K InformationTechnology I L M E A K N M A B C D E F G J I K L M N
A: B: C: D: E: F: G: J: I: H: K: L: M: N: B F C G J N H A K InformationTechnology I L M E A K N M A B C D E F G J I K L M N
A: B: C: D: E: F: G: J: I: H: K: L: M: N: B F C G J N H A) (K ) ( InformationTechnology I L M E A K N M A B C D E F G J I K L M N
A: B: C: D: E: F: G: J: I: H: K: L: M: N: B F C G J N H A K InformationTechnology I L M E A K N M A B C D E F G J I K L M N
A: B: C: D: E: F: G: J: I: H: K: L: M: N: B F C G J N H A K InformationTechnology 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
InformationTechnology May restart from white source D L: M: N: N M A B C D E F G J I K L M N
Analysis of BFS for graph(V,E) (without any restarts) InformationTechnology • 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
Refinements of BFS (at no increase in time complexity) InformationTechnology • 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 pathsfrom s to allreachable nodes.
InformationTechnology • Breadth-first tree from source A (without restart from D): A B C E F G J I H K L M N
Graph Traversals InformationTechnology • Breadth-first search (BFS) • Depth-firstsearch(DFS)
Paint all nodeswhite. InformationTechnology A B C D E F G J I H K L M N
The firstwhitenode, A,is thefirstsourcenode. Paint thenext undiscovered(=white) adjacent nodegrayandpushitontoa stack before continuing onit (recursive case); ifnosuch adjacent node (or: “child”)is found, then popthetopnode off the stack, paint it black (all adjacent nodes discovered), and stop (base case). InformationTechnology A B C D E F G J I H K L M N
Continueto A’s firstundiscoveredchild,B. InformationTechnology A B C D E F G J I H K L M N
Continueto B’s firstundiscoveredchild,C. InformationTechnology A B C D E F G J I H K L M N
Continueto C’s firstundiscoveredchild, G. InformationTechnology A B C D E F G J I H K L M N
Start at A Continueto G’s firstundiscoveredchild, K. InformationTechnology A B C D E F G J I H t K L M N
Continueto K’s firstundiscoveredchild,H. InformationTechnology A B C D E F G J I H K L M N 30
Hhasnoundiscoveredchild, backtrackuntil wefindnode K, withundiscoveredchildJ. InformationTechnology A B C D E F G J I H K L M N
Continueto J’s firstundiscoveredchild,I. InformationTechnology A B C D E F G J I H K L M N
Continueto I’s firstundiscoveredchild,E. InformationTechnology A B C D E F G J I H K L M N
Continueto E’s firstundiscoveredchild,N. InformationTechnology A B C D E F G J I H K L M N
Continueto N’s firstundiscoveredchild,M. InformationTechnology A B C D E F G J I H K L M N
Mhasnoundiscoveredchild, backtrackuntil wefindnodeJ,withundiscoveredchildL. InformationTechnology A B C D E F G J I H K L M N
Lhasnoundiscoveredchild, backtrackuntil wefindnode A, withundiscoveredchild F. InformationTechnology A B C D E F G J I H K L M N
Fhasnoundiscoveredchild; thebacktrack clears the stack. InformationTechnology A B C D E F G J I H K L M N
Restart fromnext whitenode, D, as source. InformationTechnology A B C D E F G J I H K L M N
Dhasnoundiscoveredchild; backtrack;done. InformationTechnology A B C D E F G J I H K L M N 40
DFSorder (of pushing): ABCGKHJIENMLFD InformationTechnology A B C D E F G J I H K L M N 41
Analysis of DFS for graph(V,E) (withrestarts) InformationTechnology • 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.
Refinements of DFS (at no increase in time complexity) InformationTechnology • 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).
Depth-first forest (of depth-first trees) InformationTechnology A B C D E F G J I H K L M N
Depth-first forest A B C F G H K D E J I L M N InformationTechnology Another depth-first forest, starting fromD A B C D E F G J I H K L M N Starting from L, thenM,then J, then K, thenD A B C D E F G J I H K L M N
Strongly Connected Components of a Digraph InformationTechnology • 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.