-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graph.py
64 lines (45 loc) · 1.77 KB
/
Graph.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
import numpy as np
from typing import Set
########################################
# Here, I'll store the base Graph class
########################################
class Vertex(object):
def __init__(self, label: int, passages: set):
self.label = label
self._visited = False ## This should be specified when initializing each walker. Bad puppy, Vicent
self.passages = passages # this is called passages but it may as well be called neighbours. Tomato, tomatoe
def __str__(self):
return f'{self.label}'
def _toint(self):
return self.label
class Graph(object):
# TODO: Uncouple Graph and Vertex from Tremaux Walker
def __init__(self, A: np.ndarray):
# Adjacency matrix (should be square matrix)
self.A = A
# we generate all nodes
self.nodes = list()
for n in range(len(A[0])):
passages = self._set_passages(A=A, label=n)
v = Vertex(label=n, passages=passages)
self.nodes.append(v)
def _set_passages(self, A: np.ndarray, label: int) -> Set:
# we get the vertices this one is connected to
conn = np.nonzero(A[label])[0]
return set(conn)
def get_node_from_label(self, label: int) -> Vertex:
"""Makes it easy to find nodes"""
for node in self.nodes:
if node.label == label:
return node
raise Exception('Node not found')
A = np.array([[0, 1, 0, 1],
[1, 0, 1, 1],
[0, 1, 0, 0],
[1, 1, 0, 0]])
if __name__ == "__main__":
graph = Graph(A)
for node in graph.nodes:
print(f'Node with label: {node.label}')
print(f'This node has the following passages: {node.passages}')
# I'm the fucking best