Applications of graph-search algorithms

Applications of graph-search
algorithms
Gianpiero Cabodi and Paolo Camurati
Dip. Automatica e Informatica
Politecnico di Torino
Loop detection
A graph is acyclic if and only if in a DFS there
are no edges labelled B.
A.Y. 2014/15
12 Applications of graph search algorithms
Connected components
In an undirected graph represented as an
adjacency list:
 each tree of the DFS forest ia a connected
component
 G->cc[v] is an array that stores an integer
identifying each connected component. Nodes
serve as indexes of the array.
A.Y. 2014/15
12 Applications of graph search algorithms
Example
0
6
3
5
A.Y. 2014/15
2
1
7
8
9
10
11
12
4
12 Applications of graph search algorithms
0
0/13
B
1/2
5
T
5/12
3/4
1
3
B
8
T
14/17
15/16
1
2
T
6/11
7
8/9
T
T
T
6
T
4
7/10
0
T
9
18/25
T
21/24
B
19/20
B
T
T
T
T
2
5
8
T
T
10
11
B
T
T
10
T
9
7
3
B
12
T
4
T
22/23
11
12
cc
0 0 0 0
6
0 0 0 1 1 2 2 2 2
0 1 2 3 4 5 6 7 8 9 10 11 12
A.Y. 2014/15
12 Applications of graph search algorithms
A.Y. 2014/15
void dfsRcc(Graph G, int v, int id) {
link t;
G->cc[v] = id;
for (t = G->adj[v]; t != NULL; t = t->next)
if (G->cc[t->v] == -1)
dfsRcc(G, t->v, id);
}
int GRAPHcc(Graph G) {
int v, id = 0;
G->cc = malloc(G->V * sizeof(int));
for (v = 0; v < G->V; v++)
G->cc[v] = -1;
for (v = 0; v < G->V; v++)
if (G->cc[v] == -1)
dfsRcc(G, v, id++);
printf("connected components \n");
for (v = 0; v < G->V; v++)
printf("node %d in cc %d \n", v, G->cc[v]);
return id;
}
12 Applications of graph search algorithms
Connectivity
Given an undirected and connected graph,
find out whether the property of being
connected is lost because:
 an edge is removed
 a node is removed.
