1

我是 python 新手,正在尝试编写一个程序,该程序可以将 1 和 0 的矩阵作为输入,从中创建一个图,然后继续生成另一个随机图,直到找到同构对。

我不断地弄乱我的逻辑,不断地陷入无限循环或无法完成我想要完成的事情的循环。任何帮助,将不胜感激。

这是我的程序中有问题的部分

i = 0
for i in range(counterP):
    g4 = numpy.reshape(numpy.random.random_integers(0, 1, size=matrix_sizeP), (vertex_countP, vertex_countP))
    g5 = numpy.reshape(numpy.random.random_integers(0, 1, size=matrix_sizeP), (vertex_countP, vertex_countP))
    G4 = nx.Graph(g4)
    G5 = nx.Graph(g5)
    G4G5 = iso.GraphMatcher(G4,G5)
    isomP = G4G5.is_isomorphic()

    if isomP is True:
        ed = nx.number_of_edges(G4)
        print("Iteration", i, ":", ed, "edges")
        print(G4G5.mapping)
        i = i + 1
    else:
        g5 = numpy.reshape(numpy.random.random_integers(0, 1, size=matrix_sizeP), (vertex_countP, vertex_countP))
        G5 = nx.Graph(g5)
        isomP = G4G5.is_isomorphic()
4

1 回答 1

1

你的程序逻辑确实有点不对劲。这是一个版本,它可以满足我的理解你想要的:

import numpy as np
import networkx as nx
from networkx.algorithms.isomorphism import GraphMatcher

def brute_force_find_iso_pair(shape, maxiter, template=None):
    def make_random_graph():
        return nx.Graph(np.random.random_integers(0, 1, shape))
    G4 = make_random_graph() if template is None else template
    def check_iso(G5):
        return GraphMatcher(G4, G5).is_isomorphic()
    for n in range(maxiter):
        G5 = make_random_graph()
        if check_iso(G5):
            break
    else:
        G5 = None
    return G4, G5, n + 1

# example
G4, G5, n = brute_force_find_iso_pair((4, 4), 50)
print(f"""After {n} iterations:
template: {G4.adj}
match:    {G5 and G5.adj}
""")

样品运行:

After 43 iterations:
template: {0: {1: {'weight': 1}, 3: {'weight': 1}, 2: {'weight': 1}}, 1: {0: {'weight': 1}, 1: {'weight': 1}, 2: {'weight': 1}, 3: {'weight': 1}}, 2: {1: {'weight': 1}, 0: {'weight': 1}, 2: {'weight': 1}, 3: {'weight': 1}}, 3: {0: {'weight': 1}, 1: {'weight': 1}, 2: {'weight': 1}}}
match:    {0: {2: {'weight': 1}, 1: {'weight': 1}, 3: {'weight': 1}}, 1: {0: {'weight': 1}, 3: {'weight': 1}, 2: {'weight': 1}}, 2: {0: {'weight': 1}, 1: {'weight': 1}, 2: {'weight': 1}, 3: {'weight': 1}}, 3: {1: {'weight': 1}, 0: {'weight': 1}, 2: {'weight': 1}, 3: {'weight': 1}}}

After 50 iterations:
template: {0: {1: {'weight': 1}, 2: {'weight': 1}}, 1: {0: {'weight': 1}, 3: {'weight': 1}, 2: {'weight': 1}}, 2: {0: {'weight': 1}, 1: {'weight': 1}, 3: {'weight': 1}}, 3: {1: {'weight': 1}, 2: {'weight': 1}}}
match:    None

After 25 iterations:
template: {0: {1: {'weight': 1}, 3: {'weight': 1}, 2: {'weight': 1}}, 1: {0: {'weight': 1}, 2: {'weight': 1}}, 2: {1: {'weight': 1}, 0: {'weight': 1}, 2: {'weight': 1}}, 3: {0: {'weight': 1}}}
match:    {0: {1: {'weight': 1}}, 1: {0: {'weight': 1}, 2: {'weight': 1}, 3: {'weight': 1}}, 2: {1: {'weight': 1}, 2: {'weight': 1}, 3: {'weight': 1}}, 3: {1: {'weight': 1}, 2: {'weight': 1}}}

After 2 iterations:
template: {0: {0: {'weight': 1}, 2: {'weight': 1}, 3: {'weight': 1}}, 1: {2: {'weight': 1}, 3: {'weight': 1}}, 2: {0: {'weight': 1}, 1: {'weight': 1}, 2: {'weight': 1}, 3: {'weight': 1}}, 3: {0: {'weight': 1}, 1: {'weight': 1}, 2: {'weight': 1}}}
match:    {0: {2: {'weight': 1}, 3: {'weight': 1}}, 1: {1: {'weight': 1}, 2: {'weight': 1}, 3: {'weight': 1}}, 2: {0: {'weight': 1}, 1: {'weight': 1}, 3: {'weight': 1}}, 3: {0: {'weight': 1}, 1: {'weight': 1}, 2: {'weight': 1}, 3: {'weight': 1}}}

After 32 iterations:
template: {0: {2: {'weight': 1}, 3: {'weight': 1}}, 1: {2: {'weight': 1}, 3: {'weight': 1}}, 2: {1: {'weight': 1}, 0: {'weight': 1}, 3: {'weight': 1}}, 3: {1: {'weight': 1}, 2: {'weight': 1}, 0: {'weight': 1}}}
match:    {0: {1: {'weight': 1}, 2: {'weight': 1}, 3: {'weight': 1}}, 1: {0: {'weight': 1}, 2: {'weight': 1}}, 2: {0: {'weight': 1}, 1: {'weight': 1}, 3: {'weight': 1}}, 3: {0: {'weight': 1}, 2: {'weight': 1}}}
于 2018-09-18T20:37:37.857 回答