0% found this document useful (0 votes)
20 views18 pages

The Shortest Path Problem

The document outlines various algorithms and concepts related to graph theory, including the shortest path problem, spanning trees, minimum spanning trees, maximum flow problems, and minimum cost flow problems. It provides step-by-step procedures for finding shortest paths, constructing spanning trees, and calculating maximum flow in networks. Additionally, it discusses the formulation of the minimum cost flow problem and its relation to transportation problems.

Uploaded by

sara
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views18 pages

The Shortest Path Problem

The document outlines various algorithms and concepts related to graph theory, including the shortest path problem, spanning trees, minimum spanning trees, maximum flow problems, and minimum cost flow problems. It provides step-by-step procedures for finding shortest paths, constructing spanning trees, and calculating maximum flow in networks. Additionally, it discusses the formulation of the minimum cost flow problem and its relation to transportation problems.

Uploaded by

sara
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

The Shortest Path Problem

Find the shortest path & total distance from O to T.

Initialize
start at node O (distance = 0)
all other nodes have infinite distances initially.

Evaluate neighbors and update distances


From O:
• distance to A = 2
• distance to B = 5
• distance to C = 4

Node Distance Previous Node


O 0 -
A 2 O
B 5 O
C 4 O
D ∞ -
E ∞ -
T ∞ -

Move to the node with the smallest distance, which is A = 2.


From A:
• distance to B = 2 + 2 = 4 → update the distance in B.
• distance to D = 7 + 2 = 9
Node Distance Previous Node
O 0 -
A 2 O
B 4 A
C 4 O
D 9 A
E ∞ -
T ∞ -

Moving to node B, which is B = 4.


From B:
• distance to D = 4 + 4 = 8
• distance to E = 3 + 4 = 7
• distance to C = 1 + 4 = 5 (Since 5 is greater than 4, we do not update C)
Node Distance Previous Node
O 0 -
A 2 O
B 4 A
C 4 O
D 8 B
E 7 B
T 14 E

Moving to node C, which is C = 4.


From C:
• distance to E = 4 + 4 = 8 (no update needed since 7 < 8)

Moving to node E, which is E = 7.


From E:
• distance to D = 1 + 7 = 8
• distance to T = 7 + 7 = 14

Moving to node D, which is D = 8.


• distance to T = 5 + 8 = 13. (update table since 13 < 14)
Node Distance Previous Node
O 0 -
A 2 O
B 4 A
C 4 O
D 8 B
E 7 B
T 13 D

To find the shortest path to T, backtrack using the "Previous" column:

T ← D ← B ← A ← O. == 2 + 2 + 4 + 5 = 13.
Find the shortest path & total distance from A to E.

Initialize
From node A = 0
All other nodes have infinite distance initially.

Node Distance Previous Node


A 0 -
B ∞ -
C ∞ -
D ∞ -
E ∞ -

From A:
distance to B = 2
distance to C = 3
Node Distance Previous Node
A 0 -
B 2 A
C 3 A
D ∞ -
E ∞ -

Moving to node B = 2 (smallest distance = 2)


From B:
distance to D = 4 + 2 = 6
distance to C = 5 + 2 = 7

Node Distance Previous Node


A 0 -
B 2 A
C 3 A
D 6 B
E ∞ -

Moving to node C = 3 (smallest distance = 3)


From C:
distance to E = 2 + 3 = 5
Node Distance Previous Node
A 0 -
B 2 A
C 3 A
D 6 B
E 5 C

Moving to node E = 5 (smallest distance)


E is the target, we stop.

Shortest Path: A -> C -> E = 3 + 2 = 5.

Find the shortest path from the network from A to D

Node Distance Previous Node


A 0 -
B ∞ -
C ∞ -
D ∞ -
E ∞ -

From A:
distance to B = 10
distance to C = 5

Node Distance Previous Node


A 0 -
B 10 A
C 5 A
D ∞ -
E ∞ -

Next, visit the neighbour with the smalled value which is C.


From C =5:
distance to B = 3 + 5 = 8 (update, as 8<10)
distance to E = 2 + 5 = 7

Node Distance Previous Node


