Ask your own question, for FREE!
Computer Science 17 Online
OpenStudy (anonymous):

Can somebody anybody help me run this code plz import java.util.Stack; public class EdgeWeightedGraph { private final int V; private int E; private Bag[] adj; public EdgeWeightedGraph(int V) { if (V < 0) throw new IllegalArgumentException("Number of vertices must be nonnegative"); this.V = V; this.E = 0; adj = (Bag[]) new Bag[V]; for (int v = 0; v < V; v++) { adj[v] = new Bag(); } } public EdgeWeightedGraph(int V, int E) { this(V); if (E < 0) throw new IllegalArgumentException("Number of edges must be nonnegative"); for (int i = 0; i < E; i++) { int v = (int) (Math.random() * V); int w = (int) (Math.random() * V); double weight = Math.round(100 * Math.random()) / 100.0; Edge e = new Edge(v, w, weight); addEdge(e); } } public EdgeWeightedGraph(In in) { this(in.readInt()); int E = in.readInt(); if (E < 0) throw new IllegalArgumentException("Number of edges must be nonnegative"); for (int i = 0; i < E; i++) { int v = in.readInt(); int w = in.readInt(); double weight = in.readDouble(); Edge e = new Edge(v, w, weight); addEdge(e); } } public EdgeWeightedGraph(EdgeWeightedGraph G) { this(G.V()); this.E = G.E(); for (int v = 0; v < G.V(); v++) { // reverse so that adjacency list is in same order as original Stack reverse = new Stack(); for (Edge e : G.adj[v]) { reverse.push(e); } for (Edge e : reverse) { adj[v].add(e); } } } public int V() { return V; } public int E() { return E; } // throw an IndexOutOfBoundsException unless 0 <= v < V private void validateVertex(int v) { if (v < 0 || v >= V) throw new IndexOutOfBoundsException("vertex " + v + " is not between 0 and " + (V-1)); } public void addEdge(Edge e) { int v = e.either(); int w = e.other(v); validateVertex(v); validateVertex(w); adj[v].add(e); adj[w].add(e); E++; } public Iterable adj(int v) { validateVertex(v); return adj[v]; } public int degree(int v) { validateVertex(v); return adj[v].size(); } public Iterable edges() { Bag list = new Bag(); for (int v = 0; v < V; v++) { int selfLoops = 0; for (Edge e : adj(v)) { if (e.other(v) > v) { list.add(e); } // only add one copy of each self loop (self loops will be consecutive) else if (e.other(v) == v) { if (selfLoops % 2 == 0) list.add(e); selfLoops++; } } } return list; } public String toString() { String NEWLINE = System.getProperty("line.separator"); StringBuilder s = new StringBuilder(); s.append(V + " " + E + NEWLINE); for (int v = 0; v < V; v++) { s.append(v + ": "); for (Edge e : adj[v]) { s.append(e + " "); } s.append(NEWLINE); } return s.toString(); } public static void main(String[] args) { In in = new In(args[0]); EdgeWeightedGraph G = new EdgeWeightedGraph(in); StdOut.println(G); } }

OpenStudy (anonymous):

@Abhisar @Callisto @wio help plz

OpenStudy (woodrow73):

What's the Stack class do?

OpenStudy (anonymous):

WELL its suppose to be a weighted graph

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!