Skip to content

Commit

Permalink
Polished and added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
czgdp1807 committed Oct 31, 2021
1 parent 0ff2909 commit 32e07f5
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 23 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ Keep contributing!!
Thanks to these wonderful people ✨✨:

<table>
<tr>
<td>
<a href="https://github.com/codezonediitj/pydatastructs/graphs/contributors">
<img src="https://contrib.rocks/image?repo=codezonediitj/pydatastructs" />
</a>
</td>
</tr>
<tr>
<td>
<a href="https://github.com/codezonediitj/pydatastructs/graphs/contributors">
<img src="https://contrib.rocks/image?repo=codezonediitj/pydatastructs" />
</a>
</td>
</tr>
</table>
19 changes: 10 additions & 9 deletions pydatastructs/graphs/adjacency_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,18 @@ def remove_vertex(self, name):
node_obj.adjacent.remove(name)

def add_edge(self, source, target, cost=None):
error_msg = "Vertex %s is not present in the graph. \
Call Graph.add_vertex() to add a new \
vertex. Graph.add_edge is only responsible \
for adding edges and it will not add new \
vertices on its own. This is done to maintain \
clear separation between the functionality of \
the two methods."
source, target = str(source), str(target)
error_msg = ("Vertex %s is not present in the graph."
"Call Graph.add_vertex to add a new"
"vertex. Graph.add_edge is only responsible"
"for adding edges and it will not add new"
"vertices on its own. This is done to maintain"
"clear separation between the functionality of"
"these two methods.")
if not hasattr(self, source):
raise ValueError(error_msg %(source))
raise ValueError(error_msg % (source))
if not hasattr(self, target):
raise ValueError(error_msg %(target))
raise ValueError(error_msg % (target))

source, target = self.__getattribute__(source), \
self.__getattribute__(target)
Expand Down
12 changes: 12 additions & 0 deletions pydatastructs/graphs/adjacency_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ def remove_vertex(self, node):

def add_edge(self, source, target, cost=None):
source, target = str(source), str(target)
error_msg = ("Vertex %s is not present in the graph."
"Call Graph.add_vertex to add a new"
"vertex. Graph.add_edge is only responsible"
"for adding edges and it will not add new"
"vertices on its own. This is done to maintain"
"clear separation between the functionality of"
"these two methods.")
if source not in self.matrix:
raise ValueError(error_msg % (source))
if target not in self.matrix:
raise ValueError(error_msg % (target))

self.matrix[source][target] = True
if cost is not None:
self.edge_weights[source + "_" + target] = \
Expand Down
12 changes: 5 additions & 7 deletions pydatastructs/graphs/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,11 @@ class Graph(object):
Note
====
Steps to create a graph:
1. Create nodes (AdjacencyListGraphNode or AdjacencyListMatrixNode)
2. Add nodes to the graph
3. Add edges b/w these nodes
Make sure to create nodes (AdjacencyListGraphNode or AdjacencyMatrixGraphNode)
and them in your graph using Graph.add_vertex before adding edges whose
end points require either of the nodes that you added. In other words,
Graph.add_edge doesn't add new nodes on its own if the input
nodes are not already present in the Graph.
"""

Expand Down
4 changes: 4 additions & 0 deletions pydatastructs/graphs/tests/test_adjacency_list.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from pydatastructs.graphs import Graph
from pydatastructs.utils import AdjacencyListGraphNode
from pydatastructs.utils.raises_util import raises

def test_adjacency_list():
v_1 = AdjacencyListGraphNode('v_1', 1)
Expand Down Expand Up @@ -38,3 +39,6 @@ def test_adjacency_list():
g.remove_vertex('v')
assert g.is_adjacent('v_2', 'v') is False
assert g.is_adjacent('v_3', 'v') is False

assert raises(ValueError, lambda: g.add_edge('u', 'v'))
assert raises(ValueError, lambda: g.add_edge('v', 'x'))
4 changes: 4 additions & 0 deletions pydatastructs/graphs/tests/test_adjacency_matrix.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from pydatastructs.graphs import Graph
from pydatastructs.utils import AdjacencyMatrixGraphNode
from pydatastructs.utils.raises_util import raises

def test_AdjacencyMatrix():
v_0 = AdjacencyMatrixGraphNode(0, 0)
Expand All @@ -25,3 +26,6 @@ def test_AdjacencyMatrix():
assert neighbors == [v_1]
g.remove_edge(0, 1)
assert g.is_adjacent(0, 1) is False

assert raises(ValueError, lambda: g.add_edge('u', 'v'))
assert raises(ValueError, lambda: g.add_edge('v', 'x'))

0 comments on commit 32e07f5

Please sign in to comment.