Design a linear time algorithm for each of the following problem: (1) given a graph G, test if G is connected. (2) given a graph G, test if G is a tree. (3) given a graph G, test if G has a cycle,if so,print a cycle
实例如下:
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