Answered step by step
Verified Expert Solution
Link Copied!

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
currNode++;
currentLoc = nodeStart[currNode];
}
return currentLoc;
}
else return null;
}
private int n;
private Edge[] nodeStart;
private Edge currentLoc;
}
image text in transcribed
image text in transcribed
image text in transcribed
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
<>

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2016 Riva Del Garda Italy September 19 23 2016 Proceedings Part 1 Lnai 9851

Authors: Paolo Frasconi ,Niels Landwehr ,Giuseppe Manco ,Jilles Vreeken

1st Edition

3319461273, 978-3319461274

More Books

Students also viewed these Databases questions

Question

=+2. Who are your colleagues?

Answered: 1 week ago