Recently, I tried generating trees from graph edges in C++ but that is resulting in too much if conditions or for loops. I turned my attention towards python and it seems to be generating trees in a least amount of complex code. I'm new to python which is why im asking for help. Similar questions have been posted on the forum but they don't deal the same problem which im trying to solve. So the problem is that I have some graph edges like 1---->2 with a cost 10; 1--->3 with a cost 20 etc. Now I want to generate multiple possible trees along with their cost if '1' were the root node. I guess my code would look like;
class Edge:
def __init__(v1, v2, v3):
self.v1 = v1
self.v2 = v2
self.v3 = v3
graph = [Edge(1, 2, 10), Edge(2, 4, 20)]
This is what I should get back;
map = {}
map[1] = [Edge(1, 2)] Cost: 10
map[2] = [Edge(1, 2), Edge(2, 4)] Cost: 30
if 2 was the root node;
map = {}
map[1] = [Edge(2, 4)] Cost: 20
I would apprecaite if someone can help me out as i've been stuck on this problem for the last 5 days and cant seem to come to a solution. Although, I looked at the networkx module but don't know how to use their APIs. :(
EDIT: To summarize, given a forest, how can i generate multiple unique 'trees'?