slide1 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
C h a p ter 22: E l eme n tary G ra p h A lgo r i t h ms PowerPoint Presentation
Download Presentation
C h a p ter 22: E l eme n tary G ra p h A lgo r i t h ms

Loading in 2 Seconds...

play fullscreen
1 / 99

C h a p ter 22: E l eme n tary G ra p h A lgo r i t h ms - PowerPoint PPT Presentation


  • 69 Views
  • Updated on

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 ).

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

C h a p ter 22: E l eme n tary G ra p h A lgo r i t h ms


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
    Presentation Transcript
    1. Chapter 22: Elementary Graph Algorithms

    2. 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

    3. 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

    4. 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)

    5. 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

    6. 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

    7. 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

    8. 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.

    9. Graph Traversals InformationTechnology • Breadth-firstsearch(BFS) • Depth-first search (DFS)

    10. BFS Algorithm

    11. 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

    12. 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

    13. 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

    14. 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

    15. 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

    16. 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

    17. 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

    18. 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

    19. 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

    20. 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

    21. 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

    22. 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.

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

    24. ShortestPath

    25. ShortestPath

    26. Graph Traversals InformationTechnology • Breadth-first search (BFS) • Depth-firstsearch(DFS)

    27. Depth first search Algorithm

    28. Paint all nodeswhite. InformationTechnology A B C D E F G J I H K L M N

    29. 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

    30. Continueto A’s firstundiscoveredchild,B. InformationTechnology A B C D E F G J I H K L M N

    31. Continueto B’s firstundiscoveredchild,C. InformationTechnology A B C D E F G J I H K L M N

    32. Continueto C’s firstundiscoveredchild, G. InformationTechnology A B C D E F G J I H K L M N

    33. Start at A Continueto G’s firstundiscoveredchild, K. InformationTechnology A B C D E F G J I H t K L M N

    34. Continueto K’s firstundiscoveredchild,H. InformationTechnology A B C D E F G J I H K L M N 30

    35. Hhasnoundiscoveredchild, backtrackuntil wefindnode K, withundiscoveredchildJ. InformationTechnology A B C D E F G J I H K L M N

    36. Continueto J’s firstundiscoveredchild,I. InformationTechnology A B C D E F G J I H K L M N

    37. Continueto I’s firstundiscoveredchild,E. InformationTechnology A B C D E F G J I H K L M N

    38. Continueto E’s firstundiscoveredchild,N. InformationTechnology A B C D E F G J I H K L M N

    39. Continueto N’s firstundiscoveredchild,M. InformationTechnology A B C D E F G J I H K L M N

    40. Mhasnoundiscoveredchild, backtrackuntil wefindnodeJ,withundiscoveredchildL. InformationTechnology A B C D E F G J I H K L M N

    41. Lhasnoundiscoveredchild, backtrackuntil wefindnode A, withundiscoveredchild F. InformationTechnology A B C D E F G J I H K L M N

    42. Fhasnoundiscoveredchild; thebacktrack clears the stack. InformationTechnology A B C D E F G J I H K L M N

    43. Restart fromnext whitenode, D, as source. InformationTechnology A B C D E F G J I H K L M N

    44. Dhasnoundiscoveredchild; backtrack;done. InformationTechnology A B C D E F G J I H K L M N 40

    45. DFSorder (of pushing): ABCGKHJIENMLFD InformationTechnology A B C D E F G J I H K L M N 41

    46. 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.

    47. 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).

    48. Depth-first forest (of depth-first trees) InformationTechnology A B C D E F G J I H K L M N

    49. 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

    50. 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.