-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDijkstraShortestPath.java
122 lines (85 loc) · 3.97 KB
/
DijkstraShortestPath.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
import java.util.*;
public class DijkstraShortestPath {
public void computeShortestPaths(Node sourceNode) {
// sets the distance for the source node to zero instead of infinity, as is standard
sourceNode.setDistance(0);
// creates a priority queue of the node typing
PriorityQueue<Node> priorityQueue = new PriorityQueue<>();
// sets the node given in the method parameter as the node with the priority inside the queue, thereby giving order to it
priorityQueue.add(sourceNode);
// sets the source as visited already, to not have repeats
sourceNode.setVisited(true);
// while loop for when priority queue is not empty
while ( !priorityQueue.isEmpty() ) {
// declares actualNode as the node with priority in the queue, then removes it
Node actualNode = priorityQueue.poll();
// for loop to go by each hall attached to the actualNode as the starting point
for (Hall hall : actualNode.getAdjacentList()) {
// declares n as the node that is the end point(DestinationNode) of the hall
Node n = hall.getDestinationNode();
// if statement for when the destination has not been visited
if ( !n.isVisited()) {
// declares a new distance as the weight of the hall plus the nodes distance
int newDistance = actualNode.getDistance() + hall.getWeight();
// if statement for when the new distance is less that the destinations distance
if ( newDistance < n.getDistance() ) {
// removes n from the queue
priorityQueue.remove(n);
// sets the distance on the n node to the newly found distance
n.setDistance(newDistance);
// sets the predecessor for node n as the node being looked at
n.setPredecessor(actualNode);
// adds n back to the priority queue
priorityQueue.add(n);
}
}
}
// sets the value of the node as to visited
actualNode.setVisited(true);
}
}
// method to get the shortest path to a certain node from the start node
public List<Node> getShortestPathTo(Node targetNode) {
// creates list of nodes named path
List<Node> path = new ArrayList<>();
// for loop that backwards searches from the target, adding nodes, until it gets to the start
for (Node node = targetNode; node != null; node = node.getPredecessor()) {
path.add(node);
}
// reverses the list so that it is start to end
Collections.reverse(path);
return path;
}
}
/*
// NOTE ON PRIORITY QUEUE
every node object is inside the heap after their declaration
when creating the priority queue, you can have the object type specified
added automatically from the heap
you go in order based on the chosen priority
you cannot edit objects inside the queue, thus you must remove them
//
computeShortestPath(Node startNode)
SourceNode = startNode
PriorityQueue<Node> pq = new PriorityQueue();
pq.add(startNode)
startNode.setVisited(true)
while (pq is not empty)
Node actualNode = pq.poll()
for (all halls in the actualNode adjacentList)
Node n = destination node of current hall
if (if n has not been visited)
int nd = actualNode's Distance + current hall's weight
if (nd < n's distance)
remove n from queue
update n's distance as nd
set n's predecessor node as actualNode
add n back to queue
set actualNode as visited
getShortestPath(Node targetNode)
List<Node> path = new ArrayList
for (start at target node, then increment by going to predecessor node until predecessor node is null AKA the initial point)
path.add(current node)
reverse the path
return the path
*/