0% found this document useful (0 votes)
52 views

221902285-Algorithm Lab Report 6

This lab report discusses finding the second-best minimum spanning tree (MST) of a graph. The student implemented Kruskal's algorithm to first find the minimum cost MST, then modified it to find the second-best MST by excluding each edge from the first MST and recalculating costs. The program was tested on a graph with 5 vertices and 7 edges provided in a text file. The output printed the edges and costs of both the first and second-best MSTs.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

221902285-Algorithm Lab Report 6

This lab report discusses finding the second-best minimum spanning tree (MST) of a graph. The student implemented Kruskal's algorithm to first find the minimum cost MST, then modified it to find the second-best MST by excluding each edge from the first MST and recalculating costs. The program was tested on a graph with 5 vertices and 7 edges provided in a text file. The output printed the edges and costs of both the first and second-best MSTs.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Green University of Bangladesh

Department of Computer Science and Engineering (CSE)


Faculty of Sciences and Engineering
Semester: (Fall, Year:2023), B.Sc. in CSE (Day)

Lab Report No. #06


Course Title: Algorithm Lab
Course Code: CSE-206 Section: 221-D6

Student Details

Name ID

1. Md. Tasnimur Rahman Shakir 221902285

Submission Date : 21.12.2023


Course Teacher’s Name : Jargis Ahmed

Lab Report Status


Marks: ………………………………… Signature:.....................
Comments:.............................................. Date:..............................
1. TITLE OF THE LAB REPORT EXPERIMENT
Find the second-best MST.

2. OBJECTIVES/AIM
The objective of the Second-Best Minimum Spanning Tree problem is to find the spanning
tree of a weighted graph with the second-smallest total weight. This means finding a tree
that connects all the vertices in the graph while minimizing the sum of the edge weights,
but without being the absolute minimum.
3. IMPLEMENTATION
Question 1:
Write a program to find the second-best MST.

Code:

package MST;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

public class SecondBestMstByKrushkal {

public static int maxVal = 1000;


public static int parent[] = new int[maxVal];

static class Edge {

int source, destination, weight;

public Edge(int source, int destination, int weight) {


this.source = source;
this.destination = destination;
this.weight = weight;
}
}

public static void make_set(int v) {


parent[v] = v;
}
public static int find(int v) {
if (v == parent[v]) {
return v;
}
return parent[v] = find(parent[v]);
}

public static void union(int a, int b) {


a = find(a);
b = find(b);
if (a != b) {
parent[b] = a;
}
}

public static void KrushkalForFirstMst(ArrayList<Edge> FirstMst, ArrayList<Edge>


edges, int cost, int v) {
for (int i = 0; i < v; i++) {
make_set(i);
}

System.out.println("Minimum Spanning Tree Edges (source, destination, weight):");

for (int i = 0; i < edges.size(); i++) {

int source = edges.get(i).source;


int destination = edges.get(i).destination;
int weight = edges.get(i).weight;

if (find(source) != find(destination)) {
System.out.println(source + " " + destination + " " + weight);
FirstMst.add(new Edge(source, destination, weight));
cost += weight;
union(source, destination);
}
}
System.out.println("Cost of 1st MST: " + cost);

public static void SecondBestMst(ArrayList<Edge> FirstMst, ArrayList<Edge> edges,


int cost, int v) {
int secondCost = Integer.MAX_VALUE;
ArrayList<Edge> SecondBestMst = new ArrayList<>();
ArrayList<Edge> ans = new ArrayList<>();
for (int k = 0; k < FirstMst.size(); k++) {
// Reset the disjoint-set data structure
for (int i = 0; i < v; i++) {
make_set(i);
}

cost = 0;
SecondBestMst.clear();

for (int i = 0; i < edges.size(); i++) {


int source = edges.get(i).source;
int destination = edges.get(i).destination;
int weight = edges.get(i).weight;

if (FirstMst.get(k).source == source && FirstMst.get(k).destination ==


destination) {
continue;
}

if (find(source) != find(destination)) {
SecondBestMst.add(new Edge(source, destination, weight));
cost += weight;
union(source, destination);
}
}

if (cost < secondCost) {


ans.clear();
ans.addAll(SecondBestMst);
secondCost = cost;
}
}

System.out.println("Second Best Minimum Spaning Tree: ");


for (Edge edge : ans) {
int source = edge.source;
int destination = edge.destination;
int weight = edge.weight;

System.out.println(source + " " + destination + " " + weight);


}
System.out.println("Cost of Second Best Mst: " + secondCost);
}

public static void main(String[] args) {


File file = new File("tree.txt");
int v = 0, e = 0;
ArrayList<Edge> edges = new ArrayList<>();

try {
Scanner sn = new Scanner(file);
if (sn.hasNextLine()) {
v = sn.nextInt();
}
if (sn.hasNextLine()) {
e = sn.nextInt();
}

while (sn.hasNextLine()) {
int source = 0, des = 0, weight = 0;
if (sn.hasNextInt()) {
source = sn.nextInt();
}
if (sn.hasNextInt()) {
des = sn.nextInt();
}
if (sn.hasNextInt()) {
weight = sn.nextInt();
}
edges.add(new Edge(source, des, weight));
}
sn.close();
} catch (FileNotFoundException ex) {
System.out.println("File Not Found");
}

Collections.sort(edges, new Comparator<Edge>() {


@Override
public int compare(Edge edge1, Edge edge2) {
return Integer.compare(edge1.weight, edge2.weight);
}
});
ArrayList<Edge> FirstMst = new ArrayList<>();
int cost = 0;
KrushkalForFirstMst(FirstMst, edges, cost, v);
SecondBestMst(FirstMst, edges, cost, v);

}
}
5. TEST RESULT / OUTPUT
The Graph:

Input in “tree.txt”
5
7
0 1 20
0 2 3
1 3 2
1 4 6
2 3 1
2 4 10
3 4 4
Output:

6. Discussion
Exploring the second-best minimum spanning tree introduces an additional layer of
complexity to classic MST algorithms. The adaptation involves careful consideration of
alternative edge choices while maintaining the efficiency and correctness of the algorithm.
The concept is particularly relevant in scenarios where redundancy and resilience are
critical considerations in network design and optimization.

You might also like