SlideShare a Scribd company logo
Sunawar Khan
Islamia University of Bahawalpur
Bahawalnagar Campus
Analysis o f Algorithm
‫خان‬ ‫سنور‬ Algorithm Analysis
Dijkstra Algorithm
Single Source Shortest Path
Floyd Warshall Algorithm
Bellman Ford Algorithm
Contents
‫خان‬ ‫سنور‬ Algorithm Analysis
Shortest Path in Graph - The Problems
● Given a directed graph G with edge weights, find
■ The shortest path from a given vertex s to all other vertices (Single Source
Shortest Paths)
■ The shortest paths between all pairs of vertices (All Pairs Shortest Paths)
● where the length of a path is the sum of its edge weights.
‫خان‬ ‫سنور‬ Algorithm Analysis
Shortest Paths: Applications
• Robot Applications
• Typesetting in TEX
• Urban Traffic Planning
• Tramp Steamer Problem
• Optimal pipelining of VLSI Chip
• Telemarketer Operator Scheduling
• Subroutine in higher level algorithms
• Approximating Piecewise linear functions
• Open Shortest Path First(OSPF) routing protocol for IP
‫خان‬ ‫سنور‬ Algorithm Analysis
Shortest Paths: Algorithms
● Single-Source Shortest Paths (SSSP)
■ Dijkstra’s
■ Bellman-Ford
● All-Pairs Shortest Paths (APSP)
■ Floyd-Warshall
‫خان‬ ‫سنور‬ Algorithm Analysis
Single Source Shortest Path
• Suppose G be a weighted directed graph where a minimum labeled w(u, v)
associated with each edge (u, v) in E, called weight of edge (u, v). These
weights represent the cost to traverse the edge. A path from vertex u to
vertex v is a sequence of one or more edges.
<(v1,v2), (v2,v3), . . . , (vn-1, vn)> in E[G] where u = v1 and v = vn
• The cost (or length or weight) of the path P is the sum of the weights of
edges in the sequence.
• The shortest-path weight from a vertex u ∈ V to a vertex v ∈ V in the
weighted graph is the minimum cost of all paths from u to v. If there exists
no such path from vertex u to vertex v then the weight of the shortest-path
is ∞.
‫خان‬ ‫سنور‬ Algorithm Analysis
Variant of Single Source Shortest Path
• Given a source vertex, in the weighted diagraph, find the shortest path
weights to all other vertices in the digraph.
• Given a destination vertex, t, in the weighted digraph, find the shortest
path weights from all other vertices in the digraph.
• Given any two vertices in the weighted digraph, find the shortest path
(from u to vor v to u)
‫خان‬ ‫سنور‬ Algorithm Analysis
Negative-Weight Edges
• The negative weight cycle is a cycle whose total is negative. No
path from starting vertex S to a vertex on the cycle can be a
shortest path. Since a path can run around the cycle many, many
times and get any negative cost desired. in other words, a
negative cycle invalidates the noton of distance based on edge
weights.
•
‫خان‬ ‫سنور‬ Algorithm Analysis
Relaxation Technique
• This technique consists of testing whether we can improve the shortest
path found so far if so update the shortest path. A relaxation step may or
may not decrease the value of the shortest-path estimate.
• The following code performs a relaxation step on edge(u,v)
•Relax (u, v, w)
If d[u] + w(u, v) < d[v]
then d[v] d[u] + w[u, v]
‫خان‬ ‫سنور‬ Algorithm Analysis
A Fact About Shortest Paths
● Theorem: If p is a shortest path from u to v, then any subpath of p is
also a shortest path.
● Proof: Consider a subpath of p from x to y. If there were a shorter
path from x to y, then there would be a shorter path from u to v.
u x y v
shorter?
‫خان‬ ‫سنور‬ Algorithm Analysis
The Problem-SSS
•Given a weighted graph G, find a shortest path from given
vertex to each other vertex in G.
•Note that we can solve this problem quite easily with BFS
traversal algorithm in the special case when all weights
are 1.
•The greedy approach to this problem is repeatedly
selecting the best choice from those available at that time.
‫خان‬ ‫سنور‬ Algorithm Analysis
Dijkstra’s Algorithm
• Dijkstra's algorithm solves the single-source shortest-path problem when
all edges have non-negative weights.
• It is a greedy algorithm and similar to Prim's algorithm. Algorithm starts
at the source vertex, s, it grows a tree, T, that ultimately spans all vertices
reachable from S.
• Vertices are added to T in order of distance i.e., first S, then the vertex
closest to S, then the next closest, and so on.
• Following implementation assumes that graph G is represented by
adjacency lists.
‫خان‬ ‫سنور‬ Algorithm Analysis
Implementation
1. INITIALIZE SINGLE-SOURCE (G, s)
2. S ← { } // S will ultimately contains vertices of final shortest-path weights from s
3. Initialize priority queue Q i.e., Q ← V[G]
4. while priority queue Q is not empty do
5. u ← EXTRACT_MIN(Q) // Pull out new vertex
6. S ← S ∪ {u} // Perform relaxation for each vertex v adjacent to u
7. for each vertex v in Adj[u] do
8. Relax (u, v, w)
DIJKSTRA (G, w, s)
‫خان‬ ‫سنور‬ Algorithm Analysis
Analysis
• Like Prim's algorithm, Dijkstra's algorithm runs in
O(|E|lg|V|) time.
‫خان‬ ‫سنور‬ Algorithm Analysis
Step By Step Operations . . .
• Step1. Given initial graph G=(V, E). All nodes nodes have infinite cost
except the source node, s, which has 0 cost.
‫خان‬ ‫سنور‬ Algorithm Analysis
• Step 2. First we choose the node, which is closest to the source node, s. We
initialize d[s] to 0. Add it to S. Relax all nodes adjacent to source, s. Update
predecessor (see red arrow in diagram below) for all nodes updated.
‫خان‬ ‫سنور‬ Algorithm Analysis
• Step 3. Choose the closest node, x. Relax all nodes adjacent to node x.
Update predecessors for nodes u, v and y (again notice red arrows in
diagram below).
‫خان‬ ‫سنور‬ Algorithm Analysis
• Step 4. Now, node y is the closest node, so add it to S. Relax node v and
adjust its predecessor (red arrows remember!).
‫خان‬ ‫سنور‬ Algorithm Analysis
• Step 5. Now we have node u that is closest. Choose this node and adjust its
neighbor node v.
‫خان‬ ‫سنور‬ Algorithm Analysis
• Step 6. Finally, add node v. The predecessor list now defines the shortest
path from each node to the source node, s.
‫خان‬ ‫سنور‬ Algorithm Analysis
Q as a linear Array
• EXTRACT_MIN takes O(V) time and there are |V| such operations.
Therefore, a total time for EXTRACT_MIN in while-loop is O(V2).
• Since the total number of edges in all the adjacency list is |E|.
• Therefore for-loop iterates |E| times with each iteration taking O(1) time.
• Hence, the running time of the algorithm with array implementation
is O(V2 + E) = O(V2).
‫خان‬ ‫سنور‬ Algorithm Analysis
Q as a Binary Heap
• In this case, EXTRACT_MIN operations takes O(lg V) time and there
are |V| such operations.
• The binary heap can be build in O(V) time.
• Operation DECREASE (in the RELAX) takes O(lg V) time and there are at
most such operations.
• Hence, the running time of the algorithm with binary heap provided given
graph is sparse is O((V + E) lg V).
• Note:- that this time becomes O(E lg V) if all vertices in the graph is
reachable from the source vertices
‫خان‬ ‫سنور‬ Algorithm Analysis
Bellman-Ford Algorithm
• Bellman-Ford algorithm solves the single-source shortest-path problem in
the general case in which edges of a given digraph can have negative
weight as long as G contains no negative cycles.
• This algorithm, like Dijkstra's algorithm uses the notion of edge relaxation
but does not use with greedy method. Again, it uses d[u] as an upper
bound on the distance d[u, v] from u to v.
• The algorithm progressively decreases an estimate d[v] on the weight of
the shortest path from the source vertex s to each vertex v in V until it
achieve the actual shortest-path. The algorithm returns Boolean TRUE if
the given digraph contains no negative cycles that are reachable from
source vertex s otherwise it returns Boolean FALSE.
‫خان‬ ‫سنور‬ Algorithm Analysis
The Bellman-Ford Algorithm
● Handles negative edge weights
● Detects negative cycles
● Is slower than Dijkstra
4
-10
5
a negative cycle
‫خان‬ ‫سنور‬ Algorithm Analysis
Bellman-Ford: Idea
● Repeatedly update d for all pairs of vertices connected by an
edge.
● Theorem: If u and v are two vertices with an edge from u to v,
and s  u  v is a shortest path, and d[u] = (s,u),
● then d[u]+w(u,v) is the length of a shortest path to v.
● Proof: Since s u  v is a shortest path, its length is (s,u) +
w(u,v) = d[u] + w(u,v). 
‫خان‬ ‫سنور‬ Algorithm Analysis
Why Bellman-Ford Works
• On the first pass, we find  (s,u) for all vertices whose shortest paths have one edge.
• On the second pass, the d[u] values computed for the one- edge-away vertices are correct (=
 (s,u)), so they are used to compute the correct d values for vertices whose shortest paths
