Network Analysis Vishal
Network Analysis Vishal
ANALYSIS
Learning Objective
• Finding a simple route between two locations or one that visits several locations,
people usually try to take the best route.
• The best route can be the quickest, shortest, or most scenic route, depending on the
impedance chosen.
• In consideration of finding the best route, two impedance are used, i.e. Time
• If the impedance is time, then the best route is the quickest route.
• The best route can be defined as the route that has the lowest impedance, where the
Time Impedance vs. Distance Impedance
Time Impedance Distance Impedance
• Here, time is the main constrain. • Here, distance is the main constrain.
• It gives the least time from source • It gives the least distance to from source
to destination, where distance does to destination, where time does not
not matter. matter
Three basic algorithms for finding the shortest path between two nodes.
1. Dijkstra s Algorithm
Solves only the problem with non-negative costs.
Single Source Shortest path algorithm for all the vertices.
2. Bellman-Ford Algorithm
Applicable to problems with arbitrary costs
3. Floyd-Warshall algorithm
May deals with Negative-weight edges.
Not applicable for negative-weight cycles.
• Floyd-Warshall and Bellman-Ford algorithm solve the problems on graphs that do not
have a cycle with negative cost.
Dijkstra’s Algorithm
Destinati
on
Origin
Cont.…
• Use to determine all-pairs shortest path and find the length of the
shortest path between any pair of vertices in Graph(G).
•
• Output:
• n*n distance matrix D = []
• where, is the distance from vertex i to j.
Intermediate vertices
• Let be the shortest path from i to j such that all intermediate vertices
on the path (if any) are in set {1,2,……,k}.
• is the set to be
Shortest path using intermediate vertices { , , ……. }
𝑉𝑘
𝑉𝑖
𝑉𝑗
Summary
• Distance matrix
• for k = 1,2,…n.
SHORTEST PATHS AND MATRİX MULTİPLİCATİON
• Example
2
3 4
1 2 3 4 5
3
1 0 3 8 ∞ -4
1
8
2 2 ∞ 0 ∞ 1 7
-5
1 3 ∞ 4 0 ∞ ∞
-4 7 4 2 ∞ -5 0 ∞
5
6
4 5 ∞ ∞ ∞ 6 0
57
Identity Matrix
SHORTEST PATHS AND MATRİX MULTİPLİCATİON
2 1 2 3 4 5
3 4
1 0 3 8 ∞ -4
1 3
2
8 2 ∞ 0 ∞ 1 7
-5
1 3 ∞ 4 0 ∞ ∞
-4 7 4 2 ∞ -5 0 ∞
5 4
6 5 ∞ ∞ ∞ 6 0
D1= D0W
58
SHORTEST PATHS AND MATRİX MULTİPLİCATİON
2 1 2 3 4 5
3 4
1 0 3 8 2 -4
1 3
2
8 2 3 0 -4 1 7
-5
1 3 ∞ 4 0 5 11
7
-4 4 2 -1 -5 0 -2
5 4
6
5 8 ∞ 1 6 0
D2= D1W
59
SHORTEST PATHS AND MATRİX MULTİPLİCATİON
2 1 2 3 4 5
3 4
1 0 3 -3 2 -4
3
1
2
8 2 3 0 -4 1 -1
-5
1 3 7 4 0 5 11
-4 7 4 2 -1 -5 0 -2
5 4
6 5 8 5 1 6 0
D3= D2W
60
SHORTEST PATHS AND MATRİX MULTİPLİCATİON
1 2 3 4 5
2
3 4 1 0 1 -3 2 -4
1 3 2 3 0 -4 1 -1
8
2
-5 3 7 4 0 5 3
1
-4 7 4 2 -1 -5 0 -2
5
6
4 5 8 5 1 6 0
D4= D3W
61
Bellman-Ford
Algorithm
• Published by Richard E. Bellman and Lester Ford, Jr. in 1958 in their
publication “On a Routing Problem”
• Use relaxation to find single source shortest paths on directed graphs that may
contain negative weight edges.
•
• It will also detect Negative weight cycle(such that there is no solution)
• Find the shortest paths from a source to all possible destinations using only one
link.
• Algorithm will find all the paths correctly where, we repeat that N-1 times,
where N is the number of vertices (|V| = N)!
Steps For Determining The Shortest
Path
Initialize d's, π's, and set s.d = 0 ⇒ O(V)
Loop |V|-1 times through all edges checking the relaxation condition to
Compute minimum distances ⇒ (|V|-1) O(E) = O(VE)
Loop through all edges checking for negative weight cycles which
occurs if any of the relaxation conditions fail ⇒ O(E)
The solutions utilize the concept of edge relaxation which is a test
todetermine whether going through edge (u,v) reduces the distance to v
And if so update v.π and v.d. This is accomplished using the condition
Bellman-ford Algorithm
BELLMAN-FORD(G,w,s)
INITIALIZE-SINGLE-SOURCE(G,s)
for i = 1 to |G.V|-1
for each edge (u,v) ∈ G.E
RELAX(u,v,w)
for each edge (u,v) ∈ G.E
if v.d > u.d + w(u,v)
return FALSE
return TRUE
• INITIALIZE-SINGLE-SOURCE(G,s)
• for each vertex v ∈ G.V
v.d = ∞
RELAX(u,v,w)
v.pi = NIL if v.d > u.d + w(u,v)
v.d = u.d + w(u,v)
s.d = 0
v.pi = u
Example
Given the following directed graph
(1,3)=6
(1,4)=3
(2,1)=3
(3,4)=2
(4,2)=1
(4,3)=1
(5,2)=4
(5,4)=2
Cont…
(1,3)=6
(1,4)=3
(2,1)=3
(3,4)=2
(4,2)=1
(4,3)=1
(5,2)=4
(5,4)=2
1 2 3 4 5
D ∞ 4 ∞ 2 0
п / 5 / 5 /
Cont…
1 2 3 4 5
D 6 3 3 2 0
п 2 4 4 5 /
Cont…
(1,3)=6
(1,4)=3
(2,1)=3
(3,4)=2
(4,2)=1
(4,3)=1
(5,2)=4
(5,4)=2
1 2 3 4 5
D 6 3 3 2 0
п 2 4 4 5 /
Cont…
V 1 2 3 4 5
D 6 3 3 2 0
п 2 4 4 5 /
Negative cycle checks
• We now check the relaxation condition one additional time for each
edge. If any of the checks pass then there exists a negative weight
cycle in the graph.
- --……………. -
In which,
w() + w) +…………….w( < 0
EXAMPLE
All the nodes have been covered,
we run one more step
Shortest path shown with the help of coloured
arrow with corresponding weight
THE END