Question: I have already completed Problem 1 but I need Problem 2 to be completed: import java.util.ArrayList; import java.util.List; class Edge implements Comparable { private Integer

 I have already completed Problem 1 but I need Problem 2to be completed: import java.util.ArrayList; import java.util.List; class Edge implements Comparable{ private

I have already completed Problem 1 but I need Problem 2 to be completed:

import java.util.ArrayList;

import java.util.List;

class Edge implements Comparable{

private Integer from;

private Integer to;

private int weight;

public Edge(Integer from, Integer to, int weight)

{

this.from=from;

this.to=to;

this.weight=weight;

}

public Integer getFrom()

{

return from;

}

public Integer getTo()

{

return to;

}

public int getWeight()

{

return weight;

}

public int hashCode()

{

return this.hashCode();

}

public boolean equals(Object o)

{

if(o instanceof Edge)

{

if(((Edge)o).getWeight()==weight)

return true;

else

return false;

}

return false;

}

public String toString()

{

return "From: "+from+"\tTo: "+to+"\tWeight: "+weight;

}

@Override

public int compareTo(Edge edge) {

// TODO Auto-generated method stub

if(this.weight

return -1;

else if(this.weight==edge.weight)

return 0;

else

return 1;

}

}

class Graph{

private int[][] matrix;

public Graph()

{

matrix=null;

}

public Graph(int[][] matrix)

{

this.matrix=matrix;

}

public boolean hasEdge(Integer from, Integer to)

{

return matrix[from][to]!=0;

}

public int weight(Integer from, Integer to)

{

return matrix[from][to];

}

public List getOutgoingEdges(Integer from)

{

List list=new ArrayList();

for(int i=0; i

{

if(matrix[from][i]!=0)

{

Edge edge=new Edge(from, i, matrix[from][i]);

list.add(edge);

}

}

return list;

}

public List getNodes()

{

List list=new ArrayList();

for(int i=0; i

list.add(i);

return list;

}

public String toString()

{

String result="";

for(int i=0; i

{

for(int j=0; j

{

result=result+matrix[i][j]+"\t";

}

result+=" ";

}

return result;

}

}

Problem 1 Adjacency Matrix In this part, you will implement the data model to represent a graph as well as edges in the graph. Implement the following two classes Graph.java: This class uses an adjacency matrix as instance variable that can represent any weighted directed graph. It must have a default constructor and a constructor that expects an adjacency matrix as parameter. By default, the adjacency matrix represents the following weight undirected graph 2 5 The Graph class must have all methods in the following class diagram. A description for each method is given below Edge.java: This class represents a single edge in the graph consisting of the starting node, end node, and weight of the edge. The Edge must have all methods in the following class diagram ava. lang. Comparable-Edge> Graph Edge - intrll matrix +GraphO +Graph(int[][] adjacencyMatrix) +boolean hasEdge(Integer from, Integer to) tint weight(Integer from, Integer to) +List Edge> getOutgoingEdges(Integer from) +List Integer> getNodes0) +String toString0 - Integer from; - Integer to; - int weight +Edge(Integer from, Integer to, int weight) +Integer getFromO +Integer getloo tint getWeight) tint hashCode() +boolean equals(Object o) +String toString0

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 Databases Questions!