-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDataGraph.cpp
More file actions
164 lines (146 loc) · 3.01 KB
/
DataGraph.cpp
File metadata and controls
164 lines (146 loc) · 3.01 KB
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
#include "DataGraph.h"
DataGraph::DataGraph(string fileName)
{
vector<vector<double> >().swap(AdjList);
unordered_map<double, double>().swap(id2seq);
unordered_map<double, double>().swap(seq2id);
ifstream in(fileName.c_str());
if (!in)
{
cout << "Fail to read " << fileName << "." << endl;
return;
}
string line;
getline(in,line); // read n and m no use
while (getline(in, line))
{
std::istringstream iss(line);
double src,dst;
iss>>src>>dst;
if (src != dst)
{
unordered_map<double, double>::iterator it;
int seq1, seq2;
it = id2seq.find(src);
if (it == id2seq.end())
{
seq1 = AdjList.size();
id2seq[src] = seq1;
seq2id[seq1] = src;
vector<double> newVertex;
AdjList.push_back(newVertex);
}
else
{
seq1 = it->second;
}
it = id2seq.find(dst);
if (it == id2seq.end())
{
seq2 = AdjList.size();
id2seq[dst] = seq2;
seq2id[seq2] = dst;
vector<double> newVertex;
AdjList.push_back(newVertex);
}
else
{
seq2 = it->second;
}
HashEdges.insert({ {id2seq[src],id2seq[dst]},1 });
HashEdges.insert({ {id2seq[dst],id2seq[src]},1 });
int flag;
flag = 0;
for (double i = 0; i < AdjList[seq1].size(); i++)
{
if (AdjList[seq1][i] == seq2)
{
flag = 1;
break;
}
}
if (flag == 0) AdjList[seq1].push_back(seq2);
flag = 0;
for (double i = 0; i < AdjList[seq2].size(); i++)
{
if (AdjList[seq2][i] == seq1)
{
flag = 1;
break;
}
}
if (flag == 0) AdjList[seq2].push_back(seq1);
}
}
}
void DataGraph::add_edges(string fileName2)
{
ifstream in(fileName2.c_str());
if (!in)
{
cout << "Fail to read " << fileName2 << "." << endl;
return;
}
string line;
while (getline(in, line))
{
string src_s = line.substr(0, line.find("\t"));
string dst_s = line.substr(line.find("\t") + 1, line.find("\n") - line.find("\t") - 1);
double src = stof(src_s);
double dst = stof(dst_s);
if (src != dst)
{
unordered_map<double, double>::iterator it;
int seq1, seq2;
it = id2seq.find(src);
if (it == id2seq.end())
{
seq1 = AdjList.size();
id2seq[src] = seq1;
seq2id[seq1] = src;
vector<double> newVertex;
AdjList.push_back(newVertex);
}
else
{
seq1 = it->second;
}
it = id2seq.find(dst);
if (it == id2seq.end())
{
seq2 = AdjList.size();
id2seq[dst] = seq2;
seq2id[seq2] = dst;
vector<double> newVertex;
AdjList.push_back(newVertex);
}
else
{
seq2 = it->second;
}
HashEdges.insert({ {id2seq[src],id2seq[dst]},1 });
HashEdges.insert({ {id2seq[dst],id2seq[src]},1 });
int flag;
flag = 0;
for (double i = 0; i < AdjList[seq1].size(); i++)
{
if (AdjList[seq1][i] == seq2)
{
flag = 1;
break;
}
}
if (flag == 0) AdjList[seq1].push_back(seq2);
flag = 0;
for (double i = 0; i < AdjList[seq2].size(); i++)
{
if (AdjList[seq2][i] == seq1)
{
flag = 1;
break;
}
}
if (flag == 0) AdjList[seq2].push_back(seq1);
}
}
}