Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs modification for better clarification #424

Merged
merged 6 commits into from
Oct 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: 16 additions & 3 deletions pydatastructs/graphs/adjacency_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ def __new__(cls, *vertices):
@classmethod
def methods(self):
return ['is_adjacent', 'neighbors',
'add_vertex', 'remove_vertex', 'add_edge',
'get_edge', 'remove_edge', '__new__']
'add_vertex', 'remove_vertex', 'add_edge',
'get_edge', 'remove_edge', '__new__']

def is_adjacent(self, node1, node2):
node1 = self.__getattribute__(node1)
Expand All @@ -52,8 +52,21 @@ def remove_vertex(self, name):
node_obj.adjacent.remove(name)

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 not hasattr(self, source):
raise ValueError(error_msg % (source))
if not hasattr(self, target):
raise ValueError(error_msg % (target))

source, target = self.__getattribute__(source), \
self.__getattribute__(target)
self.__getattribute__(target)
source.add_adjacent_node(target.name)
if cost is not None:
self.edge_weights[source.name + "_" + target.name] = \
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
15 changes: 13 additions & 2 deletions pydatastructs/graphs/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ class Graph(object):
==========

.. [1] https://en.wikipedia.org/wiki/Graph_(abstract_data_type)

Note
====

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.

"""

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

def add_vertex(self, node):
"""
Adds the input vertex to the node.
Adds the input vertex to the node, or does nothing
if the input vertex is already in the graph.
"""
raise NotImplementedError(
"This is an abstract method.")

def remove_vertex(self, node):
"""
Removes the input vertex along with all the edges
pointing towards to it.
pointing towards it.
"""
raise NotImplementedError(
"This is an abstract method.")
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'))
6 changes: 4 additions & 2 deletions pydatastructs/linear_data_structures/arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ def __str__(self) -> str:

class OneDimensionalArray(Array):
'''
Represents one dimensional arrays.
Represents one dimensional static arrays of
fixed size.

Parameters
==========
Expand Down Expand Up @@ -269,7 +270,8 @@ class DynamicArray(Array):

class DynamicOneDimensionalArray(DynamicArray, OneDimensionalArray):
"""
Represents dynamic one dimensional arrays.
Represents resizable and dynamic one
dimensional arrays.

Parameters
==========
Expand Down