You are on page 1of 8

Advanced Algorithms, CSE 5311, Prof.

 Chris Ding 
Shortest Path Problem – Dijkstra’s Algorithm 

All‐pair shortest Path Problem – Floyd Warshall Algorithm 

Transitive Closure of Directed Graph – Floyd‐Warshall Algorithm 

Triangle Inequality for shortest distance 

Graph Theory basics, matrix algorithms for neighbor calculation 

Scribed by: Shilpa Goley & Raghunath Ravi 

Dijkstra’s Algorithm 
Dijkstra’s algorithm solves the shortest-path problem on weighted graphs. The
graphs could be directed or undirected. Consider the graph to be G= (V,E) where V
is the set of Vertices, E is the set of edges and the weights of the edges are non-
negative. In simplest terms the algorithm can be described as follows:

1. Maintain a set S. This set contains all vertices which are closest to the source
node, s.
2. Find all neighbors of s and compute their score.
3. Pick the neighbor with the minimum score, and add to S.

We need to Initialize the graph for the single source:

Initialize-Single-Source(G,s)
Step-1 for each vertex v є V[G]
Step-2 do d[v] <- ∞
Step-3 cost[v] = NIL
Step-4 d[s] <- 0

Here d[v] for each v є V is the shortest-path estimate.

The Dijkstra’s algorithm:

Step-1 Initialize-Single-Source(G,s)

Step-2 S <- Ф

Step-3 Q<-V[G]

Step-4 while Q ≠ Ф
Step-5 do u<-Extract-Min(Q)

Step-6 S <- S U {u}

Step-7 for each vertex v є Adj[u]

Step-8 do

Step-9 if d[v] > d[u]+ w(u,v)

Step 10 then d[v] <- d[u] +w(u,v)

Example:

Let us consider the following directed graph shown in figure (a)

t 1  x
t 1 
x
10 ∞
10
∞ ∞ 9 
10 
9  3 2 4
3  2  4 6
s 0
s 6 
0  7 

5

5 ∞
∞ ∞ 2  z
2  z y
y
(a) (b)

t 1  x
t 1  x
8  14  8 13
10  10
9  9 
3  2  4  3 2 4
s 6 
0  s 0
6
7  7 
5  5
5 7 7
2  z
5
y 2  z
y

(c ) (d)

t x

t 1  x 8 9
10
8 9 9 
10 
9  3 2 4
3  2  s 6
4  0
s 6 
0  7 
7 5
5  5 7
2  z
(e) (f)

The working of the algorithm can be understood by looking at how the graph (a)
transforms to (f).

1. In the beginning, all nodes have the cost infinity (∞) as shown in (a). Only
the source s is 0. All the nodes are added to the queue Q.

2. Then the node with the minimum shortest-path estimate is extracted from Q
and added to S. In this case S = {s}

3. Now for all nodes which are adjacent to s, the shortest path estimate is
found. Hence, node t and y are made 10 and 5 respectively as shown in
figure (b).

4. In the next loop, again the minimum shortest-path estimate is extracted


from Q and added to Q. This time it is y. Hence, S = {s,y}. And the shortest-
path estimate is calculated for all nodes adjacent to y, that are t, x and z as
shown in the figure (c)

For example, using Step-9 and 10 of the algorithm, the estimate of t is made
8, since d[y] + w(y, t) which is 8 is smaller than d[t] which is 10.

5. Similarly in the following figures the shortest-path estimate is found for the
other nodes.

Complexity of Dijkstra’s Algorithm:

Let us consider that the total number of nodes are n. If we are currently in the
iteration k then the maximum neighbours found will be n-k.

Hence, the complexity of the algorithm is given as follows:

∑nk=1 { k(n-k) + (n-k-1) } = 3/2n2 – O(n) = O(n2)

This is a very efficient algorithm.


For Undirected Graphs

The same algorithm can be successfully used for undirected graphs too. It is a
completely greedy algorithm.

Consider the undirected graph shown below. The algorithm can be applied on it in
the following steps.
8  8
A  D  F
4
5
3
4  3 
7  7
E G I 

5  10
no 6

s H
C  7

1. Let us start from the source s. Its neighbors are A=7, B=5 and C=8. Hence,
we pick B.

2. Now we have the choices A=7 and C=8. We pick A in this iteration. And
hence including the adjacent nodes of A, D=15, E=10 and C=8 are in the
set.

3. Now we pick C. and H=15 is added to the set..

And so on.

As we keep continuing in the same manner we reach I, the last node and all the
nodes are picked by then such that they are reached on the shortest path from s.

When we trace back form I we get the shortest path is {I, H, C, s}. Similarly from F
it is {F, G, E, A, s}.

