001/* Analysis to check if a cyclic directed graph has a zero-length cycle. 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 ptolemy.graph.Graph; 028import ptolemy.graph.analysis.analyzer.Analyzer; 029import ptolemy.graph.analysis.analyzer.ZeroLengthCycleAnalyzer; 030import ptolemy.graph.analysis.strategy.FloydWarshallZeroLengthCycleStrategy; 031import ptolemy.graph.mapping.ToDoubleMapping; 032 033/////////////////////////////////////////////////////////////////// 034//// ZeroLengthCycleAnalysis 035 036/** 037 Analysis to check if a cyclic directed graph has a zero-length cycle. 038 A zero-length cycle is a cycle in which the sum of all the values associated 039 with the edges of the cycle is zero. 040 <p> 041 @since Ptolemy II 4.0 042 @Pt.ProposedRating Red (shahrooz) 043 @Pt.AcceptedRating Red (ssb) 044 @author Shahrooz Shahparnia 045 @version $Id$ 046 */ 047public class ZeroLengthCycleAnalysis extends Analysis { 048 /** Construct an instance of this class using a default analyzer. 049 * The default analyzer runs in O(N^3) in which N is the number of nodes. 050 * 051 * @param graph The given graph. 052 * @param edgeLengths The lengths associated with the edges of the graph. 053 */ 054 public ZeroLengthCycleAnalysis(Graph graph, ToDoubleMapping edgeLengths) { 055 super(new FloydWarshallZeroLengthCycleStrategy(graph, edgeLengths)); 056 } 057 058 /** Construct an instance of this class using a given analyzer. 059 * 060 * @param analyzer The given analyzer. 061 */ 062 public ZeroLengthCycleAnalysis(ZeroLengthCycleAnalyzer analyzer) { 063 super(analyzer); 064 } 065 066 /////////////////////////////////////////////////////////////////// 067 //// public methods //// 068 069 /** Return true if a zero length cycle exists in the graph under analysis. 070 * 071 * @return True if the graph has a zero length cycle. 072 */ 073 public boolean hasZeroLengthCycle() { 074 return ((ZeroLengthCycleAnalyzer) analyzer()).hasZeroLengthCycle(); 075 } 076 077 /** Return a description of the analysis and the associated analyzer. 078 * 079 * @return A description of the analysis and the associated analyzer. 080 */ 081 @Override 082 public String toString() { 083 return "Zero-length cycle analysis using the following analyzer:\n" 084 + analyzer().toString(); 085 } 086 087 /** Check if a given analyzer is compatible with this analysis. 088 * In other words if it is possible to use it to compute the computation 089 * associated with this analysis. 090 * 091 * @param analyzer The given analyzer. 092 * @return True if the given analyzer is valid for this analysis. 093 */ 094 @Override 095 public boolean validAnalyzerInterface(Analyzer analyzer) { 096 return analyzer instanceof ZeroLengthCycleAnalyzer; 097 } 098}