Bridge: edge whose removal disconnects the
graph.
Articulation point: node whose removal
disconnects the graph. Removing the vertex
entails the removal of insisting edges as well.
A.Y. 2014/15
12 Applications of graph search algorithms
Example
articulation point
A.Y. 2014/15
12 Applications of graph search algorithms
bridge
Bridge
An edge (v,w) labelled Back can’t be a
brigdge (nodes v and w are also connected by
a path in the DFS tree).
An edge (v,w) labelled Tree is a bridge if and
only if there are no edges labelled Back that
connect a descendant of w to an ancestor of v
in the DFS tree.
A.Y. 2014/15
12 Applications of graph search algorithms
Example
0
7
6
3
1
2
10
8
0
9
1
11
B
4
5
12
2
3
T
T
TT
T
B
T
6
B
4
5
8
T
T
12
7
10
9
11
A.Y. 2014/15
12 Applications of graph search algorithms
T
T
T
T
T
10
B
Algorithm
For each v, store in array low[v] the lowest
discovery time for a node reachable from any
node in the tree rooted in v with a path
consisting of 0 or more T edges ending with
a B edge.
Calling this node w, if this discovery time
equals the discovery time of v, there aren’t
any B edges connecting a descendant to the
ancestor, thus the w-v edge is a bridge.
A.Y. 2014/15
12 Applications of graph search algorithms
The adjacency list is scanned, keeping track of
the minimum discovery time encountered
following each edge.
For T edges we proceed recursively, for B
edges we use the discovery time of the
adjacent vertex.
A.Y. 2014/15
12 Applications of graph search algorithms
void bridgeR(Graph G, Edge e) {
link t;
int v, w = e.w;
pre[w] = time++;
low[w] = pre[w];
for (t = G->adj[w]; t != NULL; t = t->next)
if (pre[v = t->v] == -1) {
bridgeR(G, EDGE(w, v));
if (low[w] > low[v])
low[w] = low[v];
if (low[v] == pre[v]) {
bcnt++;
printf("edge %d - %d is a bridge \n", w, v);
}
}
else if (v != e.v)
if(low[w] > pre[v])
low[w] = pre[v];
}
A.Y. 2014/15
12 Applications of graph search algorithms
void GRAPHbridge(Graph G)
{
int v;
time = 0, bcnt =0;
for (v=0; v < G->V; v++)
pre[v] = -1;
for (v=0; v < G->V; v++)
if (pre[v]== -1)
bridgeR(G, EDGE(v, v));
printf("displaying pre[v] and low[v] \n");
for (v=0; v < G->V; v++)
printf("pre[%d]=%d \ tlow[%d]=%d \n",v,pre[v],v,low[v]);
}
A.Y. 2014/15
12 Applications of graph search algorithms
Example
0
7
6
1
A.Y. 2014/15
10
8
9
11
3
4
2
5
12
12 Applications of graph search algorithms
0/0 0
0/0 0
7
6
1
10
8
9
11
3
4
2
5
12
DFS to compute pre[v],
to label the edges and
to compute low[v]
A.Y. 2014/15
12 Applications of graph search algorithms
w
0/0 0
7
6
1/1 1
10
8
9
1/1
11
3
4
2
5
12
v
A.Y. 2014/15
12 Applications of graph search algorithms
0/0 0
T
1
0/0 0
7
6
1/1 1
w
A.Y. 2014/15
8
11
3
4
2
5
2/2
10
9
0/0 0
T
1/1 1
2/2 T
2
12
v
12 Applications of graph search algorithms
0/0 0
7
6
10
8
11
3 3/3 4
1/1 1
2
w
v
9
5
0/0 0
T
1/1 1
2/2 T
2
T
3/3 3
12
2/2
12/10
A.Y. 2014/15
12 Applications of graph search algorithms
0/0 0
7
6
10
8
11
3 3/1 4
1/1 1
v
A.Y. 2014/15
2
w 5
9
0/0 0
T
1/1 1
2/2 T
2
B
T
3/1 3
12
2/2
12 Applications of graph search algorithms
0/0 0
7
6
10
8
11
3 3/1 4
1/1 1
2
v
9
5
0/0 0
T
1/1 1
2/1 T
2
B
T
3/1 3
12
w 2/1
A.Y. 2014/15
12 Applications of graph search algorithms
0/0 0
7
6
1/1 1
10
8
3 3/1 4 4/4
v
2
5
12
9
11
0/0 0
T
1/1 1
2/1 T
2
T
B
T 4/4
4
3/1 3
w 2/1
A.Y. 2014/15
12 Applications of graph search algorithms
0/0 0
7
6
1/1 1
8
3 3/1 4 4/4
w
2
5
12
2/1 v
A.Y. 2014/15
10
5/5
9
11
0/0 0
T
1/1 1
2/1 T
2
T
B
T 4/4
4
3/1 3
T
5/5 5
12 Applications of graph search algorithms
0/0 0
7
6
9
10
8
11
3 3/1 4 4/4
1/1 1
2
2/1 w
A.Y. 2014/15
5
12
5/5
6/6
v
0/0 0
T
1/1 1
2/1 T
2
T
B
T 4/4
4
3/1 3
T
T
5/5 5
T
6/6 12
12 Applications of graph search algorithms
0/0 0
7
6
9
10
8
11
3 3/1 4 4/4
1/1 1
2
2/1 w
5
12
5/5
6/6
v
0/0 0
T
1/1 1
2/1 T
2
T
B
T 4/4
4
3/1 3
T
T
5/5 5
T
6/6 12
bridge
A.Y. 2014/15
12 Applications of graph search algorithms
0/0 0
7
6
10
8
11
3 3/1 4 4/4
1/1 1
2
v
A.Y. 2014/15
2/1 w
9
5
12
5/2
6/6
0/0 0
T
1/1 1
2/1 T B
2
B
T T 4/4
4
3/1 3
T
T
5/2 5
T
6/6 12
12 Applications of graph search algorithms
0/0 0
7
6
10
8
11
3 3/1 4 4/2
1/1 1
2
v
A.Y. 2014/15
2/1
w
9
5
12
5/2
6/6
0/0 0
T
1/1 1
2/1 T B
2
B
T T 4/2
4
3/1 3
T
T
5/2 5
T
6/6 12
12 Applications of graph search algorithms
0/0 0
v
7
6
10
8
11
3 3/1 4 4/4
1/1 1
w
A.Y. 2014/15
9
2
5
12
2/1
5/2
6/6
0/0 0
T
1/1 1
2/1 T B
2
B
T T 4/2 bridge
4
3/1 3
T
T
5/2 5
T
6/6 12
12 Applications of graph search algorithms
0/0 0
7
7/7
w
6
v
1/1 1
A.Y. 2014/15
10
8
9
11
3 3/1 4 4/2
2
5
12
2/1
5/2
6/6
0/0 0 T
T
6
1/1 1
2/1 T B
2
B
T T 4/2
4
3/1 3
T
T
5/2 5
T
6/6 12
12 Applications of graph search algorithms
7/7
0/0 0
7
7/7 8/8
6
8
w
1/1 1
A.Y. 2014/15
10
v
3 3/1 4 4/2
2
5
12
2/1
5/2
6/6
9
11
0/0 0 T
T
6
7/7
1/1 1
T
2/1 T B
8 8/8
2
B
T T 4/2
4
3/1 3
T
T
5/2 5
T
6/6 12
12 Applications of graph search algorithms
v
0/0 0
9/9
7 10
7/7 8/8
6
8
w
3 3/1 4 4/2
1/1 1
A.Y. 2014/15
2
5
12
2/1
5/2
6/6
9
11
0/0 0 T
T
6
7/7
1/1 1
T
2/1 T B
8 8/8
2
B
T
T T 4/2
7 9/9
4
3/1 3
T
T
5/2 5
T
6/6 12
12 Applications of graph search algorithms
w
0/0 0
9/9 10/10 9
7 10
7/7 8/8
6
8
v
3 3/1 4 4/2
1/1 1
A.Y. 2014/15
2
5
12
2/1
5/2
6/6
11
0/0 0 T
T
1/1 1
2/1 T B
2
B
T T 4/2
4
3/1 3
T
T
5/2 5
T
6/6 12
12 Applications of graph search algorithms
6
8
7
T
T
7/7
8/8
9/9
T
10 10/10
v
0/0 0
1/1 1
A.Y. 2014/15
11/11
9/9 10/10 9
7 10
7/7 8/8
1/1
6
8
2/1
w
11
B
3 3/1 4 4/2
3/1
2
5
12
2/1
5/2
6/6
12 Applications of graph search algorithms
0/0 0 T
T
1
T
B
2
T T 4/2
3
4
T
T
5/2 5
T
6/6 12
6
8
7
T
T
7/7
8/8
9/9
T
10 10/10
T
9 11/11
w
0/0 0
1/1 1
11/11
9/9 10/10 9
7 10
7/7 8/8
1/1
6
8
2/1
11
B
3 3/1 4 4/2 v 12/12
3/1
2
5
12
2/1
5/2
6/6
0/0 0 T
T
1
T
B
2
T T 4/2
3
4
T
T
5/2 5
T
6/6 12
6
8
7
7/7
T
T
8/8
9/9
T
10 10/10
T
9 11/11
T
11
12/12
A.Y. 2014/15
12 Applications of graph search algorithms
0/0 0
1/1 1
11/11
9/9 10/10 9
7 10
7/7 8/8
1/1
6
8
2/1
v
11
B
3 3/1 4 4/2 w 12/10
3/1
2
5
12
2/1
5/2
6/6
0/0 0 T
T
1
T
B
2
T T 4/2
3
4
T
T
5/2 5
T
6/6 12
6
8
7
7/7
T
T
8/8
9/9
T
10 10/10
T
9 11/11
T
11 B
12/10
A.Y. 2014/15
12 Applications of graph search algorithms
0/0 0
1/1 1
11/10 w
9/9 10/10 9
7 10
7/7 8/8
1/1
6
8
2/1
v
11
B
3 3/1 4 4/2
12/10
3/1
2
5
12
2/1
5/2
6/6
0/0 0 T
T
1
T
B
2
T T 4/2
3
4
T
T
5/2 5
T
6/6 12
6
8
7
7/7
T
T
8/8
9/9
T
10 10/10
T
9 11/10
T
11 B
12/10
A.Y. 2014/15
12 Applications of graph search algorithms
0/0 0
1/1 1
11/10
v 9/9 10/10
9
7 10
7/7 8/8
1/1
6
8
2/1
w
11
B
3 3/1 4 4/2
12/10
3/1
2
5
12
2/1
5/2
6/6
0/0 0 T
T
1
T
B
2
T T 4/2
3
4
T
T
5/2 5
T
6/6 12
bridge
A.Y. 2014/15
12 Applications of graph search algorithms
6
8
7
7/7
T
T
8/8
9/9
T
10 10/10
T
9 11/10
T
11 B
12/10
v
0/0 0
1/1 1
11/10
w
9/0 10/10 9
7 10
7/7 8/8
1/1
6
8
2/1
11
B
3 3/1 4 4/2
12/10
3/1
2
5
12
2/1
5/2
6/6
0/0 0 T
T
1
T
B
2
T T 4/2
3
4
T
T
5/2 5
T
6/6 12
B
6
8
7
7/7
T
T
8/8
9/0
T
10 10/10
T
9 11/10
T
11 B
12/10
A.Y. 2014/15
12 Applications of graph search algorithms
v
0/0 0
1/1 1
11/10
10/10
w 9/0
9
7 10
7/7 8/0
1/1
6
8
2/1
11
B
3 3/1 4 4/2
12/10
3/1
2
5
12
2/1
5/2
6/6
0/0 0 T
T
1
T
B
2
T T 4/2
3
4
T
T
5/2 5
T
6/6 12
B
6
8
7
7/7
T
T
8/0
9/0
T
10 10/10
T
9 11/10
T
11 B
12/10
A.Y. 2014/15
12 Applications of graph search algorithms
v
0/0 0
1/1 1
11/10
9/0 10/10 9
w
7 10
7/0 8/0
1/1
6
8
2/1
11
B
3 3/1 4 4/2
12/10
3/1
2
5
12
2/1
5/2
6/6
0/0 0 T
T
1
T
B
2
T T 4/2
3
4
T
T
5/2 5
T
6/6 12
B
6
8
7
7/0
T
T
8/0
9/0
T
10 10/10
T
9 11/10
T
11 B
12/10
A.Y. 2014/15
12 Applications of graph search algorithms
Articulation point
Given an undirected graph G, given the DFS
tree G ,
 the root of G is an articulation point if and
