forked from jyx-fyh/algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtopologySort1.cpp
More file actions
117 lines (95 loc) · 2.77 KB
/
topologySort1.cpp
File metadata and controls
117 lines (95 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//
// Created by ButcherX on 23-11-11.
//
//测试链接:https://www.lintcode.com/problem/127/
#include<vector>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<stack>
#include<algorithm>
using std::pair;
using std::stack;
using std::unordered_set;
using std::unordered_map;
using std::vector;
struct DirectedGraphNode
{
int label;
vector<DirectedGraphNode *> neighbors;
DirectedGraphNode(int x) : label(x) {};
};
//方法1:高度比较==============================================================================
struct wrap
{
DirectedGraphNode* node;
int depth;
wrap(DirectedGraphNode* _node, int _depth):node(_node), depth(_depth){}
};
int process(DirectedGraphNode* node, unordered_map<DirectedGraphNode*, wrap*> &map)
{
if(map.find(node) != map.end())
return map[node]->depth;
if(node->neighbors.empty())
return 1;
int maxDepth = 0;
int depth;
for(auto n : node->neighbors)
{
depth = process(n, map);
wrap* w = new wrap(n, depth);
map.emplace(n, w);
maxDepth = maxDepth > depth ? maxDepth : depth;
}
return maxDepth + 1;
}
vector<DirectedGraphNode*> topSort(vector<DirectedGraphNode*> graph)
{
unordered_map<DirectedGraphNode*, wrap*> map;
int depth;
for(auto n : graph)
{
depth = process(n, map);
wrap* w = new wrap(n, depth);
map.emplace(n, w);
}
vector<pair<DirectedGraphNode*, wrap*>> vec;
for(auto n : map)
vec.push_back(n);
std::sort(vec.begin(), vec.end()
, [](pair<DirectedGraphNode*, wrap*> a, pair<DirectedGraphNode*, wrap*> b)
{return a.second->depth > b.second->depth;});
vector<DirectedGraphNode*> ret;
for(auto n : vec)
ret.push_back(n.first);
return ret;
}
//方法2 深度优先==============================================================================
void dfs(DirectedGraphNode* node, unordered_set<DirectedGraphNode*> visitedInPath
, unordered_set<DirectedGraphNode*> &visited, stack<DirectedGraphNode*> &stk)
{
if(visitedInPath.find(node) != visitedInPath.end())
exit(0);
if(visited.find(node) != visited.end())
return;
visited.emplace(node);
visitedInPath.emplace(node);
for(auto n : node->neighbors)
dfs(n, visitedInPath, visited, stk);
stk.push(node);
}
vector<DirectedGraphNode*> topSort1(vector<DirectedGraphNode*> graph)
{
unordered_set<DirectedGraphNode*> visitedInPath;
unordered_set<DirectedGraphNode*> visited;
stack<DirectedGraphNode*> stk;
vector<DirectedGraphNode*> ret;
for(auto n : graph)
dfs(n, visited, visitedInPath, stk);
while(!stk.empty())
{
ret.push_back(stk.top());
stk.pop();
}
return ret;
}