Sunday 19 November 2017

ডায়াক্সট্রা(Dijkstra):

Dijkstra is a shortest path finding algorithm like BFS. BFS works only when the graph is unweighted.But if the graph is weighted, BFS doesn't work correctly. If the edges of a graph is non-negative and weighted, shortest path of the graph can be found by Dijkstra algorithm.

Motivating Problem:Given a weighted graph G and a starting source vertex s,what are the shortest paths from s to the other vertices of G?


Before knowing the details about Dijkstra algorithm, you need to know about path relaxation.
Relaxation: Suppose,there is  a place named Chawlkbazar where you want to go. Somehow(say,CUET->Oxyzen->GEC->New market->Chawlkbazar) you can reach Chawlkbazar from CUET by 30 minutes i.e distance[Chawlkbazar]=30.Now imagine a place named Bohaddorhat, which can be reached from CUET by 10 minutes i.e distance[Bohaddorhat]=10.Suppose, Chawlkbazar can be reached by 5 minutes from Bohaddorhat.Then we can see:

   distance[Bohaddorhat]+(time to reach from Bohaddorhat to Chawlkbazar)<distance[Chawlkbazar]

So,we can reset the cost for Chawlkbazar as:
   distance[Chawlkbazar]=distance[Bohaddorhat]+time to reach from Bohaddorhat to Chawlkbazar
This is the concept of path relaxation.If you have understood this,you have learned Dijkstra(almost😉)

Implementation: This is a greedy algorithm. It maintains a set S vertices whose final shortest path weights from source s have already been determined. The algorithm repeatedly selects a vertex u with estimated minimum cost and relaxes it. This can be implemented in many ways. Here,we'll use priority_queue.

Algorithm:
    1. Initially,
        (i) Set the distance to all vertices to be inf (a large number).
        (ii) Set distance[source]=0.

    2. Repeat the following process from source to vertex.
        (i) From the current vertex u(with minimum distance),relax all the neighbors of u. Relax(u,v).
        (ii) Set distance[v]=min(distance[v],distance[u]+cost(u,v)).

    3. Vertex u is now done and will not be visited again.Now,we'll greedily replace u with a unvisited
         vertex x with minimum distance distance[x].

Complexity: Dijkstra Algorithm's running time resembles both BFS and Prim's Algorithm for computing minimum spanning trees(O(log(V+E))). But here, O(log V) complexity will be added to sort the priority_queue every time. So, the overall complexity will be O(V log V+E).

References: 
    1. Introduction to Algorithm by Thomas H. Cormen.
    2. Competitive Programming by Steven & Felix Halim.
    3. Shafayet's Blog

Sample Code: Here is the sample code of dijkstra with path printing.

Related Problems and solutions:
    1. LightOj 1002 - Country Roads
        Solution Idea: pretty straightforward.just try to figure out the meaning of this line "The cost is defined by the cost of the maximum road you have used to go to my city.".That means the shortest path will be the one where maximum cost(among one/more than one costs) is minimum.
        Solution: https://ideone.com/UYLvoi (See at your own risk👎)

    2. LightOj 1019 - Brush (V)
        Solution Idea: Direct implementation of Dijkstra algorithm.
        Solutionhttps://ideone.com/gSyCyh (See at your own risk👎)

    3. LightOj 1174 - Commandos
        Solution Idea: Nothing but just run dijkstra twice.Once to spread from the source and another to get together.Then the sum of the maximum will be answer.
        Solution: https://ideone.com/ZnPqLk (See at your own risk👎)

No comments:

Post a Comment