SlideShare a Scribd company logo
1 of 71
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

What's hot

Gate level design -For beginners
Gate level design -For beginnersGate level design -For beginners
Gate level design -For beginnersDr.YNM
 
Pattern matching
Pattern matchingPattern matching
Pattern matchingshravs_188
 
Decision properties of reular languages
Decision properties of reular languagesDecision properties of reular languages
Decision properties of reular languagesSOMNATHMORE2
 
CMACs and MACS based on block ciphers, Digital signature
CMACs and MACS based on block ciphers, Digital signatureCMACs and MACS based on block ciphers, Digital signature
CMACs and MACS based on block ciphers, Digital signatureAdarsh Patel
 
Code optimization in compiler design
Code optimization in compiler designCode optimization in compiler design
Code optimization in compiler designKuppusamy P
 
Presentation on Shared Memory Parallel Programming
Presentation on Shared Memory Parallel ProgrammingPresentation on Shared Memory Parallel Programming
Presentation on Shared Memory Parallel ProgrammingVengada Karthik Rangaraju
 
Uncertain Knowledge and Reasoning in Artificial Intelligence
Uncertain Knowledge and Reasoning in Artificial IntelligenceUncertain Knowledge and Reasoning in Artificial Intelligence
Uncertain Knowledge and Reasoning in Artificial IntelligenceExperfy
 
Design Principles for Connected Devices
Design Principles for Connected DevicesDesign Principles for Connected Devices
Design Principles for Connected DevicesVikram Nandini
 
Automatic keyword extraction.pptx
Automatic keyword extraction.pptxAutomatic keyword extraction.pptx
Automatic keyword extraction.pptxBiswarupDas18
 
Implementation of Soft-core processor on FPGA (Final Presentation)
Implementation of Soft-core processor on FPGA (Final Presentation)Implementation of Soft-core processor on FPGA (Final Presentation)
Implementation of Soft-core processor on FPGA (Final Presentation)Deepak Kumar
 
Types of grammer - TOC
Types of grammer - TOCTypes of grammer - TOC
Types of grammer - TOCAbhayDhupar
 
Communication costs in parallel machines
Communication costs in parallel machinesCommunication costs in parallel machines
Communication costs in parallel machinesSyed Zaid Irshad
 
System Verilog (Tutorial -- 4X1 Multiplexer)
System Verilog (Tutorial -- 4X1 Multiplexer)System Verilog (Tutorial -- 4X1 Multiplexer)
System Verilog (Tutorial -- 4X1 Multiplexer)Denise Wilson
 
Resolution method in AI.pptx
Resolution method in AI.pptxResolution method in AI.pptx
Resolution method in AI.pptxAbdullah251975
 

What's hot (20)

Randomized algorithms ver 1.0
Randomized algorithms ver 1.0Randomized algorithms ver 1.0
Randomized algorithms ver 1.0
 
Gate level design -For beginners
Gate level design -For beginnersGate level design -For beginners
Gate level design -For beginners
 
Pattern matching
Pattern matchingPattern matching
Pattern matching
 
Asn unit 1
Asn unit 1Asn unit 1
Asn unit 1
 
Decision properties of reular languages
Decision properties of reular languagesDecision properties of reular languages
Decision properties of reular languages
 
Computer networks
Computer networksComputer networks
Computer networks
 
CMACs and MACS based on block ciphers, Digital signature
CMACs and MACS based on block ciphers, Digital signatureCMACs and MACS based on block ciphers, Digital signature
CMACs and MACS based on block ciphers, Digital signature
 
Code optimization in compiler design
Code optimization in compiler designCode optimization in compiler design
Code optimization in compiler design
 
Presentation on Shared Memory Parallel Programming
Presentation on Shared Memory Parallel ProgrammingPresentation on Shared Memory Parallel Programming
Presentation on Shared Memory Parallel Programming
 
NLP
NLPNLP
NLP
 
Channel routing
Channel routingChannel routing
Channel routing
 
SINGLE-SOURCE SHORTEST PATHS
SINGLE-SOURCE SHORTEST PATHS SINGLE-SOURCE SHORTEST PATHS
SINGLE-SOURCE SHORTEST PATHS
 
Uncertain Knowledge and Reasoning in Artificial Intelligence
Uncertain Knowledge and Reasoning in Artificial IntelligenceUncertain Knowledge and Reasoning in Artificial Intelligence
Uncertain Knowledge and Reasoning in Artificial Intelligence
 
Design Principles for Connected Devices
Design Principles for Connected DevicesDesign Principles for Connected Devices
Design Principles for Connected Devices
 
Automatic keyword extraction.pptx
Automatic keyword extraction.pptxAutomatic keyword extraction.pptx
Automatic keyword extraction.pptx
 
Implementation of Soft-core processor on FPGA (Final Presentation)
Implementation of Soft-core processor on FPGA (Final Presentation)Implementation of Soft-core processor on FPGA (Final Presentation)
Implementation of Soft-core processor on FPGA (Final Presentation)
 
