public class Group4 {
private static final int V = 5;
int minKey(int key[], Boolean mstSet[])
{
int min = Integer.MAX_VALUE, min_index = -1;
for (int v = 0; v < V; v++)
if (mstSet[v] == false && key[v] < min) {
min = key[v];
min_index = v;
}
return min_index;
}
void print(int parent[], int graph[][])
{
for (int i = 1; i < V; i++)
System.out.println(parent + " - " + i +
"\t" + graph[parent]+'\n');
}
void ABC(int graph[][])
{
int parent[] = new int[V];
int key[] = new int[V];
Boolean mstSet[] = new Boolean[V];
for (int i = 0; i < V; i++) {
key = Integer.MAX_VALUE;
mstSet = false;
}
key[0] = 0;
parent[0] = -1;
for (int count = 0; count < V - 1; count++) {
int u = minKey(key, mstSet);
mstSet = true;
for (int v = 0; v < V; v++)
if (graph[v] != 0 && mstSet[v] ==
false && graph[v] < key[v]) {
parent[v] = u;
key[v] = graph[v];
}
}
print(parent, graph);
}
public static void main(String[] args)
{
Group4 t = new Group4();
int graph[][] = new int[][] {
{ 0, 3, 0, 7, 0 },
{ 3, 0, 4, 9, 6 },
{ 0, 4, 0, 0, 8 },
{ 7, 9, 0, 0, 10 },
{ 0, 6, 8, 10, 0 }};
t.ABC(graph);
}
}
1. Lab Test - Group 4 Duration: 1 hour 3 Use the attached Java code (Group4.java) to answer the follow All your answers should be in the form of the word (.doc) or p need to upload your answers at e-learn. 2. 3. 4. 11 5. 71 What is the output of the program? Name the greedy algorithm used in the program and explain the algorithm in detail on how it works with an example. Draw the graph manually for the above Java program. Modify the Java Program to add a node (5) and two edges to the graph which connects the node 2 to 5 and 4 to 5 with weight 6 and 6 respectively. What is the output of the modified code?
public class Group4 { private static final int V = 5; int minKey(int key[], Boolean mstSet[]) { int min = Integer.MAX_VA
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am