You are viewing a single comment's thread. Return to all comments →
using namespace std;
class Graph { int V; vector> adj;
public: Graph(int v) { V = v; adj.resize(V); }
void addEdge(int u, int v) { adj[u].push_back(v); adj[v].push_back(u); // Undirected graph } void display() { for (int i = 0; i < V; i++) { cout << "Node " << i << ": "; for (int j : adj[i]) cout << j << " "; cout << endl; } }
};
int main() { Graph g(5); g.addEdge(0, 1); g.addEdge(0, 4); g.addEdge(1, 2); g.addEdge(1, 3); g.addEdge(1, 4); g.display(); }
Seems like cookies are disabled on this browser, please enable them to open this website
Functions in C
You are viewing a single comment's thread. Return to all comments →
include
include
using namespace std;
class Graph { int V; vector> adj;
public: Graph(int v) { V = v; adj.resize(V); }
};
int main() { Graph g(5); g.addEdge(0, 1); g.addEdge(0, 4); g.addEdge(1, 2); g.addEdge(1, 3); g.addEdge(1, 4); g.display(); }