LeetCode OJ - Clone Graph

来源:互联网 发布:网络黑市交易论坛 编辑:程序博客网 时间:2024/05/17 22:11

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         / \         \_/


/**
 * Definition for undirected graph.
 * struct UndirectedGraphNode {
 *     int label;
 *     vector<UndirectedGraphNode *> neighbors;
 *     UndirectedGraphNode(int x) : label(x) {};
 * };
 */


错误分析:此题和带random指针链表的深度拷贝类似,采用Hash表来映射,记录新旧节点的映射关系。第一遍遍历旧图新建节点和Hash表,第二遍遍历新图使用Hash表替换新的连接关系。

再分析:上述分析将图的遍历想成链表的遍历,图的遍历应考虑BFS或的DFS

BFS算法如下:(使用队列)

void BFS(graphNode *node) {    queue<graphNode *>que;    que.push(node);    while(!que.empty()) {        tmp = que.front();         que.pop();        if(tmp.isvisited())              continue;        for(int i = 0; i < tmp->neighbors.size(); i++) {            que.push(tmp->neighbors[i]);        }    }}



DFS算法如下:(使用栈)

1.入栈条件:栈顶节点的第一个未被访问的节点    2.出栈条件:无邻节点、邻节点访问完了。

void DFS(graphNode *node) {    stack<graphNode *>st;    st.push(node);    while(!st.empty()) {        tmp = st.top();        for(x in tmp->neighbors) {            if(!x.isvisited()) break;        }                if(x == null) {  //无邻节点、邻节点访问完           st.pop();        } else {         //第一个未被访问的邻节点           st.push(x);           output(x);        }    }}


采用BFS进行两边遍历:

/** * Definition for undirected graph. * struct UndirectedGraphNode { *     int label; *     vector<UndirectedGraphNode *> neighbors; *     UndirectedGraphNode(int x) : label(x) {}; * }; */class Solution {public:    UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {if(!node) return NULL;map<UndirectedGraphNode *, UndirectedGraphNode *> nodeMap;queue<UndirectedGraphNode *>q;q.push(node);while(!q.empty()) {UndirectedGraphNode *cur = q.front();q.pop();//cur节点没有被访问if(nodeMap.find(cur) == nodeMap.end()) {//创建新节点UndirectedGraphNode *born = new UndirectedGraphNode(cur->label);//创建映射关系nodeMap.insert(pair<UndirectedGraphNode *, UndirectedGraphNode *>(cur, born));for(int i = 0; i < cur->neighbors.size(); i++) {q.push(cur->neighbors[i]);}}}   q.push(node);while(!q.empty()) {UndirectedGraphNode *cur = q.front();q.pop();UndirectedGraphNode *newNode = nodeMap[cur];if(newNode ->neighbors.empty()) {for(int i = 0; i < cur->neighbors.size(); i++) {newNode->neighbors.push_back( nodeMap[ cur->neighbors[i] ]);q.push(cur->neighbors[i]);}}}return nodeMap[node];}};











0 0
原创粉丝点击