goodforgraph
goodforgraph
Graphs
• Applications of Depth-First Search
• Undirected graphs:
• Connected components, articulation points, bridges, biconnected components
• Directed graphs:
• Cyclic/acyclic graphs
• Topological sort
• Strongly connected components
Connectivity, connected components
1 5
2 7
6
3 4
Finding Connected Comps by DFS
• DFS-VISIT(G,s) reaches all nodes that are in the same connected
component as s
• The number of connected components is equal with the number of
calls of DFS-VISIT from DFS
Articulation points, Bridges, Biconnected
Components
• Let G = (V;E) be a connected, undirected graph.
• An articulation point of G is a vertex whose removal disconnects G.
• A bridge of G is an edge whose removal disconnects G.
• A biconnected component of G is a maximal set of edges such that
any two edges in the set lie on a common simple cycle
1 5 7
2
6
3 4 8
How to find all articulation points ?
• Brute-force approach: one by one remove all vertices and see if
removal of a vertex causes the graph to disconnect:
For every vertex v, do :
Remove v from graph
See if the graph remains connected (use BFS or DFS)
If graph is disconnected, add v to AP list
Add v back to the graph
• Time complexity of above method is O(V*(V+E)) for a graph
represented using adjacency list.
• Can we do better?
How to find all articulation points ?
• DFS- based-approach:
• We can prove following properties:
1. The root of a DFS-tree is an articulation point if and only if it has at least
two children.
2. A nonroot vertex v of a DFS-tree is an articulation point of G if and only if
has a child s such that there is no back edge from s or any descendant of s
to a proper ancestor of v.
3. Leafs of a DFS-tree are never articulation points
Finding articulation points by DFS
1 5 7
2 2
6
3 4 8
1 4
3 5
3 5
1 5
2
3 4
How to find all bridges ?
• Brute-force approach: one by one remove all edges and see if
removal of an edge causes the graph to disconnect:
For every edge e, do :
Remove e from graph
See if the graph remains connected (use BFS or DFS)
If graph is disconnected, add e to B list
Add e back to the graph
• Time complexity of above method is O(E*(V+E)) for a graph
represented using adjacency list.
• Can we do better?
How to find all bridges ?
• DFS- approach:
• An edge of G is a bridge if and only if it does not lie on any simple cycle of G.
• if some vertex u has a back edge pointing to it, then no edge below u
in the DFS tree can be a bridge. The reason is that
each back edge gives us a cycle, and no
edge that is a member of a cycle can be a bridge.
• if we have a vertex v whose parent in the DFS tree is u, and no
ancestor of v has a back edge pointing to it, then (u, v) is a bridge.
Biconnected Graph
• An undirected graph is called Biconnected if there are two vertex-
disjoint paths between any two vertices.
• In a Biconnected Graph, there is a simple cycle through any two
vertices.
• A connected graph is Biconnected if it is connected and doesn’t have
any Articulation Point.
Biconnected components – Example
1 5
2
6
3 4
7
8
Finding biconnected components
• Two biconnected components cannot have a common edge, but they
can have a common vertex
• The common vertex of several biconnected components is an
articulation point.
• The articulation points separate the biconnected components of a
graph.
-> We will try to identify the biconnected components while searching for
articulation points
Finding biconnected components
1
3 5
6 7
8
Finding biconnected components
• Algorithm principle:
• During DFS, use a stack to store visited edges (tree edges or back edges)
• After we finish the recursive search from a child v of a vertex u, we check if u is an
articulation point for v. If it is, we output all edges from the stack until (u,v). These edges
form a biconnected component
• When we return to the root of the DFS-tree, we have to output the edges even if the root
is no articulation point (graph may be biconnex) – we will not test the case of the root
being an articulation point
Thank You!!
Applications of DFS
• DFS has many applications
• For undirected graphs:
• Connected components
• Connectivity properties
• For directed graphs:
• Finding cycles
• Topological sorting
• Connectivity properties: Strongly connected components
Directed Acyclic Graphs
• A directed acyclic graph or DAG is a directed graph
with no directed cycles
1 1
2 2
3 3
acyclic cyclic
DFS and cycles in graph
• A graph G is acyclic if a DFS of G results in no back edges
u v w
1/ 2/
4/ 3/
x y z
Topological Sort
• Topological sort of a DAG (Directed Acyclic Graph):
• Linear ordering of all vertices in a DAG G such that vertex u comes before
vertex v if there is an edge (u, v) G
x y z
• There can be several orderings of the vertices that fulfill the
topological sorting condition:
• u, v, w, y, x, z
• w, z, u, v, y, x
• w, u, v, y, x, z
• …
Topological Sorting
• Algorithm principle:
1. Call DFS to compute finishing time v.f for every vertex
2. As every vertex is finished (BLACK) insert it onto the front of a linked list
3. Return the list as the linear ordering of vertexes
• Time: O(V+E)
Using DFS for Topological Sorting
Correctness of Topological Sort
• Claim: (u,v) G u.f > v.f
• When (u,v) is explored, u is grey
• v = grey (u,v) is back edge. Contradiction, since G is DAG and contains no back edges
• v = white v becomes descendent of u v.f < u.f
(since it must finish v before backtracking and finishing u)
• v = black v already finished v.f < u.f
Applications of DFS
• DFS has many applications
• For undirected graphs:
• Connected components
• Connectivity properties
• For directed graphs:
• Finding cycles
• Topological sorting
• Connectivity properties: Strongly connected components
Strongly Connected Components
• A strongly connected component of a directed graph G=(V,E) is a
maximal set of vertices C such that for every pair of vertices u and v
in C, both vertices u and v are reachable from each other.
Strongly connected components - Example
a b c d
e f g h
Strongly connected components – Example – The
Component Graph
cd
abe
h
fg
a b c d
13/14 11/16 1/10 8/9
a b c d
13/14 11/16 1/10 8/9
a b c d
13/14 11/16 1/10 8/9
a b c d
13/14 11/16 1/10 8/9
a b c d
13/14 11/16 1/10 8/9
a b c d
13/14 11/16 1/10 8/9
a b c d
13/14 11/16 1/10 8/9