#include <iostream>
#include <vector>
using namespace std;
void addEdge( vector<vector<int> > adj, int, int);
void print_graph(vector<vector<int> > adj);
int main()
{
vector<vector<int> > adj;
addEdge(adj,1,2); // edge from node 1 to node 2
addEdge(adj,1,3);
addEdge(adj,1,4);
addEdge(adj,2,3);
addEdge(adj,3,4);
print_graph(adj);
return 0;
}
void addEdge(vector<vector<int> > adj, int u , int v)
{
adj[u].push_back(v);
}
void print_graph( vector<vector<int> > adj)
{
for( int i = 0; i < adj.size() ; i++ )
{
for( int j = 0 ; j < adj[i].size(); j++ )
{
cout<< adj[i][j];
}
}
}
I have written the code for reading the graph and printing it.
Prior to this for reading graph I used to use
vector<int>adj[5];
But I have been told that , use
`vector<vector<int> > adj` or `list<list<int> > adj`
I tried but I am getting error as
segmentation fault (core dump )
Can anyone help me in using vector of vector ?? Please help for list of list also.
Aucun commentaire:
Enregistrer un commentaire