Java Program to Remove the Edges in a Cyclic Graph such that its Linear Extension can be Found

This is a java program to set of edges upon removal of which linear extension can be found. In simple terms this version of code finds the feedbackarc set, which when removed from graph leads to DAG for which we can find the topological sorting.

Here is the source code of the Java Program to Remove the Edges in a Given Cyclic Graph such that its Linear Extension can be Found. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1.  
  2. package com.sanfoundry.graph;
  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 GraphLE
  12. {
  13.     private Map<Integer, List<Integer>> adjacencyList;
  14.  
  15.     public GraphLE(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 GraphLE 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 removeEdgesToGetLinearExtension(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 RemoveEdgesLinearExtension
  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.         GraphLE 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 GraphLE(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.             GraphLE modified = glist.checkDAG();
  155.             if (modified.removeEdgesToGetLinearExtension(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 RemoveEdgesLinearExtension.java
$ java RemoveEdgesLinearExtension
 
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.

If you find any mistake above, kindly email to [email protected]

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.