-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
Added algorithm to deeply clone a graph #9765
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
Merged
cclauss
merged 11 commits into
TheAlgorithms:master
from
dbring:add-clone-graph-algorithm
Oct 5, 2023
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
4c8450f
Added algorithm to deeply clone a graph
dbring 0d1134d
Fixed file name and removed a function call
dbring 712c35e
Removed nested function and fixed class parameter types
dbring ebf333a
Fixed doctests
dbring bfe956b
bug fix
dbring 1552e6f
Added class decorator
dbring 01b4473
Updated doctests and fixed precommit errors
dbring 88f73a5
Cleaned up code
dbring 20f5565
Simplified doctest
dbring 3d92f87
Added doctests
dbring d4ffda8
Code simplification
dbring File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
""" | ||
LeetCode 133. Clone Graph | ||
https://leetcode.com/problems/clone-graph/ | ||
|
||
Given a reference of a node in a connected undirected graph. | ||
|
||
Return a deep copy (clone) of the graph. | ||
|
||
Each node in the graph contains a value (int) and a list (List[Node]) of its | ||
neighbors. | ||
""" | ||
from dataclasses import dataclass | ||
|
||
|
||
@dataclass | ||
class Node: | ||
value: int = 0 | ||
neighbors: list["Node"] | None = None | ||
|
||
def __post_init__(self) -> None: | ||
""" | ||
>>> Node(3).neighbors | ||
[] | ||
""" | ||
self.neighbors = self.neighbors or [] | ||
|
||
def __hash__(self) -> int: | ||
""" | ||
>>> hash(Node(3)) != 0 | ||
True | ||
""" | ||
return id(self) | ||
|
||
|
||
def clone_graph(node: Node | None) -> Node | None: | ||
""" | ||
This function returns a clone of a connected undirected graph. | ||
>>> clone_graph(Node(1)) | ||
Node(value=1, neighbors=[]) | ||
>>> clone_graph(Node(1, [Node(2)])) | ||
Node(value=1, neighbors=[Node(value=2, neighbors=[])]) | ||
>>> clone_graph(None) is None | ||
True | ||
""" | ||
if not node: | ||
return None | ||
|
||
originals_to_clones = {} # map nodes to clones | ||
|
||
stack = [node] | ||
|
||
while stack: | ||
original = stack.pop() | ||
|
||
if original in originals_to_clones: | ||
continue | ||
|
||
originals_to_clones[original] = Node(original.value) | ||
|
||
for neighbor in original.neighbors or []: | ||
stack.append(neighbor) | ||
|
||
for original, clone in originals_to_clones.items(): | ||
for neighbor in original.neighbors or []: | ||
cloned_neighbor = originals_to_clones[neighbor] | ||
|
||
if not clone.neighbors: | ||
clone.neighbors = [] | ||
|
||
clone.neighbors.append(cloned_neighbor) | ||
|
||
return originals_to_clones[node] | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.