Skip to content

Commit 60af528

Browse files
committed
..
1 parent 7553bae commit 60af528

File tree

5 files changed

+23
-300
lines changed

5 files changed

+23
-300
lines changed

pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,28 @@
2828
<version>4.13.2</version>
2929
<scope>test</scope>
3030
</dependency>
31+
3132
<dependency>
3233
<groupId>org.hamcrest</groupId>
3334
<artifactId>hamcrest-core</artifactId>
3435
<version>1.3</version>
3536
<scope>test</scope>
3637
</dependency>
38+
39+
<dependency>
40+
<groupId>com.github.coderodde.util</groupId>
41+
<artifactId>DialsHeap.java</artifactId>
42+
<version>1.0.0</version>
43+
</dependency>
3744
</dependencies>
45+
3846
<properties>
3947
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
4048
<maven.compiler.source>20</maven.compiler.source>
4149
<maven.compiler.target>20</maven.compiler.target>
4250
<exec.mainClass>com.github.coderodde.graph.FaultyGraphPathfinder</exec.mainClass>
4351
</properties>
52+
4453
<name>ThreadPoolBidirectionalBFSPathFinder.java</name>
54+
4555
</project>

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

Lines changed: 12 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
import com.github.coderodde.graph.pathfinding.delayed.AbstractDelayedGraphPathFinder;
44
import com.github.coderodde.graph.pathfinding.delayed.AbstractNodeExpander;
55
import com.github.coderodde.graph.pathfinding.delayed.ProgressLogger;
6+
import com.github.coderodde.util.DialsHeap;
7+
import com.github.coderodde.util.IntegerMinimumPriorityQueue;
68

7-
import java.util.ArrayDeque;
89
import java.util.ArrayList;
910
import java.util.Collections;
10-
import java.util.Deque;
1111
import java.util.HashMap;
1212
import java.util.HashSet;
1313
import java.util.List;
@@ -693,36 +693,6 @@ boolean pathIsOptimal() {
693693
return distance > bestPathLengthSoFar;
694694
// return getOptimalDistance() > bestPathLengthSoFar;
695695
}
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;
725-
}
726696

727697
/**
728698
* Constructs a shortest path and returns it as a list. If the target
@@ -771,28 +741,16 @@ List<N> getPath() {
771741
*/
772742
private static final class SearchState<N> {
773743

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-
780744
/**
781745
* This map maps each discovered node to its predecessor on the shortest
782746
* path.
783747
*/
784748
private final Map<N, N> parents = new HashMap<>();
785-
786-
/**
787-
* This map maps each discovered node to its shortest path distance from
788-
* the source node.
789-
*/
790-
private final Map<N, Integer> distance = new HashMap<>();
791749

792750
/**
793751
* Holds the copy of {@code queue} in sorted order by distance.
794752
*/
795-
private final TreeHeap<N> heap = new TreeHeap<>();
753+
private final IntegerMinimumPriorityQueue<N> heap = new DialsHeap<>();
796754

797755
/**
798756
* The set of all the threads working on this particular direction.
@@ -821,34 +779,24 @@ private static final class SearchState<N> {
821779
* node should be the target node.
822780
*/
823781
SearchState(final N initialNode) {
824-
queue.add(initialNode);
825-
parents.put(initialNode, null);
826-
distance.put(initialNode, 0);
827782
heap.insert(initialNode, 0);
783+
parents.put(initialNode, null);
828784
}
829785

830786
N removeQueueHead() {
831-
if (queue.isEmpty()) {
832-
return null;
833-
}
834-
835-
final N head = queue.remove();
836-
heap.remove(head);
837-
return head;
787+
return heap.extractMinimum();
838788
}
839789

840790
N getQueueHead() {
841791
return heap.minimumNode();
842-
// return queue.peek(); // Commented out May 9, 2024.
843792
}
844793

845794
boolean containsNode(final N node) {
846-
return distance.containsKey(node);
795+
return heap.containsDatum(node);
847796
}
848797

849798
Integer getDistanceOf(final N node) {
850-
Integer dist = distance.get(node);
851-
return dist;
799+
return heap.getPriority(node);
852800
}
853801

854802
N getParentOf(final N node) {
@@ -880,24 +828,21 @@ private boolean trySetNodeInfo(final N node, final N predecessor) {
880828
return false;
881829
}
882830

883-
final int distance = getDistanceOf(predecessor) + 1;
884-
distance.put(node, distance);
831+
final int d = heap.getPriority(predecessor) + 1;
885832
parents.put(node, predecessor);
886-
queue.addLast(node);
887-
heap.insert(node, distance);
833+
heap.insert(node, d);
888834
return true;
889835
}
890836

891837
private void tryUpdateIfImprovementPossible(
892838
final N node,
893839
final N predecessor) {
894840

895-
final int updatedDistance = distance.get(predecessor) + 1;
841+
final int updatedDistance = heap.getPriority(predecessor) + 1;
896842

897-
if (distance.get(node) > updatedDistance) {
898-
distance.put(node, updatedDistance);
843+
if (heap.getPriority(node) > updatedDistance) {
899844
parents.put(node, predecessor);
900-
heap.update(node, updatedDistance);
845+
heap.updatePriority(node, updatedDistance);
901846
}
902847
}
903848

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

Lines changed: 0 additions & 188 deletions
This file was deleted.

src/main/java/module-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module coderodde.ThreadPoolBidirectionalBFSPathFinder {
22
requires java.logging;
33
requires java.base;
4+
requires coderodde.DialsHeapJava;
45
exports com.github.coderodde.graph.pathfinding.delayed;
56
exports com.github.coderodde.graph.pathfinding.delayed.impl;
67
}

0 commit comments

Comments
 (0)