forked from indy256/codelibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomGraph.java
More file actions
138 lines (129 loc) · 4.49 KB
/
RandomGraph.java
File metadata and controls
138 lines (129 loc) · 4.49 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package combinatorics;
import java.util.*;
import java.util.stream.Stream;
public class RandomGraph {
// precondition: n >= 2
public static List<Integer>[] getRandomTree(int V, Random rnd) {
int[] a = new int[V - 2];
for (int i = 0; i < a.length; i++) {
a[i] = rnd.nextInt(V);
}
return PruferCode.pruferCode2Tree(a);
}
public static List<Integer>[] getRandomTree2(int n, Random rnd) {
List<Integer>[] t = Stream.generate(ArrayList::new).limit(n).toArray(List[] ::new);
int[] p = new int[n];
for (int i = 0, j; i < n; j = rnd.nextInt(i + 1), p[i] = p[j], p[j] = i, i++)
; // random permutation
for (int i = 1; i < n; i++) {
int parent = p[rnd.nextInt(i)];
t[parent].add(p[i]);
t[p[i]].add(parent);
}
return t;
}
// precondition: V >= 2, V-1 <= E <= V*(V-1)/2
public static List<Integer>[] getRandomUndirectedConnectedGraph(int V, int E, Random rnd) {
List<Integer>[] g = getRandomTree(V, rnd);
Set<Long> edgeSet = new LinkedHashSet<>();
for (int i = 0; i < V; i++) {
for (int j = i + 1; j < V; j++) {
edgeSet.add(((long) i << 32) + j);
}
}
for (int i = 0; i < V; i++) {
for (int j : g[i]) {
edgeSet.remove(((long) i << 32) + j);
}
}
List<Long> edges = new ArrayList<>(edgeSet);
for (int x : getRandomArrangement(edges.size(), E - (V - 1), rnd)) {
long e = edges.get(x);
int u = (int) (e >>> 32);
int v = (int) e;
g[u].add(v);
g[v].add(u);
}
for (int i = 0; i < V; i++) Collections.sort(g[i]);
return g;
}
// precondition: V >= 2, V-1 <= E <= V*(V-1)/2
public static List<Integer>[] getRandomUndirectedConnectedGraph2(int V, int E, Random rnd) {
List<Integer>[] g = getRandomTree(V, rnd);
Set<Long> edgeSet = new LinkedHashSet<>();
for (int i = 0; i < V; i++) {
for (int j : g[i]) {
edgeSet.add(((long) i << 32) + j);
}
}
for (int i = 0; i < E - (V - 1); i++) {
int u;
int v;
long edge;
while (true) {
u = rnd.nextInt(V);
v = rnd.nextInt(V);
edge = ((long) u << 32) + v;
if (u < v && !edgeSet.contains(edge))
break;
}
edgeSet.add(edge);
g[u].add(v);
g[v].add(u);
}
for (int i = 0; i < V; i++) Collections.sort(g[i]);
return g;
}
static int[] getRandomArrangement(int n, int m, Random rnd) {
int[] res = new int[n];
for (int i = 0; i < n; i++) {
res[i] = i;
}
for (int i = 0; i < m; i++) {
int j = n - 1 - rnd.nextInt(n - i);
int t = res[i];
res[i] = res[j];
res[j] = t;
}
return Arrays.copyOf(res, m);
}
// Usage example
public static void main(String[] args) {
List<Integer>[] tree = PruferCode.pruferCode2Tree(new int[] {3, 3, 3, 4});
System.out.println(Arrays.toString(tree));
System.out.println(Arrays.toString(PruferCode.tree2PruferCode(tree)));
System.out.println(Arrays.toString(PruferCode.pruferCode2Tree(new int[] {0, 0})));
Random rnd = new Random(1);
for (int step = 0; step < 1000; step++) {
int V = rnd.nextInt(50) + 2;
checkGraph(V, V - 1, rnd);
checkGraph(V, V * (V - 1) / 2, rnd);
checkGraph(V, rnd.nextInt(V * (V - 1) / 2 - (V - 1) + 1) + V - 1, rnd);
}
}
static void checkGraph(int V, int E, Random rnd) {
List<Integer>[] g = getRandomUndirectedConnectedGraph(V, E, rnd);
int n = g.length;
int[][] a = new int[n][n];
int edges = 0;
for (int i = 0; i < n; i++) {
for (int j : g[i]) {
++a[i][j];
++edges;
}
}
if (edges != 2 * E) {
throw new RuntimeException();
}
for (int i = 0; i < n; i++) {
if (a[i][i] != 0) {
throw new RuntimeException();
}
for (int j = 0; j < n; j++) {
if (a[i][j] != a[j][i] || a[i][j] != 0 && a[i][j] != 1) {
throw new RuntimeException();
}
}
}
}
}