资源简介
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试题12套(包括选择题和
- pywin32_python3.6_64位
- python+ selenium教程
- PycURL(Windows7/Win32)Python2.7安装包 P
- 英文原版-Scientific Computing with Python
- 7.图像风格迁移 基于深度学习 pyt
- 基于Python的学生管理系统
- A Byte of Python(简明Python教程)(第
- Python实例174946
- Python 人脸识别
- Python 人事管理系统
- 基于python-flask的个人博客系统
- 计算机视觉应用开发流程
- python 调用sftp断点续传文件
- python socket游戏
- 基于Python爬虫爬取天气预报信息
- python函数编程和讲解
- Python开发的个人博客
- 基于python的三层神经网络模型搭建
- python实现自动操作windows应用
- python人脸识别(opencv)
- python 绘图(方形、线条、圆形)
- python疫情卡UN管控
- python 连连看小游戏源码
- 基于PyQt5的视频播放器设计
- 一个简单的python爬虫
- csv文件行列转换python实现代码
- Python操作Mysql教程手册
- Python Machine Learning Case Studies
- python获取硬件信息
评论
共有 条评论