Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Question points ) : In the defined system there is one source ( 0 ) , and 4 load point ( 1 , 2 ,

Question points):
In the defined system there is one source (0), and 4 load point (1,2,3,4). In the graph the locations
of the source and load is given find the smallest path of connection to loads and source. (Minimum
Spanning Tree of the undirected graph)
Sample Output:
V=5
E=10
0,1120
02160
03300
04450
10120
12200
{:[1,3,410]
{:[1,4,500]
20160
{:[2,1,200]
{:[2,3,420]
{:[2,4,400]
30300
{:[3,1,410]
{:[3,2,420]
3,4510
40450
41500
42400 Sample Output:
The Minimum Spanning Tree value =...
The Minimum Spanning Tree Path part shows the path for example:
03300
04450
12200
14500(this is only example not the result)
Important Note: If the path contain 0. The answer must be:
04450
Not 40450
If the connection is not contain zero the lowest number is written in the path.
32420 is not correct.
23420 correct.
The Minimum Spanning Tree value for above example The Minimum Spanning Tree value=...
The Minimum Spanning Tree Path part shows the path for example:
03300
04450
12200
14500(this is only example not the result)
Important Note: If the path contain 0. The answer must be:
04450
Not 40450
If the connection is not contain zero the lowest number is written in the path.
32420 is not correct.
23420 correct.
The Minimum Spanning Tree value for above example
The Minimum Spanning Tree value =1450(this is only example not the result)
In the question you create 3 java document and one text file:
HW3_Q1_solution: Main body and the solution of Minimum Spanning Tree question
, FileRead: read the text file.
Valuefinder: take the value of txt file and convert the iCORRECT THIS CODE ACCORDING TO EXPECTED OUTPUT.
// HW3_Q1_solution.java
import java.util.*;
public class HW3_Q1_solution {
public static void main(String[] args){
FileRead fr = new FileRead();
Valuefinder vf = new Valuefinder();
List lines = fr.readFile("HW3_Q1.txt");
List> graphValues = vf.createGraph(lines);
// Print the original graph representation
System.out.println("V=5");
System.out.println("E=10");
for (String line : lines){
System.out.println(line);
}
// Minimum Spanning Tree algorithm implementation
List> mst = primMST(graphValues);
// Print Minimum Spanning Tree Path
System.out.println("
The Minimum Spanning Tree Path");
printMSTPath(mst);
// Calculate and print the total weight of the Minimum Spanning Tree
int mstValue = calculateMSTValue(mst);
System.out.println("
The Minimum Spanning Tree value="+ mstValue);
}
// Implement your Minimum Spanning Tree algorithm here
private static List> primMST(List> graph){
// Your implementation goes here
return new ArrayList>(); // Replace this with your result
}
// Print the Minimum Spanning Tree Path
private static void printMSTPath(List> mst){
for (List edge : mst){
for (int i =0; i edge.size(); i++){
System.out.print(edge.get(i)+"");
}
System.out.println();
}
}
// Calculate the value of the Minimum Spanning Tree
private static int calculateMSTValue(List> mst){
// Your implementation goes here
return 0; // Replace this with your result
}
}
// FileRead.java
import java.io.*;
import java.util.*;
public class FileRead {
public List readFile(String fileName){
List lines = new ArrayList>();
try (BufferedReader br = new BufferedReader(new FileReader(fileName))){
String line;
while ((line = br.readLine())!= null){
lines.add(line);
}
} catch (IOException e){
e.printStackTrace();
}
return lines;
}
}
// ValueFinder.java
import java.util.*;
public class Valuefinder {
public List> createGraph(List lines){
List> graph = new ArrayList>();
for (String line : lines){
String[] parts = line.split("");
List edge = new ArrayList>();
for (String part : parts){
edge.add(Integer.parseInt(part));
}
graph.add(edge);
}
return graph;
}
}
--- Program output ---
V=5
E=10
5
10
01120
02160
03300
04450
12200
13410
14500
23420
24400
34510
The Minimum Spanning Tree Path
The Minimum Spanning Tree value=0
--- Expected output (text)---
V=5
E=10
01120
02160
03300
04450
10120
12200
13410
14500
20160
21200
23420
24400
30300
31410
32420
34510
40450
41500
42400
43510
The Minimum Spanning Tree Path
01120
02160
03300
24400
The Minimum Spanning Tree value=980
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Big Data Systems A 360-degree Approach

Authors: Jawwad ShamsiMuhammad Khojaye

1st Edition

0429531575, 9780429531576

More Books

Students also viewed these Databases questions