Skip to content

Commit 94e96a2

Browse files
authored
Doc improvements (ODA, DODA) and addition of checks in graph (#424)
1 parent 9f22c7d commit 94e96a2

File tree

7 files changed

+60
-14
lines changed

7 files changed

+60
-14
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,11 @@ Keep contributing!!
101101
Thanks to these wonderful people ✨✨:
102102

103103
<table>
104-
<tr>
105-
<td>
106-
<a href="https://github.com/codezonediitj/pydatastructs/graphs/contributors">
107-
<img src="https://contrib.rocks/image?repo=codezonediitj/pydatastructs" />
108-
</a>
109-
</td>
110-
</tr>
104+
<tr>
105+
<td>
106+
<a href="https://github.com/codezonediitj/pydatastructs/graphs/contributors">
107+
<img src="https://contrib.rocks/image?repo=codezonediitj/pydatastructs" />
108+
</a>
109+
</td>
110+
</tr>
111111
</table>

pydatastructs/graphs/adjacency_list.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ def __new__(cls, *vertices):
2626
@classmethod
2727
def methods(self):
2828
return ['is_adjacent', 'neighbors',
29-
'add_vertex', 'remove_vertex', 'add_edge',
30-
'get_edge', 'remove_edge', '__new__']
29+
'add_vertex', 'remove_vertex', 'add_edge',
30+
'get_edge', 'remove_edge', '__new__']
3131

3232
def is_adjacent(self, node1, node2):
3333
node1 = self.__getattribute__(node1)
@@ -52,8 +52,21 @@ def remove_vertex(self, name):
5252
node_obj.adjacent.remove(name)
5353

5454
def add_edge(self, source, target, cost=None):
55+
source, target = str(source), str(target)
56+
error_msg = ("Vertex %s is not present in the graph."
57+
"Call Graph.add_vertex to add a new"
58+
"vertex. Graph.add_edge is only responsible"
59+
"for adding edges and it will not add new"
60+
"vertices on its own. This is done to maintain"
61+
"clear separation between the functionality of"
62+
"these two methods.")
63+
if not hasattr(self, source):
64+
raise ValueError(error_msg % (source))
65+
if not hasattr(self, target):
66+
raise ValueError(error_msg % (target))
67+
5568
source, target = self.__getattribute__(source), \
56-
self.__getattribute__(target)
69+
self.__getattribute__(target)
5770
source.add_adjacent_node(target.name)
5871
if cost is not None:
5972
self.edge_weights[source.name + "_" + target.name] = \

pydatastructs/graphs/adjacency_matrix.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,18 @@ def remove_vertex(self, node):
5757

5858
def add_edge(self, source, target, cost=None):
5959
source, target = str(source), str(target)
60+
error_msg = ("Vertex %s is not present in the graph."
61+
"Call Graph.add_vertex to add a new"
62+
"vertex. Graph.add_edge is only responsible"
63+
"for adding edges and it will not add new"
64+
"vertices on its own. This is done to maintain"
65+
"clear separation between the functionality of"
66+
"these two methods.")
67+
if source not in self.matrix:
68+
raise ValueError(error_msg % (source))
69+
if target not in self.matrix:
70+
raise ValueError(error_msg % (target))
71+
6072
self.matrix[source][target] = True
6173
if cost is not None:
6274
self.edge_weights[source + "_" + target] = \

pydatastructs/graphs/graph.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ class Graph(object):
4949
==========
5050
5151
.. [1] https://en.wikipedia.org/wiki/Graph_(abstract_data_type)
52+
53+
Note
54+
====
55+
56+
Make sure to create nodes (AdjacencyListGraphNode or AdjacencyMatrixGraphNode)
57+
and them in your graph using Graph.add_vertex before adding edges whose
58+
end points require either of the nodes that you added. In other words,
59+
Graph.add_edge doesn't add new nodes on its own if the input
60+
nodes are not already present in the Graph.
61+
5262
"""
5363

5464
__slots__ = ['_impl']
@@ -89,15 +99,16 @@ def neighbors(self, node):
8999

90100
def add_vertex(self, node):
91101
"""
92-
Adds the input vertex to the node.
102+
Adds the input vertex to the node, or does nothing
103+
if the input vertex is already in the graph.
93104
"""
94105
raise NotImplementedError(
95106
"This is an abstract method.")
96107

97108
def remove_vertex(self, node):
98109
"""
99110
Removes the input vertex along with all the edges
100-
pointing towards to it.
111+
pointing towards it.
101112
"""
102113
raise NotImplementedError(
103114
"This is an abstract method.")

pydatastructs/graphs/tests/test_adjacency_list.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from pydatastructs.graphs import Graph
22
from pydatastructs.utils import AdjacencyListGraphNode
3+
from pydatastructs.utils.raises_util import raises
34

45
def test_adjacency_list():
56
v_1 = AdjacencyListGraphNode('v_1', 1)
@@ -38,3 +39,6 @@ def test_adjacency_list():
3839
g.remove_vertex('v')
3940
assert g.is_adjacent('v_2', 'v') is False
4041
assert g.is_adjacent('v_3', 'v') is False
42+
43+
assert raises(ValueError, lambda: g.add_edge('u', 'v'))
44+
assert raises(ValueError, lambda: g.add_edge('v', 'x'))

pydatastructs/graphs/tests/test_adjacency_matrix.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from pydatastructs.graphs import Graph
22
from pydatastructs.utils import AdjacencyMatrixGraphNode
3+
from pydatastructs.utils.raises_util import raises
34

45
def test_AdjacencyMatrix():
56
v_0 = AdjacencyMatrixGraphNode(0, 0)
@@ -25,3 +26,6 @@ def test_AdjacencyMatrix():
2526
assert neighbors == [v_1]
2627
g.remove_edge(0, 1)
2728
assert g.is_adjacent(0, 1) is False
29+
30+
assert raises(ValueError, lambda: g.add_edge('u', 'v'))
31+
assert raises(ValueError, lambda: g.add_edge('v', 'x'))

pydatastructs/linear_data_structures/arrays.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ def __str__(self) -> str:
1515

1616
class OneDimensionalArray(Array):
1717
'''
18-
Represents one dimensional arrays.
18+
Represents one dimensional static arrays of
19+
fixed size.
1920
2021
Parameters
2122
==========
@@ -269,7 +270,8 @@ class DynamicArray(Array):
269270

270271
class DynamicOneDimensionalArray(DynamicArray, OneDimensionalArray):
271272
"""
272-
Represents dynamic one dimensional arrays.
273+
Represents resizable and dynamic one
274+
dimensional arrays.
273275
274276
Parameters
275277
==========

0 commit comments

Comments
 (0)