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.
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;
public class Random_Edges_Graph2
{
private Map<Integer, List<Integer>> adjacencyList;
public Random_Edges_Graph2(int v)
{
adjacencyList = new HashMap<Integer, List<Integer>>();
for (int i = 1; i <= v; i++)
adjacencyList.put(i, new LinkedList<Integer>());
}
public void setEdge(int to, int from)
{
if (to > adjacencyList.size() || from > adjacencyList.size())
System.out.println("The vertices does not exists");
List<Integer> sls = adjacencyList.get(to);
sls.add(from);
List<Integer> dls = adjacencyList.get(from);
dls.add(to);
}
public List<Integer> getEdge(int to)
{
if (to > adjacencyList.size())
{
System.out.println("The vertices does not exists");
return null;
}
return adjacencyList.get(to);
}
public static void main(String args[])
{
System.out.println("Random Graph Generation");
Random random = new Random();
int e = Math.abs(random.nextInt(21 - 1) + 1);
try
{
int minV = (int) Math.ceil((1 + Math.sqrt(1 + 8 * e)) / 2);
int maxV = e + 1;
int v = Math.abs(random.nextInt(maxV - minV) + minV);
System.out.println("Random graph has "+v+" vertices");
System.out.println("Random graph has "+e+" edges");
Random_Edges_Graph2 reg = new Random_Edges_Graph2(v);
int count = 1, to, from;
while (count <= e)
{
to = Math.abs(random.nextInt(v + 1 - 1) + 1);
from = Math.abs(random.nextInt(v + 1 - 1) + 1);
reg.setEdge(to, from);
count++;
}
System.out
.println("The Adjacency List Representation of the random graph is: ");
for (int i = 1; i <= v; i++)
{
System.out.print(i + " -> ");
List<Integer> edgeList = reg.getEdge(i);
if (edgeList.size() == 0)
System.out.print("null");
else
{
for (int j = 1;; j++)
{
if (j != edgeList.size())
System.out.print(edgeList.get(j - 1) + " -> ");
else {
System.out.print(edgeList.get(j - 1));
break;
}
}
}
System.out.println();
}
}
catch (Exception E)
{
System.out.println("Something went wrong");
}
}
}
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
Related posts:
Java Program to Describe the Representation of Graph using Adjacency List
Copy a List to Another List in Java
Java Program to Implement LinkedHashSet API
Java Program to Compute the Volume of a Tetrahedron Using Determinants
Phương thức tham chiếu trong Java 8 – Method References
Java Program to Check Whether a Given Point is in a Given Polygon
Java 8 Streams peek() API
The Difference Between map() and flatMap()
Tính trừu tượng (Abstraction) trong Java
Introduction to Liquibase Rollback
A Guide to JUnit 5
Tính kế thừa (Inheritance) trong java
Spring Boot - Admin Server
REST Web service: Basic Authentication trong Jersey 2.x
Thao tác với tập tin và thư mục trong Java
Spring Security OAuth2 – Simple Token Revocation
Using Spring ResponseEntity to Manipulate the HTTP Response
Working With Maps Using Streams
Java Web Services – Jersey JAX-RS – REST và sử dụng REST API testing tools với Postman
Java Program to Represent Graph Using Adjacency List
Hướng dẫn sử dụng biểu thức chính quy (Regular Expression) trong Java
Java Program to Compute DFT Coefficients Directly
Java toString() Method
Use Liquibase to Safely Evolve Your Database Schema
Java Program to Implement Gale Shapley Algorithm
Spring REST API + OAuth2 + Angular
Spring Boot - Cloud Configuration Server
Send email with JavaMail
XML-Based Injection in Spring
REST Web service: Tạo ứng dụng Java RESTful Client với Jersey Client 2.x
Performance Difference Between save() and saveAll() in Spring Data
Deploy a Spring Boot App to Azure