A 0 -
B 8 B
C 5 A
D ∞ -
E 7 B

From B, calculate distances to neighbors:

D: 8+1= 9.

Node Distance Previous Node


A 0 -
B 8 B
C 5 A
D 9 B
E 7 B

Visit the unvisited node with the smallest distance: E (7).

From E, calculate distances to neighbors:

D: 7+6=13 (no update, as 9<13).

Trace the path

The shortest path to D is from A→C→B→ with a total cost of 9.

Spanning Tree
For a network with n nodes, a spanning tree has (n-1) arcs that connects all
nodes of the network that contains no cycles.
Ex. 1)
The network above is NOT a spanning tree because;
1) some nodes aren’t connected, A is not connected to E, F, G.
2) number of nodes is 7 meanwhile the number of arcs is 5. When it should
be n – 1 = 7 – 1 = 6 arcs.

Ex. 2)

This network is not a spanning tree because;


1) there’s a cycle between the nodes.
2) number of nodes = 7 & number of arcs = 8. When it should be n -1 = 7 – 1
= 6 arcs.

Ex. 3)

This network is a spanning tree;


1) all nodes are connected.
2) there is no cycle
3) number of nodes = 7 number of arcs = n - 1 = 7 – 1 = 6 arcs.
Minimum Spanning Tree:
A minimum spanning tree is defined as a spanning tree that has the minimum
weight among all the possible spanning trees.

The minimum spanning tree has all the properties of a spanning tree with no
added constraint of having minimum possible weight among all possible
spanning trees.

Algorithms to Find Minimum Spanning Tree:


There are two algorithms to find a minimum spanning tree:
1. Krushkal’s Algorithm
2. Prim’s Algorithm

Krushkal’s Algorithm:
Steps to find a minimum spanning tree by using krushkal’s algorithm:
1. Sort all the edges of the graph by their weights
2. Start the iteration of finding the spanning tree
3. At each iteration, the algorithm adds the next lowest-weight edge one by
one so that it doesn’t form a cycle.

Design a spanning tree from a given diagram and find the minimum total length
of arcs:

1. Sort all the edges from the graph by their weight in accending order
CE = 1
AB = 2
AC = 3
AD = 3
BE = 3
BC = 4
CD = 5
DF = 7
EF = 8

2. Select the arcs in accending order and make sure not to form a cycle which
is CE = 1

3. Then go for the next minimum arc & check whether the edge is forming a
cycle or not, which is AB = 2
Next minimum edge is BE = 3. If we add BE then a cycle will form. So avoid BE=3.
And the same goes for BC = 4 and CD = 5.
Now, select DE = 7.

If we select EF=8, a cycle will form, so avoid EF.


The minimum spanning tree for the given network is:

Check the properties:


No cycle is there.
All nodes are connected.
N – 1, Nodes = 6, 6 – 1 = 5. Which is true.
Minimum total cost or length is: 1 + 2 + 3 + 3 + 7 = 16.
Prim’s Algorithm
Prim’s algorithm is used to find the minimum spanning tree.
Steps for prim’s algorithm:
1) Select any arbitrary node from the network.
2) Then check for the minimum edge that connects one node to another
node that’s not yet in the network.
3) This process ends until all the nodes are included.

Design the network by identifying enough arcs, so there’s a path between every
pair of nodes and the total length of these arcs is minimum
OR
Find the minimum spanning tree for the given network.

Solution:
Select any node, A. Then take the minimum arc from that node.
CF = 4 and CA = 4, but from node D
DF = 3. so select DF

All nodes are visited so stop the


process.
minimumm total distance is: 2
+ 2 + 1 + 1 + 3 + 5 = 14.
The Maximum Flow Problem
The maximum flow problem is a one type of linear programming problems.
The maximum flow problem can be described as:
1) All flow through a directed and connected network originates at one node
called the “source” and terminates at another node called the “sink”.
2) All the remaining nodes are called “transhipment nodes”.
3) The flow through an arc is allowed only in the direction by the arrow
head, where the maximum amount of flow is given by the capacity of the
arc.
4) At the “source”, all the arcs point away from the node and at the “sink” all
the arcs point into the node.
5) The objective of the maximum flow problem is to maximize the total
amount of flow from the source to the sink.

