-
Notifications
You must be signed in to change notification settings - Fork 0
/
BuildGraph.java
75 lines (66 loc) · 1.92 KB
/
BuildGraph.java
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
package emp_proj;
import java.io.*;
import java.util.*;
public class BuildGraph {
public static void main(String fileName, String directory, int vertices, int dense, int maxCapacity, int minCapacity) throws Exception {
Random random = new Random();
try {
String dirName = directory;//
if (dirName.equals("")) {
dirName = ".";
}
File outputfile = new File(dirName, fileName);
int[][] Graph = new int[vertices][vertices];
int n, m;
for (n = 0; n < vertices; n++)
for (m = n + 1; m < vertices; m++) {
int randomInt = (random.nextInt((maxCapacity - minCapacity + 1)) + minCapacity);
int k = (int) (1000.0 * Math.random() / 10.0);
int b = (k < dense) ? 1 : 0;
if (b == 0) {
Graph[n][m] = Graph[m][n] = b;
} else {
Graph[n][m] = Graph[m][n] = randomInt;
}
}
PrintWriter output = new PrintWriter(new FileWriter(outputfile));
for (int x = 0; x < Graph.length; x++) {
if (x == 0) {
for (int y = 0; y < Graph[x].length; y++) {
String value = String.valueOf(Graph[x][y]);
if (y != 0) {
if (value.equals("0") == false) {
output.print("s " + String.valueOf(y) + " " + value + "\n");
}
}
}
} else {
if (x == Graph.length - 1) {
for (int y = 0; y < Graph[x].length; y++) {
String value = String.valueOf(Graph[x][y]);
if (y != 0) {
if (value.equals("0") == false) {
output.print(String.valueOf(y) + " t " + value + "\n");
}
}
}
} else {
for (int y = 0; y < Graph[x].length; y++) {
String value = String.valueOf(Graph[x][y]);
if (y != 0) {
if (value.equals("0") == false) {
output.print(x + " " + String.valueOf(y) + " " + value + "\n");
}
}
}
}
}
}
output.close();
} catch (IOException e) {
System.err.println("Error opening file" + e);
return;
}
System.out.print("\nDone");
}
}