Skip to content

Commit 617bcd3

Browse files
committed
Updated docstrings for better code readability
1 parent 536c77d commit 617bcd3

File tree

5 files changed

+35
-26
lines changed

5 files changed

+35
-26
lines changed

pygorithm/data_structures/graph.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
# Created On: 12th August 2017
33
from collections import defaultdict
44

5-
65
class Graph(object):
6+
''' class for creating a graph '''
77
def __init__(self):
88
self.graph = defaultdict(list)
99
self.count = 0
@@ -25,7 +25,7 @@ def get_code(self):
2525
return inspect.getsource(Graph)
2626

2727

28-
class WeightedGraph:
28+
class WeightedGraph(object):
2929
"""
3030
A graph with a numerical value (weight) on edges
3131
"""
@@ -77,11 +77,12 @@ def kruskal_mst(self):
7777
return edges_explored
7878

7979
@staticmethod
80-
def kruskal_complexity():
80+
def kruskal_time_complexity():
8181
return '''Worst case: O(E log(V)) where E in the number of edges and V the number of vertexes'''
8282

8383
@classmethod
8484
def kruskal_code(cls):
85+
''' Returns the code for current class '''
8586
import inspect
8687
return inspect.getsource(cls.kruskal_mst)
8788

@@ -94,11 +95,11 @@ def topological_sort(self):
9495
for vertex in range(self.count):
9596
# Call the recursive function only if not visited
9697
if not visited[vertex]:
97-
self.topological_sort_rec(vertex, visited, stack)
98+
self._topological_sort_rec(vertex, visited, stack)
9899

99100
return stack
100101

101-
def topological_sort_rec(self, vertex, visited, stack):
102+
def _topological_sort_rec(self, vertex, visited, stack):
102103
''' Recursive function for topological Sort '''
103104
# Mark the current node in visited
104105
visited[vertex] = True
@@ -107,7 +108,7 @@ def topological_sort_rec(self, vertex, visited, stack):
107108
try:
108109
for adjacent_node in self.graph[vertex]:
109110
if visited[adjacent_node] == False:
110-
self.topological_sort_rec(adjacent_node, visited, stack)
111+
self._topological_sort_rec(adjacent_node, visited, stack)
111112
except KeyError:
112113
return
113114

@@ -121,6 +122,7 @@ def get_code(self):
121122

122123

123124
class CheckCycleDirectedGraph(object):
125+
''' Class to check cycle in directed graph '''
124126
def __init__(self):
125127
self.graph = {}
126128
self.count = 0
@@ -146,11 +148,11 @@ def check_cycle(self):
146148
stack = [False] * len(self.graph)
147149
for vertex in range(len(self.graph)):
148150
if visited[vertex] == False:
149-
if self.check_cycle_rec(visited, stack, vertex) == True:
151+
if self._check_cycle_rec(visited, stack, vertex) == True:
150152
return True
151153
return False
152154

153-
def check_cycle_rec(self, visited, stack, vertex):
155+
def _check_cycle_rec(self, visited, stack, vertex):
154156
''' Recursive function for finding the cycle '''
155157
# Mark the current node in visited and also add it to the stack
156158
visited[vertex] = True
@@ -159,7 +161,7 @@ def check_cycle_rec(self, visited, stack, vertex):
159161
# mark all adjacent nodes of the current node
160162
for adjacentNode in self.graph[vertex]:
161163
if visited[adjacentNode] == False:
162-
if self.check_cycle_rec(visited, stack, adjacentNode) == True:
164+
if self._check_cycle_rec(visited, stack, adjacentNode) == True:
163165
return True
164166
elif stack[adjacentNode] == True:
165167
return True
@@ -176,6 +178,7 @@ def get_code(self):
176178

177179

178180
class CheckCycleUndirectedGraph(object):
181+
''' Class to check cycle in undirected graph '''
179182
def __init__(self):
180183
self.graph = {}
181184
self.count = 0
@@ -202,19 +205,19 @@ def check_cycle(self):
202205
for vertex in range(len(self.graph)):
203206
# Call the recursive function only if not visited
204207
if visited[vertex] == False:
205-
if self.check_cycle_rec(visited, -1, vertex) == True:
208+
if self._check_cycle_rec(visited, -1, vertex) == True:
206209
return True
207210
return False
208211

209-
def check_cycle_rec(self, visited, parent, vertex):
212+
def _check_cycle_rec(self, visited, parent, vertex):
210213
''' Recursive function for finding the cycle '''
211214
# Mark the current node in visited
212215
visited[vertex] = True
213216

214217
# mark all adjacent nodes of the current node
215218
for adjacentNode in self.graph[vertex]:
216219
if visited[adjacentNode] == False:
217-
if self.check_cycle_rec(visited, vertex, adjacentNode) == True:
220+
if self._check_cycle_rec(visited, vertex, adjacentNode) == True:
218221
return True
219222
elif parent != adjacentNode:
220223
return True

pygorithm/data_structures/heap.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,22 @@
44

55
from pygorithm.data_structures import queue
66

