LeetCode133:Clone Graph

题目:

Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.

OJ's undirected graph serialization:

Nodes are labeled uniquely.

We use # as a separator for each node, and , as a separator for node label and each neighbor of the node.

As an example, consider the serialized graph {0,1,2#1,2#2,2}.

The graph has a total of three nodes, and therefore contains three parts as separated by #.

  1. First node is labeled as 0. Connect node 0 to both nodes 1 and 2.
  2. Second node is labeled as 1. Connect node 1 to node 2.
  3. Third node is labeled as 2. Connect node 2 to node 2 (itself), thus forming a self-cycle.

Visually, the graph looks like the following:

       1

      / \

     /   \

    0 --- 2

         / \

         \_/

解题思路:

本题要解决的问题就是对一个图进行clone,这不禁让我们想起图的遍历:DFS和BFS,所以我们可以再遍历图中某个点时,增加一些附加的操作而不仅仅是访问它,这里我们要增加的附加操作即对该点进行clone。
现在我们利用DFS和BFS两种方式进行解题。

实现代码:

#include <iostream>

#include <vector>

#include <queue> 

#include <unordered_map>

using namespace std;



/*

Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.





OJ's undirected graph serialization:

Nodes are labeled uniquely.



We use # as a separator for each node, and , as a separator for node label and each neighbor of the node.

As an example, consider the serialized graph {0,1,2#1,2#2,2}.



The graph has a total of three nodes, and therefore contains three parts as separated by #.



First node is labeled as 0. Connect node 0 to both nodes 1 and 2.

Second node is labeled as 1. Connect node 1 to node 2.

Third node is labeled as 2. Connect node 2 to node 2 (itself), thus forming a self-cycle.

Visually, the graph looks like the following:



       1

      / \

     /   \

    0 --- 2

         / \

         \_/



*/



struct UndirectedGraphNode {

     int label;

     vector<UndirectedGraphNode *> neighbors;

     UndirectedGraphNode(int x) : label(x) {};

};



class Solution {

public:

    UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {

        if(node == NULL)

            return NULL;

        unordered_map<int, UndirectedGraphNode*> hashtable;//标志位,是否已拷贝了key为label,value为新拷贝的node 

        return dfsclone(node, hashtable);

        

    }

    

    //DFS 

    UndirectedGraphNode *dfsclone(UndirectedGraphNode *node, unordered_map<int, UndirectedGraphNode*> &hashtable)

    {

        if(node == NULL)

            return NULL;

        if(hashtable.count(node->label) > 0)

            return hashtable[node->label];//已经拷贝好了,则直接返回即可,否则进行下面的拷贝操作 

        UndirectedGraphNode *copyNode = new UndirectedGraphNode(node->label);

        hashtable[copyNode->label] = copyNode;

        vector<UndirectedGraphNode *>::iterator iter;

        for(iter = node->neighbors.begin(); iter != node->neighbors.end(); ++iter)

        {

            copyNode->neighbors.push_back(dfsclone(*iter, hashtable));//进行深度优先算法拷贝 

        }

        

        return copyNode;

    }

    

        //BFS 

    UndirectedGraphNode *bfsclone(UndirectedGraphNode *node, unordered_map<int, UndirectedGraphNode*> &hashtable)

    {

        if(node == NULL)

            return NULL;

        queue<UndirectedGraphNode *> qu;

        UndirectedGraphNode *copyNode = new UndirectedGraphNode(node->label);

        hashtable[copyNode->label] = copyNode;

        qu.push(node);

        while(!qu.empty())

        {

            UndirectedGraphNode *tnode = qu.front();

            qu.pop();

            vector<UndirectedGraphNode *>::iterator iter;

            for(iter = node->neighbors.begin(); iter != node->neighbors.end(); ++iter)

            {

                if(hashtable.count((*iter)->label) == 0)

                {

                    UndirectedGraphNode *tnode = new UndirectedGraphNode((*iter)->label);

                    hashtable[(*iter)->label] = tnode;

                    qu.push(*iter);

                }

                (hashtable[tnode->label])->neighbors.push_back(hashtable[(*iter)->label]);

                     

            }

            

        }        

        return copyNode;

    }

};

int main(void)

{

    return 0;

}

你可能感兴趣的:(LeetCode)