Skip to content

Commit 32e07f5

Browse files
committed
Polished and added tests
1 parent 0ff2909 commit 32e07f5

File tree

6 files changed

+42
-23
lines changed

6 files changed

+42
-23
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: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,18 @@ def remove_vertex(self, name):
5252
node_obj.adjacent.remove(name)
5353

5454
def add_edge(self, source, target, cost=None):
55-
error_msg = "Vertex %s is not present in the graph. \
56-
Call Graph.add_vertex() to add a new \
57-
vertex. Graph.add_edge is only responsible \
58-
for adding edges and it will not add new \
59-
vertices on its own. This is done to maintain \
60-
clear separation between the functionality of \
61-
the two methods."
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.")
6263
if not hasattr(self, source):
63-
raise ValueError(error_msg %(source))
64+
raise ValueError(error_msg % (source))
6465
if not hasattr(self, target):
65-
raise ValueError(error_msg %(target))
66+
raise ValueError(error_msg % (target))
6667

6768
source, target = self.__getattribute__(source), \
6869
self.__getattribute__(target)

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: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,11 @@ class Graph(object):
5353
Note
5454
====
5555
56-
Steps to create a graph:
57-
58-
1. Create nodes (AdjacencyListGraphNode or AdjacencyListMatrixNode)
59-
60-
2. Add nodes to the graph
61-
62-
3. Add edges b/w these nodes
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.
6361
6462
"""
6563

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'))

0 commit comments

Comments
 (0)