Types of grammer - TOC
Types of grammer - TOCTypes of grammer - TOC
Types of grammer - TOC
 
Communication costs in parallel machines
Communication costs in parallel machinesCommunication costs in parallel machines
Communication costs in parallel machines
 
System Verilog (Tutorial -- 4X1 Multiplexer)
System Verilog (Tutorial -- 4X1 Multiplexer)System Verilog (Tutorial -- 4X1 Multiplexer)
System Verilog (Tutorial -- 4X1 Multiplexer)
 
Resolution method in AI.pptx
Resolution method in AI.pptxResolution method in AI.pptx
Resolution method in AI.pptx
 

Similar to Randomized algorithms all pairs shortest path

2.6 all pairsshortestpath
2.6 all pairsshortestpath2.6 all pairsshortestpath
2.6 all pairsshortestpathKrish_ver2
 
All pairs shortest path algorithm
All pairs shortest path algorithmAll pairs shortest path algorithm
All pairs shortest path algorithmSrikrishnan Suresh
 
Dijkstra's algorithm presentation
Dijkstra's algorithm presentationDijkstra's algorithm presentation
Dijkstra's algorithm presentationSubid Biswas
 
Metric dimesion of circulsnt graphs
Metric dimesion of circulsnt graphsMetric dimesion of circulsnt graphs
Metric dimesion of circulsnt graphsAmna Abunamous
 
Unit26 shortest pathalgorithm
Unit26 shortest pathalgorithmUnit26 shortest pathalgorithm
Unit26 shortest pathalgorithmmeisamstar
 
Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm
Bellman-Ford-Moore Algorithm and Dijkstra’s AlgorithmBellman-Ford-Moore Algorithm and Dijkstra’s Algorithm
Bellman-Ford-Moore Algorithm and Dijkstra’s AlgorithmFulvio Corno
 
35 dijkstras alg
35 dijkstras alg35 dijkstras alg
35 dijkstras algdouglaslyon
 
Chap10 slides
Chap10 slidesChap10 slides
Chap10 slidesHJ DS
 
Lecture warshall floyd
Lecture warshall floydLecture warshall floyd
Lecture warshall floydDivya Ks
 
parameterized complexity for graph Motif
parameterized complexity for graph Motifparameterized complexity for graph Motif
parameterized complexity for graph MotifAMR koura
 
single source shorest path
single source shorest pathsingle source shorest path
single source shorest pathsowfi
 

Similar to Randomized algorithms all pairs shortest path (20)

DAA_Presentation - Copy.pptx
DAA_Presentation - Copy.pptxDAA_Presentation - Copy.pptx
DAA_Presentation - Copy.pptx
 
2.6 all pairsshortestpath
2.6 all pairsshortestpath2.6 all pairsshortestpath
2.6 all pairsshortestpath
 
All pairs shortest path algorithm
All pairs shortest path algorithmAll pairs shortest path algorithm
All pairs shortest path algorithm
 
Dijkstra's algorithm presentation
Dijkstra's algorithm presentationDijkstra's algorithm presentation
Dijkstra's algorithm presentation
 
Metric dimesion of circulsnt graphs
Metric dimesion of circulsnt graphsMetric dimesion of circulsnt graphs
Metric dimesion of circulsnt graphs
 
Shortest Path in Graph
Shortest Path in GraphShortest Path in Graph
Shortest Path in Graph
 
Unit26 shortest pathalgorithm
Unit26 shortest pathalgorithmUnit26 shortest pathalgorithm
Unit26 shortest pathalgorithm
 
CSE633
CSE633CSE633
CSE633
 
12_Graph.pptx
12_Graph.pptx12_Graph.pptx
12_Graph.pptx
 
Optimisation random graph presentation
Optimisation random graph presentationOptimisation random graph presentation
Optimisation random graph presentation
 
Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm
Bellman-Ford-Moore Algorithm and Dijkstra’s AlgorithmBellman-Ford-Moore Algorithm and Dijkstra’s Algorithm
Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm
 
35 dijkstras alg
35 dijkstras alg35 dijkstras alg
35 dijkstras alg
 
Chap10 slides
Chap10 slidesChap10 slides
Chap10 slides
 
algorithm Unit 3
algorithm Unit 3algorithm Unit 3
algorithm Unit 3
 
Lecture warshall floyd
Lecture warshall floydLecture warshall floyd
Lecture warshall floyd
 
parameterized complexity for graph Motif
parameterized complexity for graph Motifparameterized complexity for graph Motif
parameterized complexity for graph Motif
 
Unit 3 daa
Unit 3 daaUnit 3 daa
Unit 3 daa
 
Spsp fw
Spsp fwSpsp fw
Spsp fw
 
Network Theory
Network TheoryNetwork Theory
Network Theory
 
single source shorest path
single source shorest pathsingle source shorest path
single source shorest path
 

Recently uploaded

Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsHyundai Motor Group
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetEnjoy Anytime
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Hyundai Motor Group
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 

Recently uploaded (20)

The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 

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