Python在无向n中搜索子图

2024-10-03 19:30:20 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我要做的是建立一个无定向网络,在这个网络中,我要搜索两个不同的子图,计算它们出现的时间,它必须作为输出保存在.txt文件中

我已经做了非定向网络,这是从一个.txt文件读取

example : abce

我已经在.txt文件中编写了abce,我当前的代码读取第一个字母,并将其作为节点或点。所有其他的left(b,c,e)都是与字母a的连接

所以我的非定向网络是这样的: un-directed network

现在我要做的是在其中搜索这两个图,并计算每个图出现的次数,然后将结果保存为一个输出.txt文件,如graph1出现:2 graph2出现:3。 graph1 and graph 2

我现在有密码:

    from graphs import Graph
g = {};
datoteka = open("tocke.txt")

# v i shrani tocko
# v a je vse ostalo razen tocke(s cim je povezano)

for vrstica in datoteka:
    i = vrstica.strip()[0]
    a = vrstica.strip()[1:]
    x = []
    for j in a:
        x.append(j)
    g[i] = x
print(g)


graph = Graph(g)
# izpis podatkov o grafu
print(graph)

# izpis podatkov o povezavah
print("Tocke v grafu")
print(graph.vertices())

print("")

# izpis povezav
print("Izpis povezav v grafu (izloci dupliciranje)")
print(graph.edges())

print("")

我该怎么办


Tags: 文件网络txt字母graphprint定向je