Given a directed graph and a node X. The task is to find the minimum number of edges that must be added to the graph such that any node can be reachable from the given node.
Input: X = 0
Input: X = 4
Approach: First, let’s mark all the vertices reachable from X as good, using a simple DFS. Then, for each bad vertex (vertices which are not reachable from X) v, count the number of bad vertices reachable from v (it also can be done by simple DFS). Let this number be cntv. Now, iterate over all bad vertices in non-increasing order of cntv. For the current bad vertex v, if it is still not marked as good, run a DFS from it, marking all the reachable vertices as good, and increase the answer by 1 (in fact, we are implicitly adding the edge (s, v)). It can be proved that this solution gives an optimal answer.
Below is the implementation of the above approach:
- Minimum number of Edges to be added to a Graph to satisfy the given condition
- Path with minimum XOR sum of edges in a directed graph
- Minimum edges required to make a Directed Graph Strongly Connected
- Shortest path with exactly k edges in a directed and weighted graph
- Assign directions to edges so that the directed graph remains acyclic
- Check if incoming edges in a vertex of directed graph is equal to vertex itself or not
- Shortest path with exactly k edges in a directed and weighted graph | Set 2
- Maximum number of edges to be added to a tree so that it stays a Bipartite graph
- Maximum number of edges that N-vertex graph can have such that graph is Triangle free | Mantel's Theorem
- Ways to Remove Edges from a Complete Graph to make Odd Edges
- Maximum edges that can be added to DAG so that is remains DAG
- Minimum number of edges that need to be added to form a triangle
- 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
- Find all reachable nodes from every node present in a given set
- Minimum Cost Path in a directed graph via given set of intermediate nodes
- Minimum time taken by each job to be completed given by a Directed Acyclic Graph
- Number of elements from the array which are reachable after performing given operations on D
- Minimum Cost of Simple Path between two nodes in a Directed and Weighted Graph
- Minimum number of edges between two vertices of a 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 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.