Java Program to Create a Random Graph using Random Edge Selection

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

Output:

$ javac Random_Edges_Graph2.java
$ java Random_Edges_Graph2
 
Random Graph Generation
Random graph has 4 vertices
Random graph has 5 edges
The Adjacency List Representation of the random graph is: 
1 -> 4
2 -> 3 -> 3 -> 4
3 -> 3 -> 3 -> 2 -> 2
4 -> 1 -> 2

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement

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

advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification 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 20s–40s and exploring new directions in your career, I also offer mentoring. Learn more here.