-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathErdosRenyi.java
More file actions
51 lines (39 loc) · 1.15 KB
/
ErdosRenyi.java
File metadata and controls
51 lines (39 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
public class ErdosRenyi {
public static void main(String[] args) {
if (args.length != 2) {
System.out.println("Usage: <probabiblity of edge> <number of nodes>");
return;
}
double prob = Double.parseDouble(args[0]);
int N = Integer.parseInt(args[1]);
if (prob <= 0 || prob > 1) {
System.out.println("Edge probability = " + prob + ": must be in (0, 1]");
return;
}
if (N <= 0) {
System.out.println("Number of nodes: " + N + ": must be positive");
return;
}
int[][] graph = new int[N][N];
// Generate an undirected random graph with N nodes
// and probability p of any two nodes to be connected.
for(int i=0; i < N; i++){
for(int j=i+1; j< N; j++){
double rand = Math.random();
if(rand <= prob){
graph[i][j]=1;
graph[j][i]= 1;
}
}
}
// Print out the resulting graph as NxN matrix of 0's and 1's
// Use nested loops and a combination of System.print and System.pritln.
// For example, a possible output for an 3x3 matrix may looks as follows:
for(int i=0; i<N; i++){
for(int j=0; j<N; j++){
System.out.print(graph[i][j]);
}
System.out.println();
}
}
}