7-
8-
# min-heap implementation as priority queue
97
class Heap(queue.Queue):
8+
''' min-heap implementation as queue '''
109
def parent_idx(self, idx):
10+
''' retrieve the parent '''
1111
return idx // 2
1212

1313
def left_child_idx(self, idx):
14+
''' retrieve the left child '''
1415
return (idx * 2) + 1
1516

1617
def right_child_idx(self, idx):
18+
''' retrieve the right child '''
1719
return (idx * 2) + 2
1820

1921
def insert(self, data):
22+
''' inserting an element in the heap '''
2023
super().enqueue(data)
2124
if self.rear >= 1: # heap may need to be fixed
2225
self.heapify_up()

pygorithm/data_structures/linked_list.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
# Linked List and Node can be accomodated in separate classes for convenience
55

66
class Node(object):
7-
# Each node has its data and a pointer that points to next node in the Linked List
7+
''' Node class for creating a node for linked list.
8+
Each node has its data and a pointer that points to next node in the Linked List
9+
'''
810
def __init__(self, data, next=None):
911
''' constructor '''
1012
self.data = data

pygorithm/data_structures/tree.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
# Node class to create a node for binary tree
55
class Node(object):
6+
''' Node class for creating a node for tree.
7+
Each node has its data and a pointer that points to next node in the Linked List
8+
'''
69
def __init__(self, data = None):
710
self.left = None
811
self.right = None
@@ -38,39 +41,39 @@ def get_code(self):
3841
import inspect
3942
return inspect.getsource(Node)
4043

41-
# BinaryTree class to create a binary tree
4244
class BinaryTree(object):
45+
''' BinaryTree class to create a binary tree '''
4346
def __init__(self):
4447
self._inorder = []
4548
self._preorder = []
4649
self._postorder = []
4750

48-
# in this we traverse first to the leftmost node, then print its data and then traverse for rightmost node
4951
def inorder(self, root):
5052
'''
5153
@type: root: Node object
54+
in this we traverse first to the leftmost node, then print its data and then traverse for rightmost node
5255
'''
5356
if root:
5457
self.inorder(root.get_left()) # traverse to leftmost child
5558
self._inorder.append(root.get_data()) # get the data of current node
5659
self.inorder(root.get_right()) # traverse to rightmost child
5760
return self._inorder
5861

59-
# in this we first print the root node and then traverse towards leftmost node and then to the rightmost node
6062
def preorder(self, root):
6163
'''
6264
@type: root: Node object
65+
in this we first print the root node and then traverse towards leftmost node and then to the rightmost node
6366
'''
6467
if root:
6568
self._preorder.append(root.get_data()) # get the data of current node
6669
self.preorder(root.get_left()) # traverse to leftmost child
6770
self.preorder(root.get_right()) # traverse to rightmost child
6871
return self._preorder
6972

70-
# in this we first traverse to the leftmost node and then to the rightmost node and then print the data
7173
def postorder(self, root):
7274
'''
7375
@type: root: Node object
76+
in this we first traverse to the leftmost node and then to the rightmost node and then print the data
7477
'''
7578
if root:
7679
self.postorder(root.get_left()) # traverse to leftmost child
@@ -85,6 +88,7 @@ def get_code(self):
8588
return inspect.getsource(BinaryTree)
8689

8790
class BSTNode(object):
91+
''' class for creating a node for Binary Search tree '''
8892
def __init__(self, data):
8993
self.data = data
9094
self.leftChild = None
@@ -141,6 +145,7 @@ def insert(self, data):
141145
return True
142146

143147
def min_val_bst_node(self, BSTNode):
148+
''' for returning the node with the lowest value '''
144149
current = BSTNode
145150

146151
# loop down to find the leftmost leaf
@@ -196,28 +201,29 @@ def find(self, data):
196201
def inorder(self, root):
197202
'''
198203
@type: root: Node object
204+
in this we first traverse to the leftmost node and then print the data and then move to the rightmost child
199205
'''
200206
if root:
201207
self.inorder(root.get_left()) # traverse to leftmost child
202208
self._inorder.append(root.get_data()) # get the data of current node
203209
self.inorder(root.get_right()) # traverse to rightmost child
204210
return self._inorder
205211

206-
# in this we first print the root node and then traverse towards leftmost node and then to the rightmost node
207212
def preorder(self, root):
208213
'''
209214
@type: root: Node object
215+
in this we first print the root node and then traverse towards leftmost node and then to the rightmost node
210216
'''
211217
if root:
212218
self._preorder.append(root.get_data()) # get the data of current node
213219
self.preorder(root.get_left()) # traverse to leftmost child
214220
self.preorder(root.get_right()) # traverse to rightmost child
215221
return self._preorder
216222

217-
# in this we first traverse to the leftmost node and then to the rightmost node and then print the data
218223
def postorder(self, root):
219224
'''
220225
@type: root: Node object
226+
in this we first traverse to the leftmost node and then to the rightmost node and then print the data
221227
'''
222228
if root:
223229
self.postorder(root.get_left()) # traverse to leftmost child

tests/kruskal_mst_tests.py

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)