Given an adjacency list representation undirected graph. Write a function to count the number of edges in the undirected graph. Expected time complexity : O(V) Examples:
Input : Adjacency list representation of
below graph.
Output : 9
Idea is based on Handshaking Lemma. Handshaking lemma is about undirected graph. In every finite undirected graph number of vertices with odd degree is always even. The handshaking lemma is a consequence of the degree sum formula (also sometimes called the handshaking lemma)
So we traverse all vertices, compute sum of sizes of their adjacency lists, and finally returns sum/2. Below implementation of above idea
C++
#include<bits/stdc++.h>
using namespace std;
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 () ;
};
void Graph :: addEdge ( int u, int v )
{
adj[u].push_back(v);
adj[v].push_back(u);
}
int Graph :: countEdges()
{
int sum = 0;
for ( int i = 0 ; i < V ; i++)
sum += adj[i].size();
return sum/2;
}
int main()
{
int V = 9 ;
Graph g(V);
g.addEdge(0, 1 );
g.addEdge(0, 7 );
g.addEdge(1, 2 );
g.addEdge(1, 7 );
g.addEdge(2, 3 );
g.addEdge(2, 8 );
g.addEdge(2, 5 );
g.addEdge(3, 4 );
g.addEdge(3, 5 );
g.addEdge(4, 5 );
g.addEdge(5, 6 );
g.addEdge(6, 7 );
g.addEdge(6, 8 );
g.addEdge(7, 8 );
cout << g.countEdges() << endl;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class Graph
{
int V;
Vector<Integer>[] adj;
Graph( int V)
{
this .V = V;
this .adj = new Vector[V];
for ( int i = 0 ; i < V; i++)
adj[i] = new Vector<Integer>();
}
void addEdge( int u, int v)
{
adj[u].add(v);
adj[v].add(u);
}
int countEdges()
{
int sum = 0 ;
for ( int i = 0 ; i < V; i++)
sum += adj[i].size();
return sum / 2 ;
}
}
class GFG
{
public static void main(String[] args) throws IOException
{
int V = 9 ;
Graph g = new Graph(V);
g.addEdge( 0 , 1 );
g.addEdge( 0 , 7 );
g.addEdge( 1 , 2 );
g.addEdge( 1 , 7 );
g.addEdge( 2 , 3 );
g.addEdge( 2 , 8 );
g.addEdge( 2 , 5 );
g.addEdge( 3 , 4 );
g.addEdge( 3 , 5 );
g.addEdge( 4 , 5 );
g.addEdge( 5 , 6 );
g.addEdge( 6 , 7 );
g.addEdge( 6 , 8 );
g.addEdge( 7 , 8 );
System.out.println(g.countEdges());
}
}
|
Python3
class Graph:
def __init__( self , V):
self .V = V
self .adj = [[] for i in range (V)]
def addEdge ( self , u, v ):
self .adj[u].append(v)
self .adj[v].append(u)
def countEdges( self ):
Sum = 0
for i in range ( self .V):
Sum + = len ( self .adj[i])
return Sum / / 2
if __name__ = = '__main__' :
V = 9
g = Graph(V)
g.addEdge( 0 , 1 )
g.addEdge( 0 , 7 )
g.addEdge( 1 , 2 )
g.addEdge( 1 , 7 )
g.addEdge( 2 , 3 )
g.addEdge( 2 , 8 )
g.addEdge( 2 , 5 )
g.addEdge( 3 , 4 )
g.addEdge( 3 , 5 )
g.addEdge( 4 , 5 )
g.addEdge( 5 , 6 )
g.addEdge( 6 , 7 )
g.addEdge( 6 , 8 )
g.addEdge( 7 , 8 )
print (g.countEdges())
|
C#
using System;
using System.Collections.Generic;
class Graph
{
public int V;
public List< int >[] adj;
public Graph( int V)
{
this .V = V;
this .adj = new List< int >[V];
for ( int i = 0; i < V; i++)
adj[i] = new List< int >();
}
public void addEdge( int u, int v)
{
adj[u].Add(v);
adj[v].Add(u);
}
public int countEdges()
{
int sum = 0;
for ( int i = 0; i < V; i++)
sum += adj[i].Count;
return sum / 2;
}
}
class GFG
{
public static void Main(String[] args)
{
int V = 9;
Graph g = new Graph(V);
g.addEdge(0, 1);
g.addEdge(0, 7);
g.addEdge(1, 2);
g.addEdge(1, 7);
g.addEdge(2, 3);
g.addEdge(2, 8);
g.addEdge(2, 5);
g.addEdge(3, 4);
g.addEdge(3, 5);
g.addEdge(4, 5);
g.addEdge(5, 6);
g.addEdge(6, 7);
g.addEdge(6, 8);
g.addEdge(7, 8);
Console.WriteLine(g.countEdges());
}
}
|
Javascript
class Graph {
constructor(V) {
this .V = V;
this .adj = new Array(V);
for (let i = 0; i < V; i++) {
this .adj[i] = [];
}
}
addEdge(u, v) {
this .adj[u].push(v);
this .adj[v].push(u);
}
countEdges() {
let sum = 0;
for (let i = 0; i < this .V; i++) {
sum += this .adj[i].length;
}
return sum / 2;
}
}
function main() {
let V = 9;
let g = new Graph(V);
g.addEdge(0, 1);
g.addEdge(0, 7);
g.addEdge(1, 2);
g.addEdge(1, 7);
g.addEdge(2, 3);
g.addEdge(2, 8);
g.addEdge(2, 5);
g.addEdge(3, 4);
g.addEdge(3, 5);
g.addEdge(4, 5);
g.addEdge(5, 6);
g.addEdge(6, 7);
g.addEdge(6, 8);
g.addEdge(7, 8);
console.log(g.countEdges());
}
main();
|
Output:
14
Time Complexity: O(V) This article is contributed by Nishant Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.