Skip to content

Commit bb237f1

Browse files
committed
Removed node class, works with any class
1 parent 68c6ae3 commit bb237f1

26 files changed

Lines changed: 377 additions & 599 deletions

src/main/java/info/debatty/java/graphs/Dijkstra.java

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@
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<NodeInterface> settled_nodes;
43-
private final Set<NodeInterface> unsettled_nodes;
44-
private final Map<NodeInterface, NodeInterface> predecessors;
45-
private final Map<NodeInterface, 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 NodeInterface source) {
54+
public Dijkstra(final Graph graph, final T source) {
5555

5656
this.graph = graph;
5757

58-
settled_nodes = new HashSet<NodeInterface>();
59-
unsettled_nodes = new HashSet<NodeInterface>();
60-
distances = new HashMap<NodeInterface, Integer>();
61-
predecessors = new HashMap<NodeInterface, NodeInterface>();
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-
NodeInterface 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 NodeInterface 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<NodeInterface> getPath(final NodeInterface target) throws Exception {
82-
LinkedList<NodeInterface> path = new LinkedList<NodeInterface>();
83-
NodeInterface 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 NodeInterface 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-
NodeInterface 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 NodeInterface node) {
128128
}
129129
}
130130

131-
private NodeInterface getMinimum(final Set<NodeInterface> nodes) {
132-
NodeInterface minimum = null;
133-
for (NodeInterface 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 NodeInterface getMinimum(final Set<NodeInterface> nodes) {
142142
return minimum;
143143
}
144144

145-
private int getShortestDistance(final NodeInterface destination) {
145+
private int getShortestDistance(final T destination) {
146146
Integer d = distances.get(destination);
147147
if (d == null) {
148148
return Integer.MAX_VALUE;

src/main/java/info/debatty/java/graphs/Edge.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
*
66
* @author Thibault Debatty
77
*/
8-
public class Edge {
8+
public class Edge<T> {
99

10-
public NodeInterface n1;
11-
public NodeInterface n2;
10+
public T n1;
11+
public T n2;
1212
public double weight = 0;
1313

1414
public static final String SEPARATOR = ";";
@@ -17,15 +17,15 @@ public Edge() {
1717

1818
}
1919

20-
public Edge(NodeInterface n1, NodeInterface n2, double weight) {
20+
public Edge(T n1, T n2, double weight) {
2121
this.n1 = n1;
2222
this.n2 = n2;
2323
this.weight = weight;
2424
}
2525

2626
@Override
2727
public String toString() {
28-
return n1.getId() + SEPARATOR + n2.getId() + SEPARATOR + weight;
28+
return n1.toString() + SEPARATOR + n2.toString()+ SEPARATOR + weight;
2929

3030
}
3131
}

0 commit comments

Comments
 (0)