0% found this document useful (0 votes)
5 views

Graph Algorithms

Uploaded by

Ritesh Bandaru
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Graph Algorithms

Uploaded by

Ritesh Bandaru
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1 Shortest path problem

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.

Variants of Shortest path problem

(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)

Properties of Shortest path

(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.

Input: A weighted connected graph and a source vertex s.


?: Find the shortest path from s to all the other vertices in G.
Step 1: Let S be the set which contains the set of visited nodes. Let d(u, v) denotes the distance between
u and v and initially, it set to ∞. Initially, S = {s} and V (G) \ S contains the unvisited vertices (vertices

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.

Trace of the Algorithm


Input: A weighted connected graph and a source vertex a.

30
a 2 b

12
20

8 e
10
3 5

4
c d
6

Graph G

Iterations Set S Min edge Distance label


1 {a} - ∀u ∈ V (G) ,d(a, u) = ∞
d(a, b) = 2,
d(a, c)= min{d(a, c), d(a, b) + d(b, c)},
min{10, 2 + 5} = 5.
d(a, c) = 5
d(a, d) = min{d(a, d), d(a, b) + d(b, d)},
Add b to S,
2 {a, b} min{20, 2 + 8},
{a, b}
d(a, d) = 10.
d(a, e) = min{d(a, e), d(a, b) + d(b, e)},
min{30, 2 + 12},
d(a, e) = 14.
Min edge is {a, c}
d(a, d) = min{d(a, d), d(a, c) + d(c, d)},
min{10, 5 + 4},
d(a, d) = 9.
Add c to S,
3 {b, c} d(a, e) = min{d(a, e), d(a, c) + d(c, e)},
{a, b, c}
min{14, 5 + 6},
d(a, e) = 11.
Min edge is {c, d}
d(a, e) = min{d(a, e), d(a, d) + d(d, e)},
Add d to S,
4 {c, d} min{11, 9 + 5},
{a, b, c, d}
d(a, e) = 11. No update
Add e to S, The only remaining vertex is e, the distance, d(a, e) is obtained
5 -
{a, b, c, d, e} in iteration 4.

2
a 2 b

9
c d
11

Shortest distance from a to all other vertices

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

• Choose a vertex u, whose incoming degree, dIG (u) = 0


• In G − u, look for a vertex v, dIG (v) = 0

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.

Trace of the Algorithm

s 4 a -4
b
10 8 -6
6
6 d c
-5

f 10 e

Directed Acyclic Graph G

4
s a b c d e f
4 -4 -6 ∞ -5 ∞ 10
0 ∞ ∞ ∞ ∞
8
10
6
6

Step 1: Topological ordering of G

s a b c d e f
4 -4 ∞ -6 -5 ∞ 10
0 4 ∞ 6 10

8
10
6
6

Step 2: Start with s , update the distance d(s, a) = 4, d(s, d) = 6

s a b c d e f
4 -4 -6 -5 ∞ 10 10
0 4 0 12 6
8
10
6
6

Step 3: Explore the neighbours of a, d(s, b) = 0, d(s, c) = 12, d(s, f ) = 10

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

Step 7: Visit the vertex f .

3 Graph Traversal

You might also like