This repository has been archived by the owner on Aug 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGrafoMatriz.py
181 lines (155 loc) · 5.76 KB
/
GrafoMatriz.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Universidade Federal de Pernambuco (UFPE) (http://www.ufpe.br)
Centro de Informática (CIn) (http://www.cin.ufpe.br)
Bacharelado em Sistemas de Informacao
IF 969 - Algoritmos e Estruturas de Dados
Author: Antônio Paulino de Lima Neto (apln2)
Email: apln2@cin.ufpe.br
Created on Thu May 24 01:28:08 2018
Descrição: Script contento a classe Grafo, representada através de Matriz de
Adjacências.
Licenca: The MIT License (MIT)
Copyright(c) 2018 Antônio Paulino de Lima Neto
"""
import numpy
class TADGrafoMatriz:
'''
TAD Grafo representado através de Matriz de Adjacências.
'''
def __init__(self, v = 0, **kwargs):
''' Método Construtor do Grafo por Matriz
Args:
v (int): número de vértices
Kwargs:
direcionado (bool): define se o grafo é direcionado ou não
ponderado (bool): define se o grafo é ponderado ou não
'''
self.__v = v
self.__matriz_adj = numpy.zeros((v, v))
self.__dir = kwargs.get('direcionado', False)
self.__pond = kwargs.get('ponderado', False)
iteravel = kwargs.get('iteravel', None)
if iteravel is not None:
self.__construir_grafo(iteravel)
def inserir_aresta(self, i, j, p = 1):
'''
Função de inserção de aresta. Quando o grafo não é direcionado, a
inserção acontece nos valores uxv e vxu da matriz para cada aresta.
Args:
i (int): vértice de origem
j (int): vértice de destino
p (int): peso
'''
try:
self.__matriz_adj[i][j] = p
if not self.__dir:
self.__matriz_adj[j][i] = p
except IndexError:
raise IndexError ("Um ou mais vértices não pertencem ao grafo")
def aresta(self, i, j):
'''
Verifica se existe uma aresta entre dois vértices
Args:
i (int): vértice de origem
j (int): vértice de destino
'''
try:
return bool(self.__matriz_adj[i][j])
except IndexError:
raise IndexError ("Um ou mais vértices não pertencem ao grafo")
def arestas_adjacentes(self, i):
'''
Retorna uma lista de vértices adjacentes ao vertice parâmetro.
Args:
i (int): índice do vértice
Returns:
(list): lista com arestas saindo de i
'''
try:
adj = list(self.__matriz_adj[i])
except:
IndexError ("Vértice não pertence ao grafo")
else:
return list((i, v) for i, v in enumerate(adj) if v)
def vertices_adjacentes(self, i):
'''
Retorna apenas a lista de vértices adjacentes ao vértice i
Args:
i (int): índice do vértice
Returns:
(list): lista de vértices adjacentes
'''
try:
adj = list(self.__matriz_adj[i])
except:
IndexError ("Vértice não pertence ao grafo")
else:
return list(i for i, v in enumerate(adj) if v)
def remover_aresta(self, i, j):
try:
a = self.__matriz_adj[i][j]
if a:
self.__matriz_adj[i][j] = 0
if not self.direcionado:
self.__matriz_adj[j][i] = 0
return a
else:
raise KeyError ("Aresta não pertence ao grafo")
except IndexError:
raise IndexError ("Um ou mais vértices não pertencem ao grafo")
def contar_arestas(self):
'''
Função que retorna o número de arestas do grafo.
Quando o grafo não é direcionado, a função contabilza apenas o
triângulo superior, uma vez que a matriz é espelhada.
'''
arestas = 0
for i in range (self.__v):
for j in range(self.__v):
if not(self.ponderado and i<=j):
arestas += bool(self.__matriz_adj[i][j])
return arestas
def __construir_grafo(self, iteravel):
'''Método de construção do grafo a partir de uma matriz de adjacências
Args:
iteravel: objeto iteravel (nxn) contendo a matriz de adjacências.
'''
if len(iteravel) != len(iteravel[0]): raise ValueError
self.__v = len(iteravel)
self.__matriz_adj = numpy.asarray(iteravel)
@property
def direcionado(self):
return self.__dir
@property
def ponderado(self):
return self.__pond
@property
def matriz_de_adjacencias(self):
return self.__matriz_adj
@property
def tam(self):
return self.__v
def __len__(self):
return self.vertices
def __str__(self):
body = []
maior_index = 0
maior_info = 0
for i in range(self.__v):
index = f"{i}: "
info = f"{'|'.join(str(x) for x in self.__matriz_adj[i])}"
t_info = len(info)
t_index = len(index)
body.append(index+info)
if t_info > maior_info: maior_info = t_info
if t_index > maior_index: maior_index = t_index
spc = maior_info//self.__v
header = f"{' '*maior_index}"+\
f"{(' '*spc).join(str(x) for x in list(range(self.__v)))}"
body.insert(0, header)
return '\n'.join(body)
def __repr__(self):
return f"{self.__class__.__name__}\
({list(x for x in (list(i) for i in self.__matriz_adj))})"