-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiksrasImplementation.java
More file actions
160 lines (134 loc) · 4.33 KB
/
DiksrasImplementation.java
File metadata and controls
160 lines (134 loc) · 4.33 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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package hackpack;
// NOTE: Java.util.PriorityQueue is a binary heap implementation of a
// priority queue
// I used Keith Schwarz's Implementation of Fibonnacci Heap from
// http://www.keithschwarz.com/interesting/code/?dir=fibonacci-heap
import java.util.*;
public class DiksrasImplementation {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int cases = scan.nextInt();
int counter = 1;
while(cases-->0){
int nodes = scan.nextInt();
int[][] adj = new int[nodes][nodes];
System.out.println("Graph " + counter + ":");
for(int i = 0; i < nodes; i++){
for(int j = 0; j < nodes; j++){
adj[i][j] = scan.nextInt();
System.out.print(adj[i][j] + " ");
if(adj[i][j] == 0){
adj[i][j] = Integer.MAX_VALUE;
}
}
System.out.println();
}
System.out.println();
System.out.println("Running Binary Heap Dijkstra's:");
long start = System.nanoTime();
dijkstraBinHeap(adj,nodes);
long end = System.nanoTime();
System.out.println("Time Elapsed: " + (end-start));
System.out.println();
System.out.println("Running Fibonnacci Heap Dijkstra's:");
start = System.nanoTime();
dijkstraFibHeap(adj,nodes);
end = System.nanoTime();
System.out.println("Time Elapsed: " + (end-start));
System.out.println();
counter++;
}
}
public static void dijkstraBinHeap(int[][] adj, int n){
int nodes = n;
int[] dist = new int[nodes];
HashSet<Integer> finalized = new HashSet<Integer>();
FibonacciHeap<Nd> fh = new FibonacciHeap<Nd>();
Nd evalNode;
for(int i = 1; i < nodes; i++){
dist[i] = Integer.MAX_VALUE;
}
fh.enqueue(new Nd(0,0), 0);
dist[0] = 0;
while(!fh.isEmpty()){
evalNode = fh.dequeueMin().getValue();
finalized.add(evalNode.num);
int edgeDistance = -1;
int newDistance = -1;
for (int destinationNode = 0; destinationNode < nodes; destinationNode++){
if (!finalized.contains(destinationNode)){
if (adj[evalNode.num][destinationNode] != Integer.MAX_VALUE){
edgeDistance = adj[evalNode.num][destinationNode];
newDistance = dist[evalNode.num] + edgeDistance;
if (newDistance < dist[destinationNode]){
dist[destinationNode] = newDistance;
}
fh.enqueue(new Nd(destinationNode,dist[destinationNode]),dist[destinationNode]);
}
}
}
}
for(int i = 0; i < nodes; i++){
if(dist[i] == Integer.MAX_VALUE){
System.out.println("0 to " + i + ": No Path");
}else{
System.out.println("0 to " + i + ": " + dist[i]);
}
}
}
public static void dijkstraFibHeap(int[][] adj, int n){
int nodes = n;
int[] dist = new int[nodes+1];
HashSet<Integer> finalized = new HashSet<Integer>();
PriorityQueue<Nd> pq = new PriorityQueue<Nd>(nodes);
Nd evalNode;
for(int i = 1; i < nodes; i++){
dist[i] = Integer.MAX_VALUE;
}
pq.add(new Nd(0, 0));
dist[0] = 0;
while(!pq.isEmpty()){
evalNode = pq.remove();
finalized.add(evalNode.num);
int edgeDistance = -1;
int newDistance = -1;
for (int destinationNode = 0; destinationNode < nodes; destinationNode++){
if (!finalized.contains(destinationNode)){
if (adj[evalNode.num][destinationNode] != Integer.MAX_VALUE){
edgeDistance = adj[evalNode.num][destinationNode];
newDistance = dist[evalNode.num] + edgeDistance;
if (newDistance < dist[destinationNode]){
dist[destinationNode] = newDistance;
}
pq.add(new Nd(destinationNode,dist[destinationNode]));
}
}
}
}
for(int i = 0; i < nodes; i++){
if(dist[i] == Integer.MAX_VALUE){
System.out.println("0 to " + i + ": No Path");
}else{
System.out.println("0 to " + i + ": " + dist[i]);
}
}
}
}
class Nd implements Comparable<Nd>{
public int num;
public int cost;
public Nd(int node, int cost)
{
this.num = node;
this.cost = cost;
}
@Override
public int compareTo(Nd node)
{
if (cost < node.cost)
return -1;
if (cost > node.cost)
return 1;
return 0;
}
}