-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleShortestPaths.java
More file actions
66 lines (54 loc) · 1.72 KB
/
Copy pathSimpleShortestPaths.java
File metadata and controls
66 lines (54 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package graph;
/* See restrictions in Graph.java. */
import java.util.HashMap;
/** A partial implementation of ShortestPaths that contains the weights of
* the vertices and the predecessor edges. The client needs to
* supply only the two-argument getWeight method.
* @author Khang
*/
public abstract class SimpleShortestPaths extends ShortestPaths {
/** The shortest paths in G from SOURCE. */
public SimpleShortestPaths(Graph G, int source) {
this(G, source, 0);
}
/** A shortest path in G from SOURCE to DEST. */
public SimpleShortestPaths(Graph G, int source, int dest) {
super(G, source, dest);
_predecessor = new HashMap<Integer, Integer>();
_weight = new HashMap<Integer, Double>();
}
/** Returns the current weight of edge (U, V) in the graph. If (U, V) is
* not in the graph, returns positive infinity. */
@Override
protected abstract double getWeight(int u, int v);
@Override
public double getWeight(int v) {
if (_weight.containsKey(v)) {
return _weight.get(v);
}
return Integer.MAX_VALUE;
}
@Override
protected void setWeight(int v, double w) {
_weight.put(v, w);
}
@Override
public int getPredecessor(int v) {
if (_predecessor.containsKey(v)) {
return _predecessor.get(v);
}
return 0;
}
@Override
protected void setPredecessor(int v, int u) {
_predecessor.put(v, u);
}
/**
* HashMap<Integer, Integer> contains the predecessors.
*/
private HashMap<Integer, Integer> _predecessor;
/**
* HashMap<Integer, Double> contains weights of nodes.
*/
private HashMap<Integer, Double> _weight;
}