Given a non-cyclic graph having V nodes and E edges and a source node S, the task is to calculate the sum of the minimum element at each level from source node S in the given graph.
Input: S = 0, Below is the given graph
There is only one node at depth 0 i.e. 0.
At depth 1 there are 3 nodes 1, 2, 3, and minimum of them is 1.
At depth 2 there are another 3 nodes i.e. 6, 4, 5, and a minimum of them is 4.
So the sum of minimum element at each depth is 0 + 1 + 4 = 5.
Input: S = 2, Below is the given graph
At depth 0 only 1 node exists i.e. 2.
At depth 1 minimum element is 0.
At depth 2 minimum element is 1.
At depth 3 minimum element is 5
So the sum of minimum element at each depth is 2 + 0 + 1 + 5 = 8.
Approach: The idea is to use DFS Traversal. Below are the steps:
- Initialise an array(say arr) to store the minimum element at each level.
- Start the DFS Traversal from the given source node S with a variable depth(initially 0).
- Update the minimum value of current depth in the array arr.
- Recursively recurr for child node with incrementing the value of depth from the previous recursive call such that the minimum value at corresponding depth can be updated accordingly.
- After the above steps the sum of values stored in arr is the required total sum.
Below is the implementation of the above approach:
Time Complexity: O(V + E)
Auxillary Space: O(V)
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.
- Depth First Search or DFS for a Graph
- Iterative Depth First Traversal of Graph
- Minimum valued node having maximum depth in an N-ary Tree
- Check if minimum element in array is less than or equals half of every other element
- Minimum distance between the maximum and minimum element of a given Array
- Minimum Cost Graph
- Maximum and minimum isolated vertices in a graph
- Minimum number of edges between two vertices of a Graph
- Minimum value of distance of farthest node in a Graph
- Path with minimum XOR sum of edges in a directed graph
- Minimum number of edges between two vertices of a graph using DFS
- Minimum number of colors required to color a graph
- Sum of the minimum elements in all connected components of an undirected graph
- Find minimum weight cycle in an undirected graph
- Minimum labelled node to be removed from undirected Graph such that there is no cycle
- Minimum number of Edges to be added to a Graph to satisfy the given condition
- Minimum Cost Path in a directed graph via given set of intermediate nodes
- Finding minimum vertex cover size of a graph using binary search
- Minimum edges required to make a Directed Graph Strongly Connected
- Minimum Cost of Simple Path between two nodes in a Directed and Weighted Graph
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : ronak037