diff --git a/pydatastructs/graphs/adjacency_list.py b/pydatastructs/graphs/adjacency_list.py index d596633db..c38f482d1 100644 --- a/pydatastructs/graphs/adjacency_list.py +++ b/pydatastructs/graphs/adjacency_list.py @@ -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 @@ -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): @@ -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) @@ -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) diff --git a/pydatastructs/graphs/adjacency_matrix.py b/pydatastructs/graphs/adjacency_matrix.py index 5c25e7fb0..ddec47f72 100644 --- a/pydatastructs/graphs/adjacency_matrix.py +++ b/pydatastructs/graphs/adjacency_matrix.py @@ -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): @@ -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), @@ -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) diff --git a/pydatastructs/graphs/tests/test_adjacency_matrix.py b/pydatastructs/graphs/tests/test_adjacency_matrix.py index 5fe8c72d5..3df819611 100644 --- a/pydatastructs/graphs/tests/test_adjacency_matrix.py +++ b/pydatastructs/graphs/tests/test_adjacency_matrix.py @@ -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 diff --git a/pydatastructs/utils/misc_util.py b/pydatastructs/utils/misc_util.py index 5f52d2e06..88b7a90e7 100644 --- a/pydatastructs/utils/misc_util.py +++ b/pydatastructs/utils/misc_util.py @@ -201,7 +201,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 @@ -213,11 +213,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. @@ -227,7 +227,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): """ @@ -247,7 +247,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. @@ -257,7 +257,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 diff --git a/pydatastructs/utils/tests/test_misc_util.py b/pydatastructs/utils/tests/test_misc_util.py index e144637fb..6cfd18b4c 100644 --- a/pydatastructs/utils/tests/test_misc_util.py +++ b/pydatastructs/utils/tests/test_misc_util.py @@ -18,8 +18,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)