-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.h
158 lines (123 loc) · 3.67 KB
/
graph.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#ifndef __GRAPH__
#define __GRAPH__
#include <set>
using namespace std;
template<typename V, typename D>
class DefaultEdge {
public:
DefaultEdge(V* s, V* t, D* d = NULL) : source(s), target(t), data(d) {}
V* getSource() const {
return source;
}
V* getTarget() const {
return target;
}
D* getData() const {
return data;
}
void print() {
if (data != NULL)
cout << *source << " -" << *data << "-> " << *target << endl;
else
cout << *source << " --> " << *target << endl;
}
private:
V *source, *target;
D *data;
};
template<typename V, template<typename,typename> class E, typename D>
struct EdgeCompare {
bool operator()(const E<V,D>* firstEdge, const E<V,D>* secondEdge) {
V *s1 = firstEdge->getSource(),
*e1 = firstEdge->getTarget(),
*s2 = secondEdge->getSource(),
*e2 = secondEdge->getTarget();
return (s1 < s2) || ((s1 == s2) && (e1 < e2));
}
};
template<typename V, template<typename, typename> class E, typename D>
class Graph {
public:
typedef set<E<V,D>*, EdgeCompare<V,E,D> > EdgeSet;
typedef typename EdgeSet::iterator EdgeSetIterator;
Graph(set<V*> vs, EdgeSet es)
: vertices(vs), edges(es) {}
Graph() {}
E<V,D>* addEdge(V* source, V* target, D* data = NULL) {
vertices.insert(source);
vertices.insert(target);
E<V,D>* newEdge = new E<V,D>(source, target, data);
EdgeSetIterator it = edges.find(newEdge);
if (it == edges.end()) {
edges.insert(newEdge);
return newEdge;
} else {
delete newEdge;
return *it;
}
}
set<V*>& getAllVertices() {
return vertices;
}
set<V*> verticesFrom(V* source) {
set<V*> fromVertices;
EdgeSetIterator it;
for (it = edges.begin(); it != edges.end(); ++it) {
E<V,D>* edge = *it;
if (edge->getSource() == source) {
fromVertices.insert(edge->getTarget());
}
}
return fromVertices;
}
set<V*> verticesTo(V* target) {
set<V*> toVertices;
EdgeSetIterator it;
for (it = edges.begin(); it != edges.end(); ++it) {
E<V,D>* edge = *it;
if (edge->getTarget() == target) {
toVertices.insert(edge->getTarget());
}
}
return toVertices;
}
EdgeSet& getAllEdges() {
return edges;
}
EdgeSet edgesFrom(V* source) {
EdgeSet fromEdges;
EdgeSetIterator it;
for (it = edges.begin(); it != edges.end(); ++it) {
E<V,D>* edge = *it;
if (edge->getSource() == source) {
fromEdges.insert(edge);
}
}
return fromEdges;
}
EdgeSet edgesTo(V* target) {
EdgeSet toEdges;
EdgeSetIterator it;
for (it = edges.begin(); it != edges.end(); ++it) {
E<V,D>* edge = *it;
if (edge->getTarget() == target) {
toEdges.insert(edge);
}
}
return toEdges;
}
~Graph() {
EdgeSetIterator it;
for (it = edges.begin(); it != edges.end(); ++it)
delete (*it);
}
void printEdges() {
EdgeSetIterator it;
for (it = edges.begin(); it != edges.end(); ++it)
(*it)->print();
}
private:
set<V*> vertices;
EdgeSet edges;
};
#endif