0% found this document useful (0 votes)
3 views2 pages

DAA Assignment

Uploaded by

saramukhopadhyay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

DAA Assignment

Uploaded by

saramukhopadhyay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

DAA Assignment 1

23B1005

Algorithm
The algorithm aims to find a spanning tree of a given weighted graph that has
a weight at most T while minimizing the number of red edges. The approach
consists of the following steps:

1. Graph Representation: The input graph is represented as an edge list,


where each edge contains two endpoints, a weight, and a color (red or
blue). The graph is stored in a Graph class, and the edges are stored in a
vector.
2. Using Disjoint Set Union (DSU) for Kruskal’s Algorithm: The
algorithm constructs an MST using Kruskal’s algorithm with the Disjoint
Set Union (DSU) data structure. The DSU efficiently manages connected
components and supports fast union-find operations.

3. Constructing the Minimum Spanning Tree (MST) with Priority


on Blue Edges: The edges are sorted such that blue edges (r = 0)
come first, followed by edges sorted by weight. This prioritization ensures
the MST starts with the minimum possible number of red edges while
maintaining the lowest possible weight.

4. Checking the Initial MST Weight: If the weight of this MST does
not exceed the given threshold T , it is already an optimal solution, and
the code outputs the number of red edges and the total weight.
5. Optimizing by Replacing Edges: If the MST weight exceeds T , the
algorithm attempts to replace blue edges with red edges in a way that re-
duces the total weight. This is done using an adjacency list representation
of the MST, allowing efficient pathfinding. The algorithm finds paths in
the MST and swaps out the heaviest blue edges with available red edges
if doing so results in a lower weight. This process continues iteratively
until the total weight meets the threshold T or no further improvement is
possible.

1
Code Running
The code can be compiled using the following commands:
g++ -std=c++17 -O2 -o spanning_tree spanning_tree.cpp
./spanning_tree.cpp

Example Input:
4
5
13
01 8 0
03 5 1
12 6 0
13 2 0
23 4 1

Example Output:
1
13

You might also like