-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadjacency_list_directed.py
30 lines (25 loc) · 1.22 KB
/
adjacency_list_directed.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
from collections import defaultdict
class graph(object):
def __init__(self, nodes, edge_list = None):
self.N = nodes
self.A = defaultdict(list)
self.in_degree = [0] * (self.N+1)
if edge_list != None: # let's optionally give this class a list of tuples, consisting of the edges, as a little Python exercise.
for x in edge_list:
self.add_edge(x)
def is_valid_tuple(self, x):
return isinstance(x, tuple) and len(x) == 2 and all((a > 0 and a <= self.N) for a in x)
# this function checks whether an element is a tuple meant to describe a graph edge.
# the conditions are as follows: the tuple is of length 2, and both its elements are between 1 and N.
def add_edge(self, e):
if self.is_valid_tuple(e):
if e[0] not in self.A[e[1]]:
self.A[e[0]].append(e[1])
self.in_degree[e[1]] += 1
else: print("element", e, "is not formatted correctly!")
def remove_edge(self, e):
if self.is_valid_tuple(e):
if e[0] in self.A[e[1]]:
self.A[e[0]].remove(e[1])
self.in_degree[e[1]] -= 1
else: print("element", e, "is not formatted correctly!")