# Sum of dependencies in a graph

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

Idea is to check adjacency list and find how many edges are there from each vertex and return the total number of edges.

## 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; ` `} ` |

*chevron_right*

*filter_none*

## 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` `; ` ` ` `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 ` |

*chevron_right*

*filter_none*

Output:

Sum of dependencies is 4

Time complexity : O(V) where V is number of vertices in graph.

This article is contributed by **Sahil Chhabra (akku)**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- Find dependencies of each Vertex in a Directed Graph
- Print completed tasks at end according to Dependencies
- Find the ordering of tasks from given dependencies
- Find whether it is possible to finish all tasks or not from given dependencies
- Convert the undirected graph into directed graph such that there is no path of length greater than 1
- Maximum number of edges that N-vertex graph can have such that graph is Triangle free | Mantel's Theorem
- Graph implementation using STL for competitive programming | Set 2 (Weighted graph)
- Detect cycle in the graph using degrees of nodes of graph
- Convert undirected connected graph to strongly connected directed graph
- Transpose graph
- Islands in a graph using BFS
- BFS for Disconnected Graph
- Bridges in a graph
- Hypercube Graph
- Biconnected graph
- Graph and its representations
- Dominant Set of a Graph
- Graph representations using set and hash
- Check if a given graph is Bipartite using DFS
- Fibonacci Cube Graph