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 example, an arc (x, y) is considered to be directed from x to y, and the arc (y, x) is the inverted link. Y is a direct successor of x, and x is a direct predecessor of y.
The dependency is the number of connections to different vertices which are dependent on the current vertex.
Vertex 1 dependencies -> 2-> 3
Vertex 2 dependencies -> 3-> 1
Vertex 3 dependencies -> 1-> 2
Node 1 has the minimum number of dependency of 2.
Vertex 1 is dependent on 2 and 3.
Similarly, vertex 2 and 3 on (3, 1) and (1, 2) respectively.
Therefore, the minimum number of dependency among all vertices is 2.
Vertex 1 dependency -> 2-> 3-> 4-> 5-> 6
Vertex 2 dependency -> 6
Vertex 3 dependency -> 4-> 5-> 6
Vertex 4 dependency -> 5-> 6
Vertex 5 dependency -> 6
Vertex 6 is not dependent on any vertex.
Node 6 has the minimum dependency of 0
Vertex 1 is dependent on (3, 4, 5, 6, 7). Similarly, vertex 2 on (6), vertex 3 on (4, 5, 6), vertex 4 on (5, 6), vertex 5 on (6) and vertex 6 is not dependent on any.
Therefore, the minimum number of dependency among all vertices is 0.
Approach: The idea is to use depth-first search(DFS) to solve this problem.
- Get the directed graph as the input.
- Perform the DFS on the graph and explore all the nodes of the graph.
- While exploring the neighbours of the node, add 1 to count and finally return the count which signifies the number of dependencies.
- Finally, find the node with the minimum number of dependencies.
Below is the implementation of the above approach:
Vertex 1 dependency -> 2-> 3-> 4-> 5-> 6 Vertex 2 dependency -> 6 Vertex 3 dependency -> 4-> 5-> 6 Vertex 4 dependency -> 5-> 6 Vertex 5 dependency -> 6 Vertex 6 is not dependent on any vertex. Node 6has minimum dependency of 0
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.
- Check if incoming edges in a vertex of directed graph is equal to vertex itself or not
- Check if vertex X lies in subgraph of vertex Y for the given Graph
- Check if every vertex triplet in graph contains two vertices connected to third vertex
- Sum of dependencies in a graph
- Maximum number of edges that N-vertex graph can have such that graph is Triangle free | Mantel's Theorem
- k'th heaviest adjacent node in a graph where each vertex has weight
- Convert the undirected graph into directed graph such that there is no path of length greater than 1
- Convert undirected connected graph to strongly connected directed graph
- Minimum time taken by each job to be completed given by a Directed Acyclic Graph
- Find the ordering of tasks from given dependencies
- Find whether it is possible to finish all tasks or not from given dependencies
- Find a Mother Vertex in a Graph
- Find the Degree of a Particular vertex in a Graph
- Find a Mother vertex in a Graph using Bit Masking
- Find if there is a path between two vertices in a directed graph
- Find if there is a path between two vertices in a directed graph | Set 2
- Find the number of paths of length K in a directed graph
- Print completed tasks at end according to Dependencies
- Finding minimum vertex cover size of a graph using binary search
- Topological Sort of a graph using departure time of vertex
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.