HOG2
Graph.h
Go to the documentation of this file.
1 /*
2  * $Id: Graph.h
3  * hog2
4  *
5  * Created by Nathan Sturtevant on 09/18/06.
6  * Modified by Nathan Sturtevant on 02/29/20.
7  *
8  * This file is part of HOG2. See https://github.com/nathansttt/hog2 for licensing information.
9  *
10  */
11 
12 // HOG File
13 
14 #ifndef GRAPH_H
15 #define GRAPH_H
16 
17 #include <limits.h>
18 #include <vector>
19 #include <list>
20 #include <iostream>
21 #include <stdio.h>
22 
23 //#include <values.h>
24 #define MAXINT INT_MAX
25 //#define MAXINT (1<<30)
26 //#define MAXLABELS 15
27 
28 class Graph;
29 class node;
30 class edge;
31 
32 typedef std::vector<edge *>::const_iterator edge_iterator;
33 typedef std::vector<node *>::const_iterator node_iterator;
34 typedef unsigned int neighbor_iterator;
35 
36 typedef union { double fval; long lval; } labelValue;
37 
39 enum {
41 };
42 
47 class graph_object {
48 public:
49  graph_object():key(0) {}
50  virtual ~graph_object() {}
51  virtual double GetKey() { return 0; }
52  virtual void Print(std::ostream&) const;
53  virtual graph_object *Clone() const = 0;
54  unsigned int key; // for use by a data structure to maintain a reverse-lookup
55  // to go from an object to a table key in constant time.
56 private:
57  // double val;
58 };
59 
60 std::ostream& operator <<(std::ostream & out, const graph_object &_Obj);
61 
66 class Graph : public graph_object {
67 public:
68  Graph();
69  ~Graph();
70  graph_object *Clone() const; // clones just the nodes
71  Graph *cloneAll() const; // clones everything
72  void Reset();
73 
74  void Save(const char *file);
75  void Load(const char *file);
76 
77  void Export(const char *fname);
78 
79  int AddNode(node *);
80  node *GetNode(unsigned long num);
81  const node *GetNode(unsigned long num) const;
82  edge *GetEdge(unsigned long num);
83  void AddEdge(edge *);
84  edge *findDirectedEdge(unsigned int from, unsigned int to);
85  edge *FindEdge(unsigned int from, unsigned int to);
86  const edge *FindEdge(unsigned int from, unsigned int to) const;
87 
88  bool relax(edge *e, int weightIndex);
89  bool relaxReverseEdge(edge *e, int weightIndex);
90 
93 
94  node_iterator getNodeIter() const;
96  edge_iterator getEdgeIter() const;
98 
99  void RemoveEdge(edge *);
100  // returns the node that had it's node number changed along with its previous node number, if any
101  node *RemoveNode(node *, unsigned int&);
102  void RemoveNode(node *n) { unsigned int x; RemoveNode(n, x); } // if you don't care about node #'s
103  void RemoveNode(unsigned int nodeNum) { RemoveNode(GetNode(nodeNum)); }
104 
105  int GetNumEdges();
106  int GetNumNodes();
107 
108  std::vector<node*>* getReachableNodes(node* start);
109 
110  bool verifyGraph() const;
111  void Print(std::ostream&) const;
112  void printStats();
113 private:
114  std::vector<node *> _nodes;
115  //unsigned int node_index;
116  std::vector<edge *> _edges;
117  //unsigned int edge_index;
118 };
119 
120 // Moved to GraphAbstraction.h
121 // enum {
122 // kEdgeWeight = 0,
123 // kEdgeWidth = 1
124 // };
125 
129 class edge : public graph_object {
130 public:
131  edge(unsigned int, unsigned int, double);
132  graph_object *Clone() const { return new edge(from, to, GetLabelF(kEdgeWeight)); }
133 
134  // set/get various labels for each node
135  void SetLabelF(unsigned int index, double val);
136  void SetLabelL(unsigned int index, long val);
137  inline double GetLabelF(unsigned int index) const { if (index < label.size()) return label[index].fval; return MAXINT; }
138  inline long GetLabelL(unsigned int index) const { if (index < label.size()) return label[index].lval; return MAXINT; }
139 
140  double GetWeight() { return GetLabelF(kEdgeWeight); }
141  void setWeight(double val) { SetLabelF(kEdgeWeight, val); }
142 
143  // double getWidth() { return GetLabelF(kEdgeWidth); }
144  // void setWidth(double val) { SetLabelF(kEdgeWidth, val); }
145 
146  unsigned int getFrom() const { return from; }
147  unsigned int getTo() const { return to; }
148 
149  void setMarked(bool marked) { mark = marked; }
150  bool getMarked() { return mark; }
151 
152  int getEdgeNum() const { return edgeNum; }
153 
154  void Print(std::ostream&) const;
155 private:
156  friend class Graph;
157  bool mark;
158  unsigned int from, to;
159  // double weight;
160  // double width;
161  unsigned int edgeNum;//, label[MAXLABELS];
162 
163  std::vector<labelValue> label;
164 };
165 
170 class node : public graph_object {
171 public:
172  node(const char *);
173  graph_object *Clone() const;
174 
175  const char *GetName() const { return name; }
176  unsigned int GetNum() const { return nodeNum; }
177  int getUniqueID() const { return uniqueID; }
178  void AddEdge(edge *);
179  void RemoveEdge(edge *);
180 
181  // iterator over incoming edges
184  // iterator over outcoming edges
187  // iterate over all edges
189  edge_iterator getEdgeIter() const;
190 
191  edge *getEdge(unsigned int which);
192 
193  // iterate over all neighbors
194  // don't return node* because we don't have access to them internally
197 
200  edge *GetRandomEdge();
201 
202  int getNumOutgoingEdges();
203  int getNumIncomingEdges();
205 
206  // chooses which label will be used as the key for
207  // priority queue
208  void SetKeyLabel(int which) { keyLabel = which; }
209  int GetKeyLabel() { return keyLabel; }
210  double GetKey() { return label[keyLabel].fval; }
211 
212  // set/get various labels for each node
213  void SetLabelF(unsigned int index, double val) const;
214  void SetLabelL(unsigned int index, long val) const;
215  inline double GetLabelF(unsigned int index) const {
216  if (index < label.size())
217  return label[index].fval;
218  return MAXINT;
219  }
220  inline long GetLabelL(unsigned int index) const {
221  if (index < label.size())
222  return label[index].lval;
223  return MAXINT;
224  }
225 
226  // set/get marked edge for each node (limit 1)
227  void markEdge(edge *e) { markedEdge = e; }
229 
230  void Print(std::ostream&) const;
231 private:
232  friend class Graph;
233  unsigned int nodeNum;//, label[MAXLABELS];
234  mutable std::vector<labelValue> label;
236  std::vector<edge *> _edgesOutgoing;
237  std::vector<edge *> _edgesIncoming;
238  std::vector<edge *> _allEdges;
239  char name[30];
240  int keyLabel;
241 
242  int uniqueID;
243  static unsigned int uniqueIDCounter;
244 };
245 
246 std::ostream& operator <<(std::ostream & out, const Graph &_Graph);
247 std::ostream& operator <<(std::ostream & out, const node &_Node);
248 std::ostream& operator <<(std::ostream & out, const edge &_Edge);
249 
250 #endif
node::_allEdges
std::vector< edge * > _allEdges
Definition: Graph.h:238
Graph::AddNode
int AddNode(node *)
Definition: Graph.cpp:136
node::SetLabelL
void SetLabelL(unsigned int index, long val) const
Definition: Graph.cpp:702
edge::GetWeight
double GetWeight()
Definition: Graph.h:140
node::getMarkedEdge
edge * getMarkedEdge()
Definition: Graph.h:228
graph_object::graph_object
graph_object()
Definition: Graph.h:49
node::SetLabelF
void SetLabelF(unsigned int index, double val) const
Definition: Graph.cpp:687
edge::getFrom
unsigned int getFrom() const
Definition: Graph.h:146
edge::getTo
unsigned int getTo() const
Definition: Graph.h:147
Graph::RemoveNode
node * RemoveNode(node *, unsigned int &)
Definition: Graph.cpp:356
Graph::FindEdge
edge * FindEdge(unsigned int from, unsigned int to)
Finds an edge between nodes with ids from and to, no matter which direction.
Definition: Graph.cpp:230
neighbor_iterator
unsigned int neighbor_iterator
Definition: Graph.h:34
node::getOutgoingEdgeIter
edge_iterator getOutgoingEdgeIter() const
Definition: Graph.cpp:733
graph_object::key
unsigned int key
Definition: Graph.h:54
edge::from
unsigned int from
Definition: Graph.h:158
labelValue
Definition: Graph.h:36
edge::getMarked
bool getMarked()
Definition: Graph.h:150
node::edgeIterNextIncoming
edge * edgeIterNextIncoming(edge_iterator &) const
Definition: Graph.cpp:722
edge::setWeight
void setWeight(double val)
Definition: Graph.h:141
node::RemoveEdge
void RemoveEdge(edge *)
Definition: Graph.cpp:650
node::node
node(const char *)
Definition: Graph.cpp:616
node::name
char name[30]
Definition: Graph.h:239
graph_object::Print
virtual void Print(std::ostream &) const
Definition: Graph.cpp:22
Graph::AddEdge
void AddEdge(edge *)
Definition: Graph.cpp:170
edge::setMarked
void setMarked(bool marked)
Definition: Graph.h:149
Graph::nodeIterNext
node * nodeIterNext(node_iterator &) const
Definition: Graph.cpp:303
Graph::verifyGraph
bool verifyGraph() const
Definition: Graph.cpp:496
edge_iterator
std::vector< edge * >::const_iterator edge_iterator
Definition: Graph.h:30
node::uniqueIDCounter
static unsigned int uniqueIDCounter
Definition: Graph.h:243
node::edgeIterNextOutgoing
edge * edgeIterNextOutgoing(edge_iterator &) const
Definition: Graph.cpp:738
edge::label
std::vector< labelValue > label
Definition: Graph.h:163
Graph::RemoveEdge
void RemoveEdge(edge *)
Definition: Graph.cpp:331
graph_object::Clone
virtual graph_object * Clone() const =0
node::Print
void Print(std::ostream &) const
Definition: Graph.cpp:820
Graph
A generic Graph class.
Definition: Graph.h:66
Graph::Reset
void Reset()
Definition: Graph.cpp:45
Graph::GetNode
node * GetNode(unsigned long num)
Definition: Graph.cpp:152
edge::edge
edge(unsigned int, unsigned int, double)
Definition: Graph.cpp:570
node::getEdge
edge * getEdge(unsigned int which)
Definition: Graph.cpp:794
Graph::edgeIterNext
edge * edgeIterNext(edge_iterator &) const
Definition: Graph.cpp:320
Graph::_nodes
std::vector< node * > _nodes
Definition: Graph.h:114
Graph::Print
void Print(std::ostream &) const
Definition: Graph.cpp:446
node::markEdge
void markEdge(edge *e)
Definition: Graph.h:227
graph_object
Parent class for nodes and edges allowing them to be stored in a Heap or manipulated with other data ...
Definition: Graph.h:47
node_iterator
std::vector< node * >::const_iterator node_iterator
Definition: Graph.h:33
edge::edgeNum
unsigned int edgeNum
Definition: Graph.h:161
edge::GetLabelL
long GetLabelL(unsigned int index) const
Definition: Graph.h:138
node::getEdgeIter
edge_iterator getEdgeIter() const
Definition: Graph.cpp:749
Graph::cloneAll
Graph * cloneAll() const
Definition: Graph.cpp:92
node::_edgesOutgoing
std::vector< edge * > _edgesOutgoing
Definition: Graph.h:236
edge::Clone
graph_object * Clone() const
Definition: Graph.h:132
edge::SetLabelF
void SetLabelF(unsigned int index, double val)
Definition: Graph.cpp:583
node::GetKeyLabel
int GetKeyLabel()
Definition: Graph.h:209
node::SetKeyLabel
void SetKeyLabel(int which)
Definition: Graph.h:208
node::AddEdge
void AddEdge(edge *)
Definition: Graph.cpp:636
Graph::Clone
graph_object * Clone() const
Definition: Graph.cpp:77
Graph::getEdgeIter
edge_iterator getEdgeIter() const
Definition: Graph.cpp:315
Graph::Export
void Export(const char *fname)
Definition: Graph.cpp:116
node::keyLabel
int keyLabel
Definition: Graph.h:240
Graph::GetNumEdges
int GetNumEdges()
Definition: Graph.cpp:397
node::GetRandomEdge
edge * GetRandomEdge()
Definition: Graph.cpp:777
edge::SetLabelL
void SetLabelL(unsigned int index, long val)
Definition: Graph.cpp:598
Graph::RemoveNode
void RemoveNode(node *n)
Definition: Graph.h:102
node::getRandomIncomingEdge
edge * getRandomIncomingEdge()
Definition: Graph.cpp:765
node::getRandomOutgoingEdge
edge * getRandomOutgoingEdge()
Definition: Graph.cpp:771
node::getUniqueID
int getUniqueID() const
Definition: Graph.h:177
node::getIncomingEdgeIter
edge_iterator getIncomingEdgeIter() const
Definition: Graph.cpp:717
Graph::Graph
Graph()
Definition: Graph.cpp:33
Graph::GetRandomEdge
edge * GetRandomEdge()
Definition: Graph.cpp:288
edge::getEdgeNum
int getEdgeNum() const
Definition: Graph.h:152
graph_object::GetKey
virtual double GetKey()
Definition: Graph.h:51
Graph::GetNumNodes
int GetNumNodes()
Definition: Graph.cpp:403
Graph::Save
void Save(const char *file)
Graph::getNodeIter
node_iterator getNodeIter() const
Definition: Graph.cpp:298
node::nodeNeighborNext
int nodeNeighborNext(neighbor_iterator &) const
Definition: Graph.cpp:807
edge::Print
void Print(std::ostream &) const
Definition: Graph.cpp:578
node::nodeNum
unsigned int nodeNum
Definition: Graph.h:233
node::label
std::vector< labelValue > label
Definition: Graph.h:234
node::GetLabelF
double GetLabelF(unsigned int index) const
Definition: Graph.h:215
Graph::relaxReverseEdge
bool relaxReverseEdge(edge *e, int weightIndex)
Definition: Graph.cpp:267
node::GetKey
double GetKey()
Definition: Graph.h:210
Graph::printStats
void printStats()
Definition: Graph.cpp:467
Graph::GetRandomNode
node * GetRandomNode()
Definition: Graph.cpp:281
Graph::~Graph
~Graph()
Definition: Graph.cpp:39
node::GetNum
unsigned int GetNum() const
Definition: Graph.h:176
MAXINT
#define MAXINT
Definition: Graph.h:24
Graph::RemoveNode
void RemoveNode(unsigned int nodeNum)
Definition: Graph.h:103
edge::GetLabelF
double GetLabelF(unsigned int index) const
Definition: Graph.h:137
node::Clone
graph_object * Clone() const
Definition: Graph.cpp:628
node::uniqueID
int uniqueID
Definition: Graph.h:242
node::GetLabelL
long GetLabelL(unsigned int index) const
Definition: Graph.h:220
Graph::getReachableNodes
std::vector< node * > * getReachableNodes(node *start)
Definition: Graph.cpp:409
kEdgeWeight
@ kEdgeWeight
Definition: Graph.h:40
node::getNeighborIter
neighbor_iterator getNeighborIter() const
Definition: Graph.cpp:802
Graph::_edges
std::vector< edge * > _edges
Definition: Graph.h:116
node::_edgesIncoming
std::vector< edge * > _edgesIncoming
Definition: Graph.h:237
Graph::findDirectedEdge
edge * findDirectedEdge(unsigned int from, unsigned int to)
Definition: Graph.cpp:189
node::edgeIterNext
edge * edgeIterNext(edge_iterator &) const
Definition: Graph.cpp:754
edge::mark
bool mark
Definition: Graph.h:157
labelValue::lval
long lval
Definition: Graph.h:36
node::getNumIncomingEdges
int getNumIncomingEdges()
Definition: Graph.cpp:789
node::GetNumEdges
int GetNumEdges()
Definition: Graph.h:204
node
Nodes to be stored within a Graph.
Definition: Graph.h:170
edge::to
unsigned int to
Definition: Graph.h:158
node::markedEdge
edge * markedEdge
Definition: Graph.h:235
node::getNumOutgoingEdges
int getNumOutgoingEdges()
Definition: Graph.cpp:784
Graph::Load
void Load(const char *file)
graph_object::~graph_object
virtual ~graph_object()
Definition: Graph.h:50
Graph::GetEdge
edge * GetEdge(unsigned long num)
Definition: Graph.cpp:164
edge
Edge class for connections between node in a Graph.
Definition: Graph.h:129
node::GetName
const char * GetName() const
Definition: Graph.h:175
Graph::relax
bool relax(edge *e, int weightIndex)
Definition: Graph.cpp:251
operator<<
std::ostream & operator<<(std::ostream &out, const graph_object &_Obj)