-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerateInput.java
More file actions
151 lines (122 loc) · 5.04 KB
/
Copy pathgenerateInput.java
File metadata and controls
151 lines (122 loc) · 5.04 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
139
140
141
142
143
144
145
146
147
148
149
import java.util.Map;
import java.util.HashMap;
import java.util.Set;
import java.util.Map;
import java.util.HashMap;
import java.util.Set;
import java.util.HashSet;
import java.util.List;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class generateInput {
public static final int MAX_EDGE_WEIGHT = 20;
public static void main (String[] args){
/*
Scanner sc = new Scanner(System.in);
System.out.println("Do you want sparse (s) or dense (d)?");
String input = sc.nextLine();
System.out.println("How many vertices do you want?");
int noOfVertices = Integer.parseInt(sc.nextLine());
sc.close();
boolean isSparse = input.equalsIgnoreCase("s");
generateGraph(isSparse, noOfVertices);
*/
if (args.length != 2) {
//System.out.println("Usage: java generateInput <s/d> <numOfVertices>");
System.exit(1);
}
String input = args[0];
int noOfVertices = Integer.parseInt(args[1]);
boolean isSparse = input.equalsIgnoreCase("s");
generateGraph(isSparse, noOfVertices);
}
public static void generateGraph(boolean isSparse,int noOfVertices){
Random random = new Random();
try {
String filename = isSparse ? "test/sparseTest.txt" : "test/denseTest.txt";
BufferedWriter writer = new BufferedWriter(new FileWriter(filename));
writer.write(String.valueOf(noOfVertices));
writer.newLine();
// generate sparse graph
if (isSparse) {
int count = 0;
int noOfEdges = noOfVertices - 1;
Set<Integer> setOfConnectedVertices = new HashSet<>();
List<Integer> setIndex = new ArrayList<>(); // used for indexing and randomisation
int startingVertex = random.nextInt(noOfVertices);
setOfConnectedVertices.add(startingVertex);
setIndex.add(startingVertex);
while (count < noOfEdges) {
int randomIndex = random.nextInt(setIndex.size());
int randomVertex1 = setIndex.get(randomIndex);
int randomVertex2 = random.nextInt(noOfVertices);
while (setOfConnectedVertices.contains(randomVertex2)) { // loop until a vertex that is not connected
randomVertex2 = random.nextInt(noOfVertices);
}
setOfConnectedVertices.add(randomVertex2);
setIndex.add(randomVertex2);
int randomWeight = 1 + random.nextInt(MAX_EDGE_WEIGHT);
int randomiseToAndFrom = random.nextInt(2); // output 0 or 1
String entry = String.format("%d %d %d", randomVertex1, randomVertex2, randomWeight);
if (randomiseToAndFrom == 1) { // randomisation of direction
entry = String.format("%d %d %d", randomVertex2, randomVertex1, randomWeight);
}
writer.write(entry);
writer.newLine();
count++;
}
}
// generate dense graph
else {
for (int i = 0; i < noOfVertices; i++) { // noOfEdges = noOfVertices * (noOfVertices - 1);
for (int j = 0; j < noOfVertices; j++) {
if (i == j) {
continue;
}
int randomWeight = 1 + random.nextInt(MAX_EDGE_WEIGHT);
String entry = String.format("%d %d %d", i, j, randomWeight);
writer.write(entry);
writer.newLine();
}
}
}
writer.close();
//System.out.println("Results saved to " + filename);
}
catch (IOException e){
System.err.println("Error writing to file: " + e.getMessage());
}
}
/* Helper Function */
/* ONLY FOR UNDIRECTED GRAPH
// Union-Find: Find operation with path compression
private static int find(int[] parent, int i) {
while(i!=parent[i]){
parent[i] = parent[parent[i]];
i=parent[i];
}
return i;
}
private static void Union(int[] parent, int[]sz , int p , int q) {
int i = find(parent,p);
int j = find(parent,q);
if(i==j) return;
if(sz[i]<sz[j]) {parent[i] = j ; sz[j] += sz[i];}
else {parent[j] = i ; sz[i] += sz[j];}
}
private static boolean ifConnected(int[] parent) {
int root = find(parent,0);
for(int vertex:parent)
{
// not the same root
if(root!=find(parent,vertex)) return false;
}
return true;
*/
}