Skip to content

Commit 6700871

Browse files
Fix snapshot storage and retrieval in Graph class
1 parent e844a21 commit 6700871

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

pydatastructs/graphs/graph.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11

22
from pydatastructs.utils.misc_util import Backend, raise_if_backend_is_not_python
3+
from pydatastructs.utils.misc_util import GraphEdge
4+
from pydatastructs.utils import AdjacencyListGraphNode
5+
36

47
__all__ = [
58
'Graph'
@@ -94,7 +97,16 @@ def __new__(cls, *args, **kwargs):
9497
def add_snapshot(self):
9598
"""Automatically assigns timestamps using system time."""
9699
timestamp = int(time.time())
97-
self.snapshots[timestamp] = copy.deepcopy(self)
100+
snapshot_copy = self.__class__(implementation=self._impl)
101+
for vertex_name in self.vertices:
102+
snapshot_copy.add_vertex(AdjacencyListGraphNode(vertex_name))
103+
snapshot_copy.edge_weights = {
104+
key: GraphEdge(edge.source, edge.target, edge.value)
105+
for key, edge in self.edge_weights.items()
106+
}
107+
for key, edge in snapshot_copy.edge_weights.items():
108+
snapshot_copy.__getattribute__(edge.source.name).add_adjacent_node(edge.target.name)
109+
self.snapshots[timestamp] = snapshot_copy
98110
def get_snapshot(self, timestamp: int):
99111
"""Retrieves a past version of the graph if the timestamp exists."""
100112
if timestamp not in self.snapshots:

0 commit comments

Comments
 (0)