001/* An analysis to compute the all pair shortest path of a directed graph.
002
003 Copyright (c) 2003-2014 The University of Maryland. All rights reserved.
004 Permission is hereby granted, without written agreement and without
005 license or royalty fees, to use, copy, modify, and distribute this
006 software and its documentation for any purpose, provided that the above
007 copyright notice and the following two paragraphs appear in all copies
008 of this software.
009
010 IN NO EVENT SHALL THE UNIVERSITY OF MARYLAND BE LIABLE TO ANY PARTY
011 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
012 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
013 THE UNIVERSITY OF MARYLAND HAS BEEN ADVISED OF THE POSSIBILITY OF
014 SUCH DAMAGE.
015
016 THE UNIVERSITY OF MARYLAND SPECIFICALLY DISCLAIMS ANY WARRANTIES,
017 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
018 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
019 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
020 MARYLAND HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
021 ENHANCEMENTS, OR MODIFICATIONS.
022
023
024 */
025package ptolemy.graph.analysis;
026
027import java.util.List;
028
029import ptolemy.graph.Graph;
030import ptolemy.graph.Node;
031import ptolemy.graph.analysis.analyzer.AllPairShortestPathAnalyzer;
032import ptolemy.graph.analysis.analyzer.Analyzer;
033import ptolemy.graph.analysis.strategy.FloydWarshallAllPairShortestPathStrategy;
034import ptolemy.graph.mapping.ToDoubleMapping;
035
036///////////////////////////////////////////////////////////////////
037//// AllPairShortestPathAnalysis
038
039/**
040 An analysis to compute of the all pair shortest path of a directed graph.
041 The result is in the form of two dimensional array (matrix).
042 The first dimension is indexed by the source node label while the second one is
043 indexed by the sink node label. In graphs that have multiple edges between two
044 nodes obviously the edge with the minimum weight is being considered for
045 the shortest path.
046 <p>
047 The distance between a node and itself is being considered Double.MAX_VALUE,
048 unless there is a self-edge.
049 <p>
050 The result of {@link #shortestPathMatrix()}[i][i] would be the
051 length of the shortest cycle that includes the node with label "i".
052 <p>
053
054 The default analyzer runs in O(N^3) in which N is the number of nodes.
055
056 @since Ptolemy II 4.0
057 @Pt.ProposedRating Red (shahrooz)
058 @Pt.AcceptedRating Red (ssb)
059 @version $Id$
060 @author Shahrooz Shahparnia
061 @see ptolemy.graph.Graph#nodeLabel
062 */
063public class AllPairShortestPathAnalysis extends Analysis {
064    /** Construct an instance of this class with a default analyzer.
065     *  The default analyzer runs in O(N^3) where N is the number of nodes.
066     *
067     *  @param graph The given graph.
068     *  @param edgeLength A mapping between the graph edges and double values,
069     *  which play the role of edge costs.
070     *
071     */
072    public AllPairShortestPathAnalysis(Graph graph,
073            ToDoubleMapping edgeLength) {
074        super(new FloydWarshallAllPairShortestPathStrategy(graph, edgeLength));
075    }
076
077    /** Construct an instance of this class with a given analyzer.
078     *
079     *  @param analyzer The given analyzer.
080     */
081    public AllPairShortestPathAnalysis(AllPairShortestPathAnalyzer analyzer) {
082        super(analyzer);
083    }
084
085    ///////////////////////////////////////////////////////////////////
086    ////                         public methods                    ////
087
088    /** Return the nodes on the shortest path from the node
089     *  "startNode" to the node "endNode" in the form of an ordered list.
090     *
091     *  @param startNode The starting node of the path.
092     *  @param endNode The ending node of the path.
093     *  @return Return the nodes on the shortest path from the
094     *  node "startNode" to the node "endNode" in the form of an ordered list.
095     */
096    public List shortestPath(Node startNode, Node endNode) {
097        return ((AllPairShortestPathAnalyzer) analyzer())
098                .shortestPath(startNode, endNode);
099    }
100
101    /** Return the length of the shortest path from the node
102     *  startNode to the node endNode.
103     *
104     *  @param startNode The starting node of the path.
105     *  @param endNode The end node of the path.
106     *  @return Return the length of the shortest path from the node
107     *  "startNode" to the node "endNode".
108     */
109    public double shortestPathLength(Node startNode, Node endNode) {
110        return ((AllPairShortestPathAnalyzer) analyzer())
111                .shortestPathLength(startNode, endNode);
112    }
113
114    /** Return a matrix representing the result of the all pair shortest path
115     *  algorithm.
116     *  The first dimension is indexed by the source node label while the
117     *  second one is indexed by the sink node label.
118     *  The result of {@link #shortestPathMatrix()}[i][i] would be
119     *  the length of the shortest cycle that includes the node with label "i"
120     *  and the result of {@link #shortestPathMatrix()}[i][j] would be
121     *  the length of the shortest from the node with label "i" to the node
122     *  with label "j".
123     *
124     *  @return Return a matrix representing the result of the all pair shortest
125     *  path algorithm.
126     */
127    public double[][] shortestPathMatrix() {
128        return ((AllPairShortestPathAnalyzer) analyzer()).shortestPathMatrix();
129    }
130
131    /** Return a description of the analysis and the associated analyzer.
132     *
133     *  @return A description of the analysis and the associated analyzer.
134     */
135    @Override
136    public String toString() {
137        return "All pair shortest path analysis using the following analyzer:\n"
138                + analyzer().toString();
139    }
140
141    /** Check if a given analyzer is compatible with this analysis.
142     *  In other words if it is possible to use it to compute the computation
143     *  associated with this analysis.
144     *
145     *  @param analyzer The given analyzer.
146     *  @return True if the given analyzer is valid for this analysis.
147     */
148    @Override
149    public boolean validAnalyzerInterface(Analyzer analyzer) {
150        return analyzer instanceof AllPairShortestPathAnalyzer;
151    }
152}