-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerateSparseInstance.java
More file actions
57 lines (46 loc) · 1.44 KB
/
GenerateSparseInstance.java
File metadata and controls
57 lines (46 loc) · 1.44 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
import java.util.*;
public class GenerateSparseInstance {
static int numberOfCities = 100;
public static void main(String args[]) {
Node [] city = new Node[numberOfCities];
for (int i = 0; i < numberOfCities; i++) {
city[i] = new Node(i, numberOfCities);
}
Random r = new Random(0);
int distance;
int dice;
// To generate large, sparse random graphs we use the Erdos-Renyi model of random graphs.
for (int i = 0; i < numberOfCities; i++) {
for (int j = i + 1; j < numberOfCities; j++) {
distance = r.nextInt(100) + 1;
dice = r.nextInt(numberOfCities);
if (dice <= (numberOfCities/10)) {
city[i].setNeighbor(city[j], distance);
city[j].setNeighbor(city[i], distance);
}
}
if (city[i].getNeighbors().length == 0) {
distance = r.nextInt(100) + 1;
dice = r.nextInt(numberOfCities);
city[i].setNeighbor(city[dice], distance);
city[dice].setNeighbor(city[i], distance);
}
while (city[i].getNeighbors().length == 1) {
dice = r.nextInt(numberOfCities);
if (dice != city[i].getNeighbors()[0].getID()) {
distance = r.nextInt(100) + 1;
city[i].setNeighbor(city[dice], distance);
city[dice].setNeighbor(city[i], distance);
}
}
}
for (int i = 0; i < numberOfCities; i++){
Node[] temp = city[i].getNeighbors();
int j = 0;
while (temp[j] != null){
System.out.println(i + " " + city[i].getNeighborID(j) + " " + city[i].getDistance(j));
j++;
}
}
}
}