Given an edge list of a graph we have to find the sum of degree of all nodes of a undirected graph.
Example

Examples:
Input : edge list : (1, 2), (2, 3), (1, 4), (2, 4)
Output : sum= 8
Brute force approach
We will add the degree of each node of the graph and print the sum.
C++
#include <bits/stdc++.h>
using namespace std;
int count( int edges[][2], int len, int n)
{
int degree[n + 1] = { 0 };
for ( int i = 0; i < len; i++) {
degree[edges[i][0]]++;
degree[edges[i][1]]++;
}
int sum = 0;
for ( int i = 1; i <= n; i++)
sum += degree[i];
return sum;
}
int main()
{
int edges[][2] = { { 1, 2 },
{ 2, 3 },
{ 1, 4 },
{ 2, 4 } };
int len = sizeof (edges) / ( sizeof ( int ) * 2), n = 4;
cout << "sum = " << count(edges, len, n) << endl;
return 0;
}
|
Java
class GFG {
static int count( int edges[][], int len, int n)
{
int degree[] = new int [n + 1 ];
for ( int i = 0 ; i < len; i++) {
degree[edges[i][ 0 ]]++;
degree[edges[i][ 1 ]]++;
}
int sum = 0 ;
for ( int i = 1 ; i <= n; i++)
sum += degree[i];
return sum;
}
public static void main(String[] args)
{
int edges[][] = { { 1 , 2 },
{ 2 , 3 },
{ 1 , 4 },
{ 2 , 4 } };
int len = edges.length, n = 4 ;
System.out.println( "sum = " + count(edges, len, n));
}
}
|
Python3
def count(edges, len1, n):
degree = [ 0 for i in range (n + 1 )]
for i in range (len1):
degree[edges[i][ 0 ]] + = 1
degree[edges[i][ 1 ]] + = 1
sum = 0
for i in range ( 1 , n + 1 , 1 ):
sum + = degree[i]
return sum
if __name__ = = '__main__' :
edges = [[ 1 , 2 ], [ 2 , 3 ], [ 1 , 4 ], [ 2 , 4 ]]
len1 = len (edges)
n = 4
print ( "sum =" , count(edges, len1, n))
|
C#
using System;
class GFG {
static int count( int [][] edges, int len, int n)
{
int [] degree = new int [n + 1];
for ( int i = 0; i < len; i++) {
degree[edges[i][0]]++;
degree[edges[i][1]]++;
}
int sum = 0;
for ( int i = 1; i <= n; i++)
sum += degree[i];
return sum;
}
public static void Main()
{
int [][] edges = new int [][] { new int [] { 1, 2 },
new int [] { 2, 3 },
new int [] { 1, 4 },
new int [] { 2, 4 } };
int len = edges.Length, n = 4;
Console.WriteLine( "sum = " + count(edges, len, n));
}
}
|
Javascript
<script>
function count(edges, len, n)
{
var degree = Array(n+1).fill(0);
for ( var i = 0; i < len; i++) {
degree[edges[i][0]]++;
degree[edges[i][1]]++;
}
var sum = 0;
for ( var i = 1; i <= n; i++)
sum += degree[i];
return sum;
}
var edges = [ [ 1, 2 ],
[ 2, 3 ],
[ 1, 4 ],
[ 2, 4 ] ];
var len = edges.length, n = 4;
document.write( "sum = " + count(edges, len, n));
</script>
|
PHP
<?php
function count1( $edges , $len , $n )
{
$degree = array_fill (0, $n + 1, 0);
for ( $i = 0; $i < $len ; $i ++)
{
$degree [ $edges [ $i ][0]]++;
$degree [ $edges [ $i ][1]]++;
}
$sum = 0;
for ( $i = 1; $i <= $n ; $i ++)
$sum += $degree [ $i ];
return $sum ;
}
$edges = array ( array (1, 2),
array (2, 3),
array (1, 4),
array (2, 4));
$len = count ( $edges );
$n = 4;
echo "sum = " . count1( $edges , $len , $n ) . "\n" ;
?>
|
Space complexity: O(n) as it uses an array of size n+1 (degree array) to store the degree of each node.
Time complexity: O(n) as it iterates through the edges array.
Efficient approach
If we get the number of the edges in a directed graph then we can find the sum of degree of the graph. Let us consider an graph with no edges. If we add a edge we are increasing the degree of two nodes of graph by 1, so after adding each edge the sum of degree of nodes increases by 2, hence the sum of degree is 2*e.
C++
#include <bits/stdc++.h>
using namespace std;
int count( int edges[][2], int len)
{
return 2 * len;
}
int main()
{
int edges[][2] = { { 1, 2 },
{ 2, 3 },
{ 1, 4 },
{ 2, 4 } };
int len = sizeof (edges) / ( sizeof ( int ) * 2);
cout << "sum = " << count(edges, len) << endl;
return 0;
}
|
Java
class GFG {
static int count( int edges[][], int len)
{
return 2 * len;
}
public static void main(String[] args)
{
int edges[][] = { { 1 , 2 },
{ 2 , 3 },
{ 1 , 4 },
{ 2 , 4 } };
int len = edges.length;
System.out.println( "sum = " + count(edges, len));
}
}
|
Python 3
def count(edges, length) :
return 2 * length;
if __name__ = = "__main__" :
edges = [[ 1 , 2 ],
[ 2 , 3 ],
[ 1 , 4 ],
[ 2 , 4 ]];
length = len (edges);
print ( "sum = " , count(edges, length));
|
C#
using System;
class GFG {
static int count( int [, ] edges, int len)
{
return 2 * len;
}
public static void Main(String[] args)
{
int [, ] edges = { { 1, 2 },
{ 2, 3 },
{ 1, 4 },
{ 2, 4 } };
int len = edges.GetLength(0);
Console.WriteLine( "sum = " + count(edges, len));
}
}
|
Javascript
<script>
function count(edges, len)
{
return 2 * len;
}
var edges = [ [ 1, 2 ],
[ 2, 3 ],
[ 1, 4 ],
[ 2, 4 ] ];
var len = edges.length;
document.write( "sum = " + count(edges, len));
</script>
|
PHP
<?php
function count1( $edges , $len )
{
return 2 * $len ;
}
$edges = array ( array (1, 2),
array (2, 3),
array (1, 4),
array (2, 4));
$len = sizeof( $edges );
echo "sum = " . count1( $edges , $len ) . "\n" ;
?>
|
Space complexity: O(1)
Time complexity: O(1)
Another approach(Naive Approach):
We can iterate through the edge list and count the degree of each node. This can be done by creating a dictionary that maps each node to its degree, and then summing up the degrees of all nodes.
- Create a variable sum and initialize it to 0.This variable will store the sum of degrees of all nodes in the graph and iterate over all nodes in the graph.
- The FOR loop iterates over all nodes in the graph, where N is the total number of nodes in the graph. For each node i, it counts the number of edges that are incident on i (i.e., that have i as one of their endpoints), by iterating over all edges in the edge list and checking if i is one of their endpoints. The degree of node i is equal to the number of such edges. The degree of node i is then added to the sum and return the sum of degrees of all nodes:
- Returns the sum calculated in the previous stepand in the main function, create an edge list and call the countDegrees function to find the sum of degrees of all nodes:
Here is the implementation of above approach
C++
#include <iostream>
#include <unordered_map>
using namespace std;
int countDegrees( int edges[][2], int len)
{
unordered_map< int , int > degrees;
for ( int i = 0; i < len; i++) {
degrees[edges[i][0]]++;
degrees[edges[i][1]]++;
}
int sum = 0;
for ( auto d : degrees) {
sum += d.second;
}
return sum;
}
int main()
{
int edges[][2]
= { { 1, 2 }, { 2, 3 }, { 1, 4 }, { 2, 4 } };
int len = sizeof (edges) / ( sizeof ( int ) * 2);
cout << "sum = " << countDegrees(edges, len) << endl;
return 0;
}
|
Java
import java.util.HashMap;
import java.util.Map;
public class GFG {
public static int countDegrees( int [][] edges) {
Map<Integer, Integer> degrees = new HashMap<>();
for ( int [] edge : edges) {
degrees.put(edge[ 0 ], degrees.getOrDefault(edge[ 0 ], 0 ) + 1 );
degrees.put(edge[ 1 ], degrees.getOrDefault(edge[ 1 ], 0 ) + 1 );
}
int sum = 0 ;
for ( int degree : degrees.values()) {
sum += degree;
}
return sum;
}
public static void main(String[] args) {
int [][] edges = { { 1 , 2 }, { 2 , 3 }, { 1 , 4 }, { 2 , 4 } };
System.out.println( "sum = " + countDegrees(edges));
}
}
|
Python3
from collections import defaultdict
def count_degrees(edges):
degrees = defaultdict( int )
for edge in edges:
degrees[edge[ 0 ]] + = 1
degrees[edge[ 1 ]] + = 1
total_degrees = sum (degrees.values())
return total_degrees
def main():
edges = [[ 1 , 2 ], [ 2 , 3 ], [ 1 , 4 ], [ 2 , 4 ]]
print ( "sum =" , count_degrees(edges))
if __name__ = = '__main__' :
main()
|
C#
using System;
using System.Collections.Generic;
namespace DegreeCounting
{
class Program
{
static int CountDegrees( int [,] edges)
{
Dictionary< int , int > degrees = new Dictionary< int , int >();
for ( int i = 0; i < edges.GetLength(0); i++)
{
int node1 = edges[i, 0];
int node2 = edges[i, 1];
if (!degrees.ContainsKey(node1))
degrees[node1] = 0;
if (!degrees.ContainsKey(node2))
degrees[node2] = 0;
degrees[node1]++;
degrees[node2]++;
}
int sum = 0;
foreach ( var degree in degrees.Values)
{
sum += degree;
}
return sum;
}
static void Main( string [] args)
{
int [,] edges = {
{ 1, 2 }, { 2, 3 }, { 1, 4 }, { 2, 4 }
};
Console.WriteLine( "sum = " + CountDegrees(edges));
}
}
}
|
Time complexity: O(len), where len is the length of the edge list
Auxiliary Space: O(N), where N is the total number of nodes in the graph