Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

import java.util.*; /** * A representation of a graph. * Assumes that we do not

ID: 3639732 • Letter: I

Question


import java.util.*;

/**
* A representation of a graph.
* Assumes that we do not have negative cost edges in the graph.
*/
public class MyGraph implements Graph {
private Collection<Vertex> myVertices; // the vertices in this graph
private Collection<Edge> myEdges; // the edges in this graph
//private Queue<Vertex> zero;

/**
* Creates a MyGraph object with the given collection of vertices
* and the given collection of edges.
* @param v a collection of the vertices in this graph
* @param e a collection of the edges in this graph
*/
public MyGraph(Collection<Vertex> v, Collection<Edge> e) {
// YOUR CODE HERE
myVertices.addAll(v);
myEdges.addAll(e);
}


/**
* Return the collection of vertices of this graph
* @return the vertices as a collection (which is anything iterable)
*/
public Collection<Vertex> vertices() {
// YOUR CODE HERE
return myVertices;

}

/**
* Return the collection of edges of this graph
* @return the edges as a collection (which is anything iterable)
*/
public Collection<Edge> edges() {

// YOUR CODE HERE
return myEdges;
}

/**
* Return a collection of vertices adjacent to a given vertex v.
* i.e., the set of all vertices w where edges v -> w exist in the graph.
* Return an empty collection if there are no adjacent vertices.
* @param v one of the vertices in the graph
* @return an iterable collection of vertices adjacent to v in the graph
* @throws IllegalArgumentException if v does not exist.
*/
public Collection<Vertex> adjacentVertices(Vertex v) {
if(!myVertices.contains(v)){
throw new IllegalArgumentException();
}
// YOUR CODE HERE
Collection<Vertex> temp = new TreeSet<Vertex>();
Iterator itr = myEdges.iterator();
while(itr.hasNext()){
Object b = itr.next();
Edge x = (Edge)b;
if(v.equals(x.from)){
temp.add(x.to);
}
}
return temp;

}

/**
* Test whether vertex b is adjacent to vertex a (i.e. a -> b) in a directed graph.
* Assumes that we do not have negative cost edges in the graph.
* @param a one vertex
* @param b another vertex
* @return cost of edge if there is a directed edge from a to b in the graph,
* return -1 otherwise.
* @throws IllegalArgumentException if a or b do not exist.
*/
public int isAdjacent(Vertex a, Vertex b) {

// YOUR CODE HERE
if(!myVertices.contains(a) || !myVertices.contains(b)){
throw new IllegalArgumentException();
}
Iterator itr = myEdges.iterator();
Object x;
while(itr.hasNext()){
x = itr.next();
Edge temp = (Edge)x;
if(a.equals(temp.from) && b.equals(temp.to)){
return temp.w;
}
}
return -1;
}

/**
* Returns the shortest path from a to b in the graph. Assumes positive
* edge weights. Uses Dijkstra's algorithm.
* @param a the starting vertex
* @param b the destination vertex
* @param path a list in which the path will be stored, in order, the first
* being the start vertex and the last being the destination vertex. The
* list will be empty if no such path exists. NOTE: the list will be
* cleared of any previous data. path is not expected to contain any data
* needed by the method when the method is called. It is used to allow
* us to return multiple values from the function.
* @return the length of the shortest path from a to b, -1 if no such path
* exists.
* @throws IllegalArgumentException if a or b does not exist.
*/
public int shortestPath(Vertex a, Vertex b, List<Vertex> path) {

// YOUR CODE HERE

}

}

/**
* Representation of a graph vertex
*/
public class Vertex {
private final String label; // label attached to this vertex
public final boolean known;

/**
* Construct a new vertex
* @param label the label attached to this vertex
*/
public Vertex(String label) {
if(label == null)
throw new IllegalArgumentException("null");
this.label = label;
this.known = false;
}

/**
* Get a vertex label
* @return the label attached to this vertex
*/
public String getLabel() {
return label;
}

/**
* A string representation of this object
* @return the label attached to this vertex
*/
public String toString() {
return label;
}

//auto-generated: hashes on label
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((label == null) ? 0 : label.hashCode());
return result;
}

//auto-generated: compares labels
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final Vertex other = (Vertex) obj;
if (label == null) {
if (other.label != null)
return false;
} else if (!label.equals(other.label))
return false;
return true;
}


}

/**
* Representation of a directed graph edge.
*/
public class Edge {
public final Vertex from,to;
public final int w;

/**
* Construct a new edge
* @param from start vertex
* @param to end vertex
* @param w weight of this edge
*/
public Edge(Vertex from, Vertex to, int w) {
if(from == null || to == null)
throw new IllegalArgumentException("null");
this.from = from;
this.to = to;
this.w = w;
}

/**
* A string representation of this object
* @return A string of the form <from, to, weight>
*/
public String toString() {
return "<"+from+", "+to+", "+w+">";
}

//auto-generated: hashes on all fields
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((from == null) ? 0 : from.hashCode());
result = prime * result + ((to == null) ? 0 : to.hashCode());
result = prime * result + w;
return result;
}

//auto-generated: compares all fields
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final Edge other = (Edge) obj;
if (from == null) {
if (other.from != null)
return false;
} else if (!from.equals(other.from))
return false;
if (to == null) {
if (other.to != null)
return false;
} else if (!to.equals(other.to))
return false;
if (w != other.w)
return false;
return true;
}


}


Explanation / Answer

Hi! Cramster Terms & Conditions were changed recently (I hope you have read them). Now, experts have to send the answers to askers' inbox after you rate them. That is because of people copying from older threads which have public answers. Hence we can only send the answer after you rate. So please give me a Lifesaver rating and I'll send the solution to your inbox or e-mail. You've got no other option to get the answer, because no one can give you the answer over here. Even you might be banned for rating a user that gave the answer directly on your question. You need not worry as I have the solution ready in my notebook. Hope you rate me :)