have two edges.
• Since no shortest path can have more than |V[G]|-1 edges, after that many passes all d
values are correct.
• Note: all vertices not reachable from s will have their original values of infinity.(Same,
by the way, for Dijkstra).
‫خان‬ ‫سنور‬ Algorithm Analysis
Bellman-Ford: Algorithm
● BELLMAN-FORD(G, w, s)
1 foreach vertex v V[G] do //INIT_SINGLE_SOURCE
2 d[v]  
3 [v]  NIL
4 d[s]  0
5 for i  1 to |V[G]|-1 do > each iteration is a “pass”
6 for each edge (u,v) in E[G] do
7 RELAX(u, v, w)
8 > check for negative cycles
9 for each edge (u,v) in E[G] do
10 if d[v] > d[u] + w(u,v) then
11 return FALSE
12 return TRUE
Running time VE)
‫خان‬ ‫سنور‬ Algorithm Analysis
Negative Cycle Detection
● What if there is a negative-weight
cycle reachable from s?
d[u]  d[x]+4
d[v]  d[u]+5
d[x]  d[v]-10
● Assume:
●
●
● Adding:
● d[u]+d[v]+d[x]  d[x]+d[u]+d[v]-1
● Because it’s a cycle, vertices on left are same as those on right.
Thus we get 0  -1; a contradiction.
So for at least one edge (u,v),
● d[v] > d[u] + w(u,v)
● This is exactly what Bellman-Ford checks for.
u
x
v
4
5
-10
‫خان‬ ‫سنور‬ Algorithm Analysis
Bellman-Ford Algorithm - Example
6
7
7
-3
2
8
-4
9
5
-2
‫خان‬ ‫سنور‬ Algorithm Analysis
Bellman-Ford Algorithm - Example
0




