-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery_graph.cpp
224 lines (168 loc) · 5.61 KB
/
query_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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*** Implements the query graph (of query_graph.h) and constructs the index as required
***/
#include "query_graph.h"
#include <utility>
#include <algorithm>
#include <iterator>
#include <fstream>
#include <iostream>
// Creates the query graph from two files - (1) mapping of node ID to label, (2) list of neighbour labels
Query :: Query(const string iNodeFile, const string iEdgeFile)
{
string prevId, id, label, edge; // Elements to be read from the files
prevId = id = label = edge = "";
set<string> neighbours; // Stores the ID of the neighbours for a vertex
ifstream iNF;
iNF.open(iNodeFile.c_str()); // Contains the mapping from vertex IDs to labels
if(!iNF.is_open())
cout<<"Unable to open "<<iNodeFile<<" file"<<endl;
iNF >> id >> label;
while(!iNF.eof()) // Reads the vertex IDs and the labels
{
Node *node = new Node(id, label);
graph[id] = node;
iNF >> id >> label;
}
iNF.close();
ifstream iEF;
iEF.open(iEdgeFile.c_str()); // Contains the edges between the vertices
if(!iEF.is_open())
cout<<"Unable to open input "<<iEdgeFile<<" file"<<endl;
unsigned int file_empty_flag = 1; // Initialized to true
char c;
iEF.get(c);
while(!iEF.eof()) // Checking for empty file
{
if(!isspace(c))
{
file_empty_flag = 0; // Set to false
break;
}
}
if(!file_empty_flag) // If file not empty
{
iEF.seekg(0, iEF.beg);
iEF >> id >> edge; // Reads a new node ID and its label
while(!iEF.eof()) // Read the neighbours for vertices
{
#ifdef DEBUG
cout<<id<<" "<<edge<<endl;
#endif
if(prevId.compare(id) == 0)
neighbours.insert(edge);
else
{
if (prevId != "")
{
Node *node = graph[prevId];
node->addNeighbours(neighbours);
neighbours.clear();
}
neighbours.insert(edge);
prevId = id;
}
// Add the reverse edges for undirected graph
Node *node = graph[edge];
node->addNeighbours(id);
iEF >> id >> edge; // Reads a new node ID and its label
}
Node *node = graph[prevId];
node->addNeighbours(neighbours);
neighbours.clear();
}
iEF.close();
createLabelNeighbours(); // Creates the neighbourLabels index
}
// Deallocates the graph
Query :: ~Query()
{
unordered_map<string, Node*>::iterator it = graph.begin();
for(; it!=graph.end(); it++)
delete it->second;
graph.clear();
labelNeighbours.clear();
}
// Return the address of the graph
// CRITICAL as it could result in accidental modification
const unordered_map<string, Node*>& Query :: getGraph_CRITICAL() const
{
return graph;
}
// Returns the labelNeighbours of the vertex label provided as argument minus that of visited query vertices
const unordered_map<string, vector<string> > Query :: getLabelNeighbours(set<string> visited, string lab) const
{
unordered_map<string, vector<string> > listOfLabelNeigh;
// If vertex with same label not present
// Define dummy pair<empty_label, q_vertex_label> to search in multimap
if(labelNeighbours.find(make_pair(EMPTY_LABEL, lab)) == labelNeighbours.end())
return listOfLabelNeigh;
std::pair< multimap< pair<string,string>, vector<string> >::const_iterator, multimap< pair<string,string>, vector<string> >::const_iterator> ret;
ret = labelNeighbours.equal_range(make_pair(EMPTY_LABEL, lab));
for(multimap< pair<string,string>, vector<string> >::const_iterator it=ret.first; it!=ret.second; it++)
{
// If query vertex is not in the visited list
if(find(visited.begin(), visited.end(), (it->first).first) == visited.end())
listOfLabelNeigh.insert(make_pair((it->first).first, it->second));
}
return listOfLabelNeigh;
}
// Returns the unique query graph labels
const set<string> Query :: getLabels(void) const
{
set<string> labels;
multimap< pair<string, string>, vector<string> >::const_iterator it = labelNeighbours.begin();
for(; it!=labelNeighbours.end(); it++)
labels.insert((it->first).second);
return labels;
}
// Populates the index variable labelNeighbours, mapping vertices to the labels of the neighbours
void Query :: createLabelNeighbours(void)
{
unordered_map<string, Node*>::iterator it = graph.begin();
for(; it!=graph.end(); it++)
{
string currID = (it->second)->getID();
string currLab = (it->second)->getLabel();
const set<string> neighId = (it->second)->getNeighbourIDs();
set<string>::const_iterator it1 = neighId.begin();
vector<string> nLabels;
nLabels.clear();
for(; it1!=neighId.end(); it1++)
nLabels.push_back( (graph[*it1])->getLabel() );
labelNeighbours.insert( pair< pair<string, string>, vector<string> >(make_pair(currID, currLab), nLabels) );
}
}
// Returns the number of nodes in the query graph
unsigned Query :: getGraphSize(void) const
{
return graph.size();
}
// Prints the vertex IDs of the query graph
void Query :: printVertex(void) const
{
unordered_map<string, Node*>::const_iterator it = graph.begin();
for(; it!=graph.end(); it++)
cout<<it->first<<" ("<<(it->second)->getLabel()<<"), ";
cout<<endl;
}
// Prints the graph characteristics
void Query :: printGraph(void) const
{
cout<<"\nThe query graph contains the following vertices:"<<endl;
unordered_map<string, Node*>::const_iterator it = graph.begin();
for(; it!=graph.end(); it++)
(it->second)->print();
cout<<"\nThe index of the vertex and the neighbour labels are:"<<endl;
multimap< pair<string, string>, vector<string> >::const_iterator it1 = labelNeighbours.begin();
for(; it1!=labelNeighbours.end(); it1++)
{
cout<<"("<<(it1->first).first<<", "<<(it1->first).second<<") has neighbour label: [";
if(it1->second.size()!=0)
{
copy((it1->second).begin(),(it1->second).end()-1, ostream_iterator<string>(cout, ", " ));
cout<<(it1->second).back();
}
cout<<"]"<<endl;
}
cout<<endl;
}