-
Notifications
You must be signed in to change notification settings - Fork 1
/
graph.py
69 lines (62 loc) · 2.14 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
65
66
67
68
69
from list import LinkedList
class Graph:
"""
Simulates a complete network
"""
def __init__(self):
"""
Creates object to graph
"""
self.list = LinkedList()
def connect(self, v1, v2):
"""
Create a connection between two persons
:param v1: person1
:param v2: person2
:return: None
"""
a = self.list.find_node(v1) # searches for v1 in graph
b = self.list.find_node(v2) # searches for v2 in graph
if a is None or b is None: # if any of given node is not present then return
return
if not a.value.find_node(b.get_value()): # if previsuy there is not connection then add connection
a.value.add_last(b)
a.flrs += 1 # increases number of folowers of node a
b.fol += 1 # increase number of persons that node b follows
def delete_node(self, node):
"""
Delete node from the graph
:param node: node to be deleted
:return: None
"""
n = self.list.head
while n:
n.value.delete_node(node,
1) # given the link=1, so that LinkedList.delete_node will know that it has to delete edge
n = n.next_node
def delete_edge(self, node1, node2):
"""
delete connection between two given persons
:param node1: person1
:param node2: person2
:return: True, False
"""
n = self.list.head
state = False # will return whether edge is delete or not
while n:
if n.value.delete_node(node2, 1) is not None:
state = True
if n.value.delete_node(node1, 1) is not None:
state = True
n = n.next_node
return state
def __str__(self):
"""
Converts and object of graph to am object of string
:return: string
"""
n, s = self.list.head, ""
while n:
s = s + n.value.value + "--> " + str(n.get_value()) + "\n"
n = n.next_node
return s