資源簡介
當我們處理完幾百幾千乃至上萬的圖論數據后總是不可避免地要對數據進行數據可視化等的分析
,甚至要在畫好圖的基礎上進行一些上層修改,增加連線,高亮特殊點和路徑。比如說在Dijks-
tra和 Floyd算法的時候,為了體現出它們的區別(Dijkstra是以起始點為中心向外擴散層層
遍歷得到迷宮式推 進最短路徑,而 Floyd是考慮i,j之間分為的過點和不過點分類求最小的
區別);或者生成最小二叉樹的kruscal和prim算法,分別得出最小網絡問題。都需要我們對網
絡節點進行歸類和數據可視化。

代碼片段和文件信息
import?networkx?as?nx
import?matplotlib.pyplot?as?plt
import?xlrd
import?numpy?as?np
class?Graph_Matrix:
????“““
????Adjacency?Matrix
????“““
????def?__init__(self?vertices=[]?matrix=[]):
????????“““
????????:param?vertices:a?dict?with?vertex?id?and?index?of?matrix??such?as?{vertex:index}
????????:param?matrix:?a?matrix
????????“““
????????self.matrix?=?matrix
????????self.edges_dict?=?{}??#?{(tail?head):weight}
????????self.edges_array?=?[]??#?(tail?head?weight)
????????self.vertices?=?vertices
????????self.num_edges?=?0
????????#?if?provide?adjacency?matrix?then?create?the?edges?list
????????if?len(matrix)?>?0:
????????????if?len(vertices)?!=?len(matrix):
????????????????raise?IndexError
????????????self.edges?=?self.getAllEdges()
????????????self.num_edges?=?len(self.edges)
????????#?if?do?not?provide?a?adjacency?matrix?but?provide?the?vertices?list?build?a?matrix?with?0
????????elif?len(vertices)?>?0:
????????????self.matrix?=?[[0?for?col?in?range(len(vertices))]?for?row?in?range(len(vertices))]
????????self.num_vertices?=?len(self.matrix)
????def?isOutRange(self?x):
????????try:
????????????if?x?>=?self.num_vertices?or?x?<=?0:
????????????????raise?IndexError
????????except?IndexError:
????????????print(“節點下標出界“)
????def?isEmpty(self):
????????if?self.num_vertices?==?0:
????????????self.num_vertices?=?len(self.matrix)
????????return?self.num_vertices?==?0
????def?add_vertex(self?key):
????????if?key?not?in?self.vertices:
????????????self.vertices[key]?=?len(self.vertices)?+?1
????????#?add?a?vertex?mean?add?a?row?and?a?column
????????#?add?a?column?for?every?row
????????for?i?in?range(self.getVerticesNumbers()):
????????????self.matrix[i].append(0)
????????self.num_vertices?+=?1
????????nRow?=?[0]?*?self.num_vertices
????????self.matrix.append(nRow)
????def?getVertex(self?key):
????????pass
????def?add_edges_from_list(self?edges_list):??#?edges_list?:?[(tail?head?weight)()]
????????for?i?in?range(len(edges_list)):
????????????self.add_edge(edges_list[i][0]?edges_list[i][1]?edges_list[i][2]?)
????def?add_edge(self?tail?head?cost=0):
????????#?if?self.vertices.index(tail)?>=?0:
????????#???self.addVertex(tail)
????????if?tail?not?in?self.vertices:
????????????self.add_vertex(tail)
????????#?if?self.vertices.index(head)?>=?0:
????????#???self.addVertex(head)
????????if?head?not?in?self.vertices:
????????????self.add_vertex(head)
????????#?for?directory?matrix
????????self.matrix[self.vertices.index(tail)][self.vertices.index(head)]?=?cost
????????#?for?non-directory?matrix
????????#?self.matrix[self.vertices.index(fromV)][self.vertices.index(toV)]?=?\
????????#???self.matrix[self.vertices.index(toV)][self.vertices.index(fromV)]?=?cost
????????self.edges_dict[(tail?head)]?=?cost
????????self.edges_array.append((tail?head?cost))
????????self.num_edges?=?len(self.edges_dict)
????def?getEdges(self?V):
????????pass
????d
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件??????113664??2020-08-17?17:23??data1.xls
?????文件????????8793??2020-08-17?15:54??pic.py
評論
共有 條評論