only if it has at least two children
 any other node v is an articulation point of
G if and only if v has a child s such that
there is no edge labelled B from s or from
one of its descendants to a proper ancestor
of v.
A.Y. 2014/15
12 Applications of graph search algorithms
Example
T
0
0
6
1
3
5
4
7
2
8
B
10
2
9
11
6
12
7
B
A.Y. 2014/15
1
12 Applications of graph search algorithms
8
T
T
T
T
T
T
10
5
3
4
9
11
12
T
T
B
T
T
T
B
T
0
B
1
2
6
7
B
A.Y. 2014/15
8
T
T
T
T
T
T
10
5
3
4
9
11
12
T
T
B
T
T
B
T
12 Applications of graph search algorithms
Reverse graph
Given a directed graph G =(V, E), its reverse
graph GT = (V, ET) is such that
(u, v)  E
(v,u)  ET.
Graph GRAPHreverse(Graph G)
{
int v;
link t;
Graph R = GRAPHinit(G->V);
for (v=0; v < G->V; v++)
for (t= G->adj[v]; t != NULL; t = t->next)
GRAPHinsertE(R, EDGE(t->v, v));
return R;
}
A.Y. 2014/15
12 Applications of graph search algorithms
Directed Acyclic Graph (DAG)
DAG: implicit models for partial orderings
used in scheduling problems.
Scheduling:
 given tasks and precedence constraints
 how can we schedule tasks so that they
