Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures & Algorithms Graph Search Richard Newman based on book by R. Sedgewick and slides by S. Sahni.

Similar presentations


Presentation on theme: "Data Structures & Algorithms Graph Search Richard Newman based on book by R. Sedgewick and slides by S. Sahni."— Presentation transcript:

1 Data Structures & Algorithms Graph Search Richard Newman based on book by R. Sedgewick and slides by S. Sahni

2 Graph Search Like exploring a maze Passages = edges Intersections = nodes DFS – depth first search stack BFS – breadth first search queue

3 DFS Edges classified according to role in DFS Tree edges (recursive call) Parent edges Back edges (to ancestor) Down edges (to previously visited nodes) 0 6 7 2 4 5 3 1

4 DFS 0 6 7 2 4 5 3 1 Stack: 0 752 756 754 75753 75755 7575(40) 757down 751(0) 75down 7down 02 6 43 571 0 2 6 4 3 5 7 1 tree parent back down 0 1234567 ord0 7143526 st0 7046324

5 DFS Algorithms Cycle Detection If we find a back edge, it represents a cycle Simple path Start from one, DFS until find other (or complete DFS) Simple connectivity If DFS finds all the nodes, then yes!

6 DFS Algorithms Spanning Tree DFS search order defines ST (if connected) Connected Components DFS find them – get all of one, then if there are any nodes not yet visited, start new DFS from there (new component) Two-way Euler Tour DFS tree traverse each link twice

7 Separability Given two nodes, are there two different paths connecting them (distinct edges) Ability to remain connected even if an edge fails Ability to remain connected even if a node (and all its edges) fails (induced subgraph)

8 Separability Defn. 18.1: A bridge in a graph is an edge that, if removed, partitions the graph. A graph with no such edges is called edge-connected. Also see this as 2-edge connected Generalizes to k-edge connected

9 Separability Prop. 18.5: In any DFS tree, a tree edge v-w is a bridge iff there are no back edges that connect a descendent of w to an ancestor of w. If there is such an edge, then v-w is on a cycle and is not a bridge. If v-w is not a bridge, then there must be some such edge so that w can be reached other than by v-w.

10 Separability Prop. 18.6: We can find bridges in linear time. Use DFS keeping track of the lowest preorder number (back edge) reachable from each node.

11 Separability Defn 18.2: An articulation point is a node whose removal partitions the graph. A.k.a. cut vertex or separation vertex Defn. 18.3: A graph is biconnected iff every pair of nodes is connected by (node-) disjoint paths

12 Separability Where are the bridge(s), articulation point(s) 0 6 7 2 4 5 3 1 5 bridge articulation point

13 Separability Defn 18.4: A graph is k-connected if there are at least k disjoint paths connecting every pair of vertices. The vertex (node) connectivity is the minimum number of nodes whose removal partitions the graph.

14 Separability Defn 18.5: A graph is k-edge- connected if there are at least k edge-disjoint paths connecting every pair of vertices. The edge connectivity is the minimum number of edges whose removal partitions the graph.

15 Separability s-t connectivity: what is the minimum number of edges (nodes) whose removal would separate vertices s and t? General connectivity: Is G k- connected? Is G k-edge-connected? What is the node- (edge-) connectivity of G?

16 BFS Instead of stack (like DFS), use queue (now explicit). Prop. 18.9: During BFS, nodes enter and leave the FIFO queue in order of their distance from the start node. Prop. 18.10: For any node w in the BFS tree rooted at v, the tree path from v to w is a shortest path in G.

17 BFS Queue: 0 257 576 7634 6341 341 41 1 0 6 7 2 4 5 3 1 02 6 43 571 0 257 6 3 4 1

18 BFS Observations There is a relatively short path connecting each pair of nodes During search, most nodes are adjacent to many unvisited nodes Tree is very shallow

19 BFS Shortest Path v to w Single Source Shortest Path: shortest paths from v to all nodes All-pairs Shortest Paths

20 Generalized Graph Search DFS and BFS are special cases We can separate nodes into four classes: Already visited (in tree) Never before seen Seen but not yet visited Being visited

21 Generalized Graph Search DFS and BFS are special cases We can separate edges into three classes: In tree On fringe Not yet seen

22 Generalized Graph Search General strategy: Start with self-loop to start node on fringe and empty tree, then Move an edge from fringe to tree If vertex not yet visited, visit it, and Put into fringe all edges to yet unvisited nodes Until fringe is empty

23 Generalized Graph Search What type of search depends on how fringe edges are selected If stack DFS If queue BFS If priority queue …. We get MST, or SSSP, etc.

24 Generalized Graph Search Prop. 18.12: Generalize graph search visits all nodes in a connected graph in time O(V 2 ) for adjacency matrix rep., or O(V+E) for adjacency lists rep., plus, in the worst case, the time required for V insert, V remove, and E update operations on a generalized queue of size V.

25 Generalized Graph Search Two particular generalize queue structures: Randomized queue Remove a randomly selected item Used for randomized search Priority queue Remove highest-priority edge Shortest path Very flexible, many applications

26 Summary Graph search DFS Connectivity


Download ppt "Data Structures & Algorithms Graph Search Richard Newman based on book by R. Sedgewick and slides by S. Sahni."

Similar presentations


Ads by Google