It is highly recommended to read Dijkstra’s algorithm using the Priority Queue first.
Widest Path Problem is a problem of finding a path between two vertices of the graph maximizing the weight of the minimum-weight edge in the path. See the below image to get the idea of the problem:
Practical Application Example:
This problem is a famous variant of Dijkstra’s algorithm. In the practical application, this problem can be seen as a graph with routers as its vertices and edges represent bandwidth between two vertices. Now if we want to find the maximum bandwidth path between two places in the internet connection, then this problem can be solved by this algorithm.
How to solve this problem?
We are going to solve this problem by using the priority queue ((|E|+|V|)log|V|) implementation of the Dijkstra’s algorithm with a slight change.
We solve this problem by just replacing the condition of relaxation in Dijkstra’s algorithm by:
max(min(widest_dist[u], weight(u, v)), widest_dist[v])
where u is the source vertex for v. v is the current vertex we are checking the condition.
This algorithm runs for both directed and undirected graph.
See the series of images below to get the idea about the problem and the algorithm:
The values over the edges represents weights of directed edges.
We will start from source vertex and then travel all the vertex connected to it and add in priority queue according to relaxation condition.
Now (2, 1) will pop up and 2 will be the current source vertex.
Now (3, 1) will pop from the queue. But as 3 does not have any connected vertex through directed edge nothing will happen. So (4, 2) will pop next.
Finally the algorithm stops, as there is no more elements in priority queue.
The path with the maximum value of widest distance is 1-4-3 which has the maximum bottle-neck value of 2. So we end up getting widest distance of 2 to reach the target vertex 3.
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with industry experts, please refer Geeks Classes Live