DAA Assignment
DAA Assignment
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:
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