SlideShare ist ein Scribd-Unternehmen logo
1 von 35
Downloaden Sie, um offline zu lesen
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.

Weitere ähnliche Inhalte

Was ist angesagt?

Quick sort algorithn
Quick sort algorithnQuick sort algorithn
Quick sort algorithn
Kumar
 
lecture 10
lecture 10lecture 10
lecture 10
sajinsc
 
Discrete Signal Processing
Discrete Signal ProcessingDiscrete Signal Processing
Discrete Signal Processing
margretrosy
 
asymptotic analysis and insertion sort analysis
asymptotic analysis and insertion sort analysisasymptotic analysis and insertion sort analysis
asymptotic analysis and insertion sort analysis
Anindita Kundu
 

Was ist angesagt? (20)

Quick sort algorithn
Quick sort algorithnQuick sort algorithn
Quick sort algorithn
 
Quicksort
QuicksortQuicksort
Quicksort
 
Merge sort and quick sort
Merge sort and quick sortMerge sort and quick sort
Merge sort and quick sort
 
Jmestn42351212
Jmestn42351212Jmestn42351212
Jmestn42351212
 
Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)
Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)
Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)
 
lecture 10
lecture 10lecture 10
lecture 10
 
Av 738- Adaptive Filtering - Background Material
Av 738- Adaptive Filtering - Background MaterialAv 738- Adaptive Filtering - Background Material
Av 738- Adaptive Filtering - Background Material
 
sorting
sortingsorting
sorting
 
Lecture 4 asymptotic notations
Lecture 4   asymptotic notationsLecture 4   asymptotic notations
Lecture 4 asymptotic notations
 
Av 738 - Adaptive Filtering - Kalman Filters
Av 738 - Adaptive Filtering - Kalman Filters Av 738 - Adaptive Filtering - Kalman Filters
Av 738 - Adaptive Filtering - Kalman Filters
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Presentation on binary search, quick sort, merge sort and problems
Presentation on binary search, quick sort, merge sort  and problemsPresentation on binary search, quick sort, merge sort  and problems
Presentation on binary search, quick sort, merge sort and problems
 
Asymptotic Notation and Data Structures
Asymptotic Notation and Data StructuresAsymptotic Notation and Data Structures
Asymptotic Notation and Data Structures
 
Data Structures- Hashing
Data Structures- Hashing Data Structures- Hashing
Data Structures- Hashing
 
Lecture 5: Asymptotic analysis of algorithms
Lecture 5: Asymptotic analysis of algorithmsLecture 5: Asymptotic analysis of algorithms
Lecture 5: Asymptotic analysis of algorithms
 
02 order of growth
02 order of growth02 order of growth
02 order of growth
 
Discrete Signal Processing
Discrete Signal ProcessingDiscrete Signal Processing
Discrete Signal Processing
 
Unit 3
Unit 3Unit 3
Unit 3
 
Lecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrencesLecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrences
 
asymptotic analysis and insertion sort analysis
asymptotic analysis and insertion sort analysisasymptotic analysis and insertion sort analysis
asymptotic analysis and insertion sort analysis
 

Ähnlich wie Graph 3

shortestpathalgorithm-180109112405 (1).pdf
shortestpathalgorithm-180109112405 (1).pdfshortestpathalgorithm-180109112405 (1).pdf
shortestpathalgorithm-180109112405 (1).pdf
zefergaming
 
01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf
01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf
01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf
DKTaxation
 
Inroduction_To_Algorithms_Lect14
Inroduction_To_Algorithms_Lect14Inroduction_To_Algorithms_Lect14
Inroduction_To_Algorithms_Lect14
Naor Ami
 

Ähnlich wie Graph 3 (20)

DAA_Presentation - Copy.pptx
DAA_Presentation - Copy.pptxDAA_Presentation - Copy.pptx
DAA_Presentation - Copy.pptx
 
shortestpathalgorithm-180109112405 (1).pdf
shortestpathalgorithm-180109112405 (1).pdfshortestpathalgorithm-180109112405 (1).pdf
shortestpathalgorithm-180109112405 (1).pdf
 
Shortest path algorithm
Shortest path algorithmShortest path algorithm
Shortest path algorithm
 
