-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadGraph.cpp
151 lines (126 loc) · 3 KB
/
readGraph.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
#include <iostream>
#include <fstream>
#include <vector>
#include <limits>
using namespace std;
int readGraph(ifstream& fin, double**& matrix, string*& vLabels, string**& eLabels)
{
int numVertices;
fin >> numVertices;
int numEdges;
fin >> numEdges;
vLabels = new string[numVertices];
for(int i = 0; i < numVertices; i++)
{
fin >> vLabels[i];
}
matrix = new double*[numVertices];
eLabels = new string*[numVertices];
for(int i = 0; i < numVertices; i++)
{
matrix[i] = new double[numVertices];
eLabels[i] = new string[numVertices];
for(int j = 0; j < numVertices; j++)
{
matrix[i][j] = numeric_limits<double>::infinity();
}
}
for(int i = 0; i < numVertices; i++)
{
matrix[i][i] = 0;
}
for(int i = 0; i < numEdges; i++)
{
int sourceV;
fin >> sourceV;
int destV;
fin >> destV;
double w;
fin >> w;
string l;
fin >> l;
matrix[sourceV][destV] = w;
eLabels[sourceV][destV] = l;
}
return numVertices;
}
int readGraph(ifstream& fin, int**& adj, double**& weights, int*& lengths, string*& vLabels, string**& eLabels)
{
int numVertices;
fin >> numVertices;
int numEdges;
fin >> numEdges;
vLabels = new string[numVertices];
for(int i = 0; i < numVertices; i++)
{
fin >> vLabels[i];
}
adj = new int*[numVertices];
weights = new double*[numVertices];
lengths = new int[numVertices];
eLabels = new string*[numVertices];
int sourceV;
fin >> sourceV;
for(int i = 0; i < numVertices; i++)
{
vector<int> tmpAdj;
vector<double> tmpWeights;
vector<string> tmpLabels;
lengths[i] = 0;
while(!fin.eof() && sourceV == i)
{
int destV;
fin >> destV;
tmpAdj.push_back(destV);
double w;
fin >> w;
tmpWeights.push_back(w);
string label;
fin >> label;
tmpLabels.push_back(label);
lengths[i]++;
fin >> sourceV;
}
adj[i] = new int[lengths[i]];
weights[i] = new double[lengths[i]];
eLabels[i] = new string[lengths[i]];
for(int j = 0; j < lengths[i]; j++)
{
adj[i][j] = tmpAdj[j];
weights[i][j] = tmpWeights[j];
eLabels[i][j] = tmpLabels[j];
}
}
return numVertices;
}
int readGraph(ifstream& fin, int**& edgeList, double*& weights, int& numEdges, string*& vLabels, string*& eLabels)
{
int numVertices;
fin >> numVertices;
fin >> numEdges;
vLabels = new string[numVertices];
for(int i = 0; i < numVertices; i++)
{
fin >> vLabels[i];
}
edgeList = new int*[numEdges];
weights = new double[numEdges];
eLabels = new string[numEdges];
for(int i = 0; i < numEdges; i++)
{
int sourceV;
fin >> sourceV;
int destV;
fin >> destV;
double w;
fin >> w;
string l;
fin >> l;
edgeList[i] = new int[2];
edgeList[i][0] = sourceV;
edgeList[i][1] = destV;
weights[i] = w;
eLabels[i] = l;
}
return numVertices;
}