Presentation is loading. Please wait.

Presentation is loading. Please wait.

Graph Search Applications

Similar presentations


Presentation on theme: "Graph Search Applications"— Presentation transcript:

1 Graph Search Applications

2 Application: Connected Components
Undirected graph – find connected components Loop through all vertices, when you find one unvisited: Run DFS or BFS to pull out all connected pieces Mark the nodes as visited

3 Flood Fill Basic search over a grid, identifying connected components.
Explore current cell, then look at the 8 (or 4, or in 3D 8/26/etc.) adjacent ones recursively Mark the cell by changing the value in the cell Forms basis for lots of algorithms

4 Bipartite Graph Check Check whether bipartite (2-colorable)
Just run BFS (or DFS), coloring the nodes one of two colors as encountered. If there is ever a conflict, then can’t be bipartite

5 Topological Sort Given constraints about what has to come first (i.e. directed graph), find some ordering that is OK Always start with a node that has in-degree of 0 Can modify DFS to do this After visiting all adjacent nodes, add this node to the end of the list Can modify BFS to do this Add nodes with in-degree 0 to list Pop a node off the list, remove all edges coming out If removing an edge makes another node have in-degree 0, enqueue it.

6 Classifying Back Edges
Run DFS, but also keep a third state: Unvisited (never started visiting) Exploring (began processing neighbors, but not done) Visited (visited and explored all neighbors) A “Back Edge” goes from current node to an Exploring node (or a Visited node, if a directed graph) Can be used to find cycles (back edges that aren’t a single undirected edge)

7 Articulation Points and Bridges
Find a vertex or an edge that when removed will disconnect the UNDIRECTED graph Option 1: naïve approach Calculate the number of connected components Try, one-by-one, removing each edge/vertex Recalculate number of CCs If more CCs, then it’s an articulation point or bridge.

8 Articulation Points and Bridges
Find a vertex or an edge that when removed will disconnect the UNDIRECTED graph Option 2: specialized algorithm Compute DFS, but also store two values per node: Num: the order in which the node was reached during DFS Low: lowest Num reachable from current subtree Update Low when you have a back edge If a neighbor vertex has a Low value greater or equal to your own Num, then you’re at an articulation point If Low(u) > Num (v), then u-v is a bridge Likewise, if Low(v) > Num (u), then u-v is a bridge

9 Num Low 1 2 Num is when visited Low is lowest seen in that subgraph 7 3 6 9 4 3 8 5 3

10 Num Low 1 2 Articulation point if neighbor has a low value >= your num 7 3 6 9 4 3 8 5 3

11 Num Low 1 2 Bridge if Low(u) > Num(v) for some edge u-v 7 3 6 9 4 3 8 5 3

12 Strongly Connected Components (Directed)
Notice: can replace a SCC by a single node, and result is a directed acyclic graph. Perform DFS like before. SCCs are identified when a back edge creates a cycle (and therefore connects everything in that subtree. Update Low only for Visited (not Exploring) vertices. Add each node to a stack When you finish a node where Num == Low, it marks the root of a SCC Pop nodes off the stack until you get that node off; those nodes are a SCC


Download ppt "Graph Search Applications"

Similar presentations


Ads by Google