A feedback vertex set of a graph is a set of vertices whose removal leaves a graph without cycles. The following graph becomes a Directed acyclic Graph.
Input: Enter the number of vertices: 4 Enter the number of edges: 5 Enter the graph: <Start> <end> 1 2 2 3 3 4 4 1 1 3 Output: The Graph is: 1-> 2-> 3 2-> 3 3-> 4 4-> 1 The set of edges in FeedBack arc set: 2 - 3 4 - 1 Input: Enter the number of vertices: 5 Enter the number of edges: 5 Enter the graph: <Start> <end> 1 2 2 4 5 3 5 1 4 3. Output: The Graph is: 1-> 2 2-> 4 4-> 3 5-> 3-> 1 The set of edges in FeedBack arc set: None
- We will declare all the needed variables, like count, edges, start, end and vertices
- Now we will call the Graph Function: we will take and store the values in an adjacent list which will be declared as a variable of the class.
- Then keeping the while condition in, we will take the input of all the starting and ending points of a constructor in a graph.
- Now calling the set edge function: we check if the adjacent list is empty, if not we will store the values of it, in it.
- Then we call the printGraph() function– This method will basically print the graph, wherein we iterate the loop and print the elements, and we store it in a list
- Then we call the object of the class and call the check function — Each iterator checks if we have taken the value only a single time If the value is repeated we remove it
- Then we set the Feedback vertex and call the function -We would now check if we have visited all the nodes, if we did we would count it, and if we didn’t we would keep it equal to 1. Now using all this we would find which node can be removed.
Number of vertices: 4 Number of edges: 5 The Graph is: 1-> 2-> 3 2-> 3 3-> 4 4-> 1 The set of edges in FeedBack arc set: 2 - 3 4 - 1
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.