Graph Algorithms
Graph Algorithms
In graph theory, the shortest path problem is the problem of finding a path between any two vertices of
G such that the path contains least number of edges(Unweighted graph), or least total weights(Weighted
graph). This problem has various application in many fields such as Computer networks(a peer-to-peer ap-
plication - find the shortest path from machine A to machine B), Road network, Routing protocols, Google
maps, etc.
(i) Single-source shortest path problem: The problem is to find the shortest paths from a source vertex
u ∈ V (G) to all other vertices in the graph.
(ii) All-pairs shortest path problem: we have to find the shortest paths between every pair of vertices,
∀ u, v ∈ V (G) in the graph.
(iii) Single-destination shortest-paths problem: we have to find a shortest path to a given destination vertex
t from each vertex. By reversing the direction of each edge in the graph, we can reduce this problem
to a single-source problem.
Note: A trivial approach to solve all-pairs shortest path problem is to run Single-source shortest path Al-
gorithm from every vertex u ∈ V (G) in G. Single-destination shortest-paths problem can be reduced to
Single-source shortest path problem by reversing the direction of each edge in the graph (Directed graph) or
by changing the destination vertex t as source vertex s (Undirected graph)
(i) Let P = (s, u1 , u2 , . . . , ui , t) be the shortest path between (s, t). For every (u, v) ∈ Pst , the path
connecting u, v is also an shortest path. Suppose, u, v is not the shortest path, then there exist a
0
shorter path Pst which is a contradiction to the shortest path Pst .
(ii) Shortest paths are not necessarily unique. There can exist multiple paths with same weight from one
vertex to another.
a 2 b a 2 b a 2 b
2 2 2 4 2
d c d c d c
2 4
Graph G Pac = (a, b, c) 0
Pac = (a, d, c)
Dijkstra’s Algorithm In this section, we shall see an Algorithm to solve Single-source shortest path prob-
lem for an edge weighted graph . Edsger Wybe Dijkstra was a Dutch scientist, in 1959 he published an
article ’A note on two problems in connexion with graphs’ the algorithm to find the shortest path in a graph
between any two given nodes, now called Dijkstra’s algorithm.
1
other than the source s)
Step 2: Pick min {s, u|u ∈ NG (s)} and add the vertex u to the set S, update the distance d(s, NG (s)).
Step 3: Focus on the NG (u) and for each v ∈ NG (u) update d(s, v)=min {d(s, v), d(s, u) + d(u, v)}. Add the
vertex v into the set S. Pick min {u, v|v ∈ V (G) \ S} and repeat this step until all the vertices are visited.
At the end of this Algorithm, for each u, the weight associated with distance label d(s, u) is the shortest
path between s and u.
30
a 2 b
12
20
8 e
10
3 5
4
c d
6
Graph G
2
a 2 b
9
c d
11
Observation
• If all the weights are positive, then there is no update to S(Explored set). If some weights are negative,
we may need to revisit S.
a 2 b
−200
10 −400
5c
Graph G with negative edge weights
in this case, initially, the distance of the vertex b, d(a, b) = 2. But after c has explored the distance label
of b has updated as d(a, b) = −390. Thus Dijkstra’s Algorithm fails if negative weights are allowed.
• If at all the negative weights are allowed, then it should be a cut edge.
Remarks
• Can we modify Dijkstra’s Algorithm so that it works for negative edge weights. Let us try a trivial
strategy. Suppose, if a sufficiently large weight is added to the each edge of the graph so that there is
no negative weights. Can we run Dijkstra’s Algorithm on the modified graph. This strategy fails to
produce shortest path even there are no negative edges.
a 3 b 2 c a 5 b 4 c
1 1 3 3
d i d i
1e −1 3e 1
1 f 1 g 3 f 3 g
Graph G with negative weights Modified graph G0
with no negative weights
Let G0 be the graph obtained by adding weight, = 2 to all the edges in the graph. Let source be a.
We obtain the shortest path P = (a, b, c) whose distance is d(ac) = 9 - 4 = 5. But the actual shortest
path is P = (a, d, e, f, g, h, i, c) whose distance is d(ac) = 16 - 12 = 4.
• Are there a graph with negative edges in practice? Yes. Let us consider a graph G which repre-
sents currency tradings where each node denotes currencies of various countries and edge denotes the
profit/loss incur during currency tradings. Note: profit implies Positive weight, loss implies negative
weight.
3
Rupees Dollar
ve
−
+ve
+ve Ruble
−ve +ve
+ve
Y en Euro
Graph G - Currency trading
2 Topological ordering/Sorting
Topological ordering of a graph G(V, E) is a linear arrangement, σ of vertices such that u appears before
v in σ if (u, v) ∈ E(G). This ordering can only be applied on directed acyclic graphs, commonly known as
DAG. For a general weighted graph, single source shortest path problem can be solved in O(V E) time using
BellmanFord Algorithm. For a graph with no negative weights, the above-said problem can be solved in
O(E + V log V ) time using Dijkstras algorithm. Can we do even better for Directed Acyclic Graph (DAG)?
Using Topological ordering, we can solve this problem in O(V + E) time.
Input: A weighted connected Directed Acyclic graph G and a source vertex s.
To find: Shortest path from s to every other vertices in G.
Step 1: Initialize the distance of all vertices to a larger number, say ∞. Find the Topological ordering using
the following procedure
Step 2: For each vertex u in Topological ordering, find the neighbours of u and for each neighbor v ∈ NG (u),
update the distance d(s, v) = min{d(s, v), d(s, u) + d(u, v)}.
Step 3: Repeat the step 2 until all the vertices are visited.
s 4 a -4
b
10 8 -6
6
6 d c
-5
f 10 e
4
s a b c d e f
4 -4 -6 ∞ -5 ∞ 10
0 ∞ ∞ ∞ ∞
8
10
6
6
s a b c d e f
4 -4 ∞ -6 -5 ∞ 10
0 4 ∞ 6 10
8
10
6
6
s a b c d e f
4 -4 -6 -5 ∞ 10 10
0 4 0 12 6
8
10
6
6
s a b c d e f
4 -4 -6 -5 ∞ 10 10
0 4 0 -6 6
8
10
6
6
Step 4: Explore the neighbours of b, d(s, c) = min{d(s, c), d(s, b) + d(b, c)} = −6,
s a b c d e f
4 -4 -6 -5 1 10 10
0 4 0 -6 6
8
10
6
6
Step 5: Explore the neighbours of d, d(s, e) = min{d(s, e), d(s, d) + d(d, e)} = 1,
s a b c d e f
4 -4 -6 -5 1 10 10
0 4 0 -6 6
8
10
6
6
Step 6: Explore the neighbours of e, d(s, f ) = min{d(s, f ), d(s, e) + d(e, f )} = 10, no update
5
s a b c d e f
4 -4 -6 -5 1 10 10
0 4 0 -6 6
8
10
6
6
3 Graph Traversal