Count Number of Edges For Undirected Graph

Last Updated : 28 Oct, 2025

Given an undirected graph represented using an adjacency list adj[][], return the total number of edges in the graph.

Examples:

Input: adj[][]= [[1, 2], [0, 2], [0, 1, 3], [2]]

frame_3202

Output: 4
Explanantion:
Between vertex 1 and vertex 2
Between vertex 1 and vertex 0
Between vertex 0 and vertex 2
Between vertex 2 and vertex 3

Input: adj[][] = [[1], [0, 2], [1, 3], [2]]

frame_3203

Ouput: 3
Explanation:
Between vertex 1 and vertex 2
Between vertex 1 and vertex 0
Between vertex 2 and vertex 3

Try It Yourself
redirect icon

[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>
using namespace std;

//Driver Code Ends

int countEdges(vector<vector<int>>& adj) {
    int sum = 0;

    // Traverse all vertices and add the size of their adjacency lists
    for (auto &neighbors : adj) {
        sum += neighbors.size();
    }

    // Each edge is counted twice in an undirected graph
    return sum / 2;
}

//Driver Code Starts

int main() {

    vector<vector<int>> adj = {{1, 2},{0, 2},{0, 1, 3},{2}};

    cout <<countEdges(adj) << endl; 

    return 0;
}

//Driver Code Ends
Java
//Driver Code Starts
import java.util.ArrayList;

public class GFG {

//Driver Code Ends

    static int countEdges(ArrayList<ArrayList<Integer>> adj) {
        int sum = 0;

        // Traverse all vertices and add the size of their adjacency lists
        for (ArrayList<Integer> neighbors : adj) {
            sum += neighbors.size();
        }

        // Each edge is counted twice in an undirected graph
        return sum / 2;
    }

//Driver Code Starts

    // Function to add undirected edge
    static void addEdge(ArrayList<ArrayList<Integer>> adj, int u, int v) {
        adj.get(u).add(v);
        adj.get(v).add(u);
    }

    public static void main(String[] args) {
        int V = 4;

        ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
        for (int i = 0; i < V; i++) {
            adj.add(new ArrayList<>());
        }

        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
def countEdges(adj):
    sum = 0

    # Traverse all vertices and add the size of their adjacency lists
    for neighbors in adj:
        sum += len(neighbors)

    # Each edge is counted twice in an undirected graph
    return sum // 2


#Driver Code Starts

if __name__ == "__main__":
    V = 4

    adj = [[1, 2],[0, 2],[0, 1, 3],[2]]

    print(countEdges(adj))

#Driver Code Ends
C#
//Driver Code Starts
using System;
using System.Collections.Generic;

class GFG
{
//Driver Code Ends

    static int countEdges(List<List<int>> adj)
    {
        int sum = 0;

        // Traverse all vertices and add the size of their adjacency lists
        foreach (var neighbors in adj)
        {
            sum += neighbors.Count;
        }

        // Each edge is counted twice in an undirected graph
        return sum / 2;
    }

//Driver Code Starts

    // Function to add undirected edge
    static void addEdge(List<List<int>> adj, int u, int v)
    {
        adj[u].Add(v);
        adj[v].Add(u);
    }

    static void Main()
    {
        int V = 4;

        // Create adjacency list
        List<List<int>> adj = new List<List<int>>();
        for (int i = 0; i < V; i++)
            adj.Add(new List<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
function countEdges(adj) {
    let sum = 0;

    // Traverse all vertices and add the size of their adjacency lists
    for (const neighbors of adj) {
        sum += neighbors.length;
    }

    // Each edge is counted twice in an undirected graph
    return Math.floor(sum / 2);
}


//Driver Code Starts
// Driver code
const V = 4;

const adj = [[1, 2],[0, 2],[0, 1, 3],[2]];

console.log(countEdges(adj));

//Driver Code Ends

Output
4
Comment