Question: Some one please run for me this code and help me understand why am not able to get an output and where i can adjust

Some one please run for me this code and help me understand why am not able to get an output and where i can adjust the v, e, in and max values
code
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.*;
public class RandomGraph {
private static final String NL ="
";
/**
* Main method: Entry point for the program.
* Usage: java RandomGraph v e min max f
* @param args Command-line arguments: v (vertices), e (edges per node), min (min capacity), max (max capacity), f (file path)
*/
public static void main(String[] args){
if (args.length !=5){
System.out.println("Invalid parameters!");
System.out.println("Usage:");
System.out.println("java RandomGraph v e min max f");
System.out.println("v - Number of vertices in the graph");
System.out.println("e - Number of edges leaving each node");
System.out.println("min - Lower bound on edge capacities");
System.out.println("max - Upper bound on edge capacities");
System.out.println("f - File path for saving this graph");
System.out.println("Example: java RandomGraph 103510 graph1.txt");
return;
}
try {
int v = Integer.parseInt(args[0]);
int e = Integer.parseInt(args[1]);
int min = Integer.parseInt(args[2]);
int max = Integer.parseInt(args[3]);
String filePath = args[4];
if (v <= e){
System.out.println("FAIL! The number of vertices must exceed the number of edges leaving each node.");
return;
}
if (max < min){
System.out.println("FAIL! Max must be greater than or equal to min.");
return;
}
StringBuffer graphData = graphBuilder(v, e, min, max);
toFile(graphData, filePath);
System.out.println("DONE! Graph saved to "+ filePath);
} catch (NumberFormatException e){
System.out.println("Error: Parameters v, e, min, and max must be integers.");
}
}
/**
* Builds the graph data.
* @param v Number of vertices in the graph
* @param e Number of edges leaving each vertex
* @param min Lower bound on edge capacities
* @param max Upper bound on edge capacities
* @return A StringBuffer containing the graph representation
*/
public static StringBuffer graphBuilder(int v, int e, int min, int max){
Random gen = new Random();
StringBuffer bfr = new StringBuffer();
// Add edges from source node 's'
SortedSet s = new TreeSet<>();
int j =1;
while (j <= e){
int head = gen.nextInt(v)+1;
if (!s.contains(head)){
s.add(head);
int c = min + gen.nextInt(max - min +1);
bfr.append(String.format("s v%d %d%s", head, c, NL));
j++;
}
}
// Add edges to sink node 't'
s.clear();
j =1;
while (j <= e){
int tail = gen.nextInt(v)+1;
if (!s.contains(tail)){
s.add(tail);
int c = min + gen.nextInt(max - min +1);
bfr.append(String.format("v%d t %d%s", tail, c, NL));
j++;
}
}
// Add edges for internal nodes
for (int i =1; i <= v; i++){
s.clear();
s.add(i);
j =1;
while (j <= e){
int head = gen.nextInt(v)+1;
if (!s.contains(head)){
s.add(head);
int c = min + gen.nextInt(max - min +1);
bfr.append(String.format("v%d v%d %d%s", i, head, c, NL));
j++;
}
}
}
return bfr;
}
/**
* Writes the graph data to a file.
* @param outString The graph data as a StringBuffer
* @param filename The file path where the graph will be saved
*/
private static void toFile(StringBuffer outString, String filename){
try (BufferedWriter fout = new BufferedWriter(new FileWriter(filename))){
fout.write(outString.toString());
} catch (Exception e){
System.out.println("Error saving file.");
System.out.println("Please check the file path and try again.");
System.exit(1);
}
}
}
output
s v5263
s v1143
s v2147
v9 t 235
v19 t 151

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!