6
7
7
-3
2
8
-4
9
5
-2
‫خان‬ ‫سنور‬ Algorithm Analysis
Bellman-Ford Algorithm - Example
0
7
6


6
7
7
-3
2
8
-4
9
5
-2
‫خان‬ ‫سنور‬ Algorithm Analysis
Bellman-Ford Algorithm - Example
0
7
6
2
4
6
7
7
-3
2
8
-4
9
5
-2
‫خان‬ ‫سنور‬ Algorithm Analysis
Bellman-Ford Algorithm - Example
0
7
2
2
4
6
5
7
7
-3
2
8
-2
-4
9
‫خان‬ ‫سنور‬ Algorithm Analysis
Bellman-Ford Algorithm - Example
0
7
2
-2
4
6
5
7
7
-3
2
8
-2
-4
9
‫خان‬ ‫سنور‬ Algorithm Analysis
Running Time Analysis
• The initialization in line 1 takes 𝜃 𝑣 𝑡𝑖𝑚𝑒.
• For loop of lines 2-4 takes O(E) time and For-loop of line 5-7
takes O(E) time.
• Thus, the Bellman-Ford algorithm runs in O(E) time.

More Related Content

PPT
3.8 quicksort
Krish_ver2
 
PPT
Quick sort Algorithm Discussion And Analysis
SNJ Chaudhary
 
