0

Hi all,

I've a problem fixing the syntax error. I'm doing a function, randomGraphGenerator(int n) that will generate non-negative weighted complete graph with n vertices. Could you help me out on this.

Many thanks in advance...

```
public class randomGraphGenerator {
private final int n;
private int E;
Private Bag<Edge>[] adj;
/* create an empty randomGraphGenerator with n vertices. */
public randomGraphGenerator (int n) {
if (n < 0) throw new IllegalArgumentException("Number of vertices in a Graph must be nonnegative");
this.n = n;
this.E = 0;
adj = (Bag<Edge>[]) new Bag[n];
for (int n = 0; n < n; n++) {
adj[n] = new Bag<Edge>();
}
}
/* create a random randomGraphGenerator with n vertices and E edges. The expected running time is proportional to n + E. */
public randomGraphGenerator (int n, int E) {
this(n);
if (E < 0) throw new IllegalArgumentException("Number of edges in a Graph must be nonnegative");
for (int i = 0; i < E; i++) {
int n = (int) (Math.random() * n);
int w = (int) (Math.random() * n);
double weight = Math.round(100 * Math.random()) / 100.0;
Edge e = new Edge(n, w, weight);
addEdge(e);
}
}
/* create a weighted graph from input stream. */
public randomGraphGenerator (In in) {
this(in.readInt());
int E = in.readInt();
for (int i = 0; i < E; i++) {
int v = in.readInt();
int w = in.readInt();
double weight = in.readDouble();
Edge e = new Edge(n, w, weight);
addEdge(e);
}
}
/* Copy constructor. */
public randomGraphGenerator (randomGraphGenerator G) {
this(G.n());
this.E = G.E();
for (int n = 0; n < G.n(); n++) {
// reverse so that adjacency list is in same order as original
Stack<Edge> reverse = new Stack<Edge>();
for (Edge e : G.adj[v]) {
reverse.push(e);
}
for (Edge e : reverse) {
adj[n].add(e);
}
}
}
/* Return the number of vertices in this graph. */
public int n() {
return n;
}
/* Return the number of edges in this graph. */
public int E() {
return E;
}
/* add the undirected edge e to this graph. */
public void addEdge(Edge e) {
int n = e.either();
int w = e.other(v);
adj[n].add(e);
adj[w].add(e);
E++;
}
/* Return the edges incident to vertex n as an Iterable.
* To iterate over the edges incident to vertex n, use foreach notation:
* <tt>for (Edge e : graph.adj(n))</tt>. */
public Iterable<Edge> adj(int n) {
return adj[n];
}
/* Return all edges in this graph as an Iterable.
* To iterate over the edges in the graph, use foreach notation:
* <tt>for (Edge e : G.edges())</tt>. */
public Iterable<Edge> edges() {
Bag<Edge> list = new Bag<Edge>();
for (int n = 0; n < n; n++) {
int selfLoops = 0;
for (Edge e : adj(n)) {
if (e.other(n) > n) {
list.add(e);
}
// only add one copy of each self loop
else if (e.other(n) == n) {
if (selfLoops % 2 == 0) list.add(e);
selfLoops++;
}
}
}
return list;
}
/* Return a string representation of this graph. */
public String toString() {
String NEWLINE = System.getProperty("line.separator");
StringBuilder s = new StringBuilder();
s.append(n + " " + E + NEWLINE);
for (int n = 0; n < n; n++) {
s.append(n + ": ");
for (Edge e : adj[n]) {
s.append(e + " ");
}
s.append(NEWLINE);
}
return s.toString();
}
/* Test client. */
public static void main(String[] args) {
In in = new In(args[0]);
randomGraphGenerator G = new randomGraphGenerator(in);
StdOut.println(G);
}
}
```