I am looking for a way to augment the BFS method used to find the single source shortest paths in an unweighted directed graph and solve the above problem in O(N+M) time. where N is the number of vertices, M is the number of edges
I have thought the following:
Contract the vertices of the graph that have an edge weight 0 between them. But this would definitely be wrong as then I would be changing the graph's properties and adding new edges to vertices that originally had none.
Changing the edge weights to 1 and 2. And then creating dummy vertices in the paths that are of length 2 to convert those edges to edges of weight 1. But this would give the wrong answer.
In more generality, how can I find single source shortest paths in a directed graph when the edge weights are between 0 and MAX in linear time. (MAX is the maximum edge weight)
O((n + m) log n)
with Dijkstra using a binary heap as priority queue. IfMAX
is really small, you could use buckets and a y-fast trie to implement the prio queue, resulting inO(n)
space and aO((n + m) * log log (n * MAX))
time (no idea how fast that would be in practice, but I'd really like to know :D) – Niklas B.