import sys
class Vertex:
def __init__(self, key):
self.id = key
self.connectedTo = {}
self.distance = sys.maxsize
self.predecessor = None
self.color = 'White'
self.dicoveryTime = 0
self.finishTime = 0
def setDistance(self, d):
self.distance = d
def getDistance(self):
return self.distance
def setPred(self, p):
self.predecessor = p
def getPred(self):
return self.predecessor
def setColor(self, c):
self.color = c
def getColor(self):
return self.color
def addNeighbour(self, neighbour, weight=0):
self.connectedTo[neighbour] = weight
def __str__(self):
return str(self.id) + ' connectedTo: ' + str([x.id for x in self.connectedTo])
def getConnections(self):
return self.connectedTo.keys()
def getId(self):
return self.id
def getWeight(self, neighbour):
return self.connectedTo[neighbour]
class Graph:
def __init__(self):
self.vertexlist = {}
self.numVertices = 0
def addVertex(self, key):
self.numVertices += 1
newVertex = Vertex(key)
self.vertexlist[key] = newVertex
return newVertex
def getVertex(self, key):
if key in self.vertexlist:
return self.vertexlist[key]
else:
return None
def __contains__(self, key):
return key in self.vertexlist
def addEdge(self, start, end, weight=0):
if start not in self.vertexlist:
newvertex = self.addVertex(start)
if end not in self.vertexlist:
newvertex = self.addVertex(end)
self.vertexlist[start].addNeighbour(self.vertexlist[end], weight)
def getVerteices(self):
return self.vertexlist.keys()
def __iter__(self):
return iter(self.vertexlist.values())
图Graph
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 原文链接:https://www.cnblogs.com/SivilTaram/p/graph_neural_ne...
- 问题 使用echart展示知识图谱时,我参照官网的demo,很快就用自己的测试数据(包含node、link )实现...
- 图相关的论文:GNN_Papers 一些开源的图(graph)模型 【1】Model_1: ChebNet(201...