L27: Graph (L3):
Connected Components, Common
Questions
Strongly Connected Components (Directed Graph)
• SCC: A component is SCC if and only if for every pair of
vertices A and B, there exists a path from A to B and a
path from B to A.
• DFS used twice: once on orig. graph and then on
transposed graph.
• Read Cormen book for revision.
• As per the Cormen's solution: The finish time needs to be
stored during 1st call to DFS
• Easier implementation: Instead of finish time, use stack
to store the visited vertices in 1st call of DFS and LIFO
order of these vertices can be used in 2nd call to DFS.
2
DAG: Topological Sorting (TS)
• Revise DAG, Topological Sorting from the Cormen's book
• Topological sort: linear ordering of all vertices of a directed graph
such that if G contains an edge (u->v) then u appears before v in
the ordering.
• Implementation: Depth traversal with use of a stack is easiest.
3
Water-Connection problem: geeks
• https://www.geeksforgeeks.org/water-connection-problem/
• Every house in the colony has at most one pipe going into it and at
most one pipe going out of it. Tanks and taps are to be installed in
a manner such that every house with one outgoing pipe but no
incoming pipe gets a tank installed on its roof and every house with
only an incoming pipe and no outgoing pipe gets a tap.’
Given two integers n and p denoting the number of houses and the
number of pipes. The connections of pipe among the houses
contain three input values: a_i, b_i, d_i denoting the pipe of
diameter d_i from house a_i to house b_i, find out the efficient
solution for the network.
The output will contain the number of pairs of tanks and taps t
installed in first line and the next t lines contain three integers:
house number of tank, house number of tap and the minimum
diameter of pipe between them.
4
Water-Connection: Example
• Input :9 6
7 4 98 5 9 72 4 6 10 2 8 22 9 7 17 3 1 66
• Output :3
2 8 22 3 1 66 5 6 10 5
Water-connection Problem: Connected
Component Finding
• Water connection problem : Find all disjoint connected
components.
• Intuition: DFS does similar
• Issue: DFS straightaway may now be sufficient, as
starting point of DFS may not be necessarily correct, as
start node of any existing connection chain can have a
higher vertex number
6
DFS: Reproduced
void dfs(G)
{ for (i=0;i<n;i++)
if visited[i] == 0)
dfs_visit(i) }
void dfs_visit(int curr) //used by DFS for all un-visited vertices
{ visited[curr]=1; for(i=1;i<=n;i++)
{ if(graph[curr][i] && !visited[i])
dfs_visit(i);}
• In Water-connection, if chain is 5-3-0-1-2; 0-1-2 will
come through DFS;
• Improve call to dfs_visit: call dfs_visit first time for those
vertices whose __________________ 7
Common Interview Questions
• Find whether an undirected graph has a cycle or not?
• Find whether a Directed Graph has a cycle or not?
• Check if a given graph is tree or not?
• Find out whether an undirected graph is strongly connected
or not?
• Find out whether a Directed Graph is strongly connected or
not ?
8
Cycle in an undirected Graph
• Given an undirected graph, how to check if there is a cycle
in the graph?
• Not difficult, think which of the studied algo can be useful?
• Soln A: DFS based: if a vertex being traversed, has in its
adjacents a vertex which has been already visited or is
pending in the recursion, means a cycle.
• Issue: Take care that each vertex (u,v) appears as (v,u)
also, so if edge 1-2 has been processed, 2-1 edge should not
say it a cycle.
• Soln: maintain parent node from which we came to current
node, keep parent as parameter in DFS_rec
• Coding: simple, either use visited[] (as in Horo & Sahni) or
use color as in Cormen’s DFS, 9
Cycle in an undirected Graph: Soln B
• Soln B: BFS based: In a similar way as above
• Soln C: Can disjoint set representation based algos?
• Union operation can be very useful
• Union (x,y) checks for find_set (x) vs find_set (y)
• If equal=> cycle
• It is called as union-find algorithm
• Assumption is no self loops exist;
• If self-loop or self-edge exists, it can be detected easily.
10
Cycle in a Directed Graph:
• Soln A: Will DFS based soln of undirected graph work?
Crux: there is a cycle in a graph only if there is a back
edge [i.e., a node points to one of its ancestors in a
DFS tree] present in the graph
Back edge detection: keep track of the visited nodes
that are in the current recursion stack (the current path
that we are visiting).
In DFS(..), Create a local boolean array where we use
vertex number as an index, keep on updating & pass as
parameter to DFS_rec
• Soln B: BFS based (think of storing current-path/
predecessors like in DFS; will it work?)
• Soln C: topological sorting can also be used. How? 11
Cycle in a Directed Graph:
• Soln C: topological sorting can also be used. How?
• Main Logic: after topol ordering, edge based checking.
• Topological Ordering: Mainly for DAG, but will the
standard algo (say of Cormen) generate a topological
ordering for a directed cyclic graph?
• Yes, e.g. if graph has edges: 1->2, 2->3, 3->4, 4->1;
topological sorting o/p: 1 2 3 4 (recall working of DFS)
• How to find cycle based on topological sorting: Once
topological ordering is ready, check for every directed
edge whether it follows the order or not.
• How to implement above step: Maintain an array/map
posn[] to store position of each node in the topolog sort.
• For edge (u,v), if posn[u] > posn[v] , cycle exists 12
Next lecture: Common Interview Questions
• Check if a given graph is tree or not?
• Find out whether an undirected graph is strongly connected
or not?
• Find out whether a Directed Graph is strongly connected or
not ?
• Think of possible solutions, to be discussed in next class
13