11import pytest
22
3- from boruvkas_algorithm .boruvka import Graph , find_mst_with_boruvkas_algorithm
3+ from boruvkas_algorithm .boruvka import (
4+ Graph ,
5+ UnionFind ,
6+ find_mst_with_boruvkas_algorithm ,
7+ )
48
59
610@pytest .fixture
@@ -15,6 +19,16 @@ def setup_graph():
1519 return Graph (9 ) # Example graph with 9 vertices.
1620
1721
22+ def test_graph_initialization ():
23+ """
24+ Tests that a graph is initialised with the correct number of vertices and
25+ no edges.
26+ """
27+ graph = Graph (5 )
28+ assert len (graph .vertices ) == 5 , "Graph should have 5 vertices"
29+ assert len (graph .edges ) == 0 , "Graph should be initialised with no edges"
30+
31+
1832def test_add_edge (setup_graph : Graph ):
1933 """
2034 Tests that edges are correctly added by checking the length of the edge
@@ -80,11 +94,54 @@ def test_mst(setup_graph: Graph):
8094 )
8195
8296
83- def test_graph_initialization ():
84- """
85- Test that a graph is initialized with the correct number of vertices and
86- no edges.
87- """
88- graph = Graph (5 ) # Initialize a graph with 5 vertices.
89- assert len (graph .vertices ) == 5 , "Graph should have 5 vertices"
90- assert len (graph .edges ) == 0 , "Graph should be initialized with no edges"
97+ def test_mst_with_injected_union_find (setup_graph : Graph ):
98+ """Tests that the algorithm works with an injected UnionFind instance."""
99+ graph = setup_graph
100+ graph .add_edge (0 , 1 , 4 )
101+ graph .add_edge (0 , 6 , 7 )
102+ graph .add_edge (1 , 6 , 11 )
103+ graph .add_edge (1 , 7 , 20 )
104+ graph .add_edge (1 , 2 , 9 )
105+ graph .add_edge (2 , 3 , 6 )
106+ graph .add_edge (2 , 4 , 2 )
107+ graph .add_edge (3 , 4 , 10 )
108+ graph .add_edge (3 , 5 , 5 )
109+ graph .add_edge (4 , 5 , 15 )
110+ graph .add_edge (4 , 7 , 1 )
111+ graph .add_edge (4 , 8 , 5 )
112+ graph .add_edge (5 , 8 , 12 )
113+ graph .add_edge (6 , 7 , 1 )
114+ graph .add_edge (7 , 8 , 3 )
115+
116+ # Inject a custom UnionFind instance.
117+ union_find = UnionFind (len (graph .vertices ))
118+ mst_weight , mst_edges = find_mst_with_boruvkas_algorithm (graph , union_find )
119+
120+ assert mst_weight == 29 , "MST weight should be 29"
121+ assert len (mst_edges ) == 8 , "MST should have 8 edges for 9 vertices"
122+
123+
124+ def test_mst_simple_triangle ():
125+ """Tests MST on a simple triangle graph."""
126+ graph = Graph (3 )
127+ graph .add_edge (0 , 1 , 1 )
128+ graph .add_edge (1 , 2 , 2 )
129+ graph .add_edge (0 , 2 , 3 )
130+
131+ mst_weight , mst_edges = find_mst_with_boruvkas_algorithm (graph )
132+
133+ assert mst_weight == 3 , "MST weight should be 3 (edges 1 + 2)"
134+ assert len (mst_edges ) == 2 , "MST should have 2 edges for 3 vertices"
135+
136+
137+ def test_mst_linear_graph ():
138+ """Tests MST on a linear graph (already a tree)."""
139+ graph = Graph (4 )
140+ graph .add_edge (0 , 1 , 1 )
141+ graph .add_edge (1 , 2 , 2 )
142+ graph .add_edge (2 , 3 , 3 )
143+
144+ mst_weight , mst_edges = find_mst_with_boruvkas_algorithm (graph )
145+
146+ assert mst_weight == 6 , "MST weight should be 6 (1 + 2 + 3)"
147+ assert len (mst_edges ) == 3 , "MST should have 3 edges for 4 vertices"
0 commit comments