1. Department Of Computer Science
Subject : Data Structure And Algorithm
Title : Multistage Graphs
Name : M.Anushuya
I MSC(cs)
2. A Multistage graph is a directed graph in which the nodes can
be divided into a set of stages such that all edges are from a
stage to next stage only (In other words there is no edge
between vertices of same stage and from a vertex of current
stage to previous stage).
A multistage graph, a source and a destination, we need to
find shortest path from source to destination. By convention,
we consider source at stage 1 and destination as last stage.
3. Following is an example graph we will consider in this article :-
4. Now there are various strategies we can apply :-
The Brute force method of finding all possible paths between Source and
Destination and then finding the minimum. That’s the WORST possible
strategy.
Dijkstra’s Algorithm of Single Source shortest paths. This method will find
shortest paths from source to all other nodes which is not required in this
case. So it will take a lot of time and it doesn’t even use the SPECIAL
5. The Brute force method of finding all possible paths
between Source and Destination and then finding the
minimum. That’s the WORST possible strategy.
Dijkstra’s Algorithm of Single Source shortest paths.
This method will find shortest paths from source to all
other nodes which is not required in this case. So it will
take a lot of time and it doesn’t even use the SPECIAL
feature that this MULTI-STAGE graph has.
6. Simple Greedy Method – At each node, choose
the shortest outgoing path. If we apply this
approach to the example graph give above we get
the solution as 1 + 4 + 18 = 23. But a quick look at
the graph will show much shorter paths available
than 23. So the greedy method fails !
The best option is Dynamic Programming. So we
need to find Optimal Sub-structure, Recursive
Equations and Overlapping Sub-problems.
7. Implementation details:
The below implementation assumes that nodes are numbered
from 0 to N-1 from first stage (source) to last stage
(destination). We also assume that the input graph is
multistage.
We use top to bottom approach, and use dist[] array to store
the value of overlapping sub-problem.
dist[i] will store the value of minimum distance from node i to
node n-1 (target node).
Therefore, dist[0] will store minimum distance between from
source node to target node.
9. Step-1: Cost (K-2, j)
In this step, three nodes (node 4, 5. 6) are selected as j. Hence, we have three
options to choose the minimum cost at this step.
Cost(3, 4) = min {c(4, 7) + Cost(7, 9),c(4, 8) + Cost(8, 9)} = 7
Cost(3, 5) = min {c(5, 7) + Cost(7, 9),c(5, 8) + Cost(8, 9)} = 5
Cost(3, 6) = min {c(6, 7) + Cost(7, 9),c(6, 8) + Cost(8, 9)} = 5
Step-2: Cost (K-3, j)
Two nodes are selected as j because at stage k - 3 = 2 there are two nodes, 2
and 3. So, the value i = 2 and j = 2 and 3.
Cost(2, 2) = min {c(2, 4) + Cost(4, 8) + Cost(8, 9),c(2, 6) +
Cost(6, 8) + Cost(8, 9)} = 8
Cost(2, 3) = {c(3, 4) + Cost(4, 8) + Cost(8, 9), c(3, 5) + Cost(5, 8)+ Cost(8,
9), c(3, 6) + Cost(6, 8) + Cost(8, 9)} = 10