2 * Copyright (c) 2003, the JUNG Project and the Regents of the University
6 * This software is open-source under the BSD license; see either
8 * http://jung.sourceforge.net/license.txt for a description.
10 package edu.uci.ics.jung.algorithms.shortestpath;
12 import java.util.HashMap;
15 import edu.uci.ics.jung.graph.Hypergraph;
18 * Computes the shortest path distances for graphs whose edges are not weighted (using BFS).
22 public class UnweightedShortestPath<V, E>
23 implements ShortestPath<V,E>, Distance<V>
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>();
31 * Constructs and initializes algorithm
34 public UnweightedShortestPath(Hypergraph<V,E> g)
36 mDistanceMap = new HashMap<V,Map<V,Number>>();
37 mIncomingEdgeMap = new HashMap<V,Map<V,E>>();
42 * @see edu.uci.ics.jung.algorithms.shortestpath.Distance#getDistance(Object, Object)
44 public Number getDistance(V source, V target)
46 Map<V, Number> sourceSPMap = getDistanceMap(source);
47 return sourceSPMap.get(target);
51 * @see edu.uci.ics.jung.algorithms.shortestpath.Distance#getDistanceMap(Object)
53 public Map<V,Number> getDistanceMap(V source)
55 Map<V,Number> sourceSPMap = mDistanceMap.get(source);
56 if (sourceSPMap == null)
58 computeShortestPathsFromSource(source);
59 sourceSPMap = mDistanceMap.get(source);
65 * @see edu.uci.ics.jung.algorithms.shortestpath.ShortestPath#getIncomingEdgeMap(Object)
67 public Map<V,E> getIncomingEdgeMap(V source)
69 Map<V,E> sourceIEMap = mIncomingEdgeMap.get(source);
70 if (sourceIEMap == null)
72 computeShortestPathsFromSource(source);
73 sourceIEMap = mIncomingEdgeMap.get(source);
80 * Computes the shortest path distances from a given node to all other nodes.
81 * @param source the source node
83 private void computeShortestPathsFromSource(V source)
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>();
91 for(V vertex : mGraph.getVertices()) {
93 Number distanceVal = distances.get(vertex);
94 // BFSDistanceLabeler uses -1 to indicate unreachable vertices;
95 // don't bother to store unreachable vertices
96 if (distanceVal != null && distanceVal.intValue() >= 0)
98 currentSourceSPMap.put(vertex, distanceVal);
99 int minDistance = distanceVal.intValue();
100 for(E incomingEdge : mGraph.getInEdges(vertex))
102 for (V neighbor : mGraph.getIncidentVertices(incomingEdge))
104 if (neighbor.equals(vertex))
106 // V neighbor = mGraph.getOpposite(vertex, incomingEdge);
108 Number predDistanceVal = distances.get(neighbor);
110 int pred_distance = predDistanceVal.intValue();
111 if (pred_distance < minDistance && pred_distance >= 0)
113 minDistance = predDistanceVal.intValue();
114 currentSourceEdgeMap.put(vertex, incomingEdge);
120 mDistanceMap.put(source, currentSourceSPMap);
121 mIncomingEdgeMap.put(source, currentSourceEdgeMap);
125 * Clears all stored distances for this instance.
126 * Should be called whenever the graph is modified (edge weights
127 * changed or edges added/removed). If the user knows that
128 * some currently calculated distances are unaffected by a
129 * change, <code>reset(V)</code> may be appropriate instead.
131 * @see #reset(Object)
135 mDistanceMap.clear();
136 mIncomingEdgeMap.clear();
140 * Clears all stored distances for the specified source vertex
141 * <code>source</code>. Should be called whenever the stored distances
142 * from this vertex are invalidated by changes to the graph.
146 public void reset(V v)
148 mDistanceMap.remove(v);
149 mIncomingEdgeMap.remove(v);