PPTX
asymptotic notation
SangeethaSasi1
 
3.8 quicksort
Krish_ver2
 
Quick sort Algorithm Discussion And Analysis
SNJ Chaudhary
 
asymptotic notation
SangeethaSasi1
 

What's hot (20)

PDF
Quick sort algorithn
Kumar
 
PPT
Quicksort
Gayathri Gaayu
 
PPTX
Merge sort and quick sort
Shakila Mahjabin
 
PDF
Jmestn42351212
Nertila Ismailaja
 
PPT
Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)
Mohanlal Sukhadia University (MLSU)
 
PPT
lecture 10
sajinsc
 
PPTX
Av 738- Adaptive Filtering - Background Material
Dr. Bilal Siddiqui, C.Eng., MIMechE, FRAeS
 
PDF
Lecture 4 asymptotic notations
jayavignesh86
 
PPTX
Av 738 - Adaptive Filtering - Kalman Filters
Dr. Bilal Siddiqui, C.Eng., MIMechE, FRAeS
 
PPT
Divide and conquer
Dr Shashikant Athawale
 
PPT
Presentation on binary search, quick sort, merge sort and problems
Sumita Das
 
PPTX
Asymptotic Notation and Data Structures
Amrinder Arora
 
PPTX
Data Structures- Hashing
hemalatha athinarayanan
 
PPTX
Lecture 5: Asymptotic analysis of algorithms
Vivek Bhargav
 
PPT
02 order of growth
Hira Gul
 
PPT
Discrete Signal Processing
margretrosy
 
PDF
Lecture 5 6_7 - divide and conquer and method of solving recurrences
jayavignesh86
 
PPTX
asymptotic analysis and insertion sort analysis
Anindita Kundu
 
Quick sort algorithn
Kumar
 
Quicksort
Gayathri Gaayu
 
Merge sort and quick sort
Shakila Mahjabin
 
Jmestn42351212
Nertila Ismailaja
 
Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)
Mohanlal Sukhadia University (MLSU)
 
lecture 10
sajinsc
 
Av 738- Adaptive Filtering - Background Material
Dr. Bilal Siddiqui, C.Eng., MIMechE, FRAeS
 
Lecture 4 asymptotic notations
jayavignesh86
 
Av 738 - Adaptive Filtering - Kalman Filters
Dr. Bilal Siddiqui, C.Eng., MIMechE, FRAeS
 
Divide and conquer
Dr Shashikant Athawale
 
Presentation on binary search, quick sort, merge sort and problems
Sumita Das
 
Asymptotic Notation and Data Structures
Amrinder Arora
 
Data Structures- Hashing
hemalatha athinarayanan
 
Lecture 5: Asymptotic analysis of algorithms
Vivek Bhargav
 
02 order of growth
Hira Gul
 
Discrete Signal Processing
margretrosy
 
Lecture 5 6_7 - divide and conquer and method of solving recurrences
jayavignesh86
 
asymptotic analysis and insertion sort analysis
Anindita Kundu
 
Ad

Similar to Graph 3 (20)

PPTX
DAA_Presentation - Copy.pptx
AndrewJohnson866415
 
PDF
shortestpathalgorithm-180109112405 (1).pdf
zefergaming
 
PPTX
Shortest path algorithm
sana younas
 
PPT
Shortest path
Ruchika Sinha
 
PPTX
SEMINAR ON SHORTEST PATH ALGORITHMS.pptx
bharatherltech
 
PPT
Chap10 slides
BaliThorat1
 
PPT
GraphAlgorithms.pptsfjaaaaaaaaaaaaaaaaaaa
robozenbd
 
PDF
A study on_contrast_and_comparison_between_bellman-ford_algorithm_and_dijkstr...
Khoa Mac Tu
 
PPT
1535 graph algorithms
Dr Fereidoun Dejahang
 
PDF
01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf
DKTaxation
 
PPT
Single source stortest path bellman ford and dijkstra
Roshan Tailor
 
PDF
Shortest Path in Graph
Dr Sandeep Kumar Poonia
 
