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

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

Embed Size (px)

Citation preview

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

Data Structures & Algorithms

Graph Search

Richard Newmanbased on book by R. Sedgewick

and slides by S. Sahni

Page 2: 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

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

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

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

DFS

0

6

7

2

4

5

3

1

Stack:075275675475753757557575(40)757down751(0)75down7down

02

6 4 3

571

0

2

6

4

3

5

7

1

treeparent

back

down

0 1 2 3 4 5 6 7ord 0 7 1 4 3 5 2 6

st 0 7 0 4 6 3 2 4

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

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!

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

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

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

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)

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

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

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

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.

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

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.

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

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

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

Separability

• Where are the bridge(s), articulation point(s)

0

6

7

2

4

5

3

1

5

bridge

articulation point

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

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.

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

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.

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

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?

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

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.

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

BFS

Queue:025757676346341341411

0

6

7

2

4

5

3

1

02

6 4 3

571

0

2 5 7

6 3 4 1

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

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

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

BFS

• Shortest Path v to w• Single Source Shortest Path:

shortest paths from v to all nodes• All-pairs Shortest Paths

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

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

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

Generalized Graph Search

• DFS and BFS are special cases• We can separate edges into three

classes:• In tree• On fringe• Not yet seen

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

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

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

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.

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

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.

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

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

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

Summary

• Graph search

• DFS

• Connectivity