Question: CREATE the code for public static MyGraph prim(MyGraph G) using given codes for MyGraph Dijkstra public static MyGraph prim(MyGraph G) { INSERT CODE HERE }
CREATE the code for public static MyGraph prim(MyGraph G) using given codes for MyGraph Dijkstra
public static MyGraph prim(MyGraph G)
{
INSERT CODE HERE
}
public static MyGraph cycle(MyGraph G, int source) {
INSERT CODE HERE
}
----------------------------------------------------------------------------------------
public static MyGraph dijkstra(MyGraph G, int source, int terminal)
{
int n = G.getSize();
MyGraph result = new MyGraph(n);
double [] label = new double[n];
label[source] = 0;
int[] pred = new int[n];
for (int i=0; i
for (int step=1; step
{
Edge best = null;
double min = MyGraph.BIGM;
Edge e = G.first();
while (!G.eol())
{
if (pred[e.orig] >= 0 && pred[e.dest] < 0 && label[e.orig] + e.cost < min) {
best = e;
min = label[e.orig] + e.cost;
}
e = G.next();
}
pred[best.dest] = best.orig;
label[best.dest] = label[best.orig] + best.cost;
}
int node = terminal;
while (node != pred[node]) {
result.insertEdge(pred[node], node, G.getCost(pred[node],node));
node = pred[node];
}
return result;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
