I have a text file where the first number on a line is a node number for a graph, and the second number is the number of the node that is connected to the first. The third number is the weight of the edge.
Here is a sample case where two of the lines in the file contain 3 and 5 with a weight of .5:
1 3 0.5
3 5 0.5
3 6 0.5
3 5 0.5
6 8 0.5
4 6 1
I would like to be able to merge these into just one line that has the connection for 3 and 5 but update the weight to be the sum of their weights (in this case 1). The duplicate line then should be deleted.
So I want to check for duplicate node pairs, and if I find duplicates sum up the total weight of all of them and then update one line for that pair to have the correct total weight and then delete all the other lines with those nodes.
I have made a struct for Edges:
struct Edge {
int c1, c2;
float weight;
};
I have read the file and put them all into this structure:
if (updateGraph.is_open()) {
string data[3];
Edge e;
while (getline(updateGraph, stri)) {
stringstream in(stri);
int i = 0;
while (in.good() && i < 3) {
in >> data[i];
i++;
}
e.c1 = atoi(data[0].c_str());
e.c2 = atoi(data[1].c_str());
e.weight = atof(data[2].c_str());
cout << e.c1 << " " << e.c2 << " " << e.weight << endl;
}
}
But now I am not sure how to compare them to see if any of the edges have the same c1 and c2. How would I do that?