-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDijkstra.java
More file actions
57 lines (51 loc) · 1.66 KB
/
Dijkstra.java
File metadata and controls
57 lines (51 loc) · 1.66 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
/**
* Implements Dijkstran algorithm.
*/
package graph;
import java.util.ArrayList;
import heap.Heap;
import java.util.HashMap;
public class Dijkstra {
/**
* Sets the adjList of the graph and calls getDistances function.
* @param args Array of arguments for launching the program. Ignored.
*/
public static void main(String[] args) {
int[][] adjList = {
{1, 1, 2}, {1, 1, 4}, {1, 5, 3},
{2, 1, 5},
{3, 3, 6}, {3, 8, 1},
{5, 2, 3}
};
Graph graph = new Graph(adjList);
getDistances(graph, 3);
}
/**
* Finds the distance of each node from the starting node with Dijkstra algorithm.
* @param graph The graph object.
* @param from The index of the node to calculate distance from.
*/
public static void getDistances(Graph graph, int from) {
Heap edgesQueue = new Heap(false);
int currentCost = 0;
HashMap<Integer, Integer> distanceByIndex = new HashMap<Integer, Integer>();
distanceByIndex.put(from, 0);
ArrayList<Edge> newEdges = graph.nodesByIndex.get(from).edges;
for (Edge e : newEdges) {
edgesQueue.addObj(e.cost, e);
}
Edge next = (Edge)edgesQueue.pullTopObj();
while (next != null) {
Node nextNode = next.to;
if (distanceByIndex.get(nextNode.index) == null) {
distanceByIndex.put(nextNode.index, distanceByIndex.get(next.from.index) + next.cost);
newEdges = graph.nodesByIndex.get(nextNode.index).edges;
for (Edge e : newEdges) {
edgesQueue.addObj(e.cost, e);
}
System.out.println(next.to.index + " takes " + distanceByIndex.get(next.to.index));
}
next = (Edge)edgesQueue.pullTopObj();
}
}
}