Java Program to Find a Good Feedback Edge Set in a Graph

This is a java program to find feednack arc set. This is the set which contains edges which when removed from the graph, graph becomes directed acyclic graph.

Here is the source code of the Java Program to Find a Good Feedback Edge Set in a Graph. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1.  
  2. package com.sanfoundry.hardgraph;
  3.  
  4. import java.util.HashMap;
  5. import java.util.Iterator;
  6. import java.util.LinkedList;
  7. import java.util.List;
  8. import java.util.Map;
  9. import java.util.Scanner;
  10.  
  11. class Graph
  12. {
  13.     private Map<Integer, List<Integer>> adjacencyList;
  14.  
  15.     public Graph(int v)
  16.     {
  17.         adjacencyList = new HashMap<Integer, List<Integer>>();
  18.         for (int i = 1; i <= v; i++)
  19.             adjacencyList.put(i, new LinkedList<Integer>());
  20.     }
  21.  
  22.     public void setEdge(int from, int to)
  23.     {
  24.         if (to > adjacencyList.size() || from > adjacencyList.size())
  25.             System.out.println("The vertices does not exists");
  26.         /*
  27.          * List<Integer> sls = adjacencyList.get(to);
  28.          * sls.add(from);
  29.          */
  30.         List<Integer> dls = adjacencyList.get(from);
  31.         dls.add(to);
  32.     }
  33.  
  34.     public List<Integer> getEdge(int to)
  35.     {
  36.         /*
  37.          * if (to > adjacencyList.size())
  38.          * {
  39.          * System.out.println("The vertices does not exists");
  40.          * return null;
  41.          * }
  42.          */
  43.         return adjacencyList.get(to);
  44.     }
  45.  
  46.     public Graph checkDAG()
  47.     {
  48.         Integer count = 0;
  49.         Iterator<Integer> iteratorI = this.adjacencyList.keySet().iterator();
  50.         Integer size = this.adjacencyList.size() - 1;
  51.         while (iteratorI.hasNext())
  52.         {
  53.             Integer i = iteratorI.next();
  54.             List<Integer> adjList = this.adjacencyList.get(i);
  55.             if (count == size)
  56.             {
  57.                 return this;
  58.             }
  59.             if (adjList.size() == 0)
  60.             {
  61.                 count++;
  62.                 Iterator<Integer> iteratorJ = this.adjacencyList.keySet()
  63.                         .iterator();
  64.                 while (iteratorJ.hasNext())
  65.                 {
  66.                     Integer j = iteratorJ.next();
  67.                     List<Integer> li = this.adjacencyList.get(j);
  68.                     if (li.contains(i))
  69.                     {
  70.                         li.remove(i);
  71.                     }
  72.                 }
  73.                 this.adjacencyList.remove(i);
  74.                 iteratorI = this.adjacencyList.keySet().iterator();
  75.             }
  76.         }
  77.         return this;
  78.     }
  79.  
  80.     public void printGraph()
  81.     {
  82.         System.out.println("The Graph is: ");
  83.         Iterator<Integer> iterator = this.adjacencyList.keySet().iterator();
  84.         while (iterator.hasNext())
  85.         {
  86.             Integer i = iterator.next();
  87.             List<Integer> edgeList = this.getEdge(i);
  88.             if (edgeList.size() != 0)
  89.             {
  90.                 System.out.print(i);
  91.                 for (int j = 0; j < edgeList.size(); j++)
  92.                 {
  93.                     System.out.print(" -> " + edgeList.get(j));
  94.                 }
  95.                 System.out.println();
  96.             }
  97.         }
  98.     }
  99.  
  100.     public boolean getFeedbackArcSet(int v)
  101.     {
  102.         boolean flag = false;
  103.         int[] visited = new int[v + 1];
  104.         Iterator<Integer> iterator = this.adjacencyList.keySet().iterator();
  105.         System.out.print("The set of edges in feedback arc set: ");
  106.         while (iterator.hasNext())
  107.         {
  108.             Integer i = iterator.next();
  109.             List<Integer> list = this.adjacencyList.get(i);
  110.             visited[i] = 1;
  111.             if (list.size() != 0)
  112.             {
  113.                 for (int j = 0; j < list.size(); j++)
  114.                 {
  115.                     if (visited[list.get(j)] == 1)
  116.                     {
  117.                         flag = true;
  118.                         System.out.println(i + " - " + list.get(j));
  119.                     }
  120.                     else
  121.                     {
  122.                         visited[list.get(j)] = 1;
  123.                     }
  124.                 }
  125.             }
  126.         }
  127.         return flag;
  128.     }
  129. }
  130.  
  131. public class FeedbackArcSet
  132. {
  133.     public static void main(String args[])
  134.     {
  135.         int v, e, count = 1, to, from;
  136.         Scanner sc = new Scanner(System.in);
  137.         Graph glist;
  138.         try
  139.         {
  140.             System.out.println("Enter the number of vertices: ");
  141.             v = sc.nextInt();
  142.             System.out.println("Enter the number of edges: ");
  143.             e = sc.nextInt();
  144.             glist = new Graph(v);
  145.             System.out.println("Enter the edges in the graph : <from> <to>");
  146.             while (count <= e)
  147.             {
  148.                 to = sc.nextInt();
  149.                 from = sc.nextInt();
  150.                 glist.setEdge(to, from);
  151.                 count++;
  152.             }
  153.             glist.printGraph();
  154.             Graph modified = glist.checkDAG();
  155.             if (modified.getFeedbackArcSet(v) == false)
  156.             {
  157.                 System.out.println("None");
  158.             }
  159.         }
  160.         catch (Exception E)
  161.         {
  162.             System.out
  163.                     .println("You are trying to access empty adjacency list of a node.");
  164.         }
  165.         sc.close();
  166.     }
  167. }

Output:

$ javac FeedbackArcSet.java
$ java FeedbackArcSet	
 
Enter the number of vertices: 
6
Enter the number of edges: 
7
Enter the edges in the graph : <from> <to>
1 2
2 3
2 4
4 5
5 6
6 4
6 3
The Graph is: 
1 -> 2
2 -> 3 -> 4
4 -> 5
5 -> 6
6 -> 4 -> 3
The set of edges in feedback arc set: 6 - 4

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.