File: DijkstraShortestPath.java

package info (click to toggle)
geogebra 4.0.34.0%2Bdfsg1-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 23,932 kB
  • sloc: java: 220,978; xml: 786; javascript: 211; sh: 116; makefile: 27
file content (282 lines) | stat: -rw-r--r-- 10,022 bytes parent folder | download | duplicates (3)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
* Copyright (c) 2003, the JUNG Project and the Regents of the University 
* of California
* All rights reserved.
*
* This software is open-source under the BSD license; see either
* "license.txt" or
* http://jung.sourceforge.net/license.txt for a description.
*/
package edu.uci.ics.jung.algorithms.shortestpath;

import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.collections.Transformer;

import edu.uci.ics.jung.graph.Graph;

/**
 * <p>Calculates distances and shortest paths using Dijkstra's   
 * single-source-shortest-path algorithm.  This is a lightweight
 * extension of <code>DijkstraDistance</code> that also stores
 * path information, so that the shortest paths can be reconstructed.</p>
 * 
 * <p> The elements in the maps returned by 
 * <code>getIncomingEdgeMap</code> are ordered (that is, returned 
 * by the iterator) by nondecreasing distance from <code>source</code>.</p>
 * 
 * @author Joshua O'Madadhain
 * @author Tom Nelson converted to jung2
 * @see DijkstraDistance
 */
public class DijkstraShortestPath<V,E> extends DijkstraDistance<V,E> implements ShortestPath<V,E>
{
    /**
     * <p>Creates an instance of <code>DijkstraShortestPath</code> for 
     * the specified graph and the specified method of extracting weights 
     * from edges, which caches results locally if and only if 
     * <code>cached</code> is <code>true</code>.
     * 
     * @param g     the graph on which distances will be calculated
     * @param nev   the class responsible for returning weights for edges
     * @param cached    specifies whether the results are to be cached
     */
    public DijkstraShortestPath(Graph<V,E> g, Transformer/*<E, ? extends Number>*/ nev, boolean cached)
    {
        super(g, nev, cached);
    }
    
    /**
     * <p>Creates an instance of <code>DijkstraShortestPath</code> for 
     * the specified graph and the specified method of extracting weights 
     * from edges, which caches results locally.
     * 
     * @param g     the graph on which distances will be calculated
     * @param nev   the class responsible for returning weights for edges
     */
    public DijkstraShortestPath(Graph<V,E> g, Transformer/*<E, ? extends Number>*/ nev)
    {
        super(g, nev);
    }
    
    /**
     * <p>Creates an instance of <code>DijkstraShortestPath</code> for 
     * the specified unweighted graph (that is, all weights 1) which
     * caches results locally.
     * 
     * @param g     the graph on which distances will be calculated
     */ 
    public DijkstraShortestPath(Graph<V,E> g)
    {
        super(g);
    }

    /**
     * <p>Creates an instance of <code>DijkstraShortestPath</code> for 
     * the specified unweighted graph (that is, all weights 1) which
     * caches results locally.
     * 
     * @param g     the graph on which distances will be calculated
     * @param cached    specifies whether the results are to be cached
     */ 
    public DijkstraShortestPath(Graph<V,E> g, boolean cached)
    {
        super(g, cached);
    }
    
    @Override
    protected SourceData getSourceData(V source)
    {
        SourceData sd = sourceMap.get(source);
        if (sd == null)
            sd = new SourcePathData(source);
        return sd;
    }
    
    /**
     * <p>Returns the last edge on a shortest path from <code>source</code>
     * to <code>target</code>, or null if <code>target</code> is not 
     * reachable from <code>source</code>.</p>
     * 
     * <p>If either vertex is not in the graph for which this instance
     * was created, throws <code>IllegalArgumentException</code>.</p>
     */
	public E getIncomingEdge(V source, V target)
	{
        if (!g.containsVertex(source))
            throw new IllegalArgumentException("Specified source vertex " + 
                    source + " is not part of graph " + g);
        
        if (!g.containsVertex(target))
            throw new IllegalArgumentException("Specified target vertex " + 
                    target + " is not part of graph " + g);

        Set<V> targets = new HashSet<V>();
        targets.add(target);
        singleSourceShortestPath(source, targets, g.getVertexCount());
        Map<V,E> incomingEdgeMap = 
            ((SourcePathData)sourceMap.get(source)).incomingEdges;
        E incomingEdge = incomingEdgeMap.get(target);
        
        if (!cached)
            reset(source);
        
        return incomingEdge;
	}

