00001 ///////////////////////////////////////////////////////////////////////////// 00002 /// 00003 /// @file graph.h 00004 /// 00005 /// A directed graph class and some algorithms. 00006 /// This file contains the definition of a class that represents 00007 /// a directed graph with a limited number of operations on the graph, 00008 /// optimized for the particular application. 00009 /// This specific definition is a simple wrapper for the class "diGraph" 00010 /// borrowed from the CHomP package. 00011 /// 00012 /// @author Pawel Pilarczyk 00013 /// 00014 ///////////////////////////////////////////////////////////////////////////// 00015 00016 // Copyright (C) 1997-2010 by Pawel Pilarczyk. 00017 // 00018 // This file is part of my research software package. This is free software; 00019 // you can redistribute it and/or modify it under the terms of the GNU 00020 // General Public License as published by the Free Software Foundation; 00021 // either version 2 of the License, or (at your option) any later version. 00022 // 00023 // This software is distributed in the hope that it will be useful, 00024 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00025 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00026 // GNU General Public License for more details. 00027 // 00028 // You should have received a copy of the GNU General Public License along 00029 // with this software; see the file "license.txt". If not, write to the 00030 // Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, 00031 // MA 02111-1307, USA. 00032 00033 // Started on December 22, 2008. Last revision: December 22, 2008. 00034 00035 00036 #ifndef _FINRESDYN_GRAPH_H_ 00037 #define _FINRESDYN_GRAPH_H_ 00038 00039 00040 // include some standard C++ header files 00041 #include <iostream> 00042 #include <vector> 00043 00044 // include selected header files from the CHomP library 00045 #include "chomp/struct/digraph.h" 00046 #include "chomp/struct/bitfield.h" 00047 00048 00049 // -------------------------------------------------- 00050 // ---------------- the graph class ----------------- 00051 // -------------------------------------------------- 00052 00053 /// A directed graph class with some algorithms built-in. 00054 /// This class defines a directed graph which is optimized 00055 /// for the particular application in this project. 00056 /// It supports a very limited number of operations, 00057 /// but these operations are very fast. This class also implements 00058 /// a few algorithms necessary in this project. 00059 /// This particular implementation is a wrapper for the class "diGraph" 00060 /// available in the CHomP software package. 00061 /// Note: The default constructor, destructor and assignment operator 00062 /// are good for this class, so none are defined. 00063 class Graph 00064 { 00065 public: 00066 /// Adds a vertex. 00067 void addVertex (void); 00068 00069 /// Adds an edge starting at the last vertex. 00070 /// Note: The range of the target vertex number is not verified. 00071 void addEdge (int target); 00072 00073 /// Returns the number of vertices. 00074 int countVertices (void) const; 00075 00076 /// Returns the number of edges. 00077 int countEdges (void) const; 00078 00079 /// Counts the number of edges leaving the given vertex. 00080 int countEdges (int vertex) const; 00081 00082 /// Retrieves the given edge that leaves the given vertex. 00083 int getEdge (int vertex, int i) const; 00084 00085 /// Verifies if the graph is strongly connected. 00086 bool checkStronglyConnected () const; 00087 00088 /// Verifies if the graph is aperiodic. 00089 bool checkAperiodic () const; 00090 00091 private: 00092 /// The underlying graph object. 00093 chomp::homology::diGraph<> g; 00094 00095 }; /* class Graph */ 00096 00097 // -------------------------------------------------- 00098 00099 inline void Graph::addVertex (void) 00100 { 00101 g. addVertex (); 00102 return; 00103 } /* Graph::addVertex */ 00104 00105 inline void Graph::addEdge (int target) 00106 { 00107 g. addEdge (target); 00108 return; 00109 } /* Graph::addEdge */ 00110 00111 inline int Graph::countVertices (void) const 00112 { 00113 return g. countVertices (); 00114 } /* Graph::countVertices */ 00115 00116 inline int Graph::countEdges (void) const 00117 { 00118 return g. countEdges (); 00119 } /* Graph::countEdges */ 00120 00121 inline int Graph::countEdges (int vertex) const 00122 { 00123 return g. countEdges (vertex); 00124 } /* Graph::countEdges */ 00125 00126 inline int Graph::getEdge (int vertex, int i) const 00127 { 00128 return g. getEdge (vertex, i); 00129 } /* Graph::getEdge */ 00130 00131 // -------------------------------------------------- 00132 00133 /// This function uses Tarjan's algorithm (as described in the Wikipedia) 00134 /// for the computation of strongly connected components 00135 /// in order to determine whether the given graph is strongly connected. 00136 /// Tha advantage of this approach over the one described in Cormen's 00137 /// textbook is that the transposed graph need not be computed. 00138 inline bool Graph::checkStronglyConnected () const 00139 { 00140 chomp::homology::dummyArray compVertices, compEnds; 00141 int n = chomp::homology::SCC_Tarjan (g, compVertices, compEnds); 00142 return ((n == 1) && (compEnds [0] == g. countVertices ())); 00143 } /* Graph::checkStronglyConnected */ 00144 00145 inline bool Graph::checkAperiodic () const 00146 { 00147 int period = chomp::homology::computePeriod (g); 00148 return (period == 1); 00149 } /* Graph::checkAperiodic */ 00150 00151 00152 #endif // _FINRESDYN_GRAPH_H_ 00153
1.5.3