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

Refactored graphs #225

Merged
merged 1 commit into from
Mar 28, 2020
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
10 changes: 4 additions & 6 deletions pydatastructs/graphs/adjacency_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def __new__(cls, *vertices):
obj = object.__new__(cls)
for vertex in vertices:
obj.__setattr__(vertex.name, vertex)
obj.vertices = set([vertex.name for vertex in vertices])
obj.vertices = [vertex.name for vertex in vertices]
obj.edge_weights = dict()
return obj

Expand All @@ -32,7 +32,7 @@ def neighbors(self, node):
return [self.__getattribute__(name) for name in node.adjacent]

def add_vertex(self, node):
self.vertices.add(node.name)
self.vertices.append(node.name)
self.__setattr__(node.name, node)

def remove_vertex(self, name):
Expand All @@ -47,8 +47,7 @@ def remove_vertex(self, name):
def add_edge(self, source, target, cost=None):
source, target = self.__getattribute__(source), \
self.__getattribute__(target)
source.__setattr__(target.name, target)
source.adjacent.add(target.name)
source.add_adjacent_node(target.name)
if cost is not None:
self.edge_weights[source.name + "_" + target.name] = \
GraphEdge(source, target, cost)
Expand All @@ -61,7 +60,6 @@ def get_edge(self, source, target):
def remove_edge(self, source, target):
source, target = self.__getattribute__(source), \
self.__getattribute__(target)
source.adjacent.remove(target.name)
delattr(source, target.name)
source.remove_adjacent_node(target.name)
self.edge_weights.pop(source.name + "_" + target.name,
None)
30 changes: 15 additions & 15 deletions pydatastructs/graphs/adjacency_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,28 @@ class AdjacencyMatrix(Graph):
"""
def __new__(cls, *vertices):
obj = object.__new__(cls)
num_vertices = len(vertices)
obj.vertices = [vertex.name for vertex in vertices]
for vertex in vertices:
obj.__setattr__(str(vertex.name), vertex)
obj.matrix = OneDimensionalArray(
OneDimensionalArray,
num_vertices)
for i in range(num_vertices):
obj.matrix[i] = OneDimensionalArray(
bool,
num_vertices)
obj.matrix[i].fill(False)
obj.__setattr__(vertex.name, vertex)
obj.matrix = dict()
for vertex in vertices:
obj.matrix[vertex.name] = dict()
obj.edge_weights = dict()
return obj

def is_adjacent(self, node1, node2):
return self.matrix[node1][node2]
node1, node2 = str(node1), str(node2)
row = self.matrix.get(node1, dict())
return row.get(node2, False) is not False

def neighbors(self, node):
node = str(node)
neighbors = []
for i in range(self.matrix[node]._size):
if self.matrix[node][i]:
row = self.matrix.get(node, dict())
for node, presence in row.items():
if presence:
neighbors.append(self.__getattribute__(
str(self.vertices[i])))
str(node)))
return neighbors

def add_vertex(self, node):
Expand All @@ -52,8 +50,9 @@ def remove_vertex(self, node):
"adjacency matrix for static graphs only.")

def add_edge(self, source, target, cost=None):
self.matrix[source][target] = True
source, target = str(source), str(target)
print(source, target)
self.matrix[source][target] = True
if cost is not None:
self.edge_weights[source + "_" + target] = \
GraphEdge(self.__getattribute__(source),
Expand All @@ -66,5 +65,6 @@ def get_edge(self, source, target):
None)

def remove_edge(self, source, target):
source, target = str(source), str(target)
self.matrix[source][target] = False
self.edge_weights.pop(str(source) + "_" + str(target), None)
6 changes: 3 additions & 3 deletions pydatastructs/graphs/tests/test_adjacency_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ def test_AdjacencyMatrix():
e1 = g.get_edge(0, 1)
e2 = g.get_edge(1, 2)
e3 = g.get_edge(2, 0)
assert (e1.source.name, e1.target.name) == (0, 1)
assert (e2.source.name, e2.target.name) == (1, 2)
assert (e3.source.name, e3.target.name) == (2, 0)
assert (e1.source.name, e1.target.name) == ('0', '1')
assert (e2.source.name, e2.target.name) == ('1', '2')
assert (e3.source.name, e3.target.name) == ('2', '0')
assert g.is_adjacent(0, 1) is True
assert g.is_adjacent(1, 2) is True
assert g.is_adjacent(2, 0) is True
Expand Down
14 changes: 7 additions & 7 deletions pydatastructs/utils/misc_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ class AdjacencyListGraphNode(GraphNode):
in the graph. Must be unique.
data
The data to be stored at each graph node.
adjacency_list: iterator
adjacency_list: list
Any valid iterator to initialize the adjacent
nodes of the current node.
Optional, by default, None
Expand All @@ -210,11 +210,11 @@ def __new__(cls, name, data=None, adjacency_list=None):
if adjacency_list is not None:
for node in adjacency_list:
obj.__setattr__(node.name, node)
obj.adjacent = set(adjacency_list) if adjacency_list is not None \
else set()
obj.adjacent = adjacency_list if adjacency_list is not None \
else []
return obj

def add_adjacent_node(self, name, data):
def add_adjacent_node(self, name, data=None):
"""
Adds adjacent node to the current node's
adjacency list with given name and data.
Expand All @@ -224,7 +224,7 @@ def add_adjacent_node(self, name, data):
else:
new_node = AdjacencyListGraphNode(name, data)
self.__setattr__(new_node.name, new_node)
self.adjacent.add(new_node.name)
self.adjacent.append(new_node.name)

def remove_adjacent_node(self, name):
"""
Expand All @@ -244,7 +244,7 @@ class AdjacencyMatrixGraphNode(GraphNode):
Parameters
==========

name: int
name: str
The index of the node in the AdjacencyMatrix.
data
The data to be stored at each graph node.
Expand All @@ -254,7 +254,7 @@ class AdjacencyMatrixGraphNode(GraphNode):
def __new__(cls, name, data=None):
obj = GraphNode.__new__(cls)
obj.name, obj.data, obj.is_connected = \
int(name), data, None
str(name), data, None
obj._impl = 'adjacency_matrix'
return obj

Expand Down
4 changes: 2 additions & 2 deletions pydatastructs/utils/tests/test_misc_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ def test_AdjacencyListGraphNode():
assert str(g) == "('g', 0)"

def test_AdjacencyMatrixGraphNode():
g = AdjacencyMatrixGraphNode(1, 3)
assert str(g) == "(1, 3)"
g = AdjacencyMatrixGraphNode("1", 3)
assert str(g) == "('1', 3)"

def test_GraphEdge():
g_1 = AdjacencyListGraphNode('g_1', 1)
Expand Down