280 likes | 303 Views
Data Structures & Algorithms Graph Search. Richard Newman based on book by R. Sedgewick and slides by S. Sahni. Graph Search. Like exploring a maze Passages = edges Intersections = nodes DFS – depth first search stack BFS – breadth first search queue. DFS.
E N D
Data Structures & Algorithms Graph Search Richard Newman based on book by R. Sedgewick and slides by S. Sahni
Graph Search • Like exploring a maze • Passages = edges • Intersections = nodes • DFS – depth first search • stack • BFS – breadth first search • queue
DFS • Edges classified according to role in DFS • Tree edges (recursive call) • Parent edges • Back edges (to ancestor) • Down edges (to previously visited nodes) 2 0 1 7 5 6 4 3
DFS Stack: 0 752 756 754 75753 75755 7575(40) 757down 751(0) 75down 7down 0 down 2 2 2 0 0 6 1 1 7 7 5 5 4 tree parent 6 4 4 3 3 6 3 7 0 back 1 2 3 4 5 6 7 5 1 ord 0 7 1 4 3 5 2 6 st 0 7 0 4 6 3 2 4
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!
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
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)
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
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.
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.
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
Separability • Where are the bridge(s), articulation point(s) articulation point 2 0 5 1 7 5 6 4 3 bridge
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.
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.
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?
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.
BFS Queue: 0 257 576 7634 6341 341 41 1 0 2 2 0 0 2 5 7 1 1 7 7 5 5 3 6 4 1 6 4 4 3 3 6
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
BFS • Shortest Path v to w • Single Source Shortest Path: shortest paths from v to all nodes • All-pairs Shortest Paths
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
Generalized Graph Search • DFS and BFS are special cases • We can separate edges into three classes: • In tree • On fringe • Not yet seen
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
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.
Generalized Graph Search • Prop. 18.12: Generalize graph search visits all nodes in a connected graph in time O(V2) 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.
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
Summary • Graph search • DFS • Connectivity