Answered step by step
Verified Expert Solution
Question
1 Approved Answer
In the class GraphAlgorithm, insert java code for the method prim public class MyGraph { public static final int MAXSIZE = 100; public static final
In the class GraphAlgorithm, insert java code for the method prim
public class MyGraph {
public static final int MAXSIZE = 100;
public static final double BIGM = 10000000;
public MyGraph(int size) {
n = size;
nodeStart = new Edge[MAXSIZE];
for (int i=0; i
nodeStart[i] = null;
}
}
public int getSize() {
return n;
}
public double getCost(int i, int j) {
double value = BIGM;
Edge e = nodeStart[i];
while (e !=null) {
if (e.dest == j) value = e.cost;
e=e.next;
}
return value;
}
public void setCost(int i, int j, double c) {
Edge e = nodeStart[i];
boolean found = false;
while (e !=null && !found) {
if (e.dest == j) {
e.cost = c;
found = true;
}
else e=e.next;
}
if (!found) insertEdge(i,j,c);
}
public void insertEdge(int i, int j, double c) {
deleteEdge(i,j);
Edge ins = new Edge(i,j,c);
ins.next = nodeStart[i];
nodeStart[i] = ins;
}
public void deleteEdge(int i, int j) {
boolean found = false;
Edge prev = null;
Edge e = nodeStart[i];
if (e != null) {
if (e.dest == j) {
found = true;
nodeStart[i] = e.next;
}
else {
prev = e;
e = e.next;
}
}
while (e != null && !found) {
if (e.dest == j) {
found = true;
prev.next = e.next;
}
else {
prev = e;
e = e.next;
}
}
}
public MyGraph copy() {
MyGraph result = new MyGraph(n);
Edge e = first();
while (!this.eol()) {
result.insertEdge(e.orig, e.dest, e.cost);
e = next();
}
return result;
}
public Edge first() {
int currNode = 0;
currentLoc = nodeStart[currNode];
while (currentLoc == null && currNode
<> currentLoc = nodeStart[++currNode];
}
return currentLoc;
}
public boolean eol() {
return (currentLoc == null);
}
public Edge next() {
if (!eol()) {
int currNode = currentLoc.orig;
currentLoc = currentLoc.next;
while (currentLoc == null && currNode
public class Edge { public Edge(int i, int j, double c) orig-i; dest-ji cost Ci public int orig; public int dest; public double cost; public Edge next - null currNode++;
currentLoc = nodeStart[currNode];
}
return currentLoc;
}
else return null;
}
private int n;
private Edge[] nodeStart;
private Edge currentLoc;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started