Graph Algorithms
Graph Algorithms
• Connection problems
• Scheduling problems
• Transportation problems
• Network analysis
• Games and Puzzles.
Those who would like to take a quick tour of essentials of graph theory
please go directly to "Graph Theory" from here.
Digraph
A directed graph, or digraph G consists of a finite nonempty set of vertices
V, and a finite set of edges E, where an edge is an ordered pair of vertices in
V. Vertices are also commonly referred to as nodes. Edges are sometimes
referred to as arcs.
V = {1, 2, 3, 4}
E = { (1, 2), (2, 4), (4, 2) (4, 1)}
Another example, the following Petersen Graph G=(V,E) has vertex set V
and edge set E where: V = {1,2,3,4}and E
={(1,2),(2,4),(4,3),(3,1),(1,4),(2,1),(4,2),(3,4),(1,3),(4,1)}.
We'll quickly covers following three important topics from algorithmic
perspective.
1. Transpose
2. Square
3. Incidence Matrix
1. Transpose
If graph G = (V, E) is a directed graph, its transpose, GT = (V, ET) is the same
as graph G with all arrows reversed. We define the transpose of a adjacency
matrix A = (aij) to be the adjacency matrix AT = (Taij) given by Taij = aji. In
other words, rows of matrix A become columns of matrix AT and columns of
matrix A becomes rows of matrix AT. Since in an undirected graph, (u, v)
and (v, u) represented the same edge, the adjacency matrix A of an
undirected graph is its own transpose: A = AT.
2. Square
The square of a directed graph G = (V, E) is the graph G2 = (V, E2) such that
(a, b)∈E2 if and only if for some vertex c∈V, both (u, c)∈E and (c,b)∈E.
That is, G2 contains an edge between vertex a and vertex b whenever G
contains a path with exactly two edges between vertex a and vertex b.
For each vertex, we must make a copy of at most |E| list elements. The total
time is O(|V| * |E|).
3. Incidence Matrix
The incidence matrix of a directed graph G=(V, E) is a V×E matrix B = (bij)
such that
Specifically we have
Therefore
BFS label each vertex by the length of a shortest path (in terms of number of
edges) from the start vertex.
Example (CLR)
Step1
Step 2
Step 3
Step 4
Step 5
Step 6
Step 7
Step 8
Step 9
Computing, for every vertex in graph, a path with the minimum number of
edges between start vertex and current vertex or reporting that no such path
exists.
Analysis
Total running time of BFS is O(V + E).
Bipartite Graph
We define bipartite graph as follows: A bipartite graph is an undirected
graph G = (V, E) in which V can be partitioned into two sets V1 and V2 such
that (u, v) ∈ E implies either u V1 and v V2 or u V2 and v V1. That is, all
edges go between the two sets V1 and V2.
Formally, to check if the given graph is bipartite, the algorithm traverse the
graph labeling the vertices 0, 1, or 2 corresponding to unvisited., partition 1
and partition 2 nodes. If an edge is detected between two vertices in the
same partition, the algorithm returns.
Color[u] = WHITE
d[u] = ∞
partition[u] = 0
Color[s] = gray
partition[s] = 1
d[s] = 0
Q = [s]
While Queue 'Q' is not empty do
u = head [Q]
for each v in Adj[u] do
if partition [u] = partition [v] then
return 0
else
if color[v] WHITE then
color[v] = gray
d[v] = d[u] +1
partition[v] = 3 - partition[u]
ENQUEUE (Q, v)
DEQUEUE (Q)
Color[u] = BLACK
Return 1
Correctness
As Bipartite (G, S) traverse the graph it labels the vertices with a partition
number consisted with the graph being bipartite. If at any vertex, algorithm
detects an inconsistency, it shows with an invalid return value,. Partition
value of u will always be a valid number as it was enqueued at some point
and its partition was assigned at that point. AT line 19, partition of v will
unchanged if it already set, otherwise it will be set to a value opposite to that
of vertex u.
Analysis
The lines added to BFS algorithm take constant time to execute and so the
running time is the same as that of BFS which is O(V + E).
Diameter of Tree
The diameter of a tree T = (V, E) is the largest of all shortest-path
distance in the tree and given by max[dist(u,v)]. As we have mentioned
that BSF can be use to compute, for every vertex in graph, a path with the
minimum number of edges between start vertex and current vertex. It is
quite easy to compute the diameter of a tree. For each vertex in the tree, we
use BFS algorithm to get a shortest-path. By using a global variable length,
we record the largest of all shortest-paths. This will clearly takes O(V(V
+ E)) time.
Edges leads to new vertex are called discovery or tree edges and edges lead
to already visited are called back edges.
Example (CLR)
Analysis
The running time of DSF is (V + E).
Note that converse also holds: if d[u] < d[v] < f[v] < f[u] then vertex
v is in the same DFS-tree and a vertex v is a descendent of vertex u.
Suppose vertex u and vertex v are in different DFS-trees or suppose vertex u
and vertex v are in the same DFS-tree but neither vertex is the descendent of
the other. Then one vertex was discovered and fully explored before the
other was discovered i.e., f[u] < d[v] or f[v] < d[u].
Consider a directed graph G = (V, E). After a DFS of graph G we can put
each edge into one of four classes:
Observation 1 For an edge (u, v), d[u] < f[u] and d[v] < f[v] since
for any vertex has to be discovered before we can finish exploring it.
We can prove this direction by eliminating the various possible edges that
the given relation can convey. If d[v] < d[v] < d[u] < f[u] then edge
(u, v) cannot be a tree or a forward edge. Also, it cannot be a back edge by
lemma 1. Edge (u, v) is not a forward or back edge. Hence it must be a
cross edge (please go above and look again the definition of cross edge).
DFS_visit(u)
color(u) = GRAY
d[u] = time = time + 1
For each v in adj[u] do
if color[v] = gray and Predecessor[u] v do
return "cycle exists"
if color[v] = while do
Predecessor[v] = u
Recursively DFS_visit(v)
color[u] = Black;
f[u] = time = time + 1
Correctness
To see why this algorithm works suppose the node to visited v is a gray
node, then there are two possibilities:
1. The node v is a parent node of u and we are going back the tree edge
which we traversed while visiting u after visiting v. In that case it is
not a cycle.
2. The second possibility is that v has already been encountered once
during DFS_visit and what we are traversing now will be back edge
and hence a cycle is detected.
Time Complexity
The maximum number of possible edges in the graph G if it does not have
cycle is |V| - 1. If G has a cycles, then the number of edges exceeds this
number. Hence, the algorithm will detects a cycle at the most at the Vth edge
if not before it. Therefore, the algorithm will run in O(V) time.