Pruffer
来源:互联网 发布:js 技术大牛 阿里 编辑:程序博客网 时间:2024/06/15 22:27
The simple example and code of Pruffer encoding
Pruffer encoding scheme is a wonderful technique for solving the minimum spanning tree through the genetic algorithm. It can avoid the complex representation of tree in the algorithm. In general, for a m-node tree, just m-2 bits is required in the chromosome encoding.
The encoding steps are shown as follows:
Step 1: Find the leaf node with the minimum sequence number, output the adjacent node to the pruffer sequence
Step 2: Remove the leaf node from the original tree
Step 3: Repeat the above two steps until just two nodes are remained
The decoding steps are shown as follows:
Step 1: Constitute the complementary sequence (excluding the nodes in the pruffer)in a ascending order
Step 2: Respectively scan the pruffer sequence and the complementary one, add both first one into the match set
Step 3: Remove the match nodes above from both sequences
Step 4: Check whether the removal node is still contained in the pruffer, if Ture, go to Step 5, else add the removal element into the complementary sequence, and sort it.
Step 5: Return to the Step 2 until the pruffer sequence is empty, add the remained complementary elements into the match set.
Step 6: Constitute the tree based on the adjacent relationship reflected in the match set.
Here is the python code.
# -*- coding: utf-8 -*-"""Created on 2017/3/15 21:38 2017@author: Randolph.Lee"""import sysimport numpy as nptree1 = {1:[0, 2, 3], 3:[5, 6], 2:[4]}tree2 = {1: [[0], {3: [5, 6]}, {2: [4]}]}print sys.getsizeof(tree1) # 272print sys.getsizeof(tree2) # 272def encode_chromosome(tree): chromosome = [] while len(reduce(lambda x, y: x + y, tree.values())) > 1: # search for the leaf nodes leaf_nodes = filter(lambda x: x not in tree.keys(), reduce(lambda m, n: m + n, tree.values())) # select the minimum leaf nodes min_leaf = min(leaf_nodes) # find the adjacent nodes adjacent_nodes = filter(lambda x: min_leaf in tree[x], tree.keys())[0] chromosome.append(adjacent_nodes) # delete the leaf_nodes tree1[adjacent_nodes].remove(min_leaf) if tree[adjacent_nodes] == []: del tree[adjacent_nodes] return chromosomedef decode_chromosome(chromosome_list): number = len(chromosome_list) complementary = filter(lambda x: x not in chromosome_list, range(number + 2)) matrix = np.zeros((number + 2, number + 2)) complementary.sort() while len(chromosome_list) > 0: first_ele = chromosome_list[0] matrix[min(first_ele, complementary[0]), max(first_ele, complementary[0])] = 1 # delete the element in the respective list del chromosome_list[0] del complementary[0] if first_ele not in chromosome_list: complementary.append(first_ele) complementary.sort() matrix[min(complementary), max(complementary)] = 1 # calculate the dict_list dict_relation = {key: np.where(matrix[key, :] == 1)[0].tolist() for key in range(number + 2)} for (key, item) in dict_relation.items(): if len(item) == 0: del dict_relation[key] if len(item) == 1 and item[0] in dict_relation.keys(): dict_relation[item[0]].append(key) del dict_relation[key] return dict_relationprint encode_chromosome(tree1)chrom = encode_chromosome(tree1)k = [1, 2, 1, 3, 3]g = decode_chromosome(k)print g
- Pruffer
- [Updating] Codeforces Round #404 (Div. 2) 解题报告
- C++Primer第五版 第十六章习题答案(31~40)
- hibernate 中的通用接口BaseDao 和BaseDaoImpl的制作 ,泛型制作
- python 20170315
- 再谈Mybatis之Association Collection
- Pruffer
- 网易视频云海外加速方案发布啦!
- ios客户端学习-二维码扫描和应用跳转
- 《图解HTTP[上野宣]》读书笔记二
- aws 噼里啪啦做了一天测试 花了4$
- iOS-申请邓白氏编码的超详细流程介绍
- Qt WebEngine无法启动
- 触发器
- iOS-常用宏定义大全