-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphDirected.py
More file actions
74 lines (62 loc) · 2.44 KB
/
Copy pathGraphDirected.py
File metadata and controls
74 lines (62 loc) · 2.44 KB
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
from Vertex import *
class GraphDirected:
def __init__(self):
self.g = LinkedList()
def vertexComparisson(self, node, vertexName):
"""
:param node: Es un objeto de tipo nodo LinkedList
:param vertexName: nombre del vertice a comparar
:return: Retorna True si el vértice del nodo y el vertexName son iguales, de lo contrario
retorna False
"""
vertex = node.value
if ("%s".strip() % vertex.name) == ("%s".strip() % vertexName):
return True
return False
def vertexName(self, node):
"""
:param node: Nodo de una lista enlazada que contiene un Vertice.
:return: retorna el VertexName de un vertice
"""
vertex = node.value
return vertex.name
def add(self, vertexName):
"""
:param vertexName: Nombre con el que se creara el nuevo vertice.
:return: Retorna True si el vertice se agrego al grafo, False de lo contrario.
"""
"""
Para agregar un nuevo vértice, el vértice a agregar no debe de existir previamente dentro de
la lista enlazada.
"""
if not self.g.exists(vertexName, lambda a, b: self.vertexComparisson(a, b)):
self.g.push(Vertex(vertexName))
return True
return False
def edge(self, vertexNameA, vertexNameB):
"""
:param vertexNameA: Nombre del vertice A.
:param vertexNameB: Nombre del vertice B
:return: True si se crean las aristas entre los vertices, False de lo contrario.
"""
"""
Este método crea los vértices sobre los cuales se genera una arista.
"""
self.add(vertexNameA)
self.add(vertexNameB)
if not self.g.get(vertexNameA, lambda a, b: self.vertexComparisson(a, b)).value.edges.exists(vertexNameB,lambda a,b: self.vertexComparisson(a, b)):
self.g.get(vertexNameA, lambda a, b: self.vertexComparisson(a, b)).value.edges.push(Vertex(vertexNameB))
return True
return False
def __str__(self):
result = []
graph = self.g
current = graph.first
while (current):
result += ["\tEl nodo %s tiene aristas con: %s" % (
current.value.name, current.value.edges.print(lambda a: self.vertexName(a)))]
current = current.next
return "\n".join(result)
def __len__(self):
graph = self.g
return len(graph)