Is it possible to create a graph permutation in python?

Viewed 207

I wrote a simple permutation function and I would like to graph its output. Is it even possible?

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']

I have difficulties finding materials about permutation graph so any help would be appreciated.

This is what I would like to achieve

enter image description here

Thank you!!!

1 Answers

I have no idea what you do in your function, but found out how to make the graphs you want, using networkx and matplotlib.

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()

enter image description here

Related