    /**
     * <p>Returns a <code>LinkedHashMap</code> which maps each vertex 
     * in the graph (including the <code>source</code> vertex) 
     * to the last edge on the shortest path from the 
     * <code>source</code> vertex.
     * The map's iterator will return the elements in order of 
     * increasing distance from <code>source</code>.</p>
     * 
     * @see DijkstraDistance#getDistanceMap(Object,int)
     * @see DijkstraDistance#getDistance(Object,Object)
     * @param source    the vertex from which distances are measured
     */
    public Map<V,E> getIncomingEdgeMap(V source)
	{
		return getIncomingEdgeMap(source, g.getVertexCount());
	}

    /**
     * Returns a <code>List</code> of the edges on the shortest path from 
     * <code>source</code> to <code>target</code>, in order of their
     * occurrence on this path.  
     * If either vertex is not in the graph for which this instance
     * was created, throws <code>IllegalArgumentException</code>.
     */
	public List<E> getPath(V source, V target)
	{
		if(!g.containsVertex(source)) 
            throw new IllegalArgumentException("Specified source vertex " + 
                    source + " is not part of graph " + g);
        
		if(!g.containsVertex(target)) 
            throw new IllegalArgumentException("Specified target vertex " + 
                    target + " is not part of graph " + g);
        
        LinkedList<E> path = new LinkedList<E>();

        // collect path data; must use internal method rather than
        // calling getIncomingEdge() because getIncomingEdge() may
        // wipe out results if results are not cached
        Set<V> targets = new HashSet<V>();
        targets.add(target);
        singleSourceShortestPath(source, targets, g.getVertexCount());
        Map<V,E> incomingEdges = 
            ((SourcePathData)sourceMap.get(source)).incomingEdges;
        
        if (incomingEdges.isEmpty() || incomingEdges.get(target) == null)
            return path;
        V current = target;
        while (!current.equals(source))
        {
            E incoming = incomingEdges.get(current);
            path.addFirst(incoming);
            current = ((Graph<V,E>)g).getOpposite(current, incoming);
        }
		return path;
	}

    
    /**
     * <p>Returns a <code>LinkedHashMap</code> which maps each of the closest 
     * <code>numDist</code> vertices to the <code>source</code> vertex 
     * in the graph (including the <code>source</code> vertex) 
     * to the incoming edge along the path from that vertex.  Throws 
     * an <code>IllegalArgumentException</code> if <code>source</code>
     * is not in this instance's graph, or if <code>numDests</code> is 
     * either less than 1 or greater than the number of vertices in the 
     * graph.
     * 
     * @see #getIncomingEdgeMap(Object)
     * @see #getPath(Object,Object)
     * @param source    the vertex from which distances are measured
     * @param numDests  the number of vertics for which to measure distances
     */
	public LinkedHashMap<V,E> getIncomingEdgeMap(V source, int numDests)
	{
        if (g.getVertices().contains(source) == false)
            throw new IllegalArgumentException("Specified source vertex " + 
                    source + " is not part of graph " + g);

        if (numDests < 1 || numDests > g.getVertexCount())
            throw new IllegalArgumentException("numDests must be >= 1 " + 
            "and <= g.numVertices()");

        singleSourceShortestPath(source, null, numDests);
        
        LinkedHashMap<V,E> incomingEdgeMap = 
            ((SourcePathData)sourceMap.get(source)).incomingEdges;
        
        if (!cached)
            reset(source);
        
        return incomingEdgeMap;        
	}
     
    
    /**
     * For a given source vertex, holds the estimated and final distances, 
     * tentative and final assignments of incoming edges on the shortest path from
     * the source vertex, and a priority queue (ordered by estimaed distance)
     * of the vertices for which distances are unknown.
     * 
     * @author Joshua O'Madadhain
     */
    protected class SourcePathData extends SourceData
    {
        protected Map<V,E> tentativeIncomingEdges;
		protected LinkedHashMap<V,E> incomingEdges;

		protected SourcePathData(V source)
		{
            super(source);
            incomingEdges = new LinkedHashMap<V,E>();
            tentativeIncomingEdges = new HashMap<V,E>();
		}
        
        @Override
        public void update(V dest, E tentative_edge, double new_dist)
        {
            super.update(dest, tentative_edge, new_dist);
            tentativeIncomingEdges.put(dest, tentative_edge);
        }
        
        @Override
        public Map.Entry<V,Number> getNextVertex()
        {
            Map.Entry<V,Number> p = super.getNextVertex();
            V v = p.getKey();
            E incoming = tentativeIncomingEdges.remove(v);
            incomingEdges.put(v, incoming);
            return p;
        }
        
        @Override
        public void restoreVertex(V v, double dist)
        {
            super.restoreVertex(v, dist);
            E incoming = incomingEdges.get(v);
            tentativeIncomingEdges.put(v, incoming);
        }
        
        @Override
        public void createRecord(V w, E e, double new_dist)
        {
            super.createRecord(w, e, new_dist);
            tentativeIncomingEdges.put(w, e);
        }
       
    }

}