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.
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.
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>usingnamespacestd;intsumOfDependencies(intV,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.returnedges.size();}intmain(){intV=4;vector<vector<int>>edges={{0,2},{0,3},{1,3},{2,3}};cout<<sumOfDependencies(V,edges)<<endl;return0;}
Java
publicclassGFG{staticintsumOfDependencies(intV,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.returnedges.length;}publicstaticvoidmain(String[]args){intV=4;int[][]edges={{0,2},{0,3},{1,3},{2,3}};System.out.println(sumOfDependencies(V,edges));}}
Python
defsumOfDependencies(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.returnlen(edges)# Driver Codeif__name__=="__main__":V=4edges=[[0,2],[0,3],[1,3],[2,3]]print(sumOfDependencies(V,edges))
C#
usingSystem;classGFG{staticintsumOfDependencies(intV,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.returnedges.GetLength(0);}staticvoidMain(){intV=4;int[,]edges={{0,2},{0,3},{1,3},{2,3}};Console.WriteLine(sumOfDependencies(V,edges));}}
JavaScript
functionsumOfDependencies(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.returnedges.length;}// Driver CodeletV=4;letedges=[[0,2],[0,3],[1,3],[2,3]];console.log(sumOfDependencies(V,edges));