3636 *
3737 * @author Thibault Debatty
3838 */
39- public class Dijkstra {
39+ public class Dijkstra < T > {
4040
4141 private final Graph graph ;
42- private final Set <Node > settled_nodes ;
43- private final Set <Node > unsettled_nodes ;
44- private final Map <Node , Node > predecessors ;
45- private final Map <Node , Integer > distances ;
42+ private final Set <T > settled_nodes ;
43+ private final Set <T > unsettled_nodes ;
44+ private final Map <T , T > predecessors ;
45+ private final Map <T , Integer > distances ;
4646
4747 /**
4848 * Compute the shortest path from source node to every other node in the
@@ -51,20 +51,20 @@ public class Dijkstra {
5151 * @param graph to use for computing path
5252 * @param source node from which to compute distance to every other node
5353 */
54- public Dijkstra (final Graph graph , final Node source ) {
54+ public Dijkstra (final Graph graph , final T source ) {
5555
5656 this .graph = graph ;
5757
58- settled_nodes = new HashSet <Node >();
59- unsettled_nodes = new HashSet <Node >();
60- distances = new HashMap <Node , Integer >();
61- predecessors = new HashMap <Node , Node >();
58+ settled_nodes = new HashSet <T >();
59+ unsettled_nodes = new HashSet <T >();
60+ distances = new HashMap <T , Integer >();
61+ predecessors = new HashMap <T , T >();
6262
6363 distances .put (source , 0 );
6464 unsettled_nodes .add (source );
6565
6666 while (unsettled_nodes .size () > 0 ) {
67- Node node = getMinimum (unsettled_nodes );
67+ T node = getMinimum (unsettled_nodes );
6868 settled_nodes .add (node );
6969 unsettled_nodes .remove (node );
7070 findMinimalDistances (node );
@@ -78,9 +78,9 @@ public Dijkstra(final Graph graph, final Node source) {
7878 * @return the path from the source to the selected target
7979 * @throws java.lang.Exception if no path exists to this target
8080 */
81- public final LinkedList <Node > getPath (final Node target ) throws Exception {
82- LinkedList <Node > path = new LinkedList <Node >();
83- Node step = target ;
81+ public final LinkedList <T > getPath (final T target ) throws Exception {
82+ LinkedList <T > path = new LinkedList <T >();
83+ T step = target ;
8484 // check if a path exists
8585 if (predecessors .get (step ) == null ) {
8686 throw new Exception ("No path found to this target" );
@@ -112,13 +112,13 @@ public final int getLargestDistance() {
112112 return largest ;
113113 }
114114
115- private void findMinimalDistances (final Node node ) {
116- if (!graph .containsKey (node ) || graph .get (node ) == null ) {
115+ private void findMinimalDistances (final T node ) {
116+ if (!graph .containsKey (node ) || graph .getNeighbors (node ) == null ) {
117117 return ;
118118 }
119119
120- for (Neighbor neighbor : graph .get (node )) {
121- Node target = neighbor .node ;
120+ for (Neighbor < T > neighbor : graph .getNeighbors (node )) {
121+ T target = neighbor .node ;
122122
123123 if (getShortestDistance (target ) > (getShortestDistance (node ) + 1 )) {
124124 distances .put (target , getShortestDistance (node ) + 1 );
@@ -128,9 +128,9 @@ private void findMinimalDistances(final Node node) {
128128 }
129129 }
130130
131- private Node getMinimum (final Set <Node > nodes ) {
132- Node minimum = null ;
133- for (Node node : nodes ) {
131+ private T getMinimum (final Set <T > nodes ) {
132+ T minimum = null ;
133+ for (T node : nodes ) {
134134 if (minimum == null ) {
135135 minimum = node ;
136136 } else {
@@ -142,7 +142,7 @@ private Node getMinimum(final Set<Node> nodes) {
142142 return minimum ;
143143 }
144144
145- private int getShortestDistance (final Node destination ) {
145+ private int getShortestDistance (final T destination ) {
146146 Integer d = distances .get (destination );
147147 if (d == null ) {
148148 return Integer .MAX_VALUE ;
0 commit comments