[Approach] Using Handshaking Lemma- O(V+E) Time and O(V+E) Space
This idea is based on the Handshaking Lemma in graph theory. The handshaking lemma says that in an undirected graph, the total of all vertex degrees is equal to twice the number of edges. This is because every edge joins two vertices and is counted once for each of them. We go through all the vertices, add the number of connections (edges) each vertex has, then its half will be the number of edges in the graph.
C++
//Driver Code Starts#include<iostream>#include<vector>usingnamespacestd;//Driver Code EndsintcountEdges(vector<vector<int>>&adj){intsum=0;// Traverse all vertices and add the size of their adjacency listsfor(auto&neighbors:adj){sum+=neighbors.size();}// Each edge is counted twice in an undirected graphreturnsum/2;}//Driver Code Startsintmain(){vector<vector<int>>adj={{1,2},{0,2},{0,1,3},{2}};cout<<countEdges(adj)<<endl;return0;}//Driver Code Ends
Java
//Driver Code Startsimportjava.util.ArrayList;publicclassGFG{//Driver Code EndsstaticintcountEdges(ArrayList<ArrayList<Integer>>adj){intsum=0;// Traverse all vertices and add the size of their adjacency listsfor(ArrayList<Integer>neighbors:adj){sum+=neighbors.size();}// Each edge is counted twice in an undirected graphreturnsum/2;}//Driver Code Starts// Function to add undirected edgestaticvoidaddEdge(ArrayList<ArrayList<Integer>>adj,intu,intv){adj.get(u).add(v);adj.get(v).add(u);}publicstaticvoidmain(String[]args){intV=4;ArrayList<ArrayList<Integer>>adj=newArrayList<>();for(inti=0;i<V;i++){adj.add(newArrayList<>());}addEdge(adj,0,1);addEdge(adj,0,2);addEdge(adj,1,2);addEdge(adj,2,3);System.out.println(countEdges(adj));}}//Driver Code Ends
Python
defcountEdges(adj):sum=0# Traverse all vertices and add the size of their adjacency listsforneighborsinadj:sum+=len(neighbors)# Each edge is counted twice in an undirected graphreturnsum//2#Driver Code Startsif__name__=="__main__":V=4adj=[[1,2],[0,2],[0,1,3],[2]]print(countEdges(adj))#Driver Code Ends
C#
//Driver Code StartsusingSystem;usingSystem.Collections.Generic;classGFG{//Driver Code EndsstaticintcountEdges(List<List<int>>adj){intsum=0;// Traverse all vertices and add the size of their adjacency listsforeach(varneighborsinadj){sum+=neighbors.Count;}// Each edge is counted twice in an undirected graphreturnsum/2;}//Driver Code Starts// Function to add undirected edgestaticvoidaddEdge(List<List<int>>adj,intu,intv){adj[u].Add(v);adj[v].Add(u);}staticvoidMain(){intV=4;// Create adjacency listList<List<int>>adj=newList<List<int>>();for(inti=0;i<V;i++)adj.Add(newList<int>());addEdge(adj,0,1);addEdge(adj,0,2);addEdge(adj,1,2);addEdge(adj,2,3);Console.WriteLine(countEdges(adj));}}//Driver Code Ends
JavaScript
functioncountEdges(adj){letsum=0;// Traverse all vertices and add the size of their adjacency listsfor(constneighborsofadj){sum+=neighbors.length;}// Each edge is counted twice in an undirected graphreturnMath.floor(sum/2);}//Driver Code Starts// Driver codeconstV=4;constadj=[[1,2],[0,2],[0,1,3],[2]];console.log(countEdges(adj));//Driver Code Ends