forked from SNUCSE-CTA/graph-analysis-tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.cpp
140 lines (112 loc) · 4.05 KB
/
graph.cpp
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
#include "graph.h"
namespace snu {
Graph::Graph() {}
// destructor
Graph::~Graph()
{
// delete vertex, edge, vertex_label, edge_label;
for (auto it = id_to_vertex.begin(); it != id_to_vertex.end(); ++it)
delete it->second;
for (auto it = id_to_edge.begin(); it != id_to_edge.end(); ++it)
delete it->second;
for (auto it = vlabel_to_class.begin(); it != vlabel_to_class.end(); ++it)
delete it->second;
for (auto it = elabel_to_class.begin(); it != elabel_to_class.end(); ++it)
delete it->second;
}
// do not have to define destructor of child classes
// array version
int Graph::addVertex(Vid id, long long num, Vlabel label[])
{
// error: graph already has a vertex having same id
if (id_to_vertex.count(id)) return 1;
Vertex *v = new Vertex();
id_to_vertex[id] = v;
v->id = id;
// set labels
// if no vertex label class then create it
for (long long i = 0; i < num; ++i) {
auto it = vlabel_to_class.find(label[i]);
LabelOfVertices *vl;
// no vertex label class
if (it == vlabel_to_class.end()) {
vl = new LabelOfVertices();
vlabel_to_class[label[i]] = vl;
vl->label = label[i];
}
else {
vl = it->second;
}
vl->vertices.push_back(v);
v->labels.push_back(vl);
}
return 0;
}
// vector version
int Graph::addVertex(Vid id, std::vector <Vlabel> *label)
{
return addVertex(id, label->size(), label->data());
}
// base addEdge function, DSGraph version
int Graph::addEdge(Eid id, long long num, Elabel label[], Vid from, Vid to, Weight weight)
{
// error: already have edge having same id or no from or to vertex
if (id_to_edge.count(id) || !id_to_vertex.count(from) || !id_to_vertex.count(to))
return 1;
Edge *e = new Edge();
id_to_edge[id] = e;
e->id = id;
// set edge labels
// if no edge label class then create it
for (long long i = 0; i < num; ++i) {
auto it = elabel_to_class.find(label[i]);
LabelOfEdges *el;
// no edge label class
if (it == elabel_to_class.end()) {
el = new LabelOfEdges();
elabel_to_class[label[i]] = el;
el->label = label[i];
}
else {
el = it->second;
}
el->edges.push_back(e);
e->labels.push_back(el);
}
e->from = id_to_vertex[from];
e->to = id_to_vertex[to];
e->to->indegree++;
e->weight = weight;
e->from->edges.push_back(e);
is_connected.insert(std::make_pair(from, to));
if (weight < 0) negative_edge_num++;
return 0;
}
// array version
int DSGraph::addEdge(Eid id, long long num, Elabel label[], Vid from, Vid to, Weight weight)
{
return Graph::addEdge(id, num, label, from, to, weight);
}
// vector version
int DSGraph::addEdge(Eid id, std::vector <Elabel> *label, Vid from, Vid to, Weight weight)
{
return addEdge(id, label->size(), label->data(), from, to, weight);
}
// array version
int USGraph::addEdge(Eid id, long long num, Elabel label[], Vid from, Vid to, Weight weight)
{
// there is an error
if (Graph::addEdge(id, num, label, from, to, weight)) return 1;
Edge *e = id_to_edge[id];
e->to->edges.push_back(e);
e->from->indegree++;
is_connected.insert(std::make_pair(to, from)); // insert reverse
// be careful of switching from and to in this case
return 0;
}
// vector version
int USGraph::addEdge(Eid id, std::vector <Elabel> *label, Vid from, Vid to, Weight weight)
{
return addEdge(id, label->size(), label->data(), from, to, weight);
}
}