Python判断有向图是否有环的实例讲解-创新互联

实例如下:

创新互联建站从2013年成立,是专业互联网技术服务公司,拥有项目做网站、成都网站建设网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元南县做网站,已为上家服务,为南县各地企业和个人服务,联系电话:18982081108
import numpy
from numpy import *
def dfs( v ):
 vis[v] = -1
 flag = 0
 for i in range(n):
 # print (a[v][i],'---', vis[i] )
 if a[v][i] != 0 and vis[i] != -1:
  dfs(i)
  vis[i] = 1
 else:
  pass
 if a[v][i] != 0 and vis[i] == -1:
  print ('Yes, there is A loop in this network\n')
  global swi
  swi = True
  exit()
  return
  # break
 else:
  pass
 print ('s = 0')
 return False

global swi
swi = False
'''===装载数据'''
edges = numpy.loadtxt('9_nodes_with_r_edge_8_to_3.txt')

# edges = [ int(i) for i in edges]
bian = int(shape(edges)[0]) - 1
print (bian,'edges in the network \n')
print (shape(edges),'\n')

n = int( edges[0][1] )

c = int( edges[0][0] )
# n, c = input().split()
# n = int(n)
# c = int(c)
a = [([0] * n) for i in range(n)]
vis = [0] * c
for i in range(1, c+1):
 s, t = edges[i][0:2]
 # print (s,' - ', t )
 '''GO_OBO文件则 s, t 不需要 -1 '''
 s = int(s) - 1
 t = int(t) - 1
 # s = int(s)
 # t = int(t)
 a[s][t] = 1
# print (a)
# print (vis)
dfs(0)
# print (swi)
if not swi:
 print('No loop, DAG - DAG - DAG')

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网站标题:Python判断有向图是否有环的实例讲解-创新互联
文章源于:http://myzitong.com/article/hcjei.html