Skip to content

Commit b0c3136

Browse files
authored
Removed redundancy in Graph API (#144)
1 parent b257d5a commit b0c3136

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

pydatastructs/graphs/graph.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ class Graph(object):
1212
1313
implementation: str
1414
The implementation to be used for storing
15-
graph in memory.
15+
graph in memory. It can be figured out
16+
from type of the vertices(if passed at construction).
1617
By default, 'adjacency_list'.
1718
vertices: AdjacencyListGraphNode(s)
1819
For AdjacencyList implementation vertices
@@ -47,7 +48,8 @@ class Graph(object):
4748
__slots__ = ['_impl']
4849

4950
def __new__(cls, *args, **kwargs):
50-
implementation = kwargs.get('implementation', 'adjacency_list')
51+
default_impl = args[0]._impl if args else 'adjacency_list'
52+
implementation = kwargs.get('implementation', default_impl)
5153
if implementation is 'adjacency_list':
5254
from pydatastructs.graphs.adjacency_list import AdjacencyList
5355
obj = AdjacencyList(*args)

pydatastructs/utils/misc_util.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ class AdjacencyListGraphNode(GraphNode):
153153
def __new__(cls, name, data=None, adjacency_list=None):
154154
obj = GraphNode.__new__(cls)
155155
obj.name, obj.data = str(name), data
156+
obj._impl = 'adjacency_list'
156157
if adjacency_list is not None:
157158
for node in adjacency_list:
158159
obj.__setattr__(node.name, node)
@@ -201,6 +202,7 @@ def __new__(cls, name, data=None):
201202
obj = GraphNode.__new__(cls)
202203
obj.name, obj.data, obj.is_connected = \
203204
int(name), data, None
205+
obj._impl = 'adjacency_matrix'
204206
return obj
205207

206208
class GraphEdge(object):

0 commit comments

Comments
 (0)