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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started