1
2
3
4
5
6
7
8
9
10 package edu.uci.ics.jung.algorithms.shortestpath;
11
12 import java.util.HashMap;
13 import java.util.Map;
14
15 import edu.uci.ics.jung.graph.Hypergraph;
16
17
18
19
20
21
22 public class UnweightedShortestPath<V, E>
23 implements ShortestPath<V,E>, Distance<V>
24 {
25 private Map<V,Map<V,Number>> mDistanceMap;
26 private Map<V,Map<V,E>> mIncomingEdgeMap;
27 private Hypergraph<V,E> mGraph;
28 private Map<V, Number> distances = new HashMap<V,Number>();
29
30
31
32
33
34 public UnweightedShortestPath(Hypergraph<V,E> g)
35 {
36 mDistanceMap = new HashMap<V,Map<V,Number>>();
37 mIncomingEdgeMap = new HashMap<V,Map<V,E>>();
38 mGraph = g;
39 }
40
41
42
43
44 public Number getDistance(V source, V target)
45 {
46 Map<V, Number> sourceSPMap = getDistanceMap(source);
47 return sourceSPMap.get(target);
48 }
49
50
51
52
53 public Map<V,Number> getDistanceMap(V source)
54 {
55 Map<V,Number> sourceSPMap = mDistanceMap.get(source);
56 if (sourceSPMap == null)
57 {
58 computeShortestPathsFromSource(source);
59 sourceSPMap = mDistanceMap.get(source);
60 }
61 return sourceSPMap;
62 }
63
64
65
66
67 public Map<V,E> getIncomingEdgeMap(V source)
68 {
69 Map<V,E> sourceIEMap = mIncomingEdgeMap.get(source);
70 if (sourceIEMap == null)
71 {
72 computeShortestPathsFromSource(source);
73 sourceIEMap = mIncomingEdgeMap.get(source);
74 }
75 return sourceIEMap;
76 }
77
78
79
80
81
82
83 private void computeShortestPathsFromSource(V source)
84 {
85 BFSDistanceLabeler<V,E> labeler = new BFSDistanceLabeler<V,E>();
86 labeler.labelDistances(mGraph, source);
87 distances = labeler.getDistanceDecorator();
88 Map<V,Number> currentSourceSPMap = new HashMap<V,Number>();
89 Map<V,E> currentSourceEdgeMap = new HashMap<V,E>();
90
91 for(V vertex : mGraph.getVertices()) {
92
93 Number distanceVal = distances.get(vertex);
94
95
96 if (distanceVal != null && distanceVal.intValue() >= 0)
97 {
98 currentSourceSPMap.put(vertex, distanceVal);
99 int minDistance = distanceVal.intValue();
100 for(E incomingEdge : mGraph.getInEdges(vertex))
101 {
102 for (V neighbor : mGraph.getIncidentVertices(incomingEdge))
103 {
104 if (neighbor.equals(vertex))
105 continue;
106
107 Number predDistanceVal = distances.get(neighbor);
108
109 int pred_distance = predDistanceVal.intValue();
110 if (pred_distance < minDistance && pred_distance >= 0)
111 {
112 minDistance = predDistanceVal.intValue();
113 currentSourceEdgeMap.put(vertex, incomingEdge);
114 }
115 }
116 }
117 }
118 }
119 mDistanceMap.put(source, currentSourceSPMap);
120 mIncomingEdgeMap.put(source, currentSourceEdgeMap);
121 }
122
123
124
125
126
127
128
129
130
131
132 public void reset()
133 {
134 mDistanceMap.clear();
135 mIncomingEdgeMap.clear();
136 }
137
138
139
140
141
142
143
144
145
146
147 public void reset(V v)
148 {
149 mDistanceMap.remove(v);
150 mIncomingEdgeMap.remove(v);
151 }
152 }