forked from kalaskarpranav/Java-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDijkstraAlgo.java
145 lines (117 loc) · 2.71 KB
/
DijkstraAlgo.java
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import java.util.*;
class Vertex{
public int d;
public int p;
public String id;
public int val;
public Vertex(String id,int val){
this.d = Integer.MAX_VALUE;
this.p = -1;
this.id = id;
this.val = val;
}
public String toString() {
return id;
}
}
class VertexComparator implements Comparator<Vertex>{
public int compare(Vertex v1, Vertex v2){
if(v1.d > v2.d)
return 1;
else if (v1.d < v2.d)
return -1;
else return 0;
}
}
class Graph{
public Vertex vertex [] = null;
public LinkedList<Integer> [] adj = null;
public Graph(int n,String []ids){
vertex = new Vertex[n];
adj = new LinkedList[n];
for(int i=0;i<n;i++)
{
vertex[i] = new Vertex(ids[i],i);
adj[i] = new LinkedList<Integer>();
}
}
//Adding edges to adjacency list
public void addEdge(int u,int v){
adj[u].add(v);
}
}
class Dijk{
public static void main(String [] args){
int n = 5;
String ids [] = {"s","t", "x", "y","z"};
//Adjacency matrix of graph
int [][] adjMatrix ={
// s t x y z
{0, 10, 0, 5, 0}, //s
{0, 0, 1, 2, 0}, //t
{0, 0, 0, 0, 4}, //x
{0, 3, 9, 0, 2}, //y
{7, 0, 6, 0, 0} //z
};
//Initializing graph
Graph graph = new Graph(n,ids);
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(adjMatrix[i][j]!=0)
{
graph.addEdge(i,j);
}
}
}
int s = 0;
initializeSingleSource(graph,n,s);
ArrayList<Vertex> list = new ArrayList<Vertex>();
PriorityQueue<Vertex> pq = new PriorityQueue<Vertex>(5, new VertexComparator());
for(int i=0;i<n;i++){
pq.add(graph.vertex[i]);
}
while(!pq.isEmpty()){
Vertex u = pq.poll();
int ui = u.val;
list.add(u);
for(Integer v : graph.adj[ui]){
relax(graph,ui,v,adjMatrix[ui][v]);
}
}
System.out.println(graph.vertex[s]);
for(int i=1;i<n;i++){
//System.out.print(graph.vertex[s]+" -> ");
String str = "";
str+=printPath(graph,s,i,str);
System.out.print(str.substring(0,str.length()-2)+"\n");
}
}
static void initializeSingleSource(Graph graph,int n,int s){
for(int i=0;i<n;i++){
graph.vertex[i].d = Integer.MAX_VALUE;
graph.vertex[i].p = -1;
}
graph.vertex[s].d = 0;
}
static void relax(Graph graph, int u, int v,int w){
if(graph.vertex[v].d > (graph.vertex[u].d + w))
{
graph.vertex[v].d = graph.vertex[u].d + w;
graph.vertex[v].p = u;
}
}
static String printPath(Graph graph, int s, int v,String str){
if(v==s){
return str+=" "+graph.vertex[v]+"("+graph.vertex[v].d+") ->";
}
else if(graph.vertex[v].p == -1){
return str+="No path Exists!";
}
else{
str+=printPath(graph,s,graph.vertex[v].p,str);
return str+=" "+graph.vertex[v]+" ("+graph.vertex[v].d+") ->";
}
}
}