资源简介
Python语言,用邻接矩阵实现图
通过二维数组建立无向图
通过二维数组建立有向图
通过边建立有向图
为方便查看,通过NetworkX显示图。
代码片段和文件信息
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2017/11/5 17:38
# @Author : flyhawk
# @Site :
# @File : Class_GraphMatrix.py
# @Software: PyCharm Community Edition
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
def getVerticesNumbers(self):
if self.num_vertices == 0:
self.num_vertices = len(self.matrix)
return self.num_vertices
def getAllVertices(self):
return self.vertices
def getAllEdges(self):
for i in range(len(self.matrix)):
for j in range(len(self.matrix)):
if 0 < self.matrix[i][j] <
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 4150 2017-11-12 17:56 Class_GraphMatrix.py
文件 2877 2017-11-12 18:01 Question_Create_Draw_Graph.py
----------- --------- ---------- ----- ----
7027 2
- 上一篇:Python3入门与进阶
- 下一篇:爬虫源码:分页爬取,mysql数据库连接
相关资源
- python实现SGBM图像匹配算法
- python实现灰度直方图均衡化
- scrapy_qunar_one
- Python学习全系列教程永久可用
- python简明教程.chm
- 抽奖大转盘python的图形化界面
- 双边滤波器实验报告及代码python
- python +MYSQL+HTML实现21蛋糕网上商城
- Python-直播答题助手自动检测出题搜索
- OpenCV入门教程+OpenCV官方教程中文版
- Python 串口工具源码+.exe文件
- Python开发的全栈股票系统.zip
- Python操作Excel表格并将其中部分数据写
- python书籍 PDF
- 利用python绘制散点图
- python+labview+No1.vi
- 老男孩python项目实战
- python源码制作whl文件.rar
- python3.5可用的scipy
- PYTHON3 经典50案例.pptx
- 计算机科学导论-python.pdf
- python模拟鼠标点击屏幕
- windows鼠标自动点击py脚本
- 鱼c小甲鱼零基础学python全套课后题和
- Python 练习题100道
- Practical Programming 2nd Edition
- wxPython Application Development Cookbook
- python 3.6
- Python 3.5.2 中文文档 互联网唯一CHM版本
- python3.5.2.chm官方文档
评论
共有 条评论