-
Notifications
You must be signed in to change notification settings - Fork 271
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add AdjacencyMatrix implementation for graphs (#81)
- Loading branch information
Showing
5 changed files
with
89 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from pydatastructs.graphs.graph import Graph | ||
from pydatastructs.linear_data_structures import OneDimensionalArray | ||
from pydatastructs.utils.misc_util import AdjacencyMatrixGraphNode | ||
|
||
__all__ = [ | ||
'AdjacencyMatrix' | ||
] | ||
|
||
class AdjacencyMatrix(Graph): | ||
""" | ||
Adjacency matrix implementation of graphs. | ||
See also | ||
======== | ||
pydatastructs.graphs.graph.Graph | ||
""" | ||
def __new__(cls, *vertices): | ||
obj = object.__new__(cls) | ||
num_vertices = len(vertices) | ||
obj.vertices = OneDimensionalArray( | ||
AdjacencyMatrixGraphNode, | ||
num_vertices) | ||
for vertex in vertices: | ||
obj.vertices[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) | ||
return obj | ||
|
||
def is_adjacent(self, node1, node2): | ||
return self.matrix[node1][node2] | ||
|
||
def neighbors(self, node): | ||
neighbors = [] | ||
for i in range(self.matrix[node]._size): | ||
if self.matrix[node][i]: | ||
neighbors.append(self.vertices[i]) | ||
return neighbors | ||
|
||
def add_vertex(self, node): | ||
raise NotImplementedError("Currently we allow " | ||
"adjacency matrix for static graphs only") | ||
|
||
def remove_vertex(self, node): | ||
raise NotImplementedError("Currently we allow " | ||
"adjacency matrix for static graphs only.") | ||
|
||
def add_edge(self, source, target): | ||
self.matrix[source][target] = True | ||
|
||
def remove_edge(self, source, target): | ||
self.matrix[source][target] = False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from pydatastructs.graphs import Graph | ||
from pydatastructs.utils import AdjacencyMatrixGraphNode | ||
|
||
def test_AdjacencyMatrix(): | ||
v_0 = AdjacencyMatrixGraphNode(0, 0) | ||
v_1 = AdjacencyMatrixGraphNode(1, 1) | ||
v_2 = AdjacencyMatrixGraphNode(2, 2) | ||
g = Graph(v_0, v_1, v_2, implementation='adjacency_matrix') | ||
g.add_edge(0, 1) | ||
g.add_edge(1, 2) | ||
g.add_edge(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 | ||
assert g.is_adjacent(1, 0) is False | ||
assert g.is_adjacent(2, 1) is False | ||
assert g.is_adjacent(0, 2) is False | ||
neighbors = g.neighbors(0) | ||
assert neighbors == [v_1] | ||
g.remove_edge(0, 1) | ||
assert g.is_adjacent(0, 1) is False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters