Java Program to Create a Random Graph using Random Edge Generation

This is a java program to generate a random graph by generating random number of edges. 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 Create a Random Graph Using Random Edge Generation. 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 generate a random graph using random edge generation
  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_Edges_Graph 
  10. {
  11.     private Map<Integer, List<Integer>> adjacencyList;
  12.  
  13.     public Random_Edges_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_Edges_Graph reg = new Random_Edges_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.                 reg.setEdge(to, from);
  64.                 count++;
  65.             }
  66.  
  67.             System.out
  68.                     .println("THe Adjacency List Representation of the random graph is: ");
  69.  
  70.             for (int i = 1; i <= v; i++) 
  71.             {
  72.                 System.out.print(i + " -> ");
  73.                 List<Integer> edgeList = reg.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.  
  98. }

Output:

$ javac Random_Edges_Graph.java
$ java Random_Edges_Graph
 
Enter the number of edges: 
5
Random graph has 4 vertices
THe Adjacency List Representation of the random graph is: 
1 -> 4 -> 4
2 -> 3 -> 3
3 -> 2 -> 4 -> 2
4 -> 1 -> 1 -> 3

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.