forked from UTSAVS26/PyVerse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graph_Cloning.py
60 lines (47 loc) · 1.47 KB
/
Graph_Cloning.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
class Node:
def __init__(self, _val):
self.val = _val
self.neighbors = []
class Solution:
def __init__(self):
self.visited = {}
def cloneGraph(self, node):
if not node:
return None
if node in self.visited:
return self.visited[node]
cloneNode = Node(node.val)
self.visited[node] = cloneNode
for neighbor in node.neighbors:
cloneNode.neighbors.append(self.cloneGraph(neighbor))
return cloneNode
def printGraph(node, printed):
if not node or printed.get(node, False):
return
printed[node] = True # Mark this node as printed
print(f"Node: {node.val} Neighbors: ", end="")
for neighbor in node.neighbors:
print(neighbor.val, end=" ")
print()
for neighbor in node.neighbors:
printGraph(neighbor, printed)
if __name__ == "__main__":
node0 = Node(0)
node1 = Node(1)
node2 = Node(2)
node0.neighbors.append(node1)
node0.neighbors.append(node2)
node1.neighbors.append(node0)
node1.neighbors.append(node2)
node2.neighbors.append(node0)
node2.neighbors.append(node1)
solution = Solution()
clonedGraph = solution.cloneGraph(node0)
# Print the original graph
print("Original graph:")
printed = {}
printGraph(node0, printed)
# Print the cloned graph
print("\nCloned graph:")
printed.clear() # Reset printed map for cloned graph
printGraph(clonedGraph, printed)