are all executed satisfying the constraints.
A.Y. 2014/15
12 Applications of graph search algorithms
Topological sort (reverse): reordering the
nodes according to a horizontal line, so that
if the (u, v) edge exists, node u appears to
the left (right) of node v and all edges go
from left (right) to right (left).
Endprocessing times stored in post
computed during a DFS return a reverse
topological sort of the DAG.
A.Y. 2014/15
12 Applications of graph search algorithms
Example: reverse topological sort
8
5
6
7
0
3
4
1
2
A.Y. 2014/15
12 Applications of graph search algorithms
}
8
5
6
7
0
3
4
1
2
2
A.Y. 2014/15
12 Applications of graph search algorithms
}
8
5
6
7
0
3
4
1
2
2
A.Y. 2014/15
1
12 Applications of graph search algorithms
}
8
5
6
7
0
3
4
1
2
2
A.Y. 2014/15
1
3
12 Applications of graph search algorithms
}
8
5
6
7
0
4
3
1
2
2
A.Y. 2014/15
1
3
0
12 Applications of graph search algorithms
}
8
5
6
7
0
4
3
1
2
2
A.Y. 2014/15
1
3
0
4
12 Applications of graph search algorithms
}
8
5
6
7
0
4
3
1
2
2
A.Y. 2014/15
1
3
0
4
6
12 Applications of graph search algorithms
}
8
5
6
7
0
4
3
1
2
2
A.Y. 2014/15
1
3
0
4
6
7
12 Applications of graph search algorithms
}
8
5
6
7
0
4
3
1
2
2
A.Y. 2014/15
1
3
0
4
6
7
12 Applications of graph search algorithms
5
}
8
5
6
7
0
4
3
1
2
2
A.Y. 2014/15
1
3
0
4
6
7
12 Applications of graph search algorithms
5
8
Reverse topological sort
2
A.Y. 2014/15
1
3
0
4
6
12 Applications of graph search algorithms
7
5
8
Data structures
DAG as 1st class ADT
 Graph represented with an adjacency list
 Array pre[i] where, for each node, we record its
