Sei sulla pagina 1di 14

Dijkstra algorithm dynamic approach

• One solution to this problem is to apply the simple Dijkstra


algorithm for all vertices.
• But if the graph contains a negative weight edges,Dijkstra
algorithm could not be applied.In this case we have to apply
Floyd Warshall algorithm for computing the shortest path.
For the shortest path to v, denoted d[v], the relaxation
property states that we can set d[v] = min(d[v],d[u]+w(u,v) ).
This formula indicates that the best distance to v is either
the previously known distance to v, or the result of going
from s to some vertex u and then directly from u to v.
• The dynamic programming approach is applied here similar
with floydy algorithm.
Algorithm: Dijkstra’s algorithm
• Set Dk,i,j to be the weight of the shortest path from vertex
i to vertex j using only nodes
• 0 - k as intermediaries.
• D0,i,j = w[i,j] by definition.
• Example:

2
Example
• D0 can be represented as a matrix: shortest
path between nodes using 0 intermediates
(direct links)

• Observation: The shortest path from vertex i


to vertex j that uses only up to k intermediate
nodes is the shortest path that either does not
use vertex k at all, or consists of the merging
of the two paths vertex i to vertex k and vertex
k to vertex j. This leads to the formula:
3
The algorithm(procedure)
• Dk,i,j = min { Dk-1,i,j or Dk-1,i,k + Dk-1,k,j }
• Previous best Previous best to vertex k, then best from k
to j
• Putting this together into an algorithm, named Floyd-
Warshall:
• Floyd-Warshall-All-Pairs-Shortest(G,w)
• Initialize d[i,j] ¬ w(i,j), ¥ if no such link
• Initialize path[i,j] ¬ ¥
• for k ¬1 to |V|
• for i ¬1 to |V|
• for j ¬1 to |V|
• if d[i,k]+d[k,j]<d[i,j] then ; update min 4
Contd.
• d[i,j] ¬d[i,k]+d[k,j]
• path[i,j] ¬k ; store to get path
• Here we use one matrix and overwrite it for each
iteration of k.
• Example on above graph:

5
Contd.
• Going through shortest path to vertex 1:
• 1,1: d(1,1)+d(1,1)<d(1,1)? 0+0<0? No
• 1,2: d(1,1)+d(1,2)<d(1,2? 0+3<3? No
• …
• 2,1: d(2,1)+d(1,1)<d(2,1)? ¥+3<¥? No
• 2,2: d(2,1)+d(1,2)<d(2,2)? ¥+3<0? No
• …
• 4,2: d(4,1)+d(1,2)<d(4,2)? 2+3<¥? YES! Update
d(4,2) to 5
• 4,5: d(4,1)+d(1,5)<d(4,5)? 2+-4<¥? YES! Update
d(4,5) to –2 6
Contd.

7
Contd.
• Going through shortest path to vertex 2 (includes
going through vertex 1)
• 1,4: d(1,2)+d(2,4)<d(1,4)? 3+1<¥? YES , update
d(1,4) to 4
• Keep going, when k=5:

8
Contd.
Distance matrix gives the distance of the shortest
path from i to j. By following the node in the path
matrix, we can find the path to get the shortest path
from i to j.

The implementation of the above algorithm or


operation is as shown below using java language:

9
• import static javafx.scene.input.KeyCode.M;

• /*
• * To change this license header, choose License Headers in Project Properties.
• * To change this template file, choose Tools | Templates
• * and open the template in the editor.
• */

• /**
• *
• * @author ahmed
• */

• public class algorithm {
• static int[][] P;
• static final int N=4;
• public static void main(String[] args){
• int[][] M={{0,5,999,999},{50,0,15,5},{30,999,0,15},{15,999,5,0}};
• P=new int[N][N];
• System.out.println("Matrix to find the shortest path of.");
• printMatrix(M);
• System.out.println("shortest path matrix.");
• printMatrix(FloydAlgo(M));
• System.out.println("path matrix");
• printMatrix(P);
• }
2019/10/15
• public static int[][] FloydAlgo(int[][] M){
• for(int k=0;k<N;k++){
• for(int i=0;i<N;i++){
• for(int j=0;j<N;j++){
• // to keep track;
• if(M[i][k]+M[k][j]<M[i][j]){
• M[i][j]=M[i][k]+M[k][j];
• P[i][j]=k;

• }
• // or not to keep track;
• //M[i][j]=min(M[i][j],M[i][k]+M[k][j]);

• }
• }
• }
• return M;

2019/10/15
• }

• public static int min(int i,int j){
• if(i>j){
• return j;
• }
• return i;
• }
• public static void printMatrix(int[][] Matrix){
• System.out.print("\n\t");
• for(int j=0;j<N;j++){
• System.out.print(j+"\t");

• }
• System.out.println();
• for(int j=0;j<35;j++){
• System.out.print("-");

• }
2019/10/15
• System.out.println();
• for(int i=0;i<N;i++){
• System.out.print(i + " |\t");
• for(int j=0;j<N;j++){
• System.out.print(Matrix[i][j]);
• System.out.print("\t");
• }
• System.out.println("\n");
• }
• System.out.println("\n");
• }
• }

2019/10/15

Potrebbero piacerti anche