-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.cpp
More file actions
76 lines (65 loc) · 1.82 KB
/
Copy pathgraph.cpp
File metadata and controls
76 lines (65 loc) · 1.82 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
#include "graph.h"
#include <iostream>
Graph::Graph(string filename) {
instanceDef = filename;
readGraph();
}
void Graph::readGraph() {
ifstream inFile(instanceDef.c_str());
string inLine, comment;
NodeID node1, node2;
char lineType;
Edge edge;
if(inFile.is_open()) {
getline(inFile, inLine);
istringstream iss(inLine);
lineType = iss.get();
while(lineType != 'p') {
getline(inFile, inLine);
istringstream iss(inLine);
lineType = iss.get();
}
if(lineType == 'p') {
istringstream iss(inLine);
iss>>lineType>>comment>>numNodes>>numEdges;
}
while(getline(inFile, inLine)) {
istringstream iss(inLine);
if(!(iss>>lineType>>node1>>node2)) {
cerr<<"Invalid edge representation in the graph file"<<endl<<endl;
exit(1);
}
edge = make_pair(node1, node2);
edgeList.push_back(edge);
}
inFile.close();
}
else {
cerr<<"Error opening the instanceDef "<<instanceDef<<endl<<endl;
exit(1);
}
}
unsigned int Graph::getNumNodes() {
return numNodes;
}
int Graph::getNumEdges() {
return numEdges;
}
int Graph::getNumEdgesCovered(int *coord) {
EdgeList::iterator it;
int edgesCovered = 0;
for(it=edgeList.begin(); it<edgeList.end(); it++) {
if(coord[it->first-1] == 1 || coord[it->second-1] == 1)
edgesCovered++;
}
return(edgesCovered);
}
void Graph::printGraph() {
int edges = 0;
EdgeList::iterator it;
for(it=edgeList.begin(); it<edgeList.end(); it++) {
cout<<"Edge = "<<it->first<<" "<<it->second<<endl;
edges++;
}
cout<<"Total edges read= "<<edges<<endl;
}