I need to read from file adjecancy list for Kruskal's algorith. Input file is
9 17
2 3 8 4 9 8
1 3 3 6 4 5 9 6
2 6 4 4 9 1
2 5 3 4 5 3 9 2
4 3 6 1 9 5
5 1 7 5 9 2
6 5 8 4 9 2
1 4 7 4 9 2
1 8 2 6 3 1 4 2 5 5 6 2 7 2 8 2
Let me explain how to look on this. First row is obviously number of vertices and edges. Every next row means one vertex so secound row is first vertex and it contains vertex with which it will connect and weight of the edge. And below is example on first row.
Vertex 1[Source]: 2[Destination] 3[Weight of the edge] 8[Destination] 4[Weight] 9[Destination] 8[Weight]
And I need to store edges in array of
typedef struct Edge{
int weight;
int beg, end;
}*PEdge;
And the problem is I have no clue how to read it into array. I came up with something like that but obviously it doesn't work.
in.open("In0303.txt");
out.open("Out0303.txt");
if(!in || !out)
return 0;
in>>n>>m;
Edge E[34];
while (! in.eof() ){
getline( in, line );
istringstream is( line );
while( is >> number ) {
edge++;
E[edge].beg=row;
E[edge].end=number;
E[edge].weight=number;
}
row++;
}
It sounds like you're having trouble reading two numbers in at once (the weight and destination). Are you ready to have your mind blown? You can chain the input operator!
That means that you can do:
int dest, weight;
while( is >> dest >> weight ) {
edge++;
E[edge].beg = row;
E[edge].end = dest;
E[edge].weight = weight;
}