Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Count edges in a graph
#include<bits/stdc++.h> using namespace std; // Adjacency list representation of graph class Graph { int V; list<int> *adj; public : Graph(int V) { this->V = V ; adj = new list<int>[V]; } void addEdge(int u, int v); int countEdges(); }; // add edge to graph void Graph :: addEdge ( int u, int v ) { adj[u].push_back(v); adj[v].push_back(u); } // Returns count of edge in undirected graph int Graph :: countEdges() { int sum = 0; //traverse all vertex for (int i = 0 ; i < V ; i++) // add all edge that are linked to the // current vertex sum += adj[i].size(); // The count of edge is always even because in // undirected graph every edge is connected // twice between two vertices return sum/2; } // driver program to check above function int main() { int V = 5 ; Graph g(V); // making above uhown graph g.addEdge(1,2 ); g.addEdge(1,3 ); g.addEdge(2,3 ); g.addEdge(3,4 ); g.addEdge(2,4 ); cout << g.countEdges() << endl; return 0; }
run
|
edit
|
history
|
help
0
Web Browser History - DLL
shell sort
motores
cppPyAbs
Segment Tree
HashConPar
myfirst.cpp
HashTabFold
3
copy_30-Seconds-of-C++