Java Program to Generate a Random UnDirected Graph for a Given Number of Edges

«
»
This is a java program to generate a random graph using number of edges provided by user. One important thing to note here is, that we need to decide minimum and maximum number of nodes such that all edges get accommodated. Minimum number of vertices is positive solution to n(n-1) = 2e, where e is number of edges and maximum number of vertices is e+1.

Here is the source code of the Java Program to Generate a Random UnDirected Graph for a Given Number of Edges. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. //This is a java program to randomly generate a undirected graph where numbers of edges is given by user
  2. import java.util.HashMap;
  3. import java.util.LinkedList;
  4. import java.util.List;
  5. import java.util.Map;
  6. import java.util.Random;
  7. import java.util.Scanner;
  8.  
  9. public class Random_Undirected_Graph 
  10. {
  11.     private Map<Integer, List<Integer>> adjacencyList;
  12.  
  13.     public Random_Undirected_Graph(int v) 
  14.     {
  15.         adjacencyList = new HashMap<Integer, List<Integer>>();
  16.         for (int i = 1; i <= v; i++)
  17.             adjacencyList.put(i, new LinkedList<Integer>());
  18.     }
  19.  
  20.     public void setEdge(int to, int from) 
  21.     {
  22.         if (to > adjacencyList.size() || from > adjacencyList.size())
  23.             System.out.println("The vertices does not exists");
  24.  
  25.         List<Integer> sls = adjacencyList.get(to);
  26.         sls.add(from);
  27.         List<Integer> dls = adjacencyList.get(from);
  28.         dls.add(to);
  29.     }
  30.  
  31.     public List<Integer> getEdge(int to) 
  32.     {
  33.         if (to > adjacencyList.size()) 
  34.         {
  35.             System.out.println("The vertices does not exists");
  36.             return null;
  37.         }
  38.         return adjacencyList.get(to);
  39.     }
  40.  
  41.     public static void main(String args[]) 
  42.     {
  43.         System.out.println("Enter the number of edges: ");
  44.  
  45.         Scanner sc = new Scanner(System.in);
  46.         int e = sc.nextInt();
  47.         try 
  48.         {
  49.             int minV = (int) Math.ceil((1 + Math.sqrt(1 + 8 * e)) / 2);
  50.             int maxV = e + 1;
  51.  
  52.             Random random = new Random();
  53.             int v = Math.abs(random.nextInt(maxV - minV) + minV);
  54.             System.out.println("Random graph has "+v+" vertices");
  55.  
  56.             Random_Undirected_Graph rug = new Random_Undirected_Graph(v);
  57.             int count = 1, to, from;
  58.             while (count <= e) 
  59.             {
  60.                 to = Math.abs(random.nextInt(v + 1 - 1) + 1);
  61.                 from = Math.abs(random.nextInt(v + 1 - 1) + 1);
  62.  
  63.                 rug.setEdge(to, from);
  64.                 count++;
  65.             }
  66.  
  67.             System.out
  68.                     .println("The Adjacency List Representation of the graph is: ");
  69.  
  70.             for (int i = 1; i <= v; i++) 
  71.             {
  72.                 System.out.print(i + " -> ");
  73.                 List<Integer> edgeList = rug.getEdge(i);
  74.                 if (edgeList.size() == 0)
  75.                     System.out.print("null");
  76.                 else 
  77.                 {
  78.                     for (int j = 1;; j++) 
  79.                     {
  80.                         if (j != edgeList.size())
  81.                             System.out.print(edgeList.get(j - 1) + " -> ");
  82.                         else {
  83.                             System.out.print(edgeList.get(j - 1));
  84.                             break;
  85.                         }
  86.                     }
  87.                 }
  88.                 System.out.println();
  89.             }
  90.         } 
  91.         catch (Exception E) 
  92.         {
  93.             System.out.println("Something went wrong");
  94.         }
  95.         sc.close();
  96.     }
  97. }

Output:

advertisement
$ javac Random_Undirected_Graph.java
$ java Random_Undirected_Graph
 
Enter the number of edges: 
15
Random graph has 6 vertices
The Adjacency List Representation of the graph is: 
1 -> 4 -> 2 -> 3 -> 5
2 -> 2 -> 2 -> 1 -> 5
3 -> 5 -> 6 -> 4 -> 6 -> 1 -> 5 -> 4
4 -> 6 -> 4 -> 4 -> 1 -> 3 -> 3
5 -> 3 -> 6 -> 3 -> 1 -> 2
6 -> 4 -> 3 -> 5 -> 3

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

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

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!
advertisement
advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter