-
Notifications
You must be signed in to change notification settings - Fork 0
/
loadingGraph.cpp
159 lines (129 loc) · 3.31 KB
/
loadingGraph.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
#include<iostream>
#include "loadingGraph.h"
using namespace std;
Graph::Graph()
{
}
Graph::~Graph()
{
}
void Graph::loading(string &inputFileName)
{
pair<string,string> parseLine(string &inputFileName);
std::ifstream myfile(inputFileName.c_str());
std::string line;
set<pair<string,string> > links;
set<string> Nodenamescopy;
string t;
if (myfile.is_open())
{
while (!myfile.eof())
{
getline(myfile, line);
if (!line.empty())
{
pair<string,string> p = parseLine(line);
Nodenamescopy.insert(p.first);Nodenamescopy.insert(p.second);
if (p.first>p.second)
{
t=p.first;
p.first=p.second;
p.second=t;
}
links.insert(p);
}
}
myfile.close();
}
//copy the Nodenamescopy to the Nodenames
int num = 0;
for (set<string>::const_iterator it=Nodenamescopy.begin(); it!=Nodenamescopy.end(); it++)
{
Nodenames.push_back(*it);
nodename_to_index[(string)*it] = num++;
}
//Get the number of nodes and links
vcount=Nodenames.size();
ecount=links.size();
Neighbors.resize(vcount);
set<pair<int,int> > Linkcopy;
//construct the Linklist & Neighbors
{
int index1,index2,t;
for (set<pair<string,string> >::const_iterator linkit=links.begin(); linkit!=links.end(); linkit++)
{
pair<string,string> p=*linkit;
index1=find_offset(p.first);index2=find_offset(p.second);
if (index1>index2)
{
t=index1;
index1=index2;
index2=t;
}
Linkcopy.insert(make_pair(index1,index2));
Neighbors[index1].push_back(index2);
Neighbors[index2].push_back(index1);
}
}
//copy Linkcopy to the Linklist
for (set<pair<int,int> >::const_iterator it=Linkcopy.begin(); it!=Linkcopy.end(); it++)
{
Linklist.push_back(*it);
}
nodeTolinks();
}
pair<string,string> parseLine(string &lineorig)
{
string line(lineorig);
for(string :: iterator i = line.begin(); i!=line.end(); i++)
{
if(*i == ',' || *i == ' ' || *i == '\t' || *i == '|')
*i='\n';
}
istringstream fields(line);
string sourceNodeName;
string targetNodeName;
getline(fields, sourceNodeName);
if(fields.fail())
{
//cout<<"读取边文件时,异常!"<<endl;
//exit(0);
cout<<"读取边文件,异常!"<<endl;
exit(0);
}
getline(fields, targetNodeName);
if(fields.fail())
{
//cout<<"读取边文件时,异常!"<<endl;
//exit(0);
cout<<"孤立点:"<<sourceNodeName<<endl;
targetNodeName = "";
}
pair<string,string> p;
p.first=sourceNodeName;
p.second=targetNodeName;
return p;
}
int Graph::find_offset(string nodename)
{
for (vector<string>::iterator it=Nodenames.begin(); it!=Nodenames.end(); it++)
{
if (*it==nodename)
return int(it-Nodenames.begin());
}
return -1;
}
int Graph::node_degree(int node)
{
return Neighbors[node].size();
}
void Graph::nodeTolinks()
{
node_links.resize(vcount);
for (int x=0;x<ecount;x++)
{
pair<int,int> p=Linklist[x];
node_links[p.first].insert(x);
node_links[p.second].insert(x);
}
}