discovery time (preorder numbering of the nodes)
 Array ts[i] where, for each time, we record which
node was completed at that time
 time counter for endprocessing times (it increments
only when a node is completed, not just discovered)
 time, pre[maxV], and ts[maxV] declared as
static in DAG.c

A.Y. 2014/15
12 Applications of graph search algorithms
void TSdfsR(Dag D, int v, int ts[]){
link t;
pre[v] = 0;
for (t = D->adj[v]; t != NULL; t = t->next)
if (pre[t->v] == -1)
TSdfsR(D, t->v, ts);
ts[time++] = v;
}
void DAGrts(Dag D) {
int v;
time = 0;
for (v=0; v < D->V; v++) {
pre[v] = -1; ts[v] = -1;
}
for (v=0; v < D->V; v++)
if (pre[v]== -1)
TSdfsR(D, v, ts);
printf("nodes in reverse top. order \n");
for (v=0; v < D->V; v++) printf("%d ", ts[v]);
printf("\n");
}
A.Y. 2014/15
12 Applications of graph search algorithms
Topological sort: with a DAG represented by an
adjacency matrix, it is enough to invert references
to rows and columns:
void TSdfsR(Dag D, int v, int ts[]) {
int w;
pre[v] = 0;
for (w = 0; w < D->V; w++)
if (D->adj[w][v] != 0)
if (pre[w] == -1)
TSdfsR(D, w, ts);
ts[time++] = v;
}
8
A.Y. 2014/15
5
7
6
4
0
12 Applications of graph search algorithms
3
1
2
Strongly connected components
Kosaraju’s algorithm (’80s):
 reverse the graph
 execute