Steps to find the maximum flow:


Step 1: if a path from the source to the sink exists, go to step 2 otherwisethe
optimal solution is determined by the backward arcs.
Step 2: calculate the residual flow network.
a) find the ‘smallest capacity’ of the arcs on the path. Denote it by ‘c’.
b) substract ‘c’ from the capacity of each arc on the path.
c) add a ‘backward arc’ for each arc on the path. add ‘c’ to the capacity of
each reverse arc.
d) go back to step 1.

Ex) Find the maximum flow and mincut for the following network:
Solution: Find ALL paths from ‘source’ to ‘sink’
Path 1: source → v1 → v2 → sink
Path 2: source → v3 → v4 → sink
Path 3: source → v1 → v2 → v3 → v4 → sink

First, select path 1: source → v1 → v2 → sink


find the minimum capacity arc. which is v2 -> sink = 3
c = 3. Then substract ‘c’ from every arc on path 1.
add ‘backward arc’ for each arc and add ‘c’ on the capacity of the reverse arc.

Minimum capacity = 3

Select path 2: source → v3 → v4 → sink.


find the minimum capacity arc which is ‘source’ to ‘v3’ = 10.
c = 10. substract c from every arc on path 2.
add backwards arc for each arc and c on the capacity of the reverse arc.
Minimum capacity = 10
Lastly path 3: source → v1 → v2 → v3 → v4 → sink.
c = 2.

min cut = 10 + 5 =15


minimum capacity is = 2
max flow is 2 + 3 + 10 = 15.
mincut: the max-flow min cut theorm states that for any networks with a single
source and sink the maximum flow from the source to sink equals to the
minimum cut value.

Minimum Cost Flow Problem


Minimum cost flow problem is a one type of the linear programming
problems.
The minimum cost flow problem is described as:
1. The network is a directed & connected network.
2. At least one of the nodes is a ‘supply node’.
3. At least one of the other nodes is a ‘demand node’.
4. All the remaining nodes are ‘transhipment nodes’.
5. Flow through an arc is allowed only in the direction indicated by
the arrowhead, where the maximum amount of flow is given by
the capacity of that arc.
6. The network has enough arcs with sufficient capacity to enable
all the flow generated at the supply nodes to reach all the
demand nodes.
7. The cost of the flow through each arc is propotional to the
amount of that flow where the cost per unit flow is known.
8. The objective is to minimize the total cost of sending the
available supply through the network to satisfy the given
demand.

Formulation of the model:


Consider a directed and a connected network where the ‘n’ nodes
include at least one supply node and at least one demand node.
The decision variables are
Xij = flow through i → j.
And the given information includes
Cij = cost per unit flow through arc i → j
Uij = arc capacity for arc i → j
Bi =net flow generated at node i.
The value of Bi depends on the nature of node i:
where,
Bi > 0 node i is a supply node
Bi < 0 node i is a demand node
Bi = 0 node i is a transhipment node.
The objective is to minimize the total cost of sending the supply
through the network to satisfy the given demand.

Feasible Solution Property:


For a minimum cost flow problem to have any feasible
solution is
Transportation Problem as minimum cost flow model

1 2

3 4

Cost from Node 1 to Node 3: C13 = 1  coefficient


Cost from Node 1 to Node 4: C14 = 2
Cost from Node 2 to Node 3: C23 = 3
Cost from Node 2 to Node 4: C24 = 4

Minimize Z = 𝑥13 + 2𝑥14 + 3𝑥23 + 4𝑥24

𝒙𝟏𝟑 𝒙𝟏𝟒 𝒙𝟐𝟑 𝒙𝟐𝟒 rhs


1 1 0 0 4
0 0 1 1 5
-1 0 -1 0 -6
0 -1 0 -1 -3

Minimize Z = 𝑥13 + 2𝑥14 + 3𝑥23 + 4𝑥24


subject to:
x13 + x 14 = 4
x23 + x24 = 5
-x13 – x23 = -6
-x14 – x24 = -3

All variables are non-negative.

You might also like