I have a programming assignment where I must read in a tree from a text file to an adjacency matrix, then empirically compare Prim's and Kruskal's Minimum Spanning Tree (MST) algorithms. I could do this in Java, but I'm trying to learn Python (this is my first week with Python).
For me, the hardest thing (I believe) will be converting the .txt file into workable keys that the algos can work with.
If my .txt file is given in the format:
Sample input file
1 : 2 2, 4 5
2 : 1 2, 3 14, 4 5, 5 4
3 : 2 14, 5 34
4 : 1 5, 2 5, 5 58
5 : 2 4, 3 34, 4 58
Where the number before the colon is the vertex, and the next numbers are the vertices it can reach and their costs (For example, vertex 3 can reach vertex 2 with a cost of 14, and vertex 5 with a cost of 34), how can I read this file into a workable adjacency matrix?
The little I know about python makes me assume I will use the "open" method to open the file, and a split method with separate delimiters to collect that information, but how?
Thank you in advance for your help!