I am accepting undirected weighted graph from input file. File contains 200
nodes.
I have written code like this.
typedef pair<int, int> ipair;
class Graph
{
int V;
list< pair <int, int> > *adj;
public:
Graph(int V);
};
Graph::Graph( int V)
{
this-> V = V;
adj = new list<ipair>[V];
}
bool Graph :: read_file()
{
const long int N = 1000000;
std::ifstream infile("Dijkstra.txt");
if(!infile.is_open()) return false;
std::string line;
int i = 0;
while ( i < N && getline(infile, line) )
{
std::istringstream str(line);
int u;
str >> u;
if ( u > N )
{
// Problem.
abort();
}
int v;
int w;
while ( str >> v >> w)
{
adj[u].push_back(make_pair(v,w));
}
++i;
}
}
int main()
{
Graph g(200);
g.read_file();
g.print_graph();
return 0;
}
I/P file :
1 80,982 163,8164 170,2620 145,648 200,8021 173,2069 92,647 26,4122 140,546 11,1913 160,6461 27,7905 40,9047 150,2183 61,9146 159,7420 198,1724 114,508 104,6647 30,4612 99,2367 138,7896 169,8700 49,2437 125,2909 117,2597 55,6399
2 42,1689 127,9365 5,8026 170,9342 131,7005 172,1438 34,315 30,2455 26,2328 6,8847 11,1873 17,5409 157,8643 159,1397 142,7731 182,7908 93,8177
Node 1 is connected to node 80 with weight 982
Node 1 is connected to node 163 with weight 8164
Node 2 is connected to node 42 with weight 1689
Node 2 is connected to node 127 with weight 9365
etc.....
Now with this I can accept node 1 is connected to node 80 with weight 982 (1 80,982 ) but what about remaining nodes which are connected with node 1 ??
i.e. How to make loop for accepting v and w ??
>> adj ..... I want to understand how it looks ( diagrammatically) the way fresher understands pointer , so do you have any material which I can refer ??
– Tushar May 17 '17 at 18:23