简体   繁体   中英

How to calculate all possible permutations for a tree diagram

I have a problem of tee diagram that represents city roads and need to calculate all possible ways -permutations- to establish these roads.

The role is " you can not start level 2 without finishing level 1" and so on for higher levels

this image illustrates the idea here

i tried to think of it as an array for every level and then a single column for every branch like this

level1=[1 2]
level2=[3 4
        5 6]

and l

;evel1=[1 2 3]
level2=[4 5 0; 
       6 7 8; 
       9 10 0]
level3=[11 12;
13 0;
14 15;
16 17;
0 0;
0 0;
18 19]

But i stopped and have no idea how to complete. so i need to know how to think in this kind of problems.

You should look into the concept of "abstract datatypes" as your current design won't scale. Every time you add a new level, you need to create a new array and then update your code to know about this new array.

And the process of searching one level before moving on to the next is known as "breadth first" searching. If you create a list type, you can add all the nodes from the current level to the list and then as you process the list, you add the child nodes to the end of the list.

But that isn't really applicable to generating all permutations - doing "depth first" is more suitable as you travel down the tree and when you can't go no further, the path you've taken is a permutation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM