Sum of dependencies in a graph Last Updated : 13 Sep, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report Try it on GfG Practice Given a directed and connected graph with n nodes. If there is an edge from u to v then u depends on v. Our task was to find out the sum of dependencies for every node. Example: For the graph in diagram, A depends on C and D i.e. 2 B depends on C i.e. 1 D depends on C i.e. 1 And C depends on none. Hence answer -> 0 + 1 + 1 + 2 = 4 Asked in : Flipkart Interview Recommended PracticeSum of dependencies in a graphTry It! Idea is to check adjacency list and find how many edges are there from each vertex and return the total number of edges. Implementation: C++ // C++ program to find the sum of dependencies #include <bits/stdc++.h> using namespace std; // To add an edge void addEdge(vector <int> adj[], int u,int v) { adj[u].push_back(v); } // find the sum of all dependencies int findSum(vector<int> adj[], int V) { int sum = 0; // just find the size at each vector's index for (int u = 0; u < V; u++) sum += adj[u].size(); return sum; } // Driver code int main() { int V = 4; vector<int >adj[V]; addEdge(adj, 0, 2); addEdge(adj, 0, 3); addEdge(adj, 1, 3); addEdge(adj, 2, 3); cout << "Sum of dependencies is " << findSum(adj, V); return 0; } Java // Java program to find the sum of dependencies import java.util.Vector; class Test { // To add an edge static void addEdge(Vector <Integer> adj[], int u,int v) { adj[u].addElement((v)); } // find the sum of all dependencies static int findSum(Vector<Integer> adj[], int V) { int sum = 0; // just find the size at each vector's index for (int u = 0; u < V; u++) sum += adj[u].size(); return sum; } // Driver method public static void main(String[] args) { int V = 4; @SuppressWarnings("unchecked") Vector<Integer> adj[] = new Vector[V]; for (int i = 0; i < adj.length; i++) { adj[i] = new Vector<>(); } addEdge(adj, 0, 2); addEdge(adj, 0, 3); addEdge(adj, 1, 3); addEdge(adj, 2, 3); System.out.println("Sum of dependencies is " + findSum(adj, V)); } } // This code is contributed by Gaurav Miglani Python3 # Python3 program to find the sum # of dependencies # To add an edge def addEdge(adj, u, v): adj[u].append(v) # Find the sum of all dependencies def findSum(adj, V): sum = 0 # Just find the size at each # vector's index for u in range(V): sum += len(adj[u]) return sum # Driver code if __name__=='__main__': V = 4 adj = [[] for i in range(V)] addEdge(adj, 0, 2) addEdge(adj, 0, 3) addEdge(adj, 1, 3) addEdge(adj, 2, 3) print("Sum of dependencies is", findSum(adj, V)) # This code is contributed by rutvik_56 C# // C# program to find the sum of dependencies using System; using System.Collections; class GFG{ // To add an edge static void addEdge(ArrayList []adj, int u, int v) { adj[u].Add(v); } // Find the sum of all dependencies static int findSum(ArrayList []adj, int V) { int sum = 0; // Just find the size at each // vector's index for(int u = 0; u < V; u++) sum += adj[u].Count; return sum; } // Driver code public static void Main(string[] args) { int V = 4; ArrayList []adj = new ArrayList[V]; for(int i = 0; i < V; i++) { adj[i] = new ArrayList(); } addEdge(adj, 0, 2); addEdge(adj, 0, 3); addEdge(adj, 1, 3); addEdge(adj, 2, 3); Console.Write("Sum of dependencies is " + findSum(adj, V)); } } // This code is contributed by pratham76 JavaScript let V = 4; let adj = new Array(V).fill(null).map(() => []); addEdge(adj, 0, 2); addEdge(adj, 0, 3); addEdge(adj, 1, 3); addEdge(adj, 2, 3); console.log(`Sum of dependencies is ${findSum(adj, V)}`); function addEdge(adj, u, v) { adj[u].push(v); } function findSum(adj, V) { let sum = 0; // just find the size at each vector's index for (let u = 0; u < V; u++) { sum += adj[u].length; } return sum; } // this code is contributed by devendra OutputSum of dependencies is 4 Time complexity: O(V) where V is number of vertices in graph. Comment More infoAdvertise with us Next Article Sum of dependencies in a graph S Sahil Chhabra Improve Article Tags : Graph DSA Practice Tags : Graph Similar Reads Find dependencies of each Vertex in a Directed Graph Given a directed graph containing N vertices and M edges, the task is to find all the dependencies of each vertex in the graph and the vertex with the minimum dependency. A directed graph (or digraph) is a set of nodes connected by edges, where the edges have a direction associated with them. For e 13 min read Sum of degrees of all nodes of a undirected graph 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= 8Brute force approach We will add the degree of each node of the graph and print the sum. C++ // C++ implementation o 13 min read Connecting Nodes in a Graph with Sum-based Edge Conditions Given a graph with N nodes (1 ot N) and an array A[] of size N, such that a[i] denotes the value of (i+1)th node. Initially, there are no edges in the graph. To add an edge between nodes i and j, the following condition should be satisfied: S ⥠i * j * c, where S is the sum of the value of all nodes 7 min read Generate a graph using Dictionary in Python Prerequisite - Graphs To draw graph using in built libraries - Graph plotting in PythonIn this article, we will see how to implement graph in python using dictionary data structure in python. The keys of the dictionary used are the nodes of our graph and the corresponding values are lists with each 6 min read Graph definition & meaning in DSA A Graph is a non-linear data structure consisting of vertices and edges where two vertices are connected by an edge. Example of GraphProperties of a Graph:Vertices (nodes): The points where edges meet in a graph are known as vertices or nodes. A vertex can represent a physical object, concept, or ab 4 min read Like