2 * Created on Jul 10, 2005
4 * Copyright (c) 2005, the JUNG Project and the Regents of the University
8 * This software is open-source under the BSD license; see either
10 * http://jung.sourceforge.net/license.txt for a description.
12 package edu.uci.ics.jung.algorithms.shortestpath;
14 import java.util.LinkedList;
15 import java.util.List;
18 import edu.uci.ics.jung.graph.Graph;
19 import edu.uci.ics.jung.graph.util.Pair;
22 * Utilities relating to the shortest paths in a graph.
24 public class ShortestPathUtils
27 * Returns a <code>List</code> of the edges on the shortest path from
28 * <code>source</code> to <code>target</code>, in order of their
29 * occurrence on this path.
31 public static <V, E> List<E> getPath(Graph<V,E> graph, ShortestPath<V,E> sp, V source, V target)
33 LinkedList<E> path = new LinkedList<E>();
35 Map<V,E> incomingEdges = sp.getIncomingEdgeMap(source);
37 if (incomingEdges.isEmpty() || incomingEdges.get(target) == null)
40 while (!current.equals(source))
42 E incoming = incomingEdges.get(current);
43 path.addFirst(incoming);
44 Pair<V> endpoints = graph.getEndpoints(incoming);
45 if(endpoints.getFirst().equals(current)) {
46 current = endpoints.getSecond();
48 current = endpoints.getFirst();
50 //incoming.getOpposite(current);