A C++ project to visualize how two of the most common minimum spanning tree (MST) algorithms work - mainly Kruskal's and Prim's. The application allows the user to randomly generate a tree structure or to import one from a file, and then select which algorithm to use to find the MST. Helpful playback functionality is implemented so that the user can visually see step by step how the algorithms arrive at the final solution. User has the ability to step through the algorithm solving process step by step, or select a playback speed and watch it work it's magic.
The project was inspired by the following two youtube videos, which I thought would be fun to try implement myself in C++:
- C++
- CMake
- Dear ImGui
- Catch2
In computer science, a graph is a collection of nodes which are connected together by edges, which have some associated weighting. In practical terms a weight might give the edge between two nodes some physical meaning such as distance in meters, time in hours or cost in dollars. A tree is a graph without cycles. i.e) constructed in such a way that there is no path of edges that can be followed which which will allow you to arrive back at the same node you left from. A spanning tree is therefore a subgraph of some graph which "spans", or connects, all the vertices of the graph. This spanning tree can also be a 'minimum' spanning tree (MST) when it is selected in such a way as to select edges which have a minimum weight when summed. It turns out that MSTs are pretty useful in many optimization problems, and in actual fact the first MST algorithm was discovered while designing an electricity network for the city of Moravia. 1
Whilst the first algorithm to be discovered for solving this problem was invented by Czech mathematician Otakar Borůvka, the two most popular algorithms today are Prim's and Kruskal's which are examples of greedy algorithms that work in slightly different ways.
Prim's algorithm is a greedy-algorithm which constructs the MST one edge at a time by examining all outgoing edges from some starting node, and adding the edge with lowest weight to the MST. In this way the MST is built successively until all edges have been explored.
![](https://private-user-images.githubusercontent.com/41984034/238148364-e65e5708-f02e-4450-a558-f1df3bc9951e.gif?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjU1NjMyNjcsIm5iZiI6MTcyNTU2Mjk2NywicGF0aCI6Ii80MTk4NDAzNC8yMzgxNDgzNjQtZTY1ZTU3MDgtZjAyZS00NDUwLWE1NTgtZjFkZjNiYzk5NTFlLmdpZj9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA5MDUlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwOTA1VDE5MDI0N1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTYyOGI3ZmRhNzM3YWYzY2NkOGQwYjUxZWI0MmE4ZTc0NWUyYWE3N2VhOWI0OTI2NDUwMzY0NzNjODIzZGMwMTQmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.MXmL--Jgx7_35f61VFrM3Zav50V1bQ3BfWC9p9XrWNo)
In the same way that Prim's algorithm builds the MST one edge at a time, Kruskal's algorithm also does so, however it does so by starting off with a number of disjoint sets of edges between nodes and successively joins the sets together by edges of least weight until there is only one remaining set of edges which is the MST.
![](https://private-user-images.githubusercontent.com/41984034/238148494-3d5caaeb-fd4f-471a-bb57-92416da3c67c.gif?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjU1NjMyNjcsIm5iZiI6MTcyNTU2Mjk2NywicGF0aCI6Ii80MTk4NDAzNC8yMzgxNDg0OTQtM2Q1Y2FhZWItZmQ0Zi00NzFhLWJiNTctOTI0MTZkYTNjNjdjLmdpZj9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA5MDUlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwOTA1VDE5MDI0N1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTAxMWVmMzg0NGU2ODZjN2Y2OWY2MGVhNTgzZjc5M2Y5NWU1NTNhMDlhMmQ4NzczZTFmMWU4MzdhN2I0NzEzYWImWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.iiUT2krA_c5I1wKPuvyCo2fWdvpNExSn7hMt5C6Z4ao)
Chazelle's algorithm is an almost linear time algorithm for finding an MST.
git clone
the repository to your local machine.
from the root directory of the repository run:
git clone https://github.com/Microsoft/vcpkg.git
Once done, cd
to the vcpkg folder and run:
./bootstrap-vcpkg.sh
Finally, run:
./vcpkg integrate install
run cd .. cd build
and then run cmake .. -DCMAKE_TOOLCHAIN_FILE=vcpkg/scripts/buildsystems/vcpkg.cmake
. If all goes well you will see the "Build files have been written to..." message. You may now open the generated 'minimum_spanning_tree_visualizer' project.
To run the tests, right-click and "select as startup project":
![image](https://private-user-images.githubusercontent.com/41984034/289535112-5172628c-71b1-40f8-9908-9b28607aa97a.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjU1NjMyNjcsIm5iZiI6MTcyNTU2Mjk2NywicGF0aCI6Ii80MTk4NDAzNC8yODk1MzUxMTItNTE3MjYyOGMtNzFiMS00MGY4LTk5MDgtOWIyODYwN2FhOTdhLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA5MDUlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwOTA1VDE5MDI0N1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTlkMDY3NWQ2ZmQ0MDljZmY4OTdhYWIyOGYyNzYyOTdhNzU2MmFiMmRmZDVhODVlMmRhMWQ1OTkzNDA4NTNjM2YmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.IaFKNgWMnNmOozem6TUt_g23HHPbT7YiDEFm6LtPS8Y)
To run the tests, right-click and "select as startup project":
![image](https://private-user-images.githubusercontent.com/41984034/289535200-fd38caf3-49c1-4485-92f8-2814c84fbd06.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjU1NjMyNjcsIm5iZiI6MTcyNTU2Mjk2NywicGF0aCI6Ii80MTk4NDAzNC8yODk1MzUyMDAtZmQzOGNhZjMtNDljMS00NDg1LTkyZjgtMjgxNGM4NGZiZDA2LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA5MDUlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwOTA1VDE5MDI0N1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTE2Y2VmYzVjYWIwNThjYjBhOTdkNjg0NzA0NmJlOWZkODU0ZDNmMTRhZmIwMmQzY2U2MjY4MzU3NGI5MTU5Y2YmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.Urw-ROwGRufVwozH5q3uYvH73cGhN13Nc3_wbVuXwdU)