【问题标题】:Is it possible to create a graph permutation in python?是否可以在 python 中创建图形排列?
【发布时间】:2021-12-24 17:58:30
【问题描述】:

我写了一个简单的置换函数,我想画出它的输出。有可能吗?

from itertools import permutations

def permutate(string):
    letters = [x for x in string]
    perms = list(permutations(letters, len(letters)))
    p_set = set([''.join(i) for i in perms])

    return p_set


a = list(permutate('abab'))
# output: ['abba', 'abab', 'aabb', 'baba', 'baab', 'bbaa']

我很难找到有关排列图的材料,因此我们将不胜感激。

这就是我想要实现的目标

谢谢!!!

【问题讨论】:

  • 1- 请以文本形式提供您的代码,2- 请提供示例数据集(以文本形式)并解释它们是如何概括的
  • 那么现在,您的列表如何转化为图表?
  • 我认为你可以使用matplotlib。由于您已经生成了排列,因此您拥有所有顶点。您只需要在字典中存储元素更改关系即可获得优势。听起来是个有趣的项目。
  • 如果您有一个具有唯一值的可迭代 all,其中每个值表示特定边的存在,例如 1-3、4-6 等,这将很容易转换为图表。

标签: python numpy matplotlib graph permutation


【解决方案1】:

我不知道你在函数中做了什么,但我知道如何使用networkxmatplotlib 制作你想要的图表。

import networkx as nx
import matplotlib.pyplot as plt

G = nx.Graph()
G.add_edges_from([(0, 1), (1, 2), (2, 3), (3, 4), (1, 4), (2, 5),
                  (0, 5), (1, 5), (3, 5), (2, 4), (0, 2), (1, 3)])
fig, axes = plt.subplots(figsize=(8, 6))
nx.draw(G, ax=axes, pos=nx.circular_layout(G), with_labels=True)
plt.show()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-20
    • 2015-01-20
    • 2014-04-16
    • 1970-01-01
    相关资源
    最近更新 更多