Question: Java (I use Eclips) Implement the prim(int r). The answer should be 37. You should return an integer that indicates the total weight of the

Java (I use Eclips)

Implement the prim(int r). The answer should be 37.

You should return an integer that indicates the total weight of the minimum spanning tree.

Please add to the program. Do not modify existing skeleton.

In order to associate each node with a weight and place it into the queue, you may need to create another class. Check ListNode.java and LinkedList.java. I have provided skeletons for both.

Please make sure the program works. Screen shots will be helpful.

------------------------------------------------------------------------------

public class Graph2 {

public int n; //number of vertice

public int[][] A;//the adjacency matrix

public Graph2 () {

n = 0;

A = null;

}

public Graph2 (int _n, int[][] _A) {

this.n = _n;

this.A = _A;

}

public int prim (int r) {

}

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

int n = 9;

int A[][] = {

{0, 4, 0, 0, 0, 0, 0, 8, 0},

{4, 0, 8, 0, 0, 0, 0, 11, 0},

{0, 8, 0, 7, 0, 4, 0, 0, 2},

{0, 0, 7, 0, 9, 14, 0, 0, 0},

{0, 0, 0, 9, 0, 10, 0, 0, 0},

{0, 0, 4, 14, 10, 0, 2, 0, 0},

{0, 0, 0, 0, 0, 2, 0, 1, 6},

{8, 11, 0, 0, 0, 0, 1, 0, 7},

{0, 0, 2, 0, 0, 0, 6, 7, 0}

};

Graph2 g = new Graph2(n, A);

System.out.println(g.prim(0));

}

}

-----------------------------------------------------------------------------

public class LinkedList {

public ListNode head;

public LinkedList () {

head = null;

}

/*

* Implement the LIST-SEARCH(L, k) function

*/

public ListNode search (int k) {

}

/*

* Implement the LIST-INSERT(L, x) function

* Note that x is a integer value, not a ListNode

*/

public void insert (int x) {

}

/*

* Implement the LIST-DELETE(L, x) function

*/

public void delete (ListNode x) {

}

/*

* Convert a LinkedList to a string in the format of [#elements]

*/

public String toString () {

String str;

ListNode n;

str = "[";

n = this.head;

while (n != null) {

str += n.key + ",";

n = n.next;

}

str += "]";

return str;

}

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

LinkedList l;

l = new LinkedList();

for (int i = 0; i < 5; i++)

l.insert(i);

System.out.println(l.toString());

for (int i = 0; i < 2; i++)

l.delete(l.head.next);

System.out.println(l.toString());

}

}

-----------------------------------------------------------------------------

public class ListNode {

public int key;

public ListNode prev;

public ListNode next;

public ListNode () {

prev = next = null;

}

public ListNode (int _key) {

key = _key;

prev = next = null;

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!