Advanced Algorithms, CSE 5311, Prof. Chris Ding: Shilpa Goley & Raghunath Ravi
Advanced Algorithms, CSE 5311, Prof. Chris Ding: Shilpa Goley & Raghunath Ravi
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.
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
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-8 do
Example:
t 1 x
t 1
x
10 ∞
10
∞ ∞ 9
10
9 3 2 4
3 2 4 6
s 0
s 6
0 7
7
5
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
1
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).
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.
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.
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
B
5 10
no 6
3
s H
C 7
8
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.
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.
d11 d12 . . .
D= d21
Step2: Do = D
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
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
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.
• Weighted Graphs – A graph is called weighted if the edges in the graph have
values.
• Undirected graph - Each edge connecting two vertices does not have a
direction associated to it.
B C
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
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
1
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 |
1
2 6 The matrix representation of the given weighted graph
is
5
2
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 |
(A2)ij = ∑ Aik X Akj
K=1
Finding the Second nearest neighbor:
(A3)ij = ∑ Aik X Akl X Alj
K=1