-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.py
More file actions
169 lines (145 loc) · 5.01 KB
/
Graph.py
File metadata and controls
169 lines (145 loc) · 5.01 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
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
# Author: Jingze Dai
# Email Address: [email protected] or [email protected]
# Github: https://github.com/daijingz
# Linkedin: https://www.linkedin.com/in/jingze-dai/
# Description: Graph implementation 1
class InappropriateNode(Exception):
"""! Exception when we have inappropriate nodes"""
pass
class UndirectedGraph:
def __init__(self):
"""! The UndirectedGraph class initializer.
@return An empty undirected graph object
"""
self.__startNode = None
self.__node = []
self.__body = []
def __repr__(self):
"""! Return a printable representation of object """
return str(self)
def __str__(self):
"""! Return a description string of the current object"""
str1 = "Undirected graph with start node " + str(self.getStartNode())
str2 = ", which has length " + str(len(self))
str3 = " with nodes " + str(self.getNode())
str4 = " with body " + str(self.getBody())
return str1 + str2 + str3 + str4
def __len__(self):
"""! Return the length of object"""
try:
return len(self.getBody())
except:
raise Exception()
def getStartNode(self):
"""! Return the start node"""
if self.getStartNode() is not None:
return self.getStartNode()
else:
raise Exception()
def getNode(self):
"""! Return the node list of object"""
try:
return self.__node
except:
raise Exception()
def getBody(self):
"""! Return body values of object"""
try:
return self.__body
except:
raise Exception()
def setStartNode(self, str1: str):
"""! Change the start node"""
if len(str1) > 5:
raise InappropriateNode()
elif len(str1) == 0:
raise InappropriateNode()
self.__startNode = str1
def addNode(self, node: str):
"""! Add new node to the node list"""
if len(node) > 5:
raise InappropriateNode()
elif len(node) == 0:
raise InappropriateNode()
elif self.__node.count(node) == 0:
self.__node += [node]
def addEdge(self, edge: tuple):
"""! Add edge to the body"""
if len(edge) != 2:
raise ValueError()
self.addNode(edge[0])
self.addNode(edge[1])
if self.__node.count(edge[0]) == 0 or self.__node.count(edge[1]) == 0:
self.__body += [edge]
def emptyGraph(self):
"""! Empty the object's graph"""
self.__startNode = None
self.__node = []
self.__body = []
class DirectedGraph:
def __init__(self):
"""! The DirectedGraph class initializer.
@return An empty directed graph object
"""
self.__startNode = None
self.__node = []
self.__body = []
def __str__(self):
"""! Return a description string of the current object"""
str1 = "Directed graph with start node " + str(self.getStartNode())
str2 = ", which has length " + str(len(self))
str3 = " with nodes " + str(self.getNode())
str4 = " with body " + str(self.getBody())
return str1 + str2 + str3 + str4
def __len__(self):
"""! Return the length of object"""
try:
return len(self.getBody())
except:
raise Exception()
def getStartNode(self):
"""! Return the start node"""
if self.getStartNode() is not None:
return self.getStartNode()
else:
raise Exception()
def getNode(self):
"""! Return the node list of object"""
try:
return self.__node
except:
raise Exception()
def getBody(self):
"""! Return body values of object"""
try:
return self.__body
except:
raise Exception()
def setStartNode(self, str1: str):
"""! Change the start node"""
if len(str1) > 5:
raise InappropriateNode()
elif len(str1) == 0:
raise InappropriateNode()
self.__startNode = str1
def addNode(self, node: str):
"""! Add new node to the node list"""
if len(node) > 5:
raise InappropriateNode()
elif len(node) == 0:
raise InappropriateNode()
elif self.__node.count(node) == 0:
self.__node += [node]
def addEdge(self, edge: tuple):
"""! Add edge to the body"""
if len(edge) != 2:
raise ValueError()
self.addNode(edge[0])
self.addNode(edge[1])
if self.__node.count(edge[0]) == 0 or self.__node.count(edge[1]) == 0:
self.__body += [edge]
def emptyGraph(self):
"""! Empty the object's graph"""
self.__startNode = None
self.__node = []
self.__body = []