首页 / PYTHON / Python实现数据结构 图
Python实现数据结构 图
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Python实现数据结构 图,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4820字,纯文字阅读大概需要7分钟。
内容图文
![Python实现数据结构 图](/upload/InfoBanner/zyjiaocheng/633/d681e28642c444ca88c9ff03eadcaa13.jpg)
邻接矩阵
class Vertex: def __init__(self, node): self.id = node # Mark all nodes unvisited self.visited = False def addNeighbor(self, neighbor, G): G.addEdge(self.id, neighbor) def getConnections(self, G): return G.adjMatrix[self.id] def getVertexID(self): return self.id def setVertexID(self, id): self.id = id def setVisited(self): self.visited = True def __str__(self): return str(self.id) class Graph: def __init__(self, numVertices=10, directed=False): self.adjMatrix = [[None] * numVertices for _ in range(numVertices)] self.numVertices = numVertices self.vertices = [] self.directed = directed for i in range(0, numVertices): newVertex = Vertex(i) self.vertices.append(newVertex) def addVertex(self, vtx, id): #增加点,这个function没有扩展功能 if 0 <= vtx < self.numVertices: self.vertices[vtx].setVertexID(id) def getVertex(self, n): for vertxin in range(0, self.numVertices): if n == self.vertices[vertxin].getVertexID(): return vertxin return None def addEdge(self, frm, to, cost=0): #返回全部连线/航线 #print("from",frm, self.getVertex(frm)) #print("to",to, self.getVertex(to)) if self.getVertex(frm) is not None and self.getVertex(to) is not None: self.adjMatrix[self.getVertex(frm)][self.getVertex(to)] = cost if not self.directed: # For directed graph do not add this self.adjMatrix[self.getVertex(to)][self.getVertex(frm)] = cost def getVertices(self): vertices = [] for vertxin in range(0, self.numVertices): vertices.append(self.vertices[vertxin].getVertexID()) return vertices def printMatrix(self): for u in range(0, self.numVertices): row = [] for v in range(0, self.numVertices): row.append(str(self.adjMatrix[u][v]) if self.adjMatrix[u][v] is not None else '/') print(row) def getEdges(self): edges = [] for v in range(0, self.numVertices): for u in range(0, self.numVertices): if self.adjMatrix[u][v] is not None: vid = self.vertices[v].getVertexID() wid = self.vertices[u].getVertexID() edges.append((vid, wid, self.adjMatrix[u][v])) return edges def getNeighbors(self, n): neighbors = [] for vertxin in range(0, self.numVertices): if n == self.vertices[vertxin].getVertexID(): for neighbor in range(0, self.numVertices): if (self.adjMatrix[vertxin][neighbor] is not None): neighbors.append(self.vertices[neighbor].getVertexID()) return neighbors def isConnected(self, u, v): uidx = self.getVertex(u) vidx = self.getVertex(v) return self.adjMatrix[uidx][vidx] is not None def get2Hops(self, u): #转一次机可以到达哪里 neighbors = self.getNeighbors(u) print(neighbors) hopset = set() for v in neighbors: hops = self.getNeighbors(v) hopset |= set(hops) return list(hopset)
邻接表
import sys class Vertex: def __init__(self, node): self.id = node self.adjacent = {} #为所有节点设置距离无穷大 self.distance = sys.maxsize # 标记未访问的所有节点 self.visited = False # Predecessor self.previous = None def addNeighbor(self, neighbor, weight=0): self.adjacent[neighbor] = weight # returns a list def getConnections(self): # neighbor keys return self.adjacent.keys() def getVertexID(self): return self.id def getWeight(self, neighbor): return self.adjacent[neighbor] def setDistance(self, dist): self.distance = dist def getDistance(self): return self.distance def setPrevious(self, prev): self.previous = prev def setVisited(self): self.visited = True def __str__(self): return str(self.id) + ' adjacent: ' + str([x.id for x in self.adjacent]) def __lt__(self, other): return self.distance < other.distance and self.id < other.id class Graph: def __init__(self, directed=False): # key is string, vertex id # value is Vertex self.vertDictionary = {} self.numVertices = 0 self.directed = directed def __iter__(self): return iter(self.vertDictionary.values()) def isDirected(self): return self.directed def vectexCount(self): return self.numVertices def addVertex(self, node): self.numVertices = self.numVertices + 1 newVertex = Vertex(node) self.vertDictionary[node] = newVertex return newVertex def getVertex(self, n): if n in self.vertDictionary: return self.vertDictionary[n] else: return None def addEdge(self, frm, to, cost=0): if frm not in self.vertDictionary: self.addVertex(frm) if to not in self.vertDictionary: self.addVertex(to) self.vertDictionary[frm].addNeighbor(self.vertDictionary[to], cost) if not self.directed: # For directed graph do not add this self.vertDictionary[to].addNeighbor(self.vertDictionary[frm], cost) def getVertices(self): return self.vertDictionary.keys() def setPrevious(self, current): self.previous = current def getPrevious(self, current): return self.previous def getEdges(self): edges = [] for key, currentVert in self.vertDictionary.items(): for nbr in currentVert.getConnections(): currentVertID = currentVert.getVertexID() nbrID = nbr.getVertexID() edges.append((currentVertID, nbrID, currentVert.getWeight(nbr))) # tuple return edges def getNeighbors(self, v): vertex = self.vertDictionary[v] return vertex.getConnections()
引入的这两段代码的原文链接: https://www.cnblogs.com/kumata/p/9246502.html
内容总结
以上是互联网集市为您收集整理的Python实现数据结构 图全部内容,希望文章能够帮你解决Python实现数据结构 图所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。