|
3 | 3 | import com.github.coderodde.graph.pathfinding.delayed.AbstractDelayedGraphPathFinder; |
4 | 4 | import com.github.coderodde.graph.pathfinding.delayed.AbstractNodeExpander; |
5 | 5 | import com.github.coderodde.graph.pathfinding.delayed.ProgressLogger; |
| 6 | +import com.github.coderodde.util.DialsHeap; |
| 7 | +import com.github.coderodde.util.IntegerMinimumPriorityQueue; |
6 | 8 |
|
7 | | -import java.util.ArrayDeque; |
8 | 9 | import java.util.ArrayList; |
9 | 10 | import java.util.Collections; |
10 | | -import java.util.Deque; |
11 | 11 | import java.util.HashMap; |
12 | 12 | import java.util.HashSet; |
13 | 13 | import java.util.List; |
@@ -693,36 +693,6 @@ boolean pathIsOptimal() { |
693 | 693 | return distance > bestPathLengthSoFar; |
694 | 694 | // return getOptimalDistance() > bestPathLengthSoFar; |
695 | 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; |
725 | | - } |
726 | 696 |
|
727 | 697 | /** |
728 | 698 | * Constructs a shortest path and returns it as a list. If the target |
@@ -771,28 +741,16 @@ List<N> getPath() { |
771 | 741 | */ |
772 | 742 | private static final class SearchState<N> { |
773 | 743 |
|
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 | | - |
780 | 744 | /** |
781 | 745 | * This map maps each discovered node to its predecessor on the shortest |
782 | 746 | * path. |
783 | 747 | */ |
784 | 748 | 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<>(); |
791 | 749 |
|
792 | 750 | /** |
793 | 751 | * Holds the copy of {@code queue} in sorted order by distance. |
794 | 752 | */ |
795 | | - private final TreeHeap<N> heap = new TreeHeap<>(); |
| 753 | + private final IntegerMinimumPriorityQueue<N> heap = new DialsHeap<>(); |
796 | 754 |
|
797 | 755 | /** |
798 | 756 | * The set of all the threads working on this particular direction. |
@@ -821,34 +779,24 @@ private static final class SearchState<N> { |
821 | 779 | * node should be the target node. |
822 | 780 | */ |
823 | 781 | SearchState(final N initialNode) { |
824 | | - queue.add(initialNode); |
825 | | - parents.put(initialNode, null); |
826 | | - distance.put(initialNode, 0); |
827 | 782 | heap.insert(initialNode, 0); |
| 783 | + parents.put(initialNode, null); |
828 | 784 | } |
829 | 785 |
|
830 | 786 | 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(); |
838 | 788 | } |
839 | 789 |
|
840 | 790 | N getQueueHead() { |
841 | 791 | return heap.minimumNode(); |
842 | | -// return queue.peek(); // Commented out May 9, 2024. |
843 | 792 | } |
844 | 793 |
|
845 | 794 | boolean containsNode(final N node) { |
846 | | - return distance.containsKey(node); |
| 795 | + return heap.containsDatum(node); |
847 | 796 | } |
848 | 797 |
|
849 | 798 | Integer getDistanceOf(final N node) { |
850 | | - Integer dist = distance.get(node); |
851 | | - return dist; |
| 799 | + return heap.getPriority(node); |
852 | 800 | } |
853 | 801 |
|
854 | 802 | N getParentOf(final N node) { |
@@ -880,24 +828,21 @@ private boolean trySetNodeInfo(final N node, final N predecessor) { |
880 | 828 | return false; |
881 | 829 | } |
882 | 830 |
|
883 | | - final int distance = getDistanceOf(predecessor) + 1; |
884 | | - distance.put(node, distance); |
| 831 | + final int d = heap.getPriority(predecessor) + 1; |
885 | 832 | parents.put(node, predecessor); |
886 | | - queue.addLast(node); |
887 | | - heap.insert(node, distance); |
| 833 | + heap.insert(node, d); |
888 | 834 | return true; |
889 | 835 | } |
890 | 836 |
|
891 | 837 | private void tryUpdateIfImprovementPossible( |
892 | 838 | final N node, |
893 | 839 | final N predecessor) { |
894 | 840 |
|
895 | | - final int updatedDistance = distance.get(predecessor) + 1; |
| 841 | + final int updatedDistance = heap.getPriority(predecessor) + 1; |
896 | 842 |
|
897 | | - if (distance.get(node) > updatedDistance) { |
898 | | - distance.put(node, updatedDistance); |
| 843 | + if (heap.getPriority(node) > updatedDistance) { |
899 | 844 | parents.put(node, predecessor); |
900 | | - heap.update(node, updatedDistance); |
| 845 | + heap.updatePriority(node, updatedDistance); |
901 | 846 | } |
902 | 847 | } |
903 | 848 |
|
|
0 commit comments