# Tree algorithm # The most amazing Python code

Source: Internet
Author: User

This month is the hardest day, because my mentor and I were fighting for Siggraph in the last days of my internship at Shenzhen Advanced Research Institute, I also hope to publish my previous paper at the undergraduate level, haha.

In addition, I will share with you the information about a tree used in the study.AlgorithmRight. The goal of this algorithm is to remove the intermediate node of the tree and retain the branch node. As required:

Conversion:

It is generated by http://www.graphviz.org/doc/info/lang.html

AttachedCode:

View plaincopy to clipboardprint?
  1. DefReduced_graph (subtree, V, graph ):
  2. N = graph [subtree]
  3. IfLen (n) =0:
  4. V [subtree] = []
  5. Return[Subtree]
  6. ElifLen (n) =1:# One child
  7. R = reduced_graph (N [0], V, graph)
  8. V [subtree] = r
  9. IfLen (V [n [0]) =1:
  10. DelV [n [0]
  11. ReturnV [subtree]
  12. Else:
  13. Next_sub = []
  14. ForCInN:
  15. R = reduced_graph (C, V, graph)
  16. Next_sub.append (R [0])
  17. IfLen (V [c]) =1:
  18. DelV [c]
  19. V [subtree] = next_sub
  20. Return[Subtree]

Call method:

View plaincopy to clipboardprint?
  1. V = {}
  2. Graph = {0:[1],1:[2],2:[3,4],3:[5],4:[6],5:[7,8],6:[9],9: [],7:[11],11: [],8: []}
  3. Root =0
  4. Reduced_graph (root, V, graph)

The graph representation method is the adjacent matrix representation.

Why is this Python code very strange? Because the reduced_graph function also returns the value. In fact, the parameter V also returns the value. After the project is over, I hope to refactor this wonderful code.

During the process, we also found Len (n) = 0 without N = [] efficiency. I also tested the code of the two, which is n times faster, it may be because the function call overhead is relatively large.

 

----------------- Create high-qualityArticleFor more information, thank you ---------------

To create high-quality articles, pleaseRecommendationCome on .... Thank you. Please pay attention to my subsequent articles, which will be even better.

Sina Weibo: http://weibo.com/baiyang26

The wine ghost official blog: http://www.ibaiyang.org [recommended to use Google Reader subscription]

Pour wine into the official Douban: http://www.douban.com/people/baiyang26/

If you want to repost this blog,Please specify the source

If you have any comments or suggestions on this article,Leave a message

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.