Skip to content

Commit

Permalink
Add AdjacencyMatrix implementation for graphs (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
czgdp1807 authored Jan 13, 2020
1 parent 89e277f commit 0203273
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 7 deletions.
58 changes: 58 additions & 0 deletions pydatastructs/graphs/adjacency_matrix.py
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
5 changes: 4 additions & 1 deletion pydatastructs/graphs/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Graph(object):
implementation: str
The implementation to be used for storing
graph in memory.
By default, 'adjacency_list'
By default, 'adjacency_list'.
vertices: AdjacencyListGraphNode(s)
For AdjacencyList implementation vertices
can be passed for initializing the graph.
Expand Down Expand Up @@ -48,6 +48,9 @@ def __new__(cls, *args, **kwargs):
if implementation is 'adjacency_list':
from pydatastructs.graphs.adjacency_list import AdjacencyList
return AdjacencyList(*args)
elif implementation is 'adjacency_matrix':
from pydatastructs.graphs.adjacency_matrix import AdjacencyMatrix
return AdjacencyMatrix(*args)
else:
raise NotImplementedError("%s implementation is not a part "
"of the library currently."%(implementation))
Expand Down
21 changes: 21 additions & 0 deletions pydatastructs/graphs/tests/test_adjacency_matrix.py
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
8 changes: 4 additions & 4 deletions pydatastructs/utils/misc_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,17 +190,17 @@ class AdjacencyMatrixGraphNode(GraphNode):
Parameters
==========
name: str
The name of the node by which it is identified
in the graph. Must be unique.
name: int
The index of the node in the AdjacencyMatrix.
data
The data to be stored at each graph node.
"""
__slots__ = ['name', 'data']

def __new__(cls, name, data):
obj = GraphNode.__new__(cls)
obj.name, obj.data = name, data
obj.name, obj.data, obj.is_connected = \
name, data, None
return obj

class GraphEdge(object):
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('g', 3)
assert str(g) == "('g', 3)"
g = AdjacencyMatrixGraphNode(1, 3)
assert str(g) == "(1, 3)"

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

0 comments on commit 0203273

Please sign in to comment.