Hi!
I've run into a problem which has taken hours from me, and I still don't get any closer to a meaningfull result...
The Problem: Read in a set of graphs and print their adjacency matrix.
The input has the following form:
The first line contains the number of nodes in the graph (I'll refer to this number as n). The next at most n lines describe the graph as follows:
Each line starts with an integer i followed by some other integers which are the nodes connected to node i.
After this block there comes a 0 which means that the graph description has finished. And after all the graphs description there comes a 0 to signal the end of input. No assumptions about the order can be made
An example:
3
1 2 3
2 3
0
Represents a triangle. The same graph could have been written in the form:
3
1 2
3 1
2 3
0
So a complete sample input could look like this:
4
1 2 3 4
0
2
1 2
0
5
1 2 3
2 4
5 3
0
0
The corresponding output (adjacency matrix) would then be:
0 1 1 1
1 0 0 0
1 0 0 0
1 0 0 0
0 1
1 0
0 1 1 0 0
1 0 0 1 0
1 0 0 0 1
0 1 0 0 0
0 0 1 0 0
Actually, an upper right triangular matrix would suffice to describe the graph completly, but I wan't to be able to go to the i'th row or colum to see all the neighbors of the i'th node.
So now to my problem: My code looks as follows
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
|
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main(){
int non; //number of nodes
int node;
int fnode; //first node in a line
string s; //used to read in the lines
while(cin >> non){
if (!non){
//End of input was reached
break;
}
//intializing the adjacency matrix with all ceros
bool adj_matrix[non][non];
for (int i=0; i<non; i++){
for (int j=0; j<non; j++){
adj_matrix[i][j] =0;
}
}
//////////////////////////////////////////
// HERE TO TROUBLE STARTS!!!!!!!!!!!!
//Problem-loop
//building up the graph
for (int n=0; n<non; n++){
//cout << "n: " << n << endl;
getline(cin, s);
istringstream sin(s);
sin >> fnode;
if(!fnode){
//end of graph description was reached
break;
}
while(sin >> node){
adj_matrix[fnode-1][node-1]=1;
adj_matrix[node-1][fnode-1]=1;
};
}
//HERE THE TROUBLE ENDS (Hopefully! :))
//////////////////////////////////////////
//print the matrix
for (int i=0; i<non; i++){
for (int j=0; j<non; j++){
cout << adj_matrix[i][j] << " ";
}
cout << endl;
}
}
}
|
It's the first time I use 'getline' and 'istringstream' and until now I always could use cin...
What I noticed so far:
- I go through the problem loop once, before anything from the input lines was read. I.e. only non is set, and then I go through the loop one time without setting any of the other parameters correctly, specially not fnode! Which is a probleam because this breaks the loop even if it shouldn't. This results in reading in the whole input in a wrong way.
The second problem, which is less important but also bothers me, is that running my program with gdb makes my program behave differently. After entering the first input (non), gdb already outputs the matrix... I don't understand why this happens and how to handle this. But I'm new with gdb too... and it isn't my main concern.
I hope anyone can help me with this... the actual task would be doing some computations on the graph, but I can't even handle to read the input correctly!
I'm thankfull for any sort of help.
I'm using: jedit as editor an g++-4.2.1 as compiler and running this stuff on a mac. I don't think this actually makes a difference, but it said in the "how-to-post" that I should mention this.
Thanks in advance.