Question: I would like an understanding of the following code in java without changing the meaning, it is a used to generate graphs code import java.io

I would like an understanding of the following code in java without changing the meaning, it is a used to generate graphs
code
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.*;
public class RandomGraph {
private static final String NL ="
";
/**
* Entrance point for the program.
* java RandomGraph v, e, m, f
* @param v the first endpoint of this edge
* @param e - the number of edges leaving each node
* @param min - the lower bound on the edge capacities
* @param max - the upper bound on the edge capacities
* @param f - path and file name for saving the graph
*/
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 - the number of vertices in the graph");
System.out.println("e - the number of edges leaving each node");
System.out.println("min - the lower bound on edge capacities");
System.out.println("max - the upper bound on edge capacities");
System.out.println("f - path and file name for saving this graph");
System.out.println("Example: java RandomGraph 9995075101 graph1.txt");
}else if(Integer.parseInt(args[0])> Integer.parseInt(args[1])){
if(Integer.parseInt(args[3])>= Integer.parseInt(args[2])){
toFile(graphBuilder(Integer.parseInt(args[0]),Integer.parseInt(args[1]),Integer.parseInt(args[2]),Integer.parseInt(args[3])),args[4]);
System.out.println("
DONE!");
}else{
System.out.println("
FAIL!");
System.out.println("Max must be greater than or equal to min.");
}
}else{
System.out.println("
FAIL!");
System.out.println("The number of vertices must exceed the number of edges leaving each node.");
}
}
/**
** Constructor
* This method creates a 3 token representation of a graph.
* @param v The number of vertices in the graph
* @param e The number of edges leaving each vertice
* @param min The lowerbound on the capacity value of each edge
* @param max The upperbound on the capacity value of each edge
* @return A string buffer, each line contains 3 tokens corresponding
* to a directed edge: the tail, the head, and the capacity.
*/
public static StringBuffer graphBuilder(int v, int e, int min, int max){
int i;
int j;
int head;
int c;
SortedSet s;
Random gen = new Random();
StringBuffer bfr = new StringBuffer();
//Add distinguished node s
j =1;
s = new TreeSet();
while(j <= e){
head = gen.nextInt(v)+1;
if(!s.contains(head)){
s.add(head);
c = min + gen.nextInt(max - min +1);
bfr.append("s"+""+"v"+head+""+c+NL);
j++;
}
}
//Add distinguished node t
j =1;
s = new TreeSet();
while(j <= e){
int tail = gen.nextInt(v)+1;
if(!s.contains(tail)){
s.add(tail);
c = min + gen.nextInt(max - min +1);
bfr.append("v"+tail+""+"t"+""+c+NL);
j++;
}
}
//Add internal nodes
for(i =1; i <= v; i++){
s = new TreeSet();
s.add(i);
j =1;
while(j <= e){
head = gen.nextInt(v)+1;
if(!s.contains(head)){
s.add(head);
c = min + gen.nextInt(max - min +1);
bfr.append("v"+i+""+"v"+head+""+c+NL);
j++;
}
}
}
return bfr;
}
/**
* This method attempts to save a string at a given location.
* @param outString The StringBuffer containing the data being saved
* @param filename The complete file path including file name
*/
private static void toFile(StringBuffer outString, String filename){
try{
BufferedWriter fout = new BufferedWriter(new FileWriter(filename));
fout.write(outString.toString());
fout.close();
}catch(Exception e){
System.out.println("Error saving file.");
System.out.println("Please check file paths and restart this program.");
System.exit(1);
}
}
}
Errors
Cannot resolve symbol 'v'
Cannot resolve symbol 'e'
Cannot resolve symbol 'min'
Cannot resolve symbol 'max'
Cannot resolve symbol 'f'
Raw use of parameterized class 'SortedSet'
Raw use of parameterized class 'TreeSet'
Unchecked call to 'add(E)' as a member of raw type 'java.util.Set'
String concatenation as argument to 'StringBuffer.append()' call
Raw use of parameterized class 'TreeSet'
Unchecked call to 'add(E)' as a member of raw type 'java.util.Set'
String concatenation as argument to 'StringBuffer.append()' call
Raw use of parameterized class 'TreeSet'
Unchecked call to 'add(E)' as a member of raw type 'java.util.Set'
Unchecked call to 'add(E)' as a member of raw type 'java.util.Set'
String concatenation as argument to 'StringBuffer.append()' call
output expected to a faile called graph1
10315 output.txt
s v42
s v65
s v81
v17t 3

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!