PPTX
Bellman Ford Algorithm
iqra593488
 
PPTX
Single source shortestpath
JananiJ19
 
PPTX
Algorithm Exam Help
Programming Exam Help
 
PPTX
UNIT II - Graph Algorithms techniques.pptx
mayakishore55
 
PPTX
dms slide discrete mathematics sem 2 engineering
pranavstar99
 
PPTX
Deixtras Algorithm.pptxdjjdjdjdjddddddddddddddd
OrxanMirzzad
 
PPT
Inroduction_To_Algorithms_Lect14
Naor Ami
 
PPT
dijkstraC.ppt
ManoRanjani30
 
DAA_Presentation - Copy.pptx
AndrewJohnson866415
 
shortestpathalgorithm-180109112405 (1).pdf
zefergaming
 
Shortest path algorithm
sana younas
 
Shortest path
Ruchika Sinha
 
SEMINAR ON SHORTEST PATH ALGORITHMS.pptx
bharatherltech
 
Chap10 slides
BaliThorat1
 
GraphAlgorithms.pptsfjaaaaaaaaaaaaaaaaaaa
robozenbd
 
A study on_contrast_and_comparison_between_bellman-ford_algorithm_and_dijkstr...
Khoa Mac Tu
 
1535 graph algorithms
Dr Fereidoun Dejahang
 
01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf
DKTaxation
 
Single source stortest path bellman ford and dijkstra
Roshan Tailor
 
Shortest Path in Graph
Dr Sandeep Kumar Poonia
 
Bellman Ford Algorithm
iqra593488
 
Single source shortestpath
JananiJ19
 
Algorithm Exam Help
Programming Exam Help
 
UNIT II - Graph Algorithms techniques.pptx
mayakishore55
 
dms slide discrete mathematics sem 2 engineering
pranavstar99
 
Deixtras Algorithm.pptxdjjdjdjdjddddddddddddddd
OrxanMirzzad
 
Inroduction_To_Algorithms_Lect14
Naor Ami
 
dijkstraC.ppt
ManoRanjani30
 
Ad

More from International Islamic University (20)

PDF
Binary Search Tree
International Islamic University
 
PDF
Dynamic programming
International Islamic University
 
PDF
Linear timesorting
International Islamic University
 
PDF
Facial Expression Recognitino
International Islamic University
 
PPTX
Data transmission
International Islamic University
 
PPTX
Basic organization of computer
International Islamic University
 
PPTX
Sorting techniques
International Islamic University
 
PPTX
Linear search-and-binary-search
International Islamic University
 
PPTX
Fundamentals of-algorithm
International Islamic University
 
PPTX
Fundamentals of-algorithm
International Islamic University
 
PPTX
What is computer
International Islamic University
 

Recently uploaded (20)

PPTX
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 
PPTX
Artificial Intelligence in Gastroentrology: Advancements and Future Presprec...
AyanHossain
 
DOCX
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
PPTX
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
PPTX
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
PPTX
Basics and rules of probability with real-life uses
ravatkaran694
 
PPTX
20250924 Navigating the Future: How to tell the difference between an emergen...
McGuinness Institute
 
PPTX
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
PPTX
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
PPTX
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
PDF
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
PDF
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
DOCX
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
PDF
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
PDF
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
PPTX
PROTIEN ENERGY MALNUTRITION: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
PPTX
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
PPTX
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
PPTX
Virus sequence retrieval from NCBI database
yamunaK13
 
PPTX
Gupta Art & Architecture Temple and Sculptures.pptx
Virag Sontakke
 
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 
Artificial Intelligence in Gastroentrology: Advancements and Future Presprec...
AyanHossain
 
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
Basics and rules of probability with real-life uses
ravatkaran694
 
20250924 Navigating the Future: How to tell the difference between an emergen...
McGuinness Institute
 
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
PROTIEN ENERGY MALNUTRITION: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
Virus sequence retrieval from NCBI database
yamunaK13
 
Gupta Art & Architecture Temple and Sculptures.pptx
Virag Sontakke
 

