0

I cannot figure out how to implement a basic spanning tree in Python; an un-weighted spanning tree.

I've learned how to implement an adjacency list:

for edge in adj1:
    x, y = edge[int(0)], edge[int(1)]
    if x not in adj2: adj2[x] = set()
    if y not in adj2: adj2[y] = set()
    adj2[x].add(y)
    adj2[y].add(x)
print(adj2)

But I don't know how to implement "finding nearest unconnected vertex".

7
  • Note: input would be an adjacency list. Commented Sep 10, 2016 at 18:43
  • Can you provide more information - an example of the input data, the expected output data, and preferably what you have tried. Your question is too vague for people to be able to help you. Commented Sep 10, 2016 at 18:45
  • Sorry. An input would be: Commented Sep 10, 2016 at 18:46
  • {0: {1, 2}, 1: {0, 2, 3}, 2: {0, 1}, 3: {1}} or [[1,2],[0,2,3],[0,1],[1]] Commented Sep 10, 2016 at 18:47
  • 1
    Sorry, this is a pretty vague question (long night/tired). Basically I'm trying to create an algorithm to turn a list of a list (or dictionary) representation of an adjacency list into a list representation of a spanning tree. So creating a spanning tree from an adjacency list. Commented Sep 10, 2016 at 19:22

1 Answer 1

1

You don't say which spanning-tree algorithm you need to use. DFS? BFS? Prim's with constant weights? Kruskal's with constant weights? Also, what do you mean by "nearest" unconnected vertex, since the graph is unweighted? All the vertices adjacent to a given vertex v will be at the same distance from v. Finally, are you supposed to start at an arbitrary vertex? at 0? at a user-specified vertex? I'll assume 0 and try to push you in the right direction.

You need to have some way to represent which vertices are already in the spanning tree, so that you don't re-add them to the tree along another edge. That would form a cycle, which can't happen in a tree. Since you definitely need a way to represent the tree, like the [ [1], [0,2,3], [1], [1] ] example you gave, one way to start things out is with [ [], [], [], [] ].

You also need to make sure that you eventually connect everything to a single tree, rather than, for example, finishing with two trees that, taken together, cover all the vertices, but that aren't connected to each other via any edge. There are two ways to do this: (1) Start with a single vertex and grow the tree incrementally until it covers all the nodes, so that you never have more than one tree. (2) Add edges in some other way, keeping track of the connected components, so that you can make sure eventually to connect all the components. Unless you know you need (2), I suggest sticking with (1).

Getting around to your specific question: If you have the input inputgraph = [[1,2],[0,2,3],[0,1],[1]], and you start with currtree = [ [], [], [], [] ], and you start at vertex 0, then you look at inputgraph[0], discover that 0 is adjacent to 1 and 2, and pick either (0,1) or (0,2) to be an edge in the tree. Let's say the algorithm you are using tells you to pick (0,1). Then you update currtree to be [ [1], [0], [], [] ]. Your algorithm then directs you either to pick another edge at 0, which in this inputgraph would have to be (0,2), or directs you to pick an edge at 1, which could be (1,2) or (1,3). Note that your algorithm has to keep track of the fact that (1,0) is not an acceptable choice at 1, since (0,1) is already in the tree.

You need to use one of the algorithms I listed above, or some other spanning-tree algorithm, to be systematic about which vertex to examine next, and which edge to pick next.

I hope that gave you an idea of the issues you have to consider and how you can map an abstract algorithm description to running code. I leave learning the algorithm to you!

Sign up to request clarification or add additional context in comments.

1 Comment

Hey thanks for the detailed response man. I'm pretty new to all of this, so I'm still figuring things out. I've heard of DFS and BFS before, but wasn't aware that I needed to use them for this. I'm trying to figure out how to write an algorithm that takes a list generated from an adjacency list like: 0 1 2 1 0 2 1 3, and turning that into a vertex list representation of a spanning tree. I'll have a go with your advice though. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.