Skip to content

Commit

Permalink
0133 new algo added.
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyubobobo committed Sep 15, 2018
1 parent 60365f4 commit 64e9203
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 2 deletions.
2 changes: 1 addition & 1 deletion 0133-Clone-Graph/cpp-0133/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ project(cpp_0133)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(SOURCE_FILES main.cpp)
set(SOURCE_FILES main3.cpp)
add_executable(cpp_0133 ${SOURCE_FILES})
2 changes: 1 addition & 1 deletion 0133-Clone-Graph/cpp-0133/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ struct UndirectedGraphNode {
UndirectedGraphNode(int x) : label(x) {};
};

/// Using Stacks
/// Using Two Stacks and HashMap from label to Node
/// Time Complexity: O(V+E)
/// Space Complexity: O(V)
class Solution {
Expand Down
53 changes: 53 additions & 0 deletions 0133-Clone-Graph/cpp-0133/main2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/// Source : https://leetcode.com/problems/clone-graph/description/
/// Author : liuyubobobo
/// Time : 2018-09-14

#include <iostream>
#include <stack>
#include <vector>
#include <unordered_map>

using namespace std;

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

/// Using Only One Stacks and HashMap from Node to Node
/// Time Complexity: O(V+E)
/// Space Complexity: O(V)
class Solution {
public:
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {

if(node == NULL)
return NULL;

UndirectedGraphNode* ret = new UndirectedGraphNode(node->label);
stack<UndirectedGraphNode*> stack;
stack.push(node);
unordered_map<UndirectedGraphNode*, UndirectedGraphNode*> nodeMap;
nodeMap[node] = ret;
while(!stack.empty()){
UndirectedGraphNode* cur = stack.top();
stack.pop();
for(UndirectedGraphNode *next: cur->neighbors) {
if (!nodeMap.count(next)) {
nodeMap[next] = new UndirectedGraphNode(next->label);
stack.push(next);
}
nodeMap[cur]->neighbors.push_back(nodeMap[next]);
}
}
return ret;
}
};


int main() {

return 0;
}
51 changes: 51 additions & 0 deletions 0133-Clone-Graph/cpp-0133/main3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/// Source : https://leetcode.com/problems/clone-graph/description/
/// Author : liuyubobobo
/// Time : 2018-09-14

#include <iostream>
#include <vector>
#include <unordered_map>

using namespace std;

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

/// DFS
/// Time Complexity: O(V+E)
/// Space Complexity: O(V)
class Solution {

public:
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {

if(node == NULL)
return NULL;

unordered_map<UndirectedGraphNode*, UndirectedGraphNode*> nodeMap;
return dfs(node, nodeMap);
}

private:
UndirectedGraphNode *dfs(UndirectedGraphNode *node,
unordered_map<UndirectedGraphNode*, UndirectedGraphNode*>& nodeMap){

if(nodeMap.count(node))
return nodeMap[node];

nodeMap[node] = new UndirectedGraphNode(node->label);
for(UndirectedGraphNode* next: node->neighbors)
nodeMap[node]->neighbors.push_back(dfs(next, nodeMap));
return nodeMap[node];
}
};


int main() {

return 0;
}

0 comments on commit 64e9203

Please sign in to comment.