-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDijkstra.java
More file actions
106 lines (88 loc) · 2.46 KB
/
Dijkstra.java
File metadata and controls
106 lines (88 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
From a given vertex in a weighted connected graph, find shortest paths to other
vertices using Dijkstra's algorithm. Write the program in Java.
*/
import java.io.*;
import java.util.*;
class Dijkstra
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int n;
//Input no of nodes
System.out.println("No of nodes :");
n=s.nextInt();
//Vertices are numbered starting from 1 hence we will use index no as reference to vertices
int cost[][]=new int[n+1][n+1];//Hence size of array is n+1 as we ignore the 0th index
//input cost matrix of the graph
System.out.println("Enter Cost matrix:");
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
cost[i][j]=s.nextInt();
//Input source vertex
System.out.println("Enter source vertex");
int scr=s.nextInt();
//Create a distance array
int dist[]=new int[n+1];
//Initialize with the cost to travel directly from source to i
for(int i=1;i<=n;i++)
dist[i]=cost[scr][i];
//Call function passing all the parameters
ShortestPath(n,scr,cost,dist);
}
static void ShortestPath(int n,int src,int cost[][],int dist[])
{
//reach[] is used to chk if a vertex has been reacher or not. 1=reached and 0=not_reached.
int reach[]=new int[n+1];
//path[] keeps track of the path of min cost
int path[]=new int[n+1];
//path is initialized to src initially
for(int i=0;i<n;i++)
path[i]=src;
int u=0,count=1,min;
//we loop though n-1 edges as the source is already accounted.
while(count<n)
{
min=999;
//Find an adj vertex which can be visited from the current vetrex with minimum cost
for(int i=1;i<=n;i++)
if(dist[i]<min && reach[i]==0)
{
min=dist[i];
u=i;//The vetrex is stored in u
}
//reach u which is the nearest vertex
reach[u]=1;
count++;
//check among neighbours of u , if the direct path to the neighbour is longer than path from src via u,if so update.
for(int i=1;i<=n;i++)
{
if(dist[i]>dist[u]+cost[u][i])
{
dist[i]=dist[u]+cost[u][i];//update dist[] with new min dist to i
path[i]=u;
}
}
}
System.out.println("Distance Array : "+Arrays.toString(dist));
/*
for(int i=1;i<=n;i++)
{
if(i!=src)
{ System.out.println("Distance from "+src+" to "+i+" is ---> "+dist[i]);
if(dist[i]<999)
{
System.out.println("Path : i <-- ");
int j=i;
while(j!=src)
{
j=path[j];
System.out.print("<-- "+j);
}
}
}
}
*/
}
}