Skip to content

Commit d380738

Browse files
committed
Committing before pulling.
1 parent 3f48337 commit d380738

File tree

4 files changed

+49
-90
lines changed

4 files changed

+49
-90
lines changed

src/main/java/com/github/coderodde/graph/pathfinding/delayed/AbstractDelayedGraphPathFinder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public abstract class AbstractDelayedGraphPathFinder<N> {
117117
*
118118
* @return duration in milliseconds.
119119
*/
120-
public long getDuration() {
120+
public final long getDuration() {
121121
return duration;
122122
}
123123

@@ -126,7 +126,7 @@ public long getDuration() {
126126
*
127127
* @return the number of expanded nodes.
128128
*/
129-
public int getNumberOfExpandedNodes() {
129+
public final int getNumberOfExpandedNodes() {
130130
return numberOfExpandedNodes;
131131
}
132132

src/main/java/com/github/coderodde/graph/pathfinding/delayed/impl/ThreadPoolBidirectionalBFSPathFinder.java

Lines changed: 18 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44
import com.github.coderodde.graph.pathfinding.delayed.AbstractNodeExpander;
55
import com.github.coderodde.graph.pathfinding.delayed.ProgressLogger;
66

7-
import java.util.ArrayDeque;
87
import java.util.ArrayList;
98
import java.util.Collections;
10-
import java.util.Deque;
119
import java.util.HashMap;
1210
import java.util.HashSet;
1311
import java.util.List;
@@ -32,7 +30,7 @@
3230
*
3331
* @param <N> the actual graph node type.
3432
*
35-
* @version 2.0.0 (Apr 21, 2024)
33+
* @version 2.0.1 (May 9, 2024)
3634
* @since 1.0.0
3735
*/
3836
public final class ThreadPoolBidirectionalBFSPathFinder<N>
@@ -177,10 +175,10 @@ public final class ThreadPoolBidirectionalBFSPathFinder<N>
177175
* lock.
178176
*/
179177
public ThreadPoolBidirectionalBFSPathFinder(
180-
final int numberOfThreads,
178+
final int numberOfThreads,
181179
final long masterThreadSleepDurationNanos,
182180
final long slaveThreadSleepDurationNanos,
183-
final int masterThreadTrials,
181+
final int masterThreadTrials,
184182
final long expansionThreadJoinDurationNanos,
185183
final long lockWaitDurationNanos) {
186184

@@ -247,14 +245,6 @@ public long getExpansionJoinDurationNanos() {
247245
public long getLockWaitDurationNanos() {
248246
return lockWaitDurationNanos;
249247
}
250-
251-
public int getNumberOfExpandedNodes() {
252-
return numberOfExpandedNodes;
253-
}
254-
255-
public long getDurationMillis() {
256-
return duration;
257-
}
258248

259249
/**
260250
* {@inheritDoc }
@@ -673,55 +663,19 @@ boolean pathIsOptimal() {
673663
if (touchNode == null) {
674664
return false;
675665
}
676-
677-
final N forwardSearchHead = forwardSearchState.getQueueHead();
678-
679-
if (forwardSearchHead == null) {
666+
667+
if (forwardSearchState.heap.minimumNode() == null) {
680668
return false;
681669
}
682-
683-
final N backwardSearchHead = backwardSearchState.getQueueHead();
684-
685-
if (backwardSearchHead == null) {
670+
671+
if (backwardSearchState.heap.minimumNode() == null) {
686672
return false;
687673
}
688674

689-
final int distance =
690-
forwardSearchState .getDistanceOf(forwardSearchHead) +
691-
backwardSearchState.getDistanceOf(backwardSearchHead);
675+
final int distance = forwardSearchState.heap.minimumPriority() +
676+
backwardSearchState.heap.minimumPriority();
692677

693678
return distance > bestPathLengthSoFar;
694-
// return getOptimalDistance() > bestPathLengthSoFar;
695-
}
696-
697-
int getOptimalDistance() {
698-
int bestDistance = Integer.MAX_VALUE;
699-
// long start = System.currentTimeMillis();
700-
701-
for (final N node : forwardSearchState.queue) {
702-
if (backwardSearchState.parents.containsKey(node)) {
703-
final int tentativeDistance =
704-
forwardSearchState.getDistanceOf(node) +
705-
backwardSearchState.getDistanceOf(node);
706-
707-
bestDistance = Math.min(bestDistance, tentativeDistance);
708-
}
709-
}
710-
711-
for (final N node : backwardSearchState.queue) {
712-
if (forwardSearchState.parents.containsKey(node)) {
713-
final int tentativeDistance =
714-
forwardSearchState.getDistanceOf(node) +
715-
backwardSearchState.getDistanceOf(node);
716-
717-
bestDistance = Math.min(bestDistance, tentativeDistance);
718-
}
719-
}
720-
721-
// long end = System.currentTimeMillis();
722-
723-
// System.out.printf("Duration: %d, distance: %d.\n", end - start, bestDistance);
724-
return bestDistance;
725679
}
726680

727681
/**
@@ -771,12 +725,6 @@ List<N> getPath() {
771725
*/
772726
private static final class SearchState<N> {
773727

774-
/**
775-
* This FIFO queue contains the queue of nodes reached but not yet
776-
* expanded. It is called the <b>search frontier</b>.
777-
*/
778-
private final Deque<N> queue = new ArrayDeque<>();
779-
780728
/**
781729
* This map maps each discovered node to its predecessor on the shortest
782730
* path.
@@ -821,34 +769,17 @@ private static final class SearchState<N> {
821769
* node should be the target node.
822770
*/
823771
SearchState(final N initialNode) {
824-
queue.add(initialNode);
772+
heap.insert(initialNode, 0);
825773
parents.put(initialNode, null);
826774
distance.put(initialNode, 0);
827-
heap.insert(initialNode, 0);
828-
}
829-
830-
N removeQueueHead() {
831-
if (queue.isEmpty()) {
832-
return null;
833-
}
834-
835-
final N head = queue.remove();
836-
heap.remove(head);
837-
return head;
838-
}
839-
840-
N getQueueHead() {
841-
return heap.minimumNode();
842-
// return queue.peek(); // Commented out May 9, 2024.
843775
}
844776

845777
boolean containsNode(final N node) {
846778
return distance.containsKey(node);
847779
}
848780

849781
Integer getDistanceOf(final N node) {
850-
Integer dist = distance.get(node);
851-
return dist;
782+
return distance.get(node);
852783
}
853784

854785
N getParentOf(final N node) {
@@ -880,11 +811,10 @@ private boolean trySetNodeInfo(final N node, final N predecessor) {
880811
return false;
881812
}
882813

883-
final int distance = getDistanceOf(predecessor) + 1;
884-
distance.put(node, distance);
814+
final int d = getDistanceOf(predecessor) + 1;
815+
distance.put(node, d);
885816
parents.put(node, predecessor);
886-
queue.addLast(node);
887-
heap.insert(node, distance);
817+
heap.insert(node, d);
888818
return true;
889819
}
890820

@@ -1155,7 +1085,7 @@ public String toString() {
11551085
*/
11561086
private void processCurrentInMasterThread() {
11571087
lock();
1158-
final N head = searchState.getQueueHead();
1088+
final N head = searchState.heap.minimumNode();
11591089
unlock();
11601090

11611091
searchState.wakeupAllSleepingThreads();
@@ -1168,7 +1098,7 @@ private void processCurrentInMasterThread() {
11681098

11691099
for (int trials = 0; trials < threadSleepTrials; trials++) {
11701100
mysleep(threadSleepDurationNanos);
1171-
currentHead = searchState.getQueueHead();
1101+
currentHead = searchState.heap.minimumNode();
11721102

11731103
if (currentHead != null) {
11741104
break;
@@ -1194,7 +1124,7 @@ private void processCurrentInSlaveThread() {
11941124
}
11951125

11961126
lock();
1197-
final N current = searchState.getQueueHead();
1127+
final N current = searchState.heap.minimumNode();
11981128
unlock();
11991129

12001130
if (current == null) {
@@ -1242,7 +1172,7 @@ private void unlock() {
12421172
*/
12431173
private void expand() {
12441174
lock();
1245-
final N current = searchState.removeQueueHead();
1175+
final N current = searchState.heap.extractMinimum();
12461176
unlock();
12471177

12481178
if (current == null) {

src/main/java/com/github/coderodde/graph/pathfinding/delayed/impl/TreeHeap.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,22 @@ public int minimumPriority() {
117117
}
118118

119119
public N minimumNode() {
120+
if (size == 0) {
121+
return null;
122+
}
123+
120124
return accessMinimumNode().node;
121125
}
122126

127+
public int getPriority(final N node) {
128+
return nodeMap.get(node).priority;
129+
}
130+
123131
public N extractMinimum() {
132+
if (size == 0) {
133+
return null;
134+
}
135+
124136
final TreeHeapNode<N> treeNode = accessMinimumNode();
125137
unlinkImpl(treeNode);
126138
size--;

src/test/java/com/github/coderodde/graph/pathfinding/delayed/impl/TreeHeapTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,21 @@ public void all() {
4242
assertEquals(Integer.valueOf(1), heap.extractMinimum());
4343
assertEquals(0, heap.size());
4444
}
45+
46+
@Test
47+
public void remove() {
48+
TreeHeap<String> heap = new TreeHeap<>();
49+
50+
heap.insert("a", 1);
51+
heap.insert("b", 2);
52+
heap.insert("c", 3);
53+
heap.insert("c_", 3);
54+
55+
assertEquals("a", heap.extractMinimum());
56+
assertEquals("b", heap.extractMinimum());
57+
assertEquals("c_", heap.extractMinimum());
58+
assertEquals("c", heap.extractMinimum());
59+
60+
assertEquals(0, heap.size());
61+
}
4562
}

0 commit comments

Comments
 (0)