Python图1.0
效果:>>> a = Graph([, , , , , , , , , ])
>>> a.IsSimple() # 是简单图吗?
True
>>> a.GetDegree(2) # 顶点2的度数
3
>>> a.IsEuler() # 是欧拉图吗?
False
>>> b = Graph([, , , ])
>>> b.IsRegular() # 是正则图吗?
2
>>> b.IsComplete() # 是完全图吗?
False
>>> b.IsEuler()
True
辕马:
# graph.py
# graph 1.0
def extract(edge: list]):
point = set()
biao = {}
for i in edge:
for j in range(2):
if i in point:
biao].append(i)
else:
biao] = ]
point.add(i)
return point, biao
class Graph:
def __init__(self, edge: list]):
self.edge = edge
self.point, self.biao = extract(edge)
self.n = len(self.point)
pass
@staticmethod
def equality(x: list, y: list):
return x == y or x[::-1] == y
def IsSimple(self): # 是简单图吗?
looked = []
for i in self.edge:
if i == i:
return False
elif i in looked:
return False
looked.append(i)
return True
def GetDegree(self, point: int): # 获取顶点的度数
if point in self.point:
return len(self.biao)
else:
return 0
def IsRegular(self): # 是正则图吗?
'''
this function return all point's degree, if self is a regular graph.
else return false.
'''
k = -1
for i in self.biao:
if len(self.biao) != k:
if k == -1:
k = len(self.biao)
else:
return False
return k
def IsComplete(self): # 是完全图吗?
k = self.IsRegular()
return k == self.n - 1
def IsEuler(self):#是欧拉图吗?
du = 0
for i in self.biao:
if len(self.biao) % 2:
du += 1
return du == 0 or du == 2
?
页:
[1]