Graph 3

  • 1. Sunawar Khan Islamia University of Bahawalpur Bahawalnagar Campus Analysis o f Algorithm
  • 2. ‫خان‬ ‫سنور‬ Algorithm Analysis Dijkstra Algorithm Single Source Shortest Path Floyd Warshall Algorithm Bellman Ford Algorithm Contents
  • 3. ‫خان‬ ‫سنور‬ Algorithm Analysis Shortest Path in Graph - The Problems ● Given a directed graph G with edge weights, find ■ The shortest path from a given vertex s to all other vertices (Single Source Shortest Paths) ■ The shortest paths between all pairs of vertices (All Pairs Shortest Paths) ● where the length of a path is the sum of its edge weights.
  • 4. ‫خان‬ ‫سنور‬ Algorithm Analysis Shortest Paths: Applications • Robot Applications • Typesetting in TEX • Urban Traffic Planning • Tramp Steamer Problem • Optimal pipelining of VLSI Chip • Telemarketer Operator Scheduling • Subroutine in higher level algorithms • Approximating Piecewise linear functions • Open Shortest Path First(OSPF) routing protocol for IP
  • 5. ‫خان‬ ‫سنور‬ Algorithm Analysis Shortest Paths: Algorithms ● Single-Source Shortest Paths (SSSP) ■ Dijkstra’s ■ Bellman-Ford ● All-Pairs Shortest Paths (APSP) ■ Floyd-Warshall
  • 6. ‫خان‬ ‫سنور‬ Algorithm Analysis Single Source Shortest Path • Suppose G be a weighted directed graph where a minimum labeled w(u, v) associated with each edge (u, v) in E, called weight of edge (u, v). These weights represent the cost to traverse the edge. A path from vertex u to vertex v is a sequence of one or more edges. <(v1,v2), (v2,v3), . . . , (vn-1, vn)> in E[G] where u = v1 and v = vn • The cost (or length or weight) of the path P is the sum of the weights of edges in the sequence. • The shortest-path weight from a vertex u ∈ V to a vertex v ∈ V in the weighted graph is the minimum cost of all paths from u to v. If there exists no such path from vertex u to vertex v then the weight of the shortest-path is ∞.
  • 7. ‫خان‬ ‫سنور‬ Algorithm Analysis Variant of Single Source Shortest Path • Given a source vertex, in the weighted diagraph, find the shortest path weights to all other vertices in the digraph. • Given a destination vertex, t, in the weighted digraph, find the shortest path weights from all other vertices in the digraph. • Given any two vertices in the weighted digraph, find the shortest path (from u to vor v to u)
  • 8. ‫خان‬ ‫سنور‬ Algorithm Analysis Negative-Weight Edges • The negative weight cycle is a cycle whose total is negative. No path from starting vertex S to a vertex on the cycle can be a shortest path. Since a path can run around the cycle many, many times and get any negative cost desired. in other words, a negative cycle invalidates the noton of distance based on edge weights. •
  • 9. ‫خان‬ ‫سنور‬ Algorithm Analysis Relaxation Technique • This technique consists of testing whether we can improve the shortest path found so far if so update the shortest path. A relaxation step may or may not decrease the value of the shortest-path estimate. • The following code performs a relaxation step on edge(u,v) •Relax (u, v, w) If d[u] + w(u, v) < d[v] then d[v] d[u] + w[u, v]
  • 10. ‫خان‬ ‫سنور‬ Algorithm Analysis A Fact About Shortest Paths ● Theorem: If p is a shortest path from u to v, then any subpath of p is also a shortest path. ● Proof: Consider a subpath of p from x to y. If there were a shorter path from x to y, then there would be a shorter path from u to v. u x y v shorter?
  • 11. ‫خان‬ ‫سنور‬ Algorithm Analysis The Problem-SSS •Given a weighted graph G, find a shortest path from given vertex to each other vertex in G. •Note that we can solve this problem quite easily with BFS traversal algorithm in the special case when all weights are 1. •The greedy approach to this problem is repeatedly selecting the best choice from those available at that time.
  • 12. ‫خان‬ ‫سنور‬ Algorithm Analysis Dijkstra’s Algorithm • Dijkstra's algorithm solves the single-source shortest-path problem when all edges have non-negative weights. • It is a greedy algorithm and similar to Prim's algorithm. Algorithm starts at the source vertex, s, it grows a tree, T, that ultimately spans all vertices reachable from S. • Vertices are added to T in order of distance i.e., first S, then the vertex closest to S, then the next closest, and so on. • Following implementation assumes that graph G is represented by adjacency lists.
  • 13. ‫خان‬ ‫سنور‬ Algorithm Analysis Implementation 1. INITIALIZE SINGLE-SOURCE (G, s) 2. S ← { } // S will ultimately contains vertices of final shortest-path weights from s 3. Initialize priority queue Q i.e., Q ← V[G] 4. while priority queue Q is not empty do 5. u ← EXTRACT_MIN(Q) // Pull out new vertex 6. S ← S ∪ {u} // Perform relaxation for each vertex v adjacent to u 7. for each vertex v in Adj[u] do 8. Relax (u, v, w) DIJKSTRA (G, w, s)
  • 14. ‫خان‬ ‫سنور‬ Algorithm Analysis Analysis • Like Prim's algorithm, Dijkstra's algorithm runs in O(|E|lg|V|) time.
  • 15. ‫خان‬ ‫سنور‬ Algorithm Analysis Step By Step Operations . . . • Step1. Given initial graph G=(V, E). All nodes nodes have infinite cost except the source node, s, which has 0 cost.
  • 16. ‫خان‬ ‫سنور‬ Algorithm Analysis • Step 2. First we choose the node, which is closest to the source node, s. We initialize d[s] to 0. Add it to S. Relax all nodes adjacent to source, s. Update predecessor (see red arrow in diagram below) for all nodes updated.
  • 17. ‫خان‬ ‫سنور‬ Algorithm Analysis • Step 3. Choose the closest node, x. Relax all nodes adjacent to node x. Update predecessors for nodes u, v and y (again notice red arrows in diagram below).
  • 18. ‫خان‬ ‫سنور‬ Algorithm Analysis • Step 4. Now, node y is the closest node, so add it to S. Relax node v and adjust its predecessor (red arrows remember!).
  • 19. ‫خان‬ ‫سنور‬ Algorithm Analysis • Step 5. Now we have node u that is closest. Choose this node and adjust its neighbor node v.
  • 20. ‫خان‬ ‫سنور‬ Algorithm Analysis • Step 6. Finally, add node v. The predecessor list now defines the shortest path from each node to the source node, s.
  • 21. ‫خان‬ ‫سنور‬ Algorithm Analysis Q as a linear Array • EXTRACT_MIN takes O(V) time and there are |V| such operations. Therefore, a total time for EXTRACT_MIN in while-loop is O(V2). • Since the total number of edges in all the adjacency list is |E|. • Therefore for-loop iterates |E| times with each iteration taking O(1) time. • Hence, the running time of the algorithm with array implementation is O(V2 + E) = O(V2).
  • 22. ‫خان‬ ‫سنور‬ Algorithm Analysis Q as a Binary Heap • In this case, EXTRACT_MIN operations takes O(lg V) time and there are |V| such operations. • The binary heap can be build in O(V) time. • Operation DECREASE (in the RELAX) takes O(lg V) time and there are at most such operations. • Hence, the running time of the algorithm with binary heap provided given graph is sparse is O((V + E) lg V). • Note:- that this time becomes O(E lg V) if all vertices in the graph is reachable from the source vertices
  • 23. ‫خان‬ ‫سنور‬ Algorithm Analysis Bellman-Ford Algorithm • Bellman-Ford algorithm solves the single-source shortest-path problem in the general case in which edges of a given digraph can have negative weight as long as G contains no negative cycles. • This algorithm, like Dijkstra's algorithm uses the notion of edge relaxation but does not use with greedy method. Again, it uses d[u] as an upper bound on the distance d[u, v] from u to v. • The algorithm progressively decreases an estimate d[v] on the weight of the shortest path from the source vertex s to each vertex v in V until it achieve the actual shortest-path. The algorithm returns Boolean TRUE if the given digraph contains no negative cycles that are reachable from source vertex s otherwise it returns Boolean FALSE.
  • 24. ‫خان‬ ‫سنور‬ Algorithm Analysis The Bellman-Ford Algorithm ● Handles negative edge weights ● Detects negative cycles ● Is slower than Dijkstra 4 -10 5 a negative cycle
  • 25. ‫خان‬ ‫سنور‬ Algorithm Analysis Bellman-Ford: Idea ● Repeatedly update d for all pairs of vertices connected by an edge. ● Theorem: If u and v are two vertices with an edge from u to v, and s  u  v is a shortest path, and d[u] = (s,u), ● then d[u]+w(u,v) is the length of a shortest path to v. ● Proof: Since s u  v is a shortest path, its length is (s,u) + w(u,v) = d[u] + w(u,v). 
  • 26. ‫خان‬ ‫سنور‬ Algorithm Analysis Why Bellman-Ford Works • On the first pass, we find  (s,u) for all vertices whose shortest paths have one edge. • On the second pass, the d[u] values computed for the one- edge-away vertices are correct (=  (s,u)), so they are used to compute the correct d values for vertices whose shortest paths have two edges. • Since no shortest path can have more than |V[G]|-1 edges, after that many passes all d values are correct. • Note: all vertices not reachable from s will have their original values of infinity.(Same, by the way, for Dijkstra).
  • 27. ‫خان‬ ‫سنور‬ Algorithm Analysis Bellman-Ford: Algorithm ● BELLMAN-FORD(G, w, s) 1 foreach vertex v V[G] do //INIT_SINGLE_SOURCE 2 d[v]   3 [v]  NIL 4 d[s]  0 5 for i  1 to |V[G]|-1 do > each iteration is a “pass” 6 for each edge (u,v) in E[G] do 7 RELAX(u, v, w) 8 > check for negative cycles 9 for each edge (u,v) in E[G] do 10 if d[v] > d[u] + w(u,v) then 11 return FALSE 12 return TRUE Running time VE)
  • 28. ‫خان‬ ‫سنور‬ Algorithm Analysis Negative Cycle Detection ● What if there is a negative-weight cycle reachable from s? d[u]  d[x]+4 d[v]  d[u]+5 d[x]  d[v]-10 ● Assume: ● ● ● Adding: ● d[u]+d[v]+d[x]  d[x]+d[u]+d[v]-1 ● Because it’s a cycle, vertices on left are same as those on right. Thus we get 0  -1; a contradiction. So for at least one edge (u,v), ● d[v] > d[u] + w(u,v) ● This is exactly what Bellman-Ford checks for. u x v 4 5 -10
  • 29. ‫خان‬ ‫سنور‬ Algorithm Analysis Bellman-Ford Algorithm - Example 6 7 7 -3 2 8 -4 9 5 -2
  • 30. ‫خان‬ ‫سنور‬ Algorithm Analysis Bellman-Ford Algorithm - Example 0     6 7 7 -3 2 8 -4 9 5 -2
  • 31. ‫خان‬ ‫سنور‬ Algorithm Analysis Bellman-Ford Algorithm - Example 0 7 6   6 7 7 -3 2 8 -4 9 5 -2
  • 32. ‫خان‬ ‫سنور‬ Algorithm Analysis Bellman-Ford Algorithm - Example 0 7 6 2 4 6 7 7 -3 2 8 -4 9 5 -2
  • 33. ‫خان‬ ‫سنور‬ Algorithm Analysis Bellman-Ford Algorithm - Example 0 7 2 2 4 6 5 7 7 -3 2 8 -2 -4 9
  • 34. ‫خان‬ ‫سنور‬ Algorithm Analysis Bellman-Ford Algorithm - Example 0 7 2 -2 4 6 5 7 7 -3 2 8 -2 -4 9
  • 35. ‫خان‬ ‫سنور‬ Algorithm Analysis Running Time Analysis • The initialization in line 1 takes 𝜃 𝑣 𝑡𝑖𝑚𝑒. • For loop of lines 2-4 takes O(E) time and For-loop of line 5-7 takes O(E) time. • Thus, the Bellman-Ford algorithm runs in O(E) time.