The Finite Resolution Dynamics Software
graph.h
Go to the documentation of this file.
1 /////////////////////////////////////////////////////////////////////////////
2 ///
3 /// @file graph.h
4 ///
5 /// A directed graph class and some algorithms.
6 /// This file contains the definition of a class that represents
7 /// a directed graph with a limited number of operations on the graph,
8 /// optimized for the particular application.
9 /// This specific definition is a simple wrapper for the class "diGraph"
10 /// borrowed from the CHomP package.
11 ///
12 /// @author Pawel Pilarczyk
13 ///
14 /////////////////////////////////////////////////////////////////////////////
15 
16 // Copyright (C) 1997-2010 by Pawel Pilarczyk.
17 //
18 // This file is part of my research software package. This is free software;
19 // you can redistribute it and/or modify it under the terms of the GNU
20 // General Public License as published by the Free Software Foundation;
21 // either version 2 of the License, or (at your option) any later version.
22 //
23 // This software is distributed in the hope that it will be useful,
24 // but WITHOUT ANY WARRANTY; without even the implied warranty of
25 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 // GNU General Public License for more details.
27 //
28 // You should have received a copy of the GNU General Public License along
29 // with this software; see the file "license.txt". If not, write to the
30 // Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
31 // MA 02111-1307, USA.
32 
33 // Started on December 22, 2008. Last revision: December 22, 2008.
34 
35 
36 #ifndef _FINRESDYN_GRAPH_H_
37 #define _FINRESDYN_GRAPH_H_
38 
39 
40 // include some standard C++ header files
41 #include <iostream>
42 #include <vector>
43 
44 // include selected header files from the CHomP library
45 #include "chomp/struct/digraph.h"
46 #include "chomp/struct/bitfield.h"
47 
48 
49 // --------------------------------------------------
50 // ---------------- the graph class -----------------
51 // --------------------------------------------------
52 
53 /// A directed graph class with some algorithms built-in.
54 /// This class defines a directed graph which is optimized
55 /// for the particular application in this project.
56 /// It supports a very limited number of operations,
57 /// but these operations are very fast. This class also implements
58 /// a few algorithms necessary in this project.
59 /// This particular implementation is a wrapper for the class "diGraph"
60 /// available in the CHomP software package.
61 /// Note: The default constructor, destructor and assignment operator
62 /// are good for this class, so none are defined.
63 class Graph
64 {
65 public:
66  /// Adds a vertex.
67  void addVertex (void);
68 
69  /// Adds an edge starting at the last vertex.
70  /// Note: The range of the target vertex number is not verified.
71  void addEdge (int target);
72 
73  /// Returns the number of vertices.
74  int countVertices (void) const;
75 
76  /// Returns the number of edges.
77  int countEdges (void) const;
78 
79  /// Counts the number of edges leaving the given vertex.
80  int countEdges (int vertex) const;
81 
82  /// Retrieves the given edge that leaves the given vertex.
83  int getEdge (int vertex, int i) const;
84 
85  /// Verifies if the graph is strongly connected.
86  bool checkStronglyConnected () const;
87 
88  /// Verifies if the graph is aperiodic.
89  bool checkAperiodic () const;
90 
91 private:
92  /// The underlying graph object.
93  chomp::homology::diGraph<> g;
94 
95 }; /* class Graph */
96 
97 // --------------------------------------------------
98 
99 inline void Graph::addVertex (void)
100 {
101  g. addVertex ();
102  return;
103 } /* Graph::addVertex */
104 
105 inline void Graph::addEdge (int target)
106 {
107  g. addEdge (target);
108  return;
109 } /* Graph::addEdge */
110 
111 inline int Graph::countVertices (void) const
112 {
113  return g. countVertices ();
114 } /* Graph::countVertices */
115 
116 inline int Graph::countEdges (void) const
117 {
118  return g. countEdges ();
119 } /* Graph::countEdges */
120 
121 inline int Graph::countEdges (int vertex) const
122 {
123  return g. countEdges (vertex);
124 } /* Graph::countEdges */
125 
126 inline int Graph::getEdge (int vertex, int i) const
127 {
128  return g. getEdge (vertex, i);
129 } /* Graph::getEdge */
130 
131 // --------------------------------------------------
132 
133 /// This function uses Tarjan's algorithm (as described in the Wikipedia)
134 /// for the computation of strongly connected components
135 /// in order to determine whether the given graph is strongly connected.
136 /// Tha advantage of this approach over the one described in Cormen's
137 /// textbook is that the transposed graph need not be computed.
138 inline bool Graph::checkStronglyConnected () const
139 {
140  chomp::homology::dummyArray compVertices, compEnds;
141  int n = chomp::homology::SCC_Tarjan (g, compVertices, compEnds);
142  return ((n == 1) && (compEnds [0] == g. countVertices ()));
143 } /* Graph::checkStronglyConnected */
144 
145 inline bool Graph::checkAperiodic () const
146 {
147  int period = chomp::homology::computePeriod (g);
148  return (period == 1);
149 } /* Graph::checkAperiodic */
150 
151 
152 #endif // _FINRESDYN_GRAPH_H_
153 
int countEdges(void) const
Returns the number of edges.
Definition: graph.h:116
A directed graph class with some algorithms built-in.
Definition: graph.h:63
void addVertex(void)
Adds a vertex.
Definition: graph.h:99
chomp::homology::diGraph g
The underlying graph object.
Definition: graph.h:93
bool checkAperiodic() const
Verifies if the graph is aperiodic.
Definition: graph.h:145
int countVertices(void) const
Returns the number of vertices.
Definition: graph.h:111
void addEdge(int target)
Adds an edge starting at the last vertex.
Definition: graph.h:105
int getEdge(int vertex, int i) const
Retrieves the given edge that leaves the given vertex.
Definition: graph.h:126
bool checkStronglyConnected() const
Verifies if the graph is strongly connected.
Definition: graph.h:138