(Leetcode) [Graph] Clone graph, and BFS

Question

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 and a list of its neighbors.

class Node {
    public int val;
    public List<Node> neighbors;
}


Solution

// Definition for a Node.
class Node {
public:
    int val;
    vector<Node*> neighbors;
    Node() {
        val = 0;
        neighbors = vector<Node*>();
    }
    Node(int _val) {
        val = _val;
        neighbors = vector<Node*>();
    }
    Node(int _val, vector<Node*> _neighbors) {
        val = _val;
        neighbors = _neighbors;
    }
};

Node* cloneGraph(Node* node) {
    if (node==nullptr)
        return nullptr;
    
    // map to keep track of all the nodes which have already been created
    map<Node*, Node*> copies;
    queue<Node*> q;

    q.push(node);
    // copy without its neighbours
    Node* copy = new Node(node->val, {});

    // key = address of original node
    // value = address of new node
    copies[node] = copy;
    while (!q.empty()) {
        Node* u = q.front();
        q.pop();
        vector<Node*> v = u->neighbors;
        for (int i=0; i<v.size(); i++) {
            if (copies[v[i]]==nullptr) {    // prevents duplicated copies of a node
                copies[v[i]] = new Node(v[i]->val, {});
                q.push(v[i]);
            }
            copies[u]->neighbors.push_back(copies[v[i]]);
        }
    }
    return copies[node];
}

ํด๋ž˜์Šค ๋“ฑ์žฅ๊ณผ ๊ทธ๋ž˜ํ”„์— ์ต์ˆ™ํ•˜์ง€ ์•Š์€ ๋ถ€๋ถ„ ๋•Œ๋ฌธ์— ์ง€๋ ˆ ๊ฒ์„ ๋จน๊ณ  ๊ฑฐ์˜ ๋ฐ”๋กœ geeksforgeeks์— ์ฐพ์•„๋ดค๋‹ค. ์‚ฌ์‹ค ๊ทธ๋ž˜ํ”„๋Š” ์ €๋ฒˆ ํ•™๊ธฐ COMP2711 (Discrete mathematics for computer science) ์ˆ˜์—…์„ ๋“ค์œผ๋ฉฐ ๊ธฐ๋ณธ์ ์ธ ๊ฐœ๋…์€ ์ตํ˜”๋‹ค.

์ด ๋ฌธ์ œ์—์„œ ๊ฐ€์žฅ ์ถ•์ด ๋˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ BFS (Breadth-First Search)์ด๋‹ค.
์–ด๋–ค ํ•œ ๊ฐœ์˜ node๋ฅผ ์‹œ์ž‘์ ์œผ๋กœ ์ •ํ•ด์„œ ๋ชจ๋“  node๋ฅผ traverse ํ•˜๋„๋ก ํ•  ์ˆ˜ ์žˆ๋‹ค.
์ด ๋ฌธ์ œ์—์„œ์˜ ์ค‘์ ์€ node๋ฅผ ๋“ค๋ฅผ๋•Œ๋งˆ๋‹ค ๋ณต์ œ๋ฅผ ํ•˜๋Š”๋ฐ, ์ด๋ฏธ ๋ณต์ œํ•œ node๋ฅผ ์žฌ๋ฐฉ๋ฌธํ•  ๊ฒฝ์šฐ์—” ๋„˜์–ด๊ฐ€๊ฒŒ๋” ํ•ด์ค˜์•ผ ํ•œ๋‹ค. ์ด๊ฑด ๋…ธ๋“œ๋ฅผ ๋ณต์ œํ•  ๋•Œ๋งˆ๋‹ค map์— ์ €์žฅ์„ ํ•˜๋ฏ€๋กœ ๋ฌธ์ œ๊ฐ€ ๋˜์ง€ ์•Š๋Š”๋‹ค.

How to keep track of the visited/cloned nodes?

Use of hashmap/map. Before adding a node into the map, check if it is already stored in the map.
Key: Address of original node.
Value: Address of cloned node.

How to connect clone nodes?

  1. Visit all the neighboring nodes for a node copy.
  2. For each neighbor of copy find the corresponding clone node. If not found, create one.
  3. Push the cloned neighbor into the neighboring vector of cloned version of copy.

clone-graph.jpg


์‹œ์ž‘ ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธํ•œ ํ›„ ์‹œ์ž‘ ๋…ธ๋“œ์— ์žˆ๋Š” ์ธ์ ‘ํ•œ ๋ชจ๋“  ๋…ธ๋“œ๋“ค์„ ๋ฐฉ๋ฌธํ•˜๋Š” ๋ฐฉ๋ฒ•.
๋” ์ด์ƒ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๋…ธ๋“œ๊ฐ€ ์—†์„ ๋•Œ๊นŒ์ง€ BFS๊ฐ€ ์ ์šฉ์ด ๋œ๋‹ค.
Queue ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•œ๋‹ค. Queue์—๋Š” ์ธ์ ‘ ๋…ธ๋“œ๋“ค(neighboring nodes)์ด ์ €์žฅ ๋œ๋‹ค.

์žฅ์ 

  • ์ถœ๋ฐœ ๋…ธ๋“œ์—์„œ ๋ชฉํ‘œ ๋…ธ๋“œ๊นŒ์ง€์˜ ์ตœ๋‹จ ๊ธธ์ด ๊ฒฝ๋กœ๋ฅผ ๋ณด์žฅ.

๋‹จ์ 

  • ๊ฒฝ๋กœ๊ฐ€ ๋งค์šฐ ๊ธธ ๊ฒฝ์šฐ์—๋Š” ํƒ์ƒ‰ ๊ฐ€์ง€๊ฐ€ ๊ธ‰๊ฒฉํžˆ ์ฆ๊ฐ€ํ•จ์— ๋”ฐ๋ผ ๋ณด๋‹ค ๋งŽ์€ ๊ธฐ์–ต ๊ณต๊ฐ„์„ ํ•„์š”.
  • ํ•ด๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ์œ ํ•œ ๊ทธ๋ž˜ํ”„์˜ ๊ฒฝ์šฐ, ๋ชจ๋“  ๊ทธ๋ž˜ํ”„๋ฅผ ํƒ์ƒ‰ํ•œ ํ›„์— ์‹คํŒจ๋กœ ๋๋‚œ๋‹ค.
  • ๋ฌดํ•œ ๊ทธ๋ž˜ํ”„์˜ ๊ฒฝ์šฐ์—๋Š” ๊ฒฐ์ฝ” ํ•ด๋ฅผ ์ฐพ์ง€๋„ ๋ชปํ•˜๊ณ , ๋๋‚ด์ง€๋„ ๋ชปํ•œ๋‹ค.

Reference

https://www.geeksforgeeks.org/clone-an-undirected-graph/

Categories:

Updated:

Leave a comment