graph - Convert a list of tuples(edges) into list of longest paths in Python -


please @ fig. below. part of project, in need convert list of edges of forest list of unique longest paths. longest paths paths connect root node leaf node or leaf leaf node. problem here is, have list of edges input, which, supposed derive these paths.

i trying solve problem recursively looking neighbor nodes using dictionary(created using list of edges), looks not proper way handle problem , finding hard visualize. please suggest if there known efficient algorithms/methods solve problem.

p.s.: please neglect weights(they labels). "longest" means path covering maximum nodes.

input:
list of tuples(edges)

edges = [('point', 'floating'), ('754', 'floating'), ('clock', 'ieee'), ('arm', 'barrel'),  ('barrel', 'shifter clock'), ('shifter', 'barrel'),  ('representation', 'representation'), ('cycles', '754'),  ('point representation', 'point'), ('barrel shifter', 'point')] 

expected output:

inference_paths = [     ['cycles', '754', 'floating', 'point', 'point representation'],     ['cycles', '754', 'floating', 'point', 'barrel shifter'],     ['point repr', 'point', 'barrel shifter'],     ['arm', 'barrel', 'shifter clock'],     ['shifter', 'barrel', 'shifter clock'],     ['arm', 'barrel', 'shifter'],     ['clock', 'ieee'],     ['representation'] ]  

my failed code:

edges = [('point', 'floating'), ('754', 'floating'), ('clock', 'ieee'), ('arm', 'barrel'), ('barrel', 'shifter clock'), ('shifter', 'barrel'), ('representation', 'representation'), ('cycles', '754'), ('point representation', 'point'), ('barrel shifter', 'point')] neighbors = {} inference_paths = []  edge in edges:     neighbors[edge[0]] = edge[1]  edge in edges:     neighbor = neighbors.get(edge[1])     if neighbor:         if not edge[1] == edge[0] == neighbor:             inference_paths.append([edge[0], edge[1], neighbor])         else:             inference_paths.append([edge[0]])     else:         inference_paths.append([edge[0], edge[1]])   path in inference_paths:     print path 

my output:

[['point', 'floating'], ['754', 'floating'], ['clock', 'ieee'], ['arm', 'barrel', 'shifter clock'], ['barrel', 'shifter clock'], ['shifter', 'barrel', 'shifter clock'], ['representation'], ['cycles', '754', 'floating'], ['point representation', 'point', 'floating'], ['barrel shifter', 'point', 'floating']] 

forest:

enter image description here

i believe problem is, you're using recursion, you're not. attempting iteratively painful. first lets @ basic recursive tree traversal function.

function traversetree(node) {     if(node == null) return;     traversetree(node->leftsubtree);     traversetree(node->rightsubtree); } 

the above function visit nodes in tree, have figure out logic need calculate figure out paths , such. note: answers appear suggest given node can used once in path , cannot crossed again, assumption used.

arrayofpaths; function traversetree(node) {     if(node == null) return emptypathobject;      leftpathobject = traversetree(node->leftsubtree);     rightpathobject = traversetree(node->rightsubtree);     arrayofpaths.add(leftpathobject + node + rightpathobject);     return ((node + leftpathobject) or (node + rightpathobject) whichever longer); }  //code figure out paths longest here.  simpler other 1 too! :) 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -