SlideShare a Scribd company logo
Randomized
Algorithms: All Pairs
Shortest Path
Instructor: Dr. S. N. Hashemi
Lecturer: Mohammad Akbarizadeh
m.akbarizadeh@aut.ac.ir
Outline
● Deterministic Algorithms
○ Single Source Shortest Path
■ Dijkstra
■ Bellman-Ford
○ All Pairs Shortest path
■ Floyd-Warshall
● Randomized Algorithm
○ All Pair Distance problem
○ Boolean Product Witness Matrix
○ All Pair Shortest Path
● Comparison
2
Deterministic Algorithms
Single Source Shortest Path
Question
Q: How to solve the single source shortest path problem if the given graph is unweighted?
What is the time complexity?
4
Question
Q: How to solve the single source shortest path problem if the given graph is unweighted?
What is the time complexity?
A: We can easily apply BFS to the graph and solve it in O(V+E)
5
Dijkstra Algorithm
● Given a graph and a source vertex, find shortest paths from source to all vertices in the
given graph
● Dijkstra is a greedy algorithm for finding the shortest path from a node which ends in
having a tree with the lowest total cost (similar algorithms: Prim, Kruskal)
● It can be applied to both directed and undirected graph
● Dijkstra doesn’t work for Graphs with negative weight edges
● Time complexity: O(V2)
6
Dijkstra (pseudocode)
7
Dijkstra (example)
Graph = {(0,1)=4, (0,7)=8, (1,2)=8, (1,7)=11,
(7,8)=7, (7,6)=1, (2,3)=7, (2,5)=4, (2,8)=2,
(8,6)=6, (6,5)=2, (3,4)=9, (3,5)=14, (5,4)=10}
Distance = {INF, INF, INF, INF, INF, INF,
INF, INF, INF}
Prev = {UND, UND, UND, UND, UND,
UND, UND, UND, UND}
Q = {0, 1, 2, 3, 4, 5, 6, 7, 8}
8
Dijkstra (example)
Graph = {(0,1)=4, (0,7)=8, (1,2)=8, (1,7)=11,
(7,8)=7, (7,6)=1, (2,3)=7, (2,5)=4, (2,8)=2,
(8,6)=6, (6,5)=2, (3,4)=9, (3,5)=14, (5,4)=10}
Distance = {0, 4, INF, INF, INF, INF, INF, 8,
INF}
Prev = {UND, 0, UND, UND, UND, UND,
UND, 0, UND}
Q = {1, 2, 3, 4, 5, 6, 7, 8}
9
Dijkstra (example)
Graph = {(0,1)=4, (0,7)=8, (1,2)=8, (1,7)=11,
(7,8)=7, (7,6)=1, (2,3)=7, (2,5)=4, (2,8)=2,
(8,6)=6, (6,5)=2, (3,4)=9, (3,5)=14, (5,4)=10}
Distance = {0, 4, 12, INF, INF, INF, INF, 8,
INF}
Prev = {UND, 0, 1, UND, UND, UND,
UND, 0, UND}
Q = {2, 3, 4, 5, 6, 7, 8}
10
Dijkstra (example)
Graph = {(0,1)=4, (0,7)=8, (1,2)=8, (1,7)=11,
(7,8)=7, (7,6)=1, (2,3)=7, (2,5)=4, (2,8)=2,
(8,6)=6, (6,5)=2, (3,4)=9, (3,5)=14, (5,4)=10}
Distance = {0, 4, 12, INF, INF, INF, 9, 8, 15}
Prev = {UND, 0, 1, UND, UND, UND, 7, 0,
7}
Q = {2, 3, 4, 5, 6, 8}
11
Dijkstra (example)
Graph = {(0,1)=4, (0,7)=8, (1,2)=8, (1,7)=11,
(7,8)=7, (7,6)=1, (2,3)=7, (2,5)=4, (2,8)=2,
(8,6)=6, (6,5)=2, (3,4)=9, (3,5)=14, (5,4)=10}
Distance = {0, 4, 12, INF, INF, 11, 9, 8, 15}
Prev = {UND, 0, 1, UND, UND, 6, 7, 0, 6}
Q = {2, 3, 4, 5, 8}
12
Dijkstra (example)
Graph = {(0,1)=4, (0,7)=8, (1,2)=8, (1,7)=11,
(7,8)=7, (7,6)=1, (2,3)=7, (2,5)=4, (2,8)=2,
(8,6)=6, (6,5)=2, (3,4)=9, (3,5)=14, (5,4)=10}
Distance = {0, 4, 12, 19, 21, 11, 9, 8, 14}
Prev = {UND, 0, 1, 2, 5, 6, 7, 0, 2}
Q = {}
13
Dijkstra (Question)
Q: What if one of the weights was zero?
14
Dijkstra (Question)
Q: What if one of the weights was zero?
A: No problem for Dijkstra algorithm. The
answer will be correct if we run dijkstra on the
graph but we can merge two nodes can be
merged
15
Dijkstra (Question)
Q: What if one of the weights was zero?
A: No problem for Dijkstra algorithm. The
answer will be correct if we run dijkstra on the
graph but we can merge two nodes can be
merged
Q: What if the graph is not connected? What
happens to the weights?
16
Dijkstra (Question)
Q: What if one of the weights was zero?
A: No problem for Dijkstra algorithm. The
answer will be correct if we run dijkstra on the
graph but we can merge two nodes can be
merged
Q: What if the graph is not connected? What
happens to the weights?
A: Nodes that are not reachable from the
source will have INF value and the algorithm
will not proceed 17
Bellman-Ford Algorithm
● Given a graph and a source vertex src in graph, find shortest paths from src to all
vertices in the given graph.
● This algorithm works with graphs having negative weight edges
● Greedy algorithm but simpler than Dijkstra and uses relaxation like Dijkstra
● It relaxes all the edges at most |V|-1 times until it finds the answer (no change happens
after update)
● Time complexity: O(VE)
18
Bellman-Ford Algorithm (pseudocode)
19
Bellman-Ford Algorithm (example)
20
G = { (A,B)=-1, (A,C)=4, (B,C)=3, (D,C)=5, (B,D)=2,
(D,B)=1, (B,E)=2, (E,D)=-3 }
S = A
Process order: (B, E), (D, B), (B, D), (A, B), (A, C), (D,
C), (B, C), (E, D)
Bellman-Ford Algorithm (example)
21
G = { (A,B)=-1, (A,C)=4, (B,C)=3, (D,C)=5, (B,D)=2,
(D,B)=1, (B,E)=2, (E,D)=-3 }
S = A
Process order: (B, E), (D, B), (B, D), (A, B), (A, C), (D,
C), (B, C), (E, D)
Bellman-Ford Algorithm (example)
22
G = { (A,B)=-1, (A,C)=4, (B,C)=3, (D,C)=5, (B,D)=2,
(D,B)=1, (B,E)=2, (E,D)=-3 }
S = A
Process order: (B, E), (D, B), (B, D), (A, B), (A, C), (D,
C), (B, C), (E, D)
Bellman-Ford Algorithm (Question)
23
Q = What happens if (B,D)=-2 and (D,B)=-1 ?
Bellman-Ford Algorithm (Question)
24
Q = What happens if (B,D)=-2 and (D,B)=-1 ?
A = The algorithm gets stuck in a loop and updates the
weights until it reaches the end of the algorithm
(Algorithm won't work in this situation)
Process order: (B, E), (D, B), (B, D), (A, B), (A, C), (D,
C), (B, C), (E, D)
Bellman-Ford Algorithm (Question)
25
Q = When does the worst case scenario happens? What
is the worst case scenario for this algorithm?
Bellman-Ford Algorithm (Question)
26
Q = When does the worst case scenario happens? What
is the worst case scenario for this algorithm?
A = When we a complete graph like K5 we will have
V(V-1)/2 edges which have to processed V times, which
has O(V3) time complexity
Deterministic Algorithms
All Pairs Shortest Path
Floyd-Warshall Algorithm
● Given a graph, finds the shortest paths from all vertices to all other vertices
● Uses Dynamic Programming to solve the problem
● Edge weights can be negative (but no negative cycles) and graph can be directed
● Time complexity: O(V3)
28
Floyd-Warshall Algorithm (pseudocode)
29
Floyd-Warshall Algorithm (example)
30
Floyd-Warshall Algorithm (example)
31
Floyd-Warshall Algorithm (example)
32
Floyd-Warshall Algorithm (example)
33
Floyd-Warshall Algorithm (example)
34
Floyd-Warshall Algorithm (another example)
35
Floyd-Warshall Algorithm (another example)
36
Floyd-Warshall Algorithm (another example)
37
Floyd-Warshall Algorithm (another example)
38
Floyd-Warshall Algorithm (another example)
39
Randomized Algorithm
40
Randomized Algorithm
● A Las Vegas algorithm is a randomized algorithm that always gives the correct result but
gambles with resources.
● Monte Carlo simulations are a broad class of algorithms that use repeated random
sampling to obtain numerical results.
○ Monte Carlo simulations are typically used to simulate the behaviour of other systems.
○ Monte Carlo algorithms, on the other hand, are randomized algorithms whose output may be incorrect with
a certain, typically small, probability.
41
How to solve APSP problem using Randomized
algorithms?
● We will show that the problem of computing all-pairs shortest path is reducible, via a
randomized reduction, to the problem of multiplying two integer matrices
● First we solve the easier version of problem for unweighted graph and then get to solve the
weighted graph
42
Randomized Algorithm
All Pair Distance Problem
All Pair Distance problem( APD problem)
● Given an unweighted graph G and its adjacency matrix A, compute the distance
matrix D
● Let G’(V,E’) be a graph obtained from G(V,E) by placing an edge between every pair of
vertices i≠j ∊V that are at distance 1 or 2 in G
● The graph G is a subgraph of G’, and we could view G’ as the square of the graph G
● Lemma 1: If we compute Z=A2, then there is path of length 2 in G between pair of
vertices i and j if and only if Zij>0. Further, the value of Zij indicates the number of distinct
length 2 paths between i and j
● If we compute Z=A3, we can obtain if there is a path of length 3 between i and j in G if and
only if Zij>0
44
All Pair Distance problem( APD problem)
45
G = {(1,2), (2,3), (2,4), (3,4)}
All Pair Distance problem( APD problem)
46
G = {(1,2), (2,3), (2,4), (3,4)}
A = [[ 0, 1, 0, 0],
[ 1, 0, 1, 1],
[0, 1, 0, 1],
[0, 1, 1, 0]]
All Pair Distance problem( APD problem)
47
G = {(1,2), (2,3), (2,4), (3,4)}
A = [[ 0, 1, 0, 0],
[ 1, 0, 1, 1],
[0, 1, 0, 1],
[0, 1, 1, 0]]
A2= [[1, 0, 1, 1],
[0, 3, 1, 1],
[1, 1, 2, 1],
[1, 1, 1, 2]]
All Pair Distance problem( APD problem)
● If given graph G with adjacency matrix A and distance matrix D and we compute Z=G2
with adjacency matrix A’ and distance matrix D’
● To ensure A’ has a zero diagonal, we compute it by setting A’
ij=1 if and only if i≠j and at
least one of A’
ij or Aij is non-zero
● We can observe G’ is complete if and only if G has diameter at most 2, where diameter of a
graph is the maximum shortest path length over all pairs of vertices
● is there any way we could compute D’ from D?
48
All Pair Distance problem( APD problem)
● Observation: if G has a diameter at most 2, then G’ is complete and in D=2A’-A
● This can be obtained from A and A’ in time O(n2)
● Lemma 2: consider any pair of vertices i,j ∊V
○ If Dij is even then Dij=2D’
ij
○ If Dij is odd then Dij=2D’
ij - 1
● By using this lemma we can recursively compute distance matrix of graph G from distance
matrix of graph G’
● But how can we compute the parities of the shortest path lengths?
49
All Pair Distance problem( APD problem)
● Lemma 3: consider any pair of distinct vertices i and j in G
○ For any neighbor k of i, Dij- 1 ≤ Dkj ≤ Dij+ 1
○ There exists a neighbor k of i such that Dkj = Dij - 1
● Now using this lemma we can have a structural property to compute the parities of the
shortest path lengths
● Lemma 4: consider any pair of distinct vertices i and j in G:
○ If Dij is even, then D’
kj ≥ D’
ij for every neighbor k of i in G
○ If Dij is odd, then D’
kj ≤ D’
ij for every neighbor k of i in G. moreover, there exists a neighbor k of i in G such
that D’
kj < D’
ij
50
All Pair Distance problem( APD problem)
● Let Γ(i) denote the set of neighbors of i in G and d(i) be the degree of i. Note that A’
ii=d(i)
● By summing the inequalities in lemma 4 over the neighbors of vertex i we can obtain the
following result:
● Lemma 5: consider any pair of distinct vertices i and j in G:
○ Dij is even if and only if ∑k∊Γ(i) D’
kj ≥ D’
ijd(i)
○ Dij is odd if and only if ∑k∊Γ(i) D’
kj < D’
ijd(i)
● In this lemma matrix multiplication is used to compute:
○ ∑k∊Γ(i) D’
kj= ∑n
k=1 Aik D’
kj = Sij
51
All Pair Distance problem( APD problem)
52
All Pair Distance problem( APD problem)
53
● The APD algorithm computes the distance matrix for an n-vertex graph G in time
O(MM(n) log n) using integer matrix multiplication
● MM(n) indicates matrix multiplication of square matrices with length n
● The ordinary matrix multiplication algorithm has time complexity of O(n3) and currently
the best time complexity has time complexity O(n2.373)
Randomized Algorithm
Boolean Product Witness Matrix(BPWM)
Boolean Product Witness Matrix(BPWM)
55
● Suppose A and B are n*n boolean(0,1) matrices and P=AB is their product under boolean
matrix multiplication
● A witness for Pij is an index k∊{1, ..., n} such that Aik = Bkj = 1
● Observe that Pij=1 if and only if it has some witness k
● A Boolean Product Witness Matrix(BPWM) for P is an integer matrix W such that each
entry Wij contains a witness k for Pij if any, and is 0 if there is no such witness
● There could be as many as n witnesses for each entry in p
● The integer matrix multiplication of A and B, treating their entries as the integer 0 and 1,
yields a matrix C whose entry Cij corresponds exactly to the number of witnesses for
boolean matrix entry Pij
Boolean Product Witness Matrix(BPWM)
56
G = {(1,2), (1,6), (2,3), (2,4), (2,5), (2,6), (3,4), (4,5),
(5,6)}
A =[[0, 1, 0, 0, 0, 1],
[1, 0, 1, 1, 1, 1],
[0, 1, 0, 1, 0, 0],
[0, 1, 1, 0, 1, 0],
[0, 1, 0, 1, 0, 1],
[1, 1, 0, 0, 1, 0]]
A2=[[2, 1, 1, 1, 2, 1],
[1, 5, 1, 2, 2, 2],
[1, 1, 2, 1, 2, 1],
[1, 2, 1, 3, 1, 2],
[2, 2, 2, 1, 3, 1],
[1, 2, 1, 2, 1, 3]]
Boolean Product Witness Matrix(BPWM)
57
● If there is a unique witness for each vertex there is no problem with BPWM but if there are
many witnesses we can use some randomness
● Lemma 6: suppose an urn contains n balls of which w are white, and n-w are black.
Consider choosing r balls at random(without replacement), where n/2 ≤ wr ≤ n then:
○ PR[exactly one white ball is chosen] ≥ 1/2e
Boolean Product Witness Matrix(BPWM)
58
Consider we have n=100 balls
W = 4 balls are white
n/2 <= 4r <=n
50 <= 4r <= 100
13 <=r <= 25
Boolean Product Witness Matrix(BPWM)
60
● So what is the usage of this usage?
● Assume we have set R which contains unique witness for Pij
● R is represented as incidence vector that has Rk=1 for k∈R and Rk=0 for k∉R
● Let AR be the matrix obtained from A by setting AR
ik=kRkAik
● Let BR be the matrix obtained from B by setting BR
kj=RkBkj
● So if the entry Pij has a unique witness in the set R, corresponding entry in the integer
matrix multiplication of AR and BR is the index of this unique witness
Boolean Product Witness Matrix(BPWM)
61
● A key point is that the product of AR and BR yields witnesses for all entries in P that have a
unique witness in R
● By lemma 6 there is a constant probability that a random set R of size r has a unique
witness for an entry in P with w witness where n/2r ≤ w ≤ n/r
● Repeating this for O(log n) independent choices of R makes it extremely unlikely that
witnesses are not identified for such entries in P
● Combining the thoughts above results the following algorithm
Boolean Product Witness Matrix(BPWM)
62
Boolean Product Witness Matrix(BPWM)
63
● The BPWM algorithm is Las Vegas algorithm for BPWM problem with expected
running time O(MM(n) log2 n)
● Now we use APD and BPWM algorithm to solve APSP problem
Randomized Algorithm
All Pair Shortest Path
All Pair Shortest Path(randomized)
65
● We define a successor matrix S for an n-vertex graph G is and n*n matrix such that Sij is the
index k of a neighbor of vertex i that lies on a shortest path from i to j
● Fact: The successor entry for Sij=k iff
○ Dij=d
○ Dik=1
○ Dkj=d-1
○ Aik=1
● Let Bd denote the n*n boolean matrix in which Bd
kj=1 iff Dkj=d-1
● We can compute Bd from D in O(n2) time
● The problem is that this process must be repeated for n different values of d, leading to
super-cubic algorithm which is not good!
All Pair Shortest Path(randomized)
66
All Pair Shortest Path(randomized)
67
● The APSP computes the successor matrix for and n-vertex graph G in expected time
O(MM(n) log2n)
Comparison
68
Comparison
Deterministic algorithms:
Dijkstra+Johnson: O(EV + V2 log V)
Floyd–Warshall algorithm: O(V3)
Randomized Algorithm:
APSP: O(n2.373 log2 n)
69
References
1. Prof. Eric Price, CS 388R Randomized Algorithms, Fall 2019, University of Texas ( available at :
https://www.cs.utexas.edu/~ecprice/courses/randomized/ )
2. David Karger. 6.856J Randomized Algorithms. Fall 2002. Massachusetts Institute of Technology: MIT OpenCourseWare (
available at : https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-856j-randomized-algorithms-fall-
2002/index.htm )
3. Motwani, R., & Raghavan, P. (1995). Randomized Algorithms. Cambridge: Cambridge University Press.
doi:10.1017/CBO9780511814075
4. https://en.wikipedia.org/wiki/Shortest_path_problem#Single-source_shortest_paths
5. https://www.geeksforgeeks.org/dijkstras-shortest-path-algorithm-greedy-algo-7/
6. https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm
7. https://www.youtube.com/watch?v=FtN3BYH2Zes
8. https://www.youtube.com/watch?v=oNI0rf2P9gE
9. https://www.youtube.com/watch?v=XB4MIexjvY0
10. https://www.geeksforgeeks.org/bellman-ford-algorithm-dp-23/
11. https://stackoverflow.com/questions/49460439/can-dijkstras-algorithm-work-on-a-graph-with-weights-of-0
12. https://www.programiz.com/dsa/bellman-ford-algorithm
70
References
12. Algorithms course notes, University of Illinois,(available at: https://jeffe.cs.illinois.edu/teaching/algorithms/book/09-
apsp.pdf )
13. https://www.gatevidyalay.com/floyd-warshall-algorithm-shortest-path-algorithm/
14. https://www.programiz.com/dsa/floyd-warshall-algorithm
15. https://yourbasic.org/algorithms/las-vegas/
16. (44) Lec 26: All pair shortest path-I - YouTube
17. (44) Lec 27: All pair shortest path-II - YouTube
71
exercises
1. Prover lemma 4 and 6
2. Explain and prove time complexity of APD and BPWM algorithm
3. (Bonus) Implement this algorithm and compare its performance with deterministic
algorithms on multiple graphs with different sizes of V and E
72

More Related Content

PPTX
daa-unit-3-greedy method
PPTX
Knapsack Problem (DP & GREEDY)
PPTX
Job sequencing with deadline
PPT
Minimum spanning tree
PDF
Unit 2 mpmc
PPS
Cache memory
PPTX
Basic Learning Algorithms of ANN
PPTX
Back tracking and branch and bound class 20
daa-unit-3-greedy method
Knapsack Problem (DP & GREEDY)
Job sequencing with deadline
Minimum spanning tree
Unit 2 mpmc
Cache memory
Basic Learning Algorithms of ANN
Back tracking and branch and bound class 20

What's hot (20)

PDF
Shortest Path in Graph
PPTX
Iv defuzzification methods
PPT
0/1 knapsack
PDF
Unit 4- Dynamic Programming.pdf
PPT
Parallel processing
PPTX
8257 DMA Controller
PPT
5.1 greedy
PPTX
Travelling salesman dynamic programming
PPT
Data transfer instruction set of 8085 micro processor
PPTX
Uni Processor Architecture
PPTX
Bellman ford Algorithm
PPTX
01 Knapsack using Dynamic Programming
PPT
8085-microprocessor
PDF
PPTX
Direct access memory
PPT
Static and Dynamic Read/Write memories
DOCX
Cs6660 compiler design may june 2016 Answer Key
PPT
Data transfer and manipulation
PPTX
Multiplexers
Shortest Path in Graph
Iv defuzzification methods
0/1 knapsack
Unit 4- Dynamic Programming.pdf
Parallel processing
8257 DMA Controller
5.1 greedy
Travelling salesman dynamic programming
Data transfer instruction set of 8085 micro processor
Uni Processor Architecture
Bellman ford Algorithm
01 Knapsack using Dynamic Programming
8085-microprocessor
Direct access memory
Static and Dynamic Read/Write memories
Cs6660 compiler design may june 2016 Answer Key
Data transfer and manipulation
Multiplexers
Ad

Similar to Randomized algorithms all pairs shortest path (20)

PPTX
DAA_Presentation - Copy.pptx
PPT
2.6 all pairsshortestpath
PPT
Lec-35Graph - Graph - Copy in Data Structure
PDF
All pairs shortest path algorithm
PPTX
Dijkstra's algorithm presentation
PPTX
Metric dimesion of circulsnt graphs
PPT
Unit26 shortest pathalgorithm
PPTX
12_Graph.pptx
PPTX
Optimisation random graph presentation
PPT
Dijkstra algorithm ds 57612334t4t44.ppt
PPT
Dijkstra Shortest Path Algorithm in Network.ppt
PDF
Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm
PPT
35 dijkstras alg
PPT
Expander Graph and application_tutorial_June2010.ppt
PDF
Chap10 slides
DOC
algorithm Unit 3
PPTX
Lecture warshall floyd
ODP
parameterized complexity for graph Motif
DOC
Unit 3 daa
DAA_Presentation - Copy.pptx
2.6 all pairsshortestpath
Lec-35Graph - Graph - Copy in Data Structure
All pairs shortest path algorithm
Dijkstra's algorithm presentation
Metric dimesion of circulsnt graphs
Unit26 shortest pathalgorithm
12_Graph.pptx
Optimisation random graph presentation
Dijkstra algorithm ds 57612334t4t44.ppt
Dijkstra Shortest Path Algorithm in Network.ppt
Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm
35 dijkstras alg
Expander Graph and application_tutorial_June2010.ppt
Chap10 slides
algorithm Unit 3
Lecture warshall floyd
parameterized complexity for graph Motif
Unit 3 daa
Ad

Recently uploaded (20)

PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
KodekX | Application Modernization Development
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
Cloud computing and distributed systems.
PDF
Encapsulation theory and applications.pdf
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
Big Data Technologies - Introduction.pptx
PPT
Teaching material agriculture food technology
PDF
cuic standard and advanced reporting.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Electronic commerce courselecture one. Pdf
PDF
Machine learning based COVID-19 study performance prediction
PDF
Approach and Philosophy of On baking technology
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
Mobile App Security Testing_ A Comprehensive Guide.pdf
Review of recent advances in non-invasive hemoglobin estimation
Dropbox Q2 2025 Financial Results & Investor Presentation
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
KodekX | Application Modernization Development
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
The AUB Centre for AI in Media Proposal.docx
Cloud computing and distributed systems.
Encapsulation theory and applications.pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
Big Data Technologies - Introduction.pptx
Teaching material agriculture food technology
cuic standard and advanced reporting.pdf
Encapsulation_ Review paper, used for researhc scholars
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Electronic commerce courselecture one. Pdf
Machine learning based COVID-19 study performance prediction
Approach and Philosophy of On baking technology
MYSQL Presentation for SQL database connectivity
Diabetes mellitus diagnosis method based random forest with bat algorithm

Randomized algorithms all pairs shortest path

  • 1. Randomized Algorithms: All Pairs Shortest Path Instructor: Dr. S. N. Hashemi Lecturer: Mohammad Akbarizadeh m.akbarizadeh@aut.ac.ir
  • 2. Outline ● Deterministic Algorithms ○ Single Source Shortest Path ■ Dijkstra ■ Bellman-Ford ○ All Pairs Shortest path ■ Floyd-Warshall ● Randomized Algorithm ○ All Pair Distance problem ○ Boolean Product Witness Matrix ○ All Pair Shortest Path ● Comparison 2
  • 4. Question Q: How to solve the single source shortest path problem if the given graph is unweighted? What is the time complexity? 4
  • 5. Question Q: How to solve the single source shortest path problem if the given graph is unweighted? What is the time complexity? A: We can easily apply BFS to the graph and solve it in O(V+E) 5
  • 6. Dijkstra Algorithm ● Given a graph and a source vertex, find shortest paths from source to all vertices in the given graph ● Dijkstra is a greedy algorithm for finding the shortest path from a node which ends in having a tree with the lowest total cost (similar algorithms: Prim, Kruskal) ● It can be applied to both directed and undirected graph ● Dijkstra doesn’t work for Graphs with negative weight edges ● Time complexity: O(V2) 6
  • 8. Dijkstra (example) Graph = {(0,1)=4, (0,7)=8, (1,2)=8, (1,7)=11, (7,8)=7, (7,6)=1, (2,3)=7, (2,5)=4, (2,8)=2, (8,6)=6, (6,5)=2, (3,4)=9, (3,5)=14, (5,4)=10} Distance = {INF, INF, INF, INF, INF, INF, INF, INF, INF} Prev = {UND, UND, UND, UND, UND, UND, UND, UND, UND} Q = {0, 1, 2, 3, 4, 5, 6, 7, 8} 8
  • 9. Dijkstra (example) Graph = {(0,1)=4, (0,7)=8, (1,2)=8, (1,7)=11, (7,8)=7, (7,6)=1, (2,3)=7, (2,5)=4, (2,8)=2, (8,6)=6, (6,5)=2, (3,4)=9, (3,5)=14, (5,4)=10} Distance = {0, 4, INF, INF, INF, INF, INF, 8, INF} Prev = {UND, 0, UND, UND, UND, UND, UND, 0, UND} Q = {1, 2, 3, 4, 5, 6, 7, 8} 9
  • 10. Dijkstra (example) Graph = {(0,1)=4, (0,7)=8, (1,2)=8, (1,7)=11, (7,8)=7, (7,6)=1, (2,3)=7, (2,5)=4, (2,8)=2, (8,6)=6, (6,5)=2, (3,4)=9, (3,5)=14, (5,4)=10} Distance = {0, 4, 12, INF, INF, INF, INF, 8, INF} Prev = {UND, 0, 1, UND, UND, UND, UND, 0, UND} Q = {2, 3, 4, 5, 6, 7, 8} 10
  • 11. Dijkstra (example) Graph = {(0,1)=4, (0,7)=8, (1,2)=8, (1,7)=11, (7,8)=7, (7,6)=1, (2,3)=7, (2,5)=4, (2,8)=2, (8,6)=6, (6,5)=2, (3,4)=9, (3,5)=14, (5,4)=10} Distance = {0, 4, 12, INF, INF, INF, 9, 8, 15} Prev = {UND, 0, 1, UND, UND, UND, 7, 0, 7} Q = {2, 3, 4, 5, 6, 8} 11
  • 12. Dijkstra (example) Graph = {(0,1)=4, (0,7)=8, (1,2)=8, (1,7)=11, (7,8)=7, (7,6)=1, (2,3)=7, (2,5)=4, (2,8)=2, (8,6)=6, (6,5)=2, (3,4)=9, (3,5)=14, (5,4)=10} Distance = {0, 4, 12, INF, INF, 11, 9, 8, 15} Prev = {UND, 0, 1, UND, UND, 6, 7, 0, 6} Q = {2, 3, 4, 5, 8} 12
  • 13. Dijkstra (example) Graph = {(0,1)=4, (0,7)=8, (1,2)=8, (1,7)=11, (7,8)=7, (7,6)=1, (2,3)=7, (2,5)=4, (2,8)=2, (8,6)=6, (6,5)=2, (3,4)=9, (3,5)=14, (5,4)=10} Distance = {0, 4, 12, 19, 21, 11, 9, 8, 14} Prev = {UND, 0, 1, 2, 5, 6, 7, 0, 2} Q = {} 13
  • 14. Dijkstra (Question) Q: What if one of the weights was zero? 14
  • 15. Dijkstra (Question) Q: What if one of the weights was zero? A: No problem for Dijkstra algorithm. The answer will be correct if we run dijkstra on the graph but we can merge two nodes can be merged 15
  • 16. Dijkstra (Question) Q: What if one of the weights was zero? A: No problem for Dijkstra algorithm. The answer will be correct if we run dijkstra on the graph but we can merge two nodes can be merged Q: What if the graph is not connected? What happens to the weights? 16
  • 17. Dijkstra (Question) Q: What if one of the weights was zero? A: No problem for Dijkstra algorithm. The answer will be correct if we run dijkstra on the graph but we can merge two nodes can be merged Q: What if the graph is not connected? What happens to the weights? A: Nodes that are not reachable from the source will have INF value and the algorithm will not proceed 17
  • 18. Bellman-Ford Algorithm ● Given a graph and a source vertex src in graph, find shortest paths from src to all vertices in the given graph. ● This algorithm works with graphs having negative weight edges ● Greedy algorithm but simpler than Dijkstra and uses relaxation like Dijkstra ● It relaxes all the edges at most |V|-1 times until it finds the answer (no change happens after update) ● Time complexity: O(VE) 18
  • 20. Bellman-Ford Algorithm (example) 20 G = { (A,B)=-1, (A,C)=4, (B,C)=3, (D,C)=5, (B,D)=2, (D,B)=1, (B,E)=2, (E,D)=-3 } S = A Process order: (B, E), (D, B), (B, D), (A, B), (A, C), (D, C), (B, C), (E, D)
  • 21. Bellman-Ford Algorithm (example) 21 G = { (A,B)=-1, (A,C)=4, (B,C)=3, (D,C)=5, (B,D)=2, (D,B)=1, (B,E)=2, (E,D)=-3 } S = A Process order: (B, E), (D, B), (B, D), (A, B), (A, C), (D, C), (B, C), (E, D)
  • 22. Bellman-Ford Algorithm (example) 22 G = { (A,B)=-1, (A,C)=4, (B,C)=3, (D,C)=5, (B,D)=2, (D,B)=1, (B,E)=2, (E,D)=-3 } S = A Process order: (B, E), (D, B), (B, D), (A, B), (A, C), (D, C), (B, C), (E, D)
  • 23. Bellman-Ford Algorithm (Question) 23 Q = What happens if (B,D)=-2 and (D,B)=-1 ?
  • 24. Bellman-Ford Algorithm (Question) 24 Q = What happens if (B,D)=-2 and (D,B)=-1 ? A = The algorithm gets stuck in a loop and updates the weights until it reaches the end of the algorithm (Algorithm won't work in this situation) Process order: (B, E), (D, B), (B, D), (A, B), (A, C), (D, C), (B, C), (E, D)
  • 25. Bellman-Ford Algorithm (Question) 25 Q = When does the worst case scenario happens? What is the worst case scenario for this algorithm?
  • 26. Bellman-Ford Algorithm (Question) 26 Q = When does the worst case scenario happens? What is the worst case scenario for this algorithm? A = When we a complete graph like K5 we will have V(V-1)/2 edges which have to processed V times, which has O(V3) time complexity
  • 28. Floyd-Warshall Algorithm ● Given a graph, finds the shortest paths from all vertices to all other vertices ● Uses Dynamic Programming to solve the problem ● Edge weights can be negative (but no negative cycles) and graph can be directed ● Time complexity: O(V3) 28
  • 41. Randomized Algorithm ● A Las Vegas algorithm is a randomized algorithm that always gives the correct result but gambles with resources. ● Monte Carlo simulations are a broad class of algorithms that use repeated random sampling to obtain numerical results. ○ Monte Carlo simulations are typically used to simulate the behaviour of other systems. ○ Monte Carlo algorithms, on the other hand, are randomized algorithms whose output may be incorrect with a certain, typically small, probability. 41
  • 42. How to solve APSP problem using Randomized algorithms? ● We will show that the problem of computing all-pairs shortest path is reducible, via a randomized reduction, to the problem of multiplying two integer matrices ● First we solve the easier version of problem for unweighted graph and then get to solve the weighted graph 42
  • 43. Randomized Algorithm All Pair Distance Problem
  • 44. All Pair Distance problem( APD problem) ● Given an unweighted graph G and its adjacency matrix A, compute the distance matrix D ● Let G’(V,E’) be a graph obtained from G(V,E) by placing an edge between every pair of vertices i≠j ∊V that are at distance 1 or 2 in G ● The graph G is a subgraph of G’, and we could view G’ as the square of the graph G ● Lemma 1: If we compute Z=A2, then there is path of length 2 in G between pair of vertices i and j if and only if Zij>0. Further, the value of Zij indicates the number of distinct length 2 paths between i and j ● If we compute Z=A3, we can obtain if there is a path of length 3 between i and j in G if and only if Zij>0 44
  • 45. All Pair Distance problem( APD problem) 45 G = {(1,2), (2,3), (2,4), (3,4)}
  • 46. All Pair Distance problem( APD problem) 46 G = {(1,2), (2,3), (2,4), (3,4)} A = [[ 0, 1, 0, 0], [ 1, 0, 1, 1], [0, 1, 0, 1], [0, 1, 1, 0]]
  • 47. All Pair Distance problem( APD problem) 47 G = {(1,2), (2,3), (2,4), (3,4)} A = [[ 0, 1, 0, 0], [ 1, 0, 1, 1], [0, 1, 0, 1], [0, 1, 1, 0]] A2= [[1, 0, 1, 1], [0, 3, 1, 1], [1, 1, 2, 1], [1, 1, 1, 2]]
  • 48. All Pair Distance problem( APD problem) ● If given graph G with adjacency matrix A and distance matrix D and we compute Z=G2 with adjacency matrix A’ and distance matrix D’ ● To ensure A’ has a zero diagonal, we compute it by setting A’ ij=1 if and only if i≠j and at least one of A’ ij or Aij is non-zero ● We can observe G’ is complete if and only if G has diameter at most 2, where diameter of a graph is the maximum shortest path length over all pairs of vertices ● is there any way we could compute D’ from D? 48
  • 49. All Pair Distance problem( APD problem) ● Observation: if G has a diameter at most 2, then G’ is complete and in D=2A’-A ● This can be obtained from A and A’ in time O(n2) ● Lemma 2: consider any pair of vertices i,j ∊V ○ If Dij is even then Dij=2D’ ij ○ If Dij is odd then Dij=2D’ ij - 1 ● By using this lemma we can recursively compute distance matrix of graph G from distance matrix of graph G’ ● But how can we compute the parities of the shortest path lengths? 49
  • 50. All Pair Distance problem( APD problem) ● Lemma 3: consider any pair of distinct vertices i and j in G ○ For any neighbor k of i, Dij- 1 ≤ Dkj ≤ Dij+ 1 ○ There exists a neighbor k of i such that Dkj = Dij - 1 ● Now using this lemma we can have a structural property to compute the parities of the shortest path lengths ● Lemma 4: consider any pair of distinct vertices i and j in G: ○ If Dij is even, then D’ kj ≥ D’ ij for every neighbor k of i in G ○ If Dij is odd, then D’ kj ≤ D’ ij for every neighbor k of i in G. moreover, there exists a neighbor k of i in G such that D’ kj < D’ ij 50
  • 51. All Pair Distance problem( APD problem) ● Let Γ(i) denote the set of neighbors of i in G and d(i) be the degree of i. Note that A’ ii=d(i) ● By summing the inequalities in lemma 4 over the neighbors of vertex i we can obtain the following result: ● Lemma 5: consider any pair of distinct vertices i and j in G: ○ Dij is even if and only if ∑k∊Γ(i) D’ kj ≥ D’ ijd(i) ○ Dij is odd if and only if ∑k∊Γ(i) D’ kj < D’ ijd(i) ● In this lemma matrix multiplication is used to compute: ○ ∑k∊Γ(i) D’ kj= ∑n k=1 Aik D’ kj = Sij 51
  • 52. All Pair Distance problem( APD problem) 52
  • 53. All Pair Distance problem( APD problem) 53 ● The APD algorithm computes the distance matrix for an n-vertex graph G in time O(MM(n) log n) using integer matrix multiplication ● MM(n) indicates matrix multiplication of square matrices with length n ● The ordinary matrix multiplication algorithm has time complexity of O(n3) and currently the best time complexity has time complexity O(n2.373)
  • 54. Randomized Algorithm Boolean Product Witness Matrix(BPWM)
  • 55. Boolean Product Witness Matrix(BPWM) 55 ● Suppose A and B are n*n boolean(0,1) matrices and P=AB is their product under boolean matrix multiplication ● A witness for Pij is an index k∊{1, ..., n} such that Aik = Bkj = 1 ● Observe that Pij=1 if and only if it has some witness k ● A Boolean Product Witness Matrix(BPWM) for P is an integer matrix W such that each entry Wij contains a witness k for Pij if any, and is 0 if there is no such witness ● There could be as many as n witnesses for each entry in p ● The integer matrix multiplication of A and B, treating their entries as the integer 0 and 1, yields a matrix C whose entry Cij corresponds exactly to the number of witnesses for boolean matrix entry Pij
  • 56. Boolean Product Witness Matrix(BPWM) 56 G = {(1,2), (1,6), (2,3), (2,4), (2,5), (2,6), (3,4), (4,5), (5,6)} A =[[0, 1, 0, 0, 0, 1], [1, 0, 1, 1, 1, 1], [0, 1, 0, 1, 0, 0], [0, 1, 1, 0, 1, 0], [0, 1, 0, 1, 0, 1], [1, 1, 0, 0, 1, 0]] A2=[[2, 1, 1, 1, 2, 1], [1, 5, 1, 2, 2, 2], [1, 1, 2, 1, 2, 1], [1, 2, 1, 3, 1, 2], [2, 2, 2, 1, 3, 1], [1, 2, 1, 2, 1, 3]]
  • 57. Boolean Product Witness Matrix(BPWM) 57 ● If there is a unique witness for each vertex there is no problem with BPWM but if there are many witnesses we can use some randomness ● Lemma 6: suppose an urn contains n balls of which w are white, and n-w are black. Consider choosing r balls at random(without replacement), where n/2 ≤ wr ≤ n then: ○ PR[exactly one white ball is chosen] ≥ 1/2e
  • 58. Boolean Product Witness Matrix(BPWM) 58 Consider we have n=100 balls W = 4 balls are white n/2 <= 4r <=n 50 <= 4r <= 100 13 <=r <= 25
  • 59. Boolean Product Witness Matrix(BPWM) 60 ● So what is the usage of this usage? ● Assume we have set R which contains unique witness for Pij ● R is represented as incidence vector that has Rk=1 for k∈R and Rk=0 for k∉R ● Let AR be the matrix obtained from A by setting AR ik=kRkAik ● Let BR be the matrix obtained from B by setting BR kj=RkBkj ● So if the entry Pij has a unique witness in the set R, corresponding entry in the integer matrix multiplication of AR and BR is the index of this unique witness
  • 60. Boolean Product Witness Matrix(BPWM) 61 ● A key point is that the product of AR and BR yields witnesses for all entries in P that have a unique witness in R ● By lemma 6 there is a constant probability that a random set R of size r has a unique witness for an entry in P with w witness where n/2r ≤ w ≤ n/r ● Repeating this for O(log n) independent choices of R makes it extremely unlikely that witnesses are not identified for such entries in P ● Combining the thoughts above results the following algorithm
  • 61. Boolean Product Witness Matrix(BPWM) 62
  • 62. Boolean Product Witness Matrix(BPWM) 63 ● The BPWM algorithm is Las Vegas algorithm for BPWM problem with expected running time O(MM(n) log2 n) ● Now we use APD and BPWM algorithm to solve APSP problem
  • 64. All Pair Shortest Path(randomized) 65 ● We define a successor matrix S for an n-vertex graph G is and n*n matrix such that Sij is the index k of a neighbor of vertex i that lies on a shortest path from i to j ● Fact: The successor entry for Sij=k iff ○ Dij=d ○ Dik=1 ○ Dkj=d-1 ○ Aik=1 ● Let Bd denote the n*n boolean matrix in which Bd kj=1 iff Dkj=d-1 ● We can compute Bd from D in O(n2) time ● The problem is that this process must be repeated for n different values of d, leading to super-cubic algorithm which is not good!
  • 65. All Pair Shortest Path(randomized) 66
  • 66. All Pair Shortest Path(randomized) 67 ● The APSP computes the successor matrix for and n-vertex graph G in expected time O(MM(n) log2n)
  • 68. Comparison Deterministic algorithms: Dijkstra+Johnson: O(EV + V2 log V) Floyd–Warshall algorithm: O(V3) Randomized Algorithm: APSP: O(n2.373 log2 n) 69
  • 69. References 1. Prof. Eric Price, CS 388R Randomized Algorithms, Fall 2019, University of Texas ( available at : https://www.cs.utexas.edu/~ecprice/courses/randomized/ ) 2. David Karger. 6.856J Randomized Algorithms. Fall 2002. Massachusetts Institute of Technology: MIT OpenCourseWare ( available at : https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-856j-randomized-algorithms-fall- 2002/index.htm ) 3. Motwani, R., & Raghavan, P. (1995). Randomized Algorithms. Cambridge: Cambridge University Press. doi:10.1017/CBO9780511814075 4. https://en.wikipedia.org/wiki/Shortest_path_problem#Single-source_shortest_paths 5. https://www.geeksforgeeks.org/dijkstras-shortest-path-algorithm-greedy-algo-7/ 6. https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm 7. https://www.youtube.com/watch?v=FtN3BYH2Zes 8. https://www.youtube.com/watch?v=oNI0rf2P9gE 9. https://www.youtube.com/watch?v=XB4MIexjvY0 10. https://www.geeksforgeeks.org/bellman-ford-algorithm-dp-23/ 11. https://stackoverflow.com/questions/49460439/can-dijkstras-algorithm-work-on-a-graph-with-weights-of-0 12. https://www.programiz.com/dsa/bellman-ford-algorithm 70
  • 70. References 12. Algorithms course notes, University of Illinois,(available at: https://jeffe.cs.illinois.edu/teaching/algorithms/book/09- apsp.pdf ) 13. https://www.gatevidyalay.com/floyd-warshall-algorithm-shortest-path-algorithm/ 14. https://www.programiz.com/dsa/floyd-warshall-algorithm 15. https://yourbasic.org/algorithms/las-vegas/ 16. (44) Lec 26: All pair shortest path-I - YouTube 17. (44) Lec 27: All pair shortest path-II - YouTube 71
  • 71. exercises 1. Prover lemma 4 and 6 2. Explain and prove time complexity of APD and BPWM algorithm 3. (Bonus) Implement this algorithm and compare its performance with deterministic algorithms on multiple graphs with different sizes of V and E 72