I am trying to figure out how to print a Spanning Tree list from a given list of edges. For example, if I read in:
0 1
2 1
0 2
1 3
I want to print out a Spanning Tree list of:
[[1], [0,2,3], [1], [1]]
I know how to create an adjacency list using the code:
n = int(input("Enter number of vertices: "))
adjList = [[] for i in range(n)]
with open("graph.txt") as edges:
for line in edges:
line = line.replace("\n", "").split(" ")
adjList[int(line[0])].append(int(line[1]))
adjList[int(line[1])].append(int(line[0]))
print(l)
But creating a Spanning Tree is a different story. Given that the Spanning Tree would be unweighted, I am not sure if I need to use some version of Prim's Algorithm here?
Any help is appreciated!