3. Draw the graph manually for the above Java program. 4. Modify the Java Program to add a node (5) and two edges to the

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

3. Draw the graph manually for the above Java program. 4. Modify the Java Program to add a node (5) and two edges to the

Post by answerhappygod »

3. Draw the graph manually for the above Javaprogram.
4. Modify the Java Program to add a node (5) and twoedges to the graph which connects the node 2 to 5 and 4 to 5 withweight 6 and 6 respectively.
public class Group4 { private static final int V = 5; int minKey(int key[], BooleanmstSet[]) { 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; }
returnmin_index; }
void print(int parent[], intgraph[][]) { for (int i = 1; i < V;i++) System.out.println(parent + " - " + i + "\t" +graph[parent]+'\n'); }
void ABC(int graph[][]) { int parent[] = newint[V]; int key[] = newint[V]; Boolean mstSet[] = newBoolean[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 = newGroup4(); 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); } }
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply