Given an undirected graph G = (V, E) with no negative weights. What is the complexity for checking uniqueness of shortest path for every vertex in the given graph?
Dijkstra's algorithm finds the shortest path between two vertices in a graph. It can also be used to generate a Shortest Path Tree - which will be the shortest path to all vertices in the graph (from a given source vertex).
Dijkstra's algorithm Dijkstra in 1956. It is used to find the shortest path between a node/vertex (source node) to any (or every) other nodes/vertices (destination nodes) in a graph.
The Floyd Warshall Algorithm is for solving all pairs shortest path problems. The problem is to find the shortest distances between every pair of vertices in a given edge-weighted directed Graph.
You can easily modify shortest path algorithms to find number of shortest paths too. For instance consider this Dijkstra code:
def dijkstra(self, source, dest):
assert source in self.vertices
dist = {vertex: inf for vertex in self.vertices}
previous = {vertex: None for vertex in self.vertices}
dist[source] = 0
q = self.vertices.copy()
neighbours = {vertex: set() for vertex in self.vertices}
for start, end, cost in self.edges:
neighbours[start].add((end, cost))
while q:
u = min(q, key=lambda vertex: dist[vertex])
q.remove(u)
if dist[u] == inf or u == dest:
break
for v, cost in neighbours[u]:
alt = dist[u] + cost
if alt < dist[v]: # Relax (u,v,a)
dist[v] = alt
previous[v] = u
We add another list to store the number of shortest path to each node.
num_path = {vertex: 0 for vertex in self.vertices}
Then in the relaxing stage, instead of checking whether the new distance (alt
) is less than previous distance, we check if it is equal too. If it is equal we increment the number of shortest paths for that node:
if alt == dist[v]:
num_path[v] += 1
When we find a new shortest for a node, the number of shortest path for the new node is equal to the number of shortest path of its parent:
if alt < distance:
num_path[v] = num_path[u]
...
So in the end if num_path[v]==1
then we can conclude that there is a unique shortest path from source
to v
.
Here is the final code:
def dijkstra(self, source, dest):
assert source in self.vertices
dist = {vertex: inf for vertex in self.vertices}
previous = {vertex: None for vertex in self.vertices}
num_path = {vertex: 0 for vertex in self.vertices}
dist[source] = 0
num_path[source] = 1
q = self.vertices.copy()
neighbours = {vertex: set() for vertex in self.vertices}
for start, end, cost in self.edges:
neighbours[start].add((end, cost))
while q:
u = min(q, key=lambda vertex: dist[vertex])
q.remove(u)
if dist[u] == inf or u == dest:
break
for v, cost in neighbours[u]:
alt = dist[u] + cost
if alt < dist[v]: # Relax (u,v,a)
dist[v] = alt
previous[v] = u
num_path[v] = num_path[u]
elif alt == dist[v]:
num_path[v] += 1
So the complexity will be equal to the complexity of your shortest path algorithm.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With