-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDijkstra Algorithm.java
104 lines (101 loc) · 2.51 KB
/
Dijkstra Algorithm.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
import java.util.*;
class Node implements Comparator<Node>
{
int node, weight;
Node(int v, int u)
{
node=v;
weight=u;
}
Node(){
}
int getNode() {
return node;
}
int getWeight()
{
return weight;
}
@Override
public int compare(Node node1,Node node2){
if(node1.weight<node2.weight)
return -1;
if(node1.weight>node2.weight)
return 1;
return 0;
}
}
public class DijkstraAlgo {
static ArrayList<ArrayList<Node>> adj;
static void shortestPath(ArrayList<ArrayList<Node>> adj, int node)
{
int dis[] = new int[node];
for(int i =0;i<node;i++)
{
dis[i]=100000000;
}
PriorityQueue<Node> pq = new PriorityQueue<Node>(node,new Node());
int k =0;
pq.add(new Node(k,0));
while(!pq.isEmpty())
{
Node x = pq.poll();
for(Node z : adj.get(x.getNode()))
{
if(dis[x.getNode()] + z.getWeight() < dis[z.getNode()])
{
dis[z.getNode()] = dis[x.getNode()]+z.getWeight();
pq.add(new Node(z.getNode(),dis[z.getNode()]));
}
}
}
System.out.print("shortest distance between "+k+" is ");
for(int b=0;b<node;b++)
{
System.out.print(dis[b]+" ");
}
}
public static void main(String arg[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter total nodes");
int node = sc.nextInt();
adj=new ArrayList<ArrayList<Node>>();
for(int i=0;i<node;i++)
{
adj.add(new ArrayList<Node>());
}
// System.out.println("enter v and u");
// int v=0;
// int u=0;
// for(int i=0;i<node;i++)
// {
// v = sc.nextInt();
// u = sc.nextInt();
// }
adj.get(0).add(new Node(1,2));
adj.get(1).add(new Node(0,2));
adj.get(1).add(new Node(2 ,4));
adj.get(2).add(new Node(1, 4));
adj.get(0).add(new Node(3, 1));
adj.get(3).add(new Node(0, 1));
adj.get(3).add(new Node(2, 3));
adj.get(2).add(new Node(3, 3));
adj.get(1).add(new Node(4,5));
adj.get(4).add(new Node(1,5));
adj.get(2).add(new Node(4,1));
adj.get(4).add(new Node(2,1));
shortestPath(adj,node);
}
}
//Some sample user inputs
//1 2
//0 2
//2 4
//1 4
//3 1
//0 1
//2 3
//3 3
//4 5
//1 5