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.
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.
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 email@example.com. 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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find dependencies of each Vertex in a Directed Graph
- Find the ordering of tasks from given dependencies
- Find whether it is possible to finish all tasks or not from given dependencies
- Print completed tasks at end according to Dependencies
- Graph implementation using STL for competitive programming | Set 2 (Weighted graph)
- 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
- Detect cycle in the graph using degrees of nodes of graph
- Convert undirected connected graph to strongly connected directed graph
- Check if there is a cycle with odd weight sum in an undirected graph
- Sum of the minimum elements in all connected components of an undirected graph
- Sum of degrees of all nodes of a undirected graph
- Sum of minimum element at each depth of a given non cyclic graph
- Path with minimum XOR sum of edges in a directed graph
- Maximum sum of values of nodes among all connected components of an undirected graph
- Difference Between sum of degrees of odd and even degree nodes in an Undirected Graph
- Largest subarray sum of all connected components in undirected graph
- Find if there is a path between two vertices in a directed graph
- Detect Cycle in a Directed Graph
- Graph and its representations