Floyd Warshall Algorithm 
This algorithm is used to solve all-pairs shortest paths problem in directed graphs.
The complexity is O(V3) where V is the number of vertices. We assume that there
are no negative weight edges in the graph. In this algorithm we consider the
intermediate vertices of a shortest path, where an intermediate vertex of a simple
path {v1, v2… vj} is any vertex in the set {v2… vj-1}. So basically in this algorithm
we find the shortest path between i and j, considering the paths including all the
intermediate vertices say {1, 2 … k-1} for given k.
This is a very efficient algorithm in which the adjacency matrix is used.

We consider a distance matrix of the graph:

d11 d12 . . .

D= d21

The algorithm is given by the following pseudo code

Step 1: n <- rows[D]

Step2: Do = D

Step 3: for k <- 1 to n

Step 4: do for i <- 1 to n

Step 5: do for j <- 1 to n

Step 6: do dij(k) <- min ( dij(k-1), dik(k-1) + dkj(k-1) )

Step 7: return Dn

Transitive Closure of Directed Graph 
Given a graph G = (V,E ), we may want to find out if there is a path in G from
vertex i to j for all i, j є V. The transitive closure of G is defined as the graph G* =
(V, E*), where

E* = { (i, j) : there is a path from vertex i to j in G}

The algorithm for finding the transitive closure is as follows:

Step 1: n <- |V[G]|

Step 2: for I <- 1 to n

Step 3: do for j <- 1 to n

Step 4: do if i=j or (i, j) є E[G]

Step 5: then tij(0) <- 1

Step 6: else tij(0) <- 0


Step 7: for k <- 1 to n

Step 8: do for i <- 1 to n

Step 9: do for j <- 1 to n

Step 10: do tij(k) <- tij(k-1) V ( tik(k-1) Λ tkj(k-1) )

Step 11: return Tn

The basic idea of the algorithm is that we construct a transitive closure matrix Tn by
putting edge (i, j) in it if and only if tij(n)=1. This is achieved by using binary
operations of AND (Λ) and OR (V) in the algorithm.

The complexity of this algorithm is also (n3) like Floyd Warshall algorithm.

Triangle Inequality 
This is a simple property of shortest distances in weighted graphs.

It states that:

Let G = (V,E) be a weighted, directed graph with the weight function w : E->R and
source vertex s. Then, for all edges (u,v) є E, we have

D(s,v) ≤ D(s,u) + w(u,v)

Graph Theory 
• Graphs are a useful kind of mathematics.

• Each graph is made up of Nodes and Edges. Nodes are the vertices of the
graph and edge is the path connecting two vertices.

Graphs can be classified as

• Weighted Graphs – A graph is called weighted if the edges in the graph have
values.

• Unweighted Graphs - A graph is called unweighted if all the edges in the


graph have value 1.

Graphs can also be classified as


• Directed graph – Each edge connecting two vertices has a direction
associated to it. It is also called the Theoretical Graph.

• Undirected graph - Each edge connecting two vertices does not have a
direction associated to it.

B C

Degree of a node: The degree of a graph is the number of nodes F

incident on the node. E.g. Degree of (F) = 5 A D

Outdegree of a node: The outdegree of a node is the number of E


nodes pointing out of it.

G
Indegree of a node: Indegree is defined as the as the number of
edges pointing to that particular node. When a webpage has a
large indegree it signifies a very useful page.
J H

Reachable node: A node is reachable if there exists a path from


I
a specific node.

Unconnected: When there is no edge connecting 2 nodes they


are called unconnected.

Transitive closure: The transitive closure property of a graph is


derived from the transitive property. i.e. a=b; b=c; then a=c;

For e.g. A is connected to G, H, I & J with a edge connecting A &


E and E is connected to G through another edge. This is similar
to A connected to G, H, I & J directly.

Hamiltonian Path: A path that passes thru over all the nodes of a graph (each node
is passed thru exactly once) is called a Hamiltonian path.
Eulers Path: A path that passes thru all the edges of a graph (each edge is passed
thru exactly once) is called an Euler’s Path.

Matrix Representation

Adjacency Matrix


The matrix representation of the given unweighted
graph is
5  2 
1 2 3 4 5
1 | 0 1 0 0 0 |
2 | 1 0 1 0 0 |
3 | 0 1 0 1 0 |
4  3  4 | 0 0 1 0 1 |
5 | 0 0 0 1 0 |

2 6  The matrix representation of the given weighted graph
is
5   

1 2 3 4 5
6  1  1 | 0 2 0 0 0 |
2 | 2 0 1 0 0 |
3 | 0 1 0 7 0 |
4  3  4 | 0 0 7 0 6 |
7  5 | 0 0 0 6 0 |

Counting the number of neighbors:


N

(A2)ij = ∑  Aik X Akj 
  K=1
Finding the Second nearest neighbor:

(A3)ij = ∑  Aik X Akl X Alj 
K=1

You might also like