-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphGenerator.java
More file actions
69 lines (55 loc) · 1.83 KB
/
GraphGenerator.java
File metadata and controls
69 lines (55 loc) · 1.83 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
/** -----------------------------------------------------------------------
GraphGenerator.java
@author William Clift
Data Structures and Algorithms
Ursinus College
Project 4 - Karger's Algorith
13 May 2020
Compile and Run Instructions:
Compile: javac GraphGenerator.java
Run: java GraphGenerator [vertices] [edges]
------------------------------------------------------------------- **/
import java.util.Random;
import java.util.*;
import java.io.*;
public class GraphGenerator {
public static void main(String[] args) {
if(args.length != 2) {
System.err.println("Usage: java GraphGenerator [vertices] [edges]");
return;
}
int vertices = Integer.parseInt(args[0]);
int edges = Integer.parseInt(args[1]);
randomGraph(vertices, edges);
//randomGraphFile(vertices, edges);
}
public static void randomGraph(int vertices, int edges) {
Random rand = new Random();
System.out.println(vertices);
System.out.println(edges);
for(int i = 0; i < edges; i++) {
final int s = rand.nextInt(vertices);
final int t = rand.nextInt(vertices);
final double weight = rand.nextDouble();
System.out.println(s + " " + t + " " + weight);
}
}
public static void randomGraphFile(int vertices, int edges) {
Random rand = new Random();
try
{
PrintWriter print = new PrintWriter(new File("graph.txt"));
print.println(vertices);
print.println(edges);
for(int i = 0; i < edges; i++) {
final int s = rand.nextInt(vertices);
final int t = rand.nextInt(vertices);
final double weight = rand.nextDouble();
print.println(s + " " + t + " " + weight);
}
print.close();
}catch (IOException e){
e.printStackTrace();
}
}
}