-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.java
More file actions
52 lines (37 loc) · 1.25 KB
/
Copy pathGraph.java
File metadata and controls
52 lines (37 loc) · 1.25 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
import java.security.InvalidParameterException;
import java.util.Random;
public final class Graph {
private static final Random rand = new Random();
/*
* Количество вершин
*/
private final int size;
/*
* Матрица смежности
*/
private final int[][] adjacencyMatrix;
private Graph(int size, int[][] adjacencyMatrix) {
this.size = size;
this.adjacencyMatrix = adjacencyMatrix;
}
public int getSize() {
return size;
}
public int[][] getAdjacencyMatrix() {
return adjacencyMatrix;
}
public static Graph createByProbability(int size, double probability) {
if (size <= 0 || probability < 0.0001 || probability > 0.9999) {
throw new InvalidParameterException();
}
int[][] adjacencyMatrix = new int[size][size];
for (int i = 0; i < size - 1; ++i) {
for (int j = i + 1; j < size; ++j) {
int value = ((rand.nextDouble() - probability < 0.0001) ? 1 : 0);
adjacencyMatrix[i][j] = value;
adjacencyMatrix[j][i] = value;
}
}
return new Graph(size, adjacencyMatrix);
}
}