Skip to content

Enabling Support for Dynamic Graphs: Allow adding and removing vertices #595

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
43 changes: 39 additions & 4 deletions pydatastructs/graphs/adjacency_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,47 @@ def neighbors(self, node):
return neighbors

def add_vertex(self, node):
raise NotImplementedError("Currently we allow "
"adjacency matrix for static graphs only")
if node.name in self.matrix:
raise ValueError("Vertex %s already exists in the graph." % node.name)
self.vertices.append(node.name)
setattr(self, node.name, node)
self.matrix[node.name] = {}

def remove_vertex(self, node):
raise NotImplementedError("Currently we allow "
"adjacency matrix for static graphs only.")
node = str(node)
if node not in self.matrix:
raise ValueError("Vertex '%s' is not present in the graph." % node)

# first we need to remove the edges involving the `node`

# removing records from dict while iterating over them is tricky
# so we'll first identify which edges to remove first

edges_to_remove = []

for target in self.matrix[node]:
if self.matrix[node].get(target, False):
edges_to_remove.append((node, target))

for source in self.vertices:
if self.matrix[source].get(node):
edges_to_remove.append((source, node))

# remove the identified edge weights
for source, target in edges_to_remove:
edge_key = str(source) + "_" + str(target)
self.edge_weights.pop(edge_key)

self.vertices.remove(node)
# eliminate all outgoing edges
self.matrix.pop(node, None)

# eliminate all incoming edges
for source in self.vertices:
self.matrix[source].pop(node, None)

if hasattr(self, node):
delattr(self, node)

def add_edge(self, source, target, cost=None):
source, target = str(source), str(target)
Expand Down
20 changes: 20 additions & 0 deletions pydatastructs/graphs/tests/test_adjacency_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,23 @@ def test_AdjacencyMatrix():
assert raises(ValueError, lambda: g.add_edge('v', 'x'))
assert raises(ValueError, lambda: g.add_edge(2, 3))
assert raises(ValueError, lambda: g.add_edge(3, 2))
assert g.num_vertices() == 3
v_3 = AdjacencyMatrixGraphNode(3, 3)
g.add_vertex(v_3)
assert g.num_vertices() == 4
g.add_edge(3, 1, 0)
g.add_edge(3, 2, 0)
g.add_edge(2, 3, 0)
assert g.is_adjacent(3, 1) is True
assert g.is_adjacent(0, 2) is False
assert g.is_adjacent(1, 3) is False
assert g.is_adjacent(2, 3) is True
assert g.is_adjacent(3, 2) is True
neighbors = g.neighbors(3)
assert neighbors == [v_1, v_2]
neighbors = g.neighbors(2)
assert neighbors == [v_0, v_3]
g.remove_vertex(3)
neighbors = g.neighbors(2)
assert neighbors == [v_0]
assert g.num_vertices() == 3
Loading