DFS on the reverse graph,
computing Discovery/Endprocessing times
 execute
DFS on the original graph
according to decresasing Endprocessing
times
 the trees of this last DFS are the strongly
connected components.
A.Y. 2014/15
12 Applications of graph search algorithms
Example
0
1
2
3
4
5
6
7
0
1
2
3
4
5
6
7
G
GT
A.Y. 2014/15
12 Applications of graph search algorithms
DFS of the reverse graph GT (time stamps pre[v] and
post[v] are displayed, though only post[v] time stamps
are used).
0/5
2/3
6/9
7/8
0
1
2
3
4
5
6
7
1/4
A.Y. 2014/15
10/13
11/12
12 Applications of graph search algorithms
14/15
DFS of the original graph according to decreasing
endprocessing times computed in the DFS of the reverse
graph GT
scc2
scc3
0
1
2
3
4
5
6
7
G
scc1
A.Y. 2014/15
12 Applications of graph search algorithms
scc0
SCCs are equivalence classes with respect
to the mutual reachability property
 We can “extract” a reduced graph G’
considering 1 node as representing each
equivalence class
 The reduced graph G’ is a DAG.

A.Y. 2014/15
12 Applications of graph search algorithms
Algorithm and data structure

G->scc[w] is an array used to store integers
that identify each SCC. Nodes act as indices for
the array

The struct for the graph is extended to include
the scc array as well
struct graph {int V; int E; link *adj; int *scc;};
struct graph {int V; int E; int **adj; int *scc;};

time0,
time1,
post[maxV]
and
st[maxV] are declared as
static in
Graph.c
A.Y. 2014/15
12 Applications of graph search algorithms

In the reverse graph DFS the time0 time
counter increases only when a node is completely
processed (discovery time not needed). time1 is
the counter of SCCs.

Statement post[time0++]=w records that at
time time0 w has been completed, so there is
an implicit sorting for increasing endprocessing
times. Nodes are considered according to
descending endprocessing time with the
descending loop from G->V-1 down to 0.
A.Y. 2014/15
12 Applications of graph search algorithms
void SCCdfsR(Graph G, int w) {
link t;
G->scc[w] = time1;
for (t = G->adj[w]; t != NULL; t = t->next)
if (G->scc[t->v] == -1)
SCCdfsR(G, t->v);
post[time0++]= w;
}
int GRAPHstrongconnect(Graph G, int s, int t) {
return G->scc[s] == G->scc[t];
}
A.Y. 2014/15
12 Applications of graph search algorithms
int GRAPHscc(Graph G) {
int v; Graph R;
R = GRAPHreverse(G);
time0 = 0; time1 = 0;
G->scc = malloc(G->V * sizeof(int));
R->scc = malloc(G->V * sizeof(int));
for (v=0; v < G->V; v++) R->scc[v] = -1;
for (v=0; v < G->V; v++)
if (R->scc[v] == -1) SCCdfsR(R, v);
time0 = 0; time1 = 0;
for (v=0; v < G->V; v++) G->scc[v] = -1;
for (v=0; v < G->V; v++) postR[v] = post[v];
for (v = G->V-1; v >= 0; v--)
if (G->scc[postR[v]]==-1){SCCdfsR(G,postR[v]);time1++;}
printf("strongly connected components \n");
for (v = 0; v < G->V; v++)
printf("node %d in scc %d \n", v, G->scc[v]);
return time1;
}
A.Y. 2014/15
12 Applications of graph search algorithms
References
 Connected components:
 Sedgewick Part 5 18.5
 Bridge and articulation points:
 Sedgewick Part 5 18.6
 DAGs and DAG topological sorting:
 Sedgewick Part 5 19.5 e 19.6
 Cormen 23.4
 Strongly connected components:
 Sedgewick Part 5 19.8
 Cormen 23.5
A.Y. 2014/15
12 Applications of graph search algorithms