Sum of Dependencies in Graph - GeeksforGeeks

Sum of Dependencies in Graph

Last Updated : 14 Sep, 2026

Given a directed graph with V nodes and E edges, the edges are represented as a 2D array, edges[][]. Each row in the array represents a directed edge (u, v), meaning there is a directed edge from node u to node v. If there is a directed edge from u to v, it means that u depends on v.

The Number of Dependencies (NoD) for a node x is the total count of nodes that x depends upon. 

Calculate the sum of the Number of Dependencies (NoD) for all nodes in the graph.

Examples: 

Input: V = 4, E = 4, edges[][] = [[0,2],[0,3],[1,3],[2,3]]

1291

Output: 4
Explanation: For the graph in diagram, A depends on C and D i.e. A's NoD is 2, B depends on D i.e. B's NoD is 1, C depends on D i.e. D's NoD is 1 and D depends on none. Hence answer is 2 + 1 + 1 + 0 = 4.

Input: V = 4, E = 3, edges[][] = [[0,3],[0,2],[0,1]]
Output: 3
Explanation: The sum of dependencies: 3 + 0 + 0 + 0 = 3.

Try It Yourself
redirect icon

Approach - Directly Count the Edges - O(1) Time and O(1) Space

The idea is to observe that the Number of Dependencies of a node is exactly its outdegree, because every outgoing edge (u, v) represents one node v that u depends upon.

The sum of the outdegrees of all nodes in a directed graph is equal to the total number of edges. Hence, we can directly return the number of edges without traversing the graph.

  • The Number of Dependencies of a node is its outdegree.
  • Every directed edge contributes exactly 1 to the outdegree of its source node.
  • Therefore, the sum of the outdegrees of all nodes equals the total number of edges.
  • Return edges.size().
C++
#include <bits/stdc++.h>
using namespace std;

int sumOfDependencies(int V, vector<vector<int>> &edges)
{
    // Number of Dependencies of a node is its outdegree.
    // Sum of outdegrees of all nodes is equal to
    // the total number of edges.
    return edges.size();
}

int main()
{
    int V = 4;
    vector<vector<int>> edges = {{0, 2}, {0, 3}, {1, 3}, {2, 3}};

    cout << sumOfDependencies(V, edges) << endl;

    return 0;
}
Java
public class GFG {
    static int sumOfDependencies(int V, int[][] edges)
    {
        // Number of Dependencies of a node is its
        // outdegree. Sum of outdegrees of all nodes is
        // equal to the total number of edges.
        return edges.length;
    }

    public static void main(String[] args)
    {
        int V = 4;
        int[][] edges
            = { { 0, 2 }, { 0, 3 }, { 1, 3 }, { 2, 3 } };

        System.out.println(sumOfDependencies(V, edges));
    }
}
Python
def sumOfDependencies(V, edges):

    # Number of Dependencies of a node is its outdegree.
    # Sum of outdegrees of all nodes is equal to
    # the total number of edges.
    return len(edges)


# Driver Code
if __name__ == "__main__":
    V = 4
    edges = [[0, 2], [0, 3], [1, 3], [2, 3]]

    print(sumOfDependencies(V, edges))
C#
using System;

class GFG {
    static int sumOfDependencies(int V, int[, ] edges)
    {
        // Number of Dependencies of a node is its
        // outdegree. Sum of outdegrees of all nodes is
        // equal to the total number of edges.
        return edges.GetLength(0);
    }

    static void Main()
    {
        int V = 4;
        int[, ] edges = { { 0, 2 }, { 0, 3 }, { 1, 3 }, { 2, 3 } };

        Console.WriteLine(sumOfDependencies(V, edges));
    }
}
JavaScript
function sumOfDependencies(V, edges)
{
    // Number of Dependencies of a node is its outdegree.
    // Sum of outdegrees of all nodes is equal to
    // the total number of edges.
    return edges.length;
}

// Driver Code
let V = 4;
let edges = [ [ 0, 2 ], [ 0, 3 ], [ 1, 3 ], [ 2, 3 ] ];

console.log(sumOfDependencies(V, edges));

Output
4
Comment