0
/ 1.0
Description
You are been given a partially implemented GraphAdjList class that represents an unweighted, undirected graph using an...
You are been given a partially implemented GraphAdjList class that represents an unweighted, undirected graph using an adjacency-list structure.
ArrayList<Integer>, where each index corresponds to a node.n nodes, then the instance variable nodes, declared as private ArrayList<Integer>[] nodes, is an array of length n allocated in the constructor.v, nodes[v] contains all nodes that are adjacent to v.ArrayList<Integer> for each node.u and v, both adjacency lists must be updated. The code for addEdge is given below.You have to complete the boolean hasEdge(int fromNode, int toNode) that is part of the class GraphAdjList. This method returns true if there is an edge from fromNode to toNode.
ArrayList class is available online.GraphADT interface is available online.A partial definition of GraphAdjList is included below for reference.
public class GraphAdjList extends GraphADT<Integer>
{
private ArrayList<Integer> nodes[];
// creates nodes array and the lists
public GraphAdjList(int n) {}
// Adds a new edge for undirected graph,
// so we put the edge in both nodes...
public void addEdge(int fromNode, int toNode)
{
// some error checking is not shown for clarity
nodes[fromNode].add(toNode);
nodes[toNode].add(fromNode);
}
public boolean hasEdge(int fromNode, int toNode)
{ } // to be implemented
// other details not shown
}
Your feedback will appear here when you check your answer.