Can I have the code in Java please using NetBeans program with output c) Program
ID: 3839005 • Letter: C
Question
Can I have the code in Java please using NetBeans program with output c) Programming Assignment The ABC Utility Company wants to lay cable lines from its field stations to its customers. The locations and the distances between adjacent customers and the field stations are shown field in the figure below. The stations are located at nodes 7, 14, 25, 28, and 40. des show the locations of its customers. The edges show the possible cable routes and the distance between the customers. customers can be served from any one of these switching stations, show how company can serve all its customers while minimizing the total cable used. You do not need exclusive cables from field stations to its customers. The cable lines can be extended from customer to another, but all customers must be served from one of its field stations. write a program to determine the edges along which the cables must be laid. Show the final layout of the cables lines. You can draw final cable layout plan by hand. What is the total length of the cable used? If you could build only one field station that minimizes the total cost of the cable used, which node(s) would you choose for your field station?Explanation / Answer
import java.util.InputMismatchException;
import java.util.Scanner;
public class Prims
{
private boolean unsettled[];
private boolean settled[];
private int numberofnodes;
private int adjacencyMatrix[][];
private int key[];
public static final int INFINITE = 999;
private int parent[];
public Prims(int numberofnodes)
{
this.numberofnodes = numberofnodes;
unsettled = new boolean[numberofnodes + 1];
settled = new boolean[numberofnodes + 1];
adjacencyMatrix = new int[numberofnodes + 1][numberofnodes + 1];
key = new int[numberofnodes + 1];
parent = new int[numberofnodes + 1];
}
public int getUnsettledCount(boolean unsettled[])
{
int count = 0;
for (int index = 0; index < unsettled.length; index++)
{
if (unsettled[index])
{
count++;
}
}
return count;
}
public void primsAlgorithm(int adjacencyMatrix[][])
{
int evaluationNode;
for (int source = 1; source <= numberofnodes; source++)
{
for (int target = 1; target <= numberofnodes; target++)
{
this.adjacencyMatrix[source][target] = adjacencyMatrix[source][target];
}
}
for (int index = 1; index <= numberofnodes; index++)
{
key[index] = INFINITE;
}
key[1] = 0;
unsettled[1] = true;
parent[1] = 1;
{
evaluationNode = getMimumKeyNodeFromUnsettled(unsettled);
unsettled[evaluationNode] = false;
settled[evaluationNode] = true;
evaluateNeighbours(evaluationNode);
}
}
private int getMimumKeyNodeFromUnsettled(boolean[] unsettled2)
{
int min = Integer.MAX_VALUE;
int node = 0;
for (int node = 1; node <= numberofnodes; node++)
{
if (unsettled[node] == true && key[node] < min)
{
node = node;
min = key[node];
}
}
return node;
}
public void evaluateNeighbours(int evaluationNode)
{
for (int targetnode = 1; targetnode <= numberofnodes; targetnode++)
{
if (settled[targetnode] == false)
{
if (adjacencyMatrix[evaluationNode][targetnode] != INFINITE)
{
if (adjacencyMatrix[evaluationNode][targetnode] < key[targetnode])
{
key[targetnode] = adjacencyMatrix[evaluationNode][targetnode];
parent[targetnode] = evaluationNode;
}
unsettled[targetnode] = true;
}
}
}
}
public void printMST()
{
System.out.println("SOURCE : TARGET = WEIGHT");
for (int node = 2; node <= numberofnodes; node++)
{
System.out.println(parent[node] + " : " + node +" = "+ adjacencyMatrix[parent[node]][node]);
}
}
public static void main(String... arg)
{
int adjacency_matrix[][];
int number_of_nodes;
Scanner scan = new Scanner(System.in);
try
{
System.out.println("Enter the number of nodes");
number_of_nodes = scan.nextInt();
adjacency_matrix = new int[number_of_nodes + 1][number_of_nodes + 1];
System.out.println("Enter the Weighted Matrix for the graph");
for (int i = 1; i <= number_of_nodes; i++)
{
for (int j = 1; j <= number_of_nodes; j++)
{
adjacency_matrix[i][j] = scan.nextInt();
if (i == j)
{
adjacency_matrix[i][j] = 0;
continue;
}
if (adjacency_matrix[i][j] == 0)
{
adjacency_matrix[i][j] = INFINITE;
}
}
}
Prims prims = new Prims(number_of_nodes);
prims.primsAlgorithm(adjacency_matrix);
prims.printMST();
} catch (InputMismatchException inputMismatch)
{
System.out.println("Wrong Input Format");
}
scan.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.