From the course: Data Structures in JavaScript: Trees and Graphs
Unlock this course with a free trial
Join today to access over 25,500 courses taught by industry experts.
Solution: Clone graph
From the course: Data Structures in JavaScript: Trees and Graphs
Solution: Clone graph
(upbeat music) - [Instructor] In this lesson, we're walking through two possible solutions for the clone graph problem, one using DFS and one using BFS. You are given a reference to a node in a connected undirected graph. Each node has two things, a unique value, and a list of neighboring nodes. Your task is to create a deep copy of the entire graph. This means that, one, every node in the original graph must be copied into a new graph. Two, the new graph should have the same structure and values. Three, no original node can be reused. All nodes in the clone must be newly created. This is a classic graph traversal problem. We can solve it using neither DFS or BFS. As we explore this graph, we must also rebuild it simultaneously. That means we'll need to traverse each node, clone it, and reconnect it to its clone neighbors, all while tracking visited nodes to avoid cloning the same node more than once, and to prevent infinite cycles in cyclic graphs. Let's look at example one. Each…
Contents
-
-
-
-
-
-
-
(Locked)
Depth-first search concept4m 42s
-
Depth-first search implementation3m 3s
-
(Locked)
Breadth-first search concept4m 57s
-
(Locked)
Breadth-first search implementation2m 31s
-
(Locked)
Complexity analysis of DFS and BFS2m 33s
-
(Locked)
Solution: Number of islands5m 18s
-
(Locked)
Solution: Clone graph6m 4s
-
(Locked)
-
-