Given an undirected graph consisting of N nodes containing values from the range [1, N] and M edges in a matrix Edges, the task is to determine the minimum number of edges required to be removed such that the resulting graph does not contain any cycle.
Input: N = 3, M = 3, edges = [[1, 2], [2, 3], [3, 1]]
Removing any one of the edges will make the graph acyclic. Therefore, at least one edge needs to be removed.
Input: N = 3, M = 2, edges = [[1, 2], [2, 3]]
Explanation: Graph is already acyclic. Therefore, no edge removal is required.
Naive Approach: The simplest approach is to try deleting all possible combination of sequence of edges from the given graph one by one and for each combination, count the number of removals required to make the graph acyclic. Finally, among these combinations, choose the one which deletes the minimum number of edges to obtain an acyclic graph.
Time Complexity: O(M!)
Auxiliary Space: O(N + M)
Efficient Approach: The above approach can be optimized based on the following observations:
- A graph is acyclic when it is a Tree or a forest of trees(disconnected groups of trees).
- A tree with C nodes will have (C – 1) edges.
- If there are K connected components from C1 to CK, then minimum number of edges to be removed is equal to:
M – (C1 – 1) – (C2 – 1) … (Ck -1 )
=> M – (C1 + … + CK) + K
=> M – N + K
Follow the steps below to solve the problem:
- Find the number of connected components from the given graph using DFS.
- Considering the count of connected components to be K, then print M – N + K as the required minimum number of edges to be removed to make the resulting graph acyclic.
Below is the implementation of the above approach:
Time Complexity: O(N + M)
Auxiliary Space: O(N + M)
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.
- Assign directions to edges so that the directed graph remains acyclic
- Count ways to change direction of edges such that graph becomes acyclic
- Minimum labelled node to be removed from undirected Graph such that there is no cycle
- Minimum edges required to make a Directed Graph Strongly Connected
- Maximum number of edges to be removed to contain exactly K connected components in the Graph
- Maximum number of edges among all connected components of an undirected graph
- Minimum time taken by each job to be completed given by a Directed Acyclic Graph
- Calculate number of nodes between two vertices in an acyclic Graph by DFS method
- Convert undirected connected graph to strongly connected directed graph
- Clone a Directed Acyclic Graph
- DFS for a n-ary tree (acyclic graph) represented as adjacency list
- Longest path in a directed Acyclic graph | Dynamic Programming
- Minimum number of edges between two vertices of a graph using DFS
- Minimum characters required to be removed to sort binary string in ascending order
- Minimum colors required such that edges forming cycle do not have same color
- Minimum size substring to be removed to make a given string palindromic
- Minimum edges to be added in a directed graph so that any node can be reachable from a given node
- Find minimum weight cycle in an undirected graph
- Sum of the minimum elements in all connected components of an undirected graph
- Mimimum number of leaves required to be removed from a Tree to satisfy the given condition
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.