Skip to content

Commit

Permalink
Update Graph-Edgeset-Array.py
Browse files Browse the repository at this point in the history
  • Loading branch information
itcharge committed Mar 15, 2022
1 parent 5d62e45 commit 930a7eb
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions Templates/08.Graph/Graph-Edgeset-Array.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ def __init__(self, vi, vj, val):
self.vj = vj # 边的终点
self.val = val # 边的权值

class Graph: # 基本图类,采用边集数组表示
def __init__(self, vertices=[]):
self.ver_count = len(vertices) # 顶点个数
self.vertices = vertices # 顶点数组
class Graph: # 基本图类,采用边集数组表示
def __init__(self):
self.edges = [] # 边数组

# 图的创建操作,edges 为边信息
Expand All @@ -17,8 +15,8 @@ def creatGraph(self, edges=[]):

# 向图的边数组中添加边:vi - vj,权值为 val
def add_edge(self, vi, vj, val):
edge = EdgeNode(vi, vj, val)
self.edges.append(edge)
edge = EdgeNode(vi, vj, val) # 创建边节点
self.edges.append(edge) # 将边节点添加到边数组中

# 获取 vi - vj 边的权值
def get_edge(self, vi, vj):
Expand All @@ -31,11 +29,10 @@ def get_edge(self, vi, vj):
# 根据边数组打印图
def printGraph(self):
for edge in self.edges:
print(edge.vi + ' - ' + edge.vj + ' : ' + str(edge.val))
print(str(edge.vi) + ' - ' + str(edge.vj) + ' : ' + str(edge.val))

vertices = ['v1', 'v2', 'v3', 'v4', 'v5']
graph = Graph(vertices)
edges = [['v1', 'v4', 3],['v1', 'v3', 9],['v3', 'v4', 6],['v2', 'v5', 4],['v4', 'v5', 2]]
graph = Graph()
edges = [[1, 4, 3],[1, 3, 9],[3, 4, 6],[2, 5, 4],[4, 5, 2]]
graph.creatGraph(edges)
print(graph.get_edge('v3', 'v4'))
print(graph.get_edge(3, 4))
graph.printGraph()

0 comments on commit 930a7eb

Please sign in to comment.