Java Program to Create a Minimal Set of All Edges Whose Addition will Convert it to a Strongly Connected DAG

This is a java program to find the edges other than feedback arc set so that all the edges contribute to directed acyclic graph.

Here is the source code of the Java Program to Create a Minimal Set of All Edges Whose Addition will Convert it to a Strongly Connected DAG. 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 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.         System.out.println("Minimal set of edges: ");
  52.         while (iteratorI.hasNext())
  53.         {
  54.             Integer i = iteratorI.next();
  55.             List<Integer> adjList = this.adjacencyList.get(i);
  56.             if (count == size)
  57.             {
  58.                 return this;
  59.             }
  60.             if (adjList.size() == 0)
  61.             {
  62.                 count++;
  63.                 Iterator<Integer> iteratorJ = this.adjacencyList.keySet()
  64.                         .iterator();
  65.                 while (iteratorJ.hasNext())
  66.                 {
  67.                     Integer j = iteratorJ.next();
  68.                     List<Integer> li = this.adjacencyList.get(j);
  69.                     if (li.contains(i))
  70.                     {
  71.                         li.remove(i);
  72.                         System.out.println(i + " -> " + j);
  73.                     }
  74.                 }
  75.                 this.adjacencyList.remove(i);
  76.                 iteratorI = this.adjacencyList.keySet().iterator();
  77.             }
  78.         }
  79.         return this;
  80.     }
  81.  
  82.     public Map<Integer, List<Integer>> getFeedbackArcSet(int v)
  83.     {
  84.         int[] visited = new int[v + 1];
  85.         Iterator<Integer> iterator = this.adjacencyList.keySet().iterator();
  86.         Map<Integer, List<Integer>> l = new HashMap<Integer, List<Integer>>();
  87.         while (iterator.hasNext())
  88.         {
  89.             Integer i = iterator.next();
  90.             List<Integer> list = this.adjacencyList.get(i);
  91.             visited[i] = 1;
  92.             if (list.size() != 0)
  93.             {
  94.                 for (int j = 0; j < list.size(); j++)
  95.                 {
  96.                     if (visited[list.get(j)] == 1)
  97.                     {
  98.                         l.put(i, new LinkedList<Integer>());
  99.                         l.get(i).add(j);
  100.                     }
  101.                     else
  102.                     {
  103.                         visited[list.get(j)] = 1;
  104.                     }
  105.                 }
  106.             }
  107.         }
  108.         return l;
  109.     }
  110.  
  111.     public void printAllEdges(Graph copyG, int v)
  112.     {
  113.         Map<Integer, List<Integer>> edges = this.getFeedbackArcSet(v);
  114.         Iterator<Integer> iterator = copyG.adjacencyList.keySet().iterator();
  115.         while (iterator.hasNext())
  116.         {
  117.             Integer i = iterator.next();
  118.             List<Integer> edgeList = this.getEdge(i);
  119.             if (edgeList.size() != 0)
  120.             {
  121.                 for (int j = 0; j < edgeList.size(); j++)
  122.                 {
  123.                     if (edges.containsKey(i) && edges.get(i).contains(j))
  124.                         continue;
  125.                     else
  126.                     {
  127.                         System.out.print(i + " -> " + edgeList.get(j));
  128.                     }
  129.                 }
  130.                 System.out.println();
  131.             }
  132.         }
  133.     }
  134.  
  135.     public void printGraph()
  136.     {
  137.         System.out.println("The Graph is: ");
  138.         Iterator<Integer> iterator = this.adjacencyList.keySet().iterator();
  139.         while (iterator.hasNext())
  140.         {
  141.             Integer i = iterator.next();
  142.             List<Integer> edgeList = this.getEdge(i);
  143.             if (edgeList.size() != 0)
  144.             {
  145.                 System.out.print(i);
  146.                 for (int j = 0; j < edgeList.size(); j++)
  147.                 {
  148.                     System.out.print(" -> " + edgeList.get(j));
  149.                 }
  150.                 System.out.println();
  151.             }
  152.         }
  153.     }
  154. }
  155.  
  156. public class MinimalSetofEdgesforDAG
  157. {
  158.     public static void main(String args[])
  159.     {
  160.         int v, e, count = 1, to, from;
  161.         Scanner sc = new Scanner(System.in);
  162.         Graph glist;
  163.         try
  164.         {
  165.             System.out.println("Enter the number of vertices: ");
  166.             v = sc.nextInt();
  167.             System.out.println("Enter the number of edges: ");
  168.             e = sc.nextInt();
  169.             glist = new Graph(v);
  170.             System.out.println("Enter the edges in the graph : <from> <to>");
  171.             while (count <= e)
  172.             {
  173.                 to = sc.nextInt();
  174.                 from = sc.nextInt();
  175.                 glist.setEdge(to, from);
  176.                 count++;
  177.             }
  178.             Graph copyofGlist = new Graph(v);
  179.             copyofGlist = glist;
  180.             glist.printGraph();
  181.             Graph modified = glist.checkDAG();
  182.             modified.printAllEdges(copyofGlist, v);
  183.         }
  184.         catch (Exception E)
  185.         {
  186.             System.out
  187.                     .println("You are trying to access empty adjacency list of a node.");
  188.         }
  189.         sc.close();
  190.     }
  191. }

Output:

$ javac MinimalSetofEdgesforDAG.java
$ java MinimalSetofEdgesforDAG
 
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 3
6 4
The Graph is: 
1 -> 2
2 -> 3 -> 4
4 -> 5
5 -> 6
6 -> 3 -> 4
Minimal set of edges: 
3 -> 2
3 -> 6
1 -> 2
2 -> 4
4 -> 5
5 -> 6

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

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
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.