Moving an unusual tree in Python

I have an unusual array of trees like this:

[[0, 1], [1, 2], [2, 3], [2, 4], [2, 5], [5, 6], 
 [4, 6], [3, 6], [0, 7], [7, 6], [8, 9], [9, 6]]

Each element of the array is a pair, which means that the second is a follower of the first, for example:

[0, 1] - 0 is followed by 1
[1, 2] - 1 is followed by 2

I am trying to extract an array as follows:

0 1 2 3 6    
0 1 2 4 6    
0 1 2 5 6
0 7 6
8 9 6

I have not been able to encode a reliable bypass to extract all possible paths like this. How can I do this using Python?

+3
source share
8 answers

Here you go. Not the nicest code on earth, but it works:

inputValues = [[0, 1], [1, 2], [2, 3], [2, 4], [2, 5], [5, 6], [4, 6], [3, 6], [0, 7], [7, 6], [8, 9], [9, 6]]

tree = {}
numberOfChildren = {}
for (f, t) in inputValues:
  if not tree.has_key(f):
    tree[f] = []
  tree[f].append(t)
  if not numberOfChildren.has_key(t):
    numberOfChildren[t] = 0
  numberOfChildren[t] += 1

roots = [c for c in tree if c not in numberOfChildren]
permutations = []

def findPermutations(node, currentList):
  global tree
  global permutations
  if not tree.has_key(node):
    permutations.append(currentList)
    return
  for child in tree[node]:
    l = list()
    l.extend(currentList)
    l.append(child)
    findPermutations(child, l)

for r in roots:
  findPermutations(r, [r])

print permutations
+1
source

You can do this using the recursive generator function. I assume that the root of the node in the tree always appears in front of all its children in the source list.

tree = [[0, 1], [1, 2], [2, 3], [2, 4], [2, 5], [5, 6], [4, 6], [3, 6],
        [0, 7], [7, 6], [8, 9], [9, 6]]

paths = {}
for t in tree:
    if t[0] not in paths: paths[t[0]] = []
    paths[t[0]].append(tuple(t))

used = set()

def get_paths(node):
    if node[1] in paths:
        for next_node in paths[node[1]]:
            used.add(next_node)
            for path in get_paths(next_node):
                yield [node[0]] + path
    else:
        yield [node[0], node[1]]

for node in tree:
    if tuple(node) in used: continue
    for path in get_paths(node):
        print path

Output:

[0, 1, 2, 3, 6]
[0, 1, 2, 4, 6]
[0, 1, 2, 5, 6]
[0, 7, 6]
[8, 9, 6]

: node. node, , , node , . node, node, .

, . , , node .

+4

, , , parent-child , tree. , , , , . , , "" node, .

- , . node, : node, - . , node, . , , , node ( ). node .

, . .

+2

, , - , , :

d = {}

for parent, child in tree:
    try:
        d[parent].append(child)
    except KeyError:
        d[parent] = [child]

= [[0, 1], [1, 2], [2, 3], [2, 4], [2, 5], [5, 6], [4, 6], [ 3, 6], [0, 7], [7, 6], [8, 9], [9, 6]], :

{0: [1, 7],
 1: [2],
 2: [3, 4, 5],
 3: [6],
 4: [6],
 5: [6],
 7: [6],
 8: [9],
 9: [6]}

:

def printPaths(d, currentPath):
    if currentPath[-1] not in d:
        print currentPath # last node can't possibly be a parent, so stop
    else:
        for child in d[currentPath[-1]]:
            printPaths(d, currentPath + [child])


for root in d:
    printPaths(d, [root])

, :)

+2

, , , . , , , , :

  • =
  • , :
    • ( ):
      • ,

, - , , .

0

find_all_paths : http://www.python.org/doc/essays/graphs/

, . , :

    graph = {0: [1, 7],
             1: [2],
             2: [3, 4, 5],
             ...}
Secondly create a supersink (in your example case you could call it 10) and attach all vertices with no edges leading from them to this new node.

find_all_paths(graph, 0, 10), .

0

:

tree = [[0, 1], [1, 2], [2, 3], ...]

dtree = {}
for (k, v) in tree:
   dtree.setdefault(k, []).append(v)

parts = [[root] for root in range(10)]

while parts:
   path = parts.pop(0)
   if path[-1] in dtree:
      for n in dtree[path[-1]]:
         parts.append(path + [n])
   else:
      print path

, , , node, parts, , [p[1] for p in tree]. , , , while .

0

- , . , .

import operator
def genpaths(data):
    # Initialize dictionary
    ddata = {}
    for item in data:
        ddata.setdefault(item[0], []).append(item[1])
    def genpath(root):
        "Generate paths starting with root"
        if root not in ddata:
            yield (root, )
        else:
            for child in ddata[root]:
                for path in genpath(child):
                    yield (root, ) + path

    for root in set(ddata.keys()) - set(reduce(operator.add, ddata.values())):
        for path in genpath(root):
            print path
0
source

Source: https://habr.com/ru/post/1728051/


All Articles