Skip to content

Commit 0203273

Browse files
authored
Add AdjacencyMatrix implementation for graphs (#81)
1 parent 89e277f commit 0203273

File tree

5 files changed

+89
-7
lines changed

5 files changed

+89
-7
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
from pydatastructs.graphs.graph import Graph
2+
from pydatastructs.linear_data_structures import OneDimensionalArray
3+
from pydatastructs.utils.misc_util import AdjacencyMatrixGraphNode
4+
5+
__all__ = [
6+
'AdjacencyMatrix'
7+
]
8+
9+
class AdjacencyMatrix(Graph):
10+
"""
11+
Adjacency matrix implementation of graphs.
12+
13+
See also
14+
========
15+
16+
pydatastructs.graphs.graph.Graph
17+
"""
18+
def __new__(cls, *vertices):
19+
obj = object.__new__(cls)
20+
num_vertices = len(vertices)
21+
obj.vertices = OneDimensionalArray(
22+
AdjacencyMatrixGraphNode,
23+
num_vertices)
24+
for vertex in vertices:
25+
obj.vertices[vertex.name] = vertex
26+
obj.matrix = OneDimensionalArray(
27+
OneDimensionalArray,
28+
num_vertices)
29+
for i in range(num_vertices):
30+
obj.matrix[i] = OneDimensionalArray(
31+
bool,
32+
num_vertices)
33+
obj.matrix[i].fill(False)
34+
return obj
35+
36+
def is_adjacent(self, node1, node2):
37+
return self.matrix[node1][node2]
38+
39+
def neighbors(self, node):
40+
neighbors = []
41+
for i in range(self.matrix[node]._size):
42+
if self.matrix[node][i]:
43+
neighbors.append(self.vertices[i])
44+
return neighbors
45+
46+
def add_vertex(self, node):
47+
raise NotImplementedError("Currently we allow "
48+
"adjacency matrix for static graphs only")
49+
50+
def remove_vertex(self, node):
51+
raise NotImplementedError("Currently we allow "
52+
"adjacency matrix for static graphs only.")
53+
54+
def add_edge(self, source, target):
55+
self.matrix[source][target] = True
56+
57+
def remove_edge(self, source, target):
58+
self.matrix[source][target] = False

pydatastructs/graphs/graph.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Graph(object):
1313
implementation: str
1414
The implementation to be used for storing
1515
graph in memory.
16-
By default, 'adjacency_list'
16+
By default, 'adjacency_list'.
1717
vertices: AdjacencyListGraphNode(s)
1818
For AdjacencyList implementation vertices
1919
can be passed for initializing the graph.
@@ -48,6 +48,9 @@ def __new__(cls, *args, **kwargs):
4848
if implementation is 'adjacency_list':
4949
from pydatastructs.graphs.adjacency_list import AdjacencyList
5050
return AdjacencyList(*args)
51+
elif implementation is 'adjacency_matrix':
52+
from pydatastructs.graphs.adjacency_matrix import AdjacencyMatrix
53+
return AdjacencyMatrix(*args)
5154
else:
5255
raise NotImplementedError("%s implementation is not a part "
5356
"of the library currently."%(implementation))
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from pydatastructs.graphs import Graph
2+
from pydatastructs.utils import AdjacencyMatrixGraphNode
3+
4+
def test_AdjacencyMatrix():
5+
v_0 = AdjacencyMatrixGraphNode(0, 0)
6+
v_1 = AdjacencyMatrixGraphNode(1, 1)
7+
v_2 = AdjacencyMatrixGraphNode(2, 2)
8+
g = Graph(v_0, v_1, v_2, implementation='adjacency_matrix')
9+
g.add_edge(0, 1)
10+
g.add_edge(1, 2)
11+
g.add_edge(2, 0)
12+
assert g.is_adjacent(0, 1) is True
13+
assert g.is_adjacent(1, 2) is True
14+
assert g.is_adjacent(2, 0) is True
15+
assert g.is_adjacent(1, 0) is False
16+
assert g.is_adjacent(2, 1) is False
17+
assert g.is_adjacent(0, 2) is False
18+
neighbors = g.neighbors(0)
19+
assert neighbors == [v_1]
20+
g.remove_edge(0, 1)
21+
assert g.is_adjacent(0, 1) is False

pydatastructs/utils/misc_util.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,17 +190,17 @@ class AdjacencyMatrixGraphNode(GraphNode):
190190
Parameters
191191
==========
192192
193-
name: str
194-
The name of the node by which it is identified
195-
in the graph. Must be unique.
193+
name: int
194+
The index of the node in the AdjacencyMatrix.
196195
data
197196
The data to be stored at each graph node.
198197
"""
199198
__slots__ = ['name', 'data']
200199

201200
def __new__(cls, name, data):
202201
obj = GraphNode.__new__(cls)
203-
obj.name, obj.data = name, data
202+
obj.name, obj.data, obj.is_connected = \
203+
name, data, None
204204
return obj
205205

206206
class GraphEdge(object):

pydatastructs/utils/tests/test_misc_util.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ def test_AdjacencyListGraphNode():
1717
assert str(g) == "('g', 0)"
1818

1919
def test_AdjacencyMatrixGraphNode():
20-
g = AdjacencyMatrixGraphNode('g', 3)
21-
assert str(g) == "('g', 3)"
20+
g = AdjacencyMatrixGraphNode(1, 3)
21+
assert str(g) == "(1, 3)"
2222

2323
def test_GraphEdge():
2424
g_1 = AdjacencyListGraphNode('g_1', 1)

0 commit comments

Comments
 (0)