Shortest path
Shortest pathShortest path
Shortest path
 
Chap10 slides
Chap10 slidesChap10 slides
Chap10 slides
 
A study on_contrast_and_comparison_between_bellman-ford_algorithm_and_dijkstr...
A study on_contrast_and_comparison_between_bellman-ford_algorithm_and_dijkstr...A study on_contrast_and_comparison_between_bellman-ford_algorithm_and_dijkstr...
A study on_contrast_and_comparison_between_bellman-ford_algorithm_and_dijkstr...
 
1535 graph algorithms
1535 graph algorithms1535 graph algorithms
1535 graph algorithms
 
01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf
01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf
01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf
 
Single source stortest path bellman ford and dijkstra
Single source stortest path bellman ford and dijkstraSingle source stortest path bellman ford and dijkstra
Single source stortest path bellman ford and dijkstra
 
Shortest Path in Graph
Shortest Path in GraphShortest Path in Graph
Shortest Path in Graph
 
Bellman Ford Algorithm
Bellman Ford AlgorithmBellman Ford Algorithm
Bellman Ford Algorithm
 
Single source shortestpath
Single source shortestpathSingle source shortestpath
Single source shortestpath
 
Algorithm Exam Help
Algorithm Exam Help Algorithm Exam Help
Algorithm Exam Help
 
Inroduction_To_Algorithms_Lect14
Inroduction_To_Algorithms_Lect14Inroduction_To_Algorithms_Lect14
Inroduction_To_Algorithms_Lect14
 
dijkstraC.ppt
dijkstraC.pptdijkstraC.ppt
dijkstraC.ppt
 
Lecture 16 - Dijkstra's Algorithm.pdf
Lecture 16 - Dijkstra's Algorithm.pdfLecture 16 - Dijkstra's Algorithm.pdf
Lecture 16 - Dijkstra's Algorithm.pdf
 
Shortest Path Problem.docx
Shortest Path Problem.docxShortest Path Problem.docx
Shortest Path Problem.docx
 
Single sourceshortestpath by emad
Single sourceshortestpath by emadSingle sourceshortestpath by emad
Single sourceshortestpath by emad
 
Data structure and algorithm
Data structure and algorithmData structure and algorithm
Data structure and algorithm
 
Lecture_10_Parallel_Algorithms_Part_II.ppt
Lecture_10_Parallel_Algorithms_Part_II.pptLecture_10_Parallel_Algorithms_Part_II.ppt
Lecture_10_Parallel_Algorithms_Part_II.ppt
 

Mehr von International Islamic University

Mehr von International Islamic University (20)

Hash tables
Hash tablesHash tables
Hash tables
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
Quick sort
Quick sortQuick sort
Quick sort
 
Linear timesorting
Linear timesortingLinear timesorting
Linear timesorting
 
Facial Expression Recognitino
Facial Expression RecognitinoFacial Expression Recognitino
Facial Expression Recognitino
 
Lecture#4
Lecture#4Lecture#4
Lecture#4
 
Lecture#3
Lecture#3 Lecture#3
Lecture#3
 
Lecture#2
Lecture#2 Lecture#2
Lecture#2
 
Case study
Case studyCase study
Case study
 
Arrays
ArraysArrays
Arrays
 
Pcb
PcbPcb
Pcb
 
Data transmission
Data transmissionData transmission
Data transmission
 
Basic organization of computer
Basic organization of computerBasic organization of computer
Basic organization of computer
 
Sorting techniques
Sorting techniquesSorting techniques
Sorting techniques
 
Linear search-and-binary-search
Linear search-and-binary-searchLinear search-and-binary-search
Linear search-and-binary-search
 
Fundamentals of-algorithm
Fundamentals of-algorithmFundamentals of-algorithm
Fundamentals of-algorithm
 
Fundamentals of-algorithm
Fundamentals of-algorithmFundamentals of-algorithm
Fundamentals of-algorithm
 
What is computer
What is computerWhat is computer
What is computer
 
Storage devices
Storage devicesStorage devices
Storage devices
 

Kürzlich hochgeladen

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
SoniaTolstoy
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
fonyou31
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 

Kürzlich hochgeladen (20)

Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 

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.