-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhasCircle.js
More file actions
48 lines (44 loc) · 1.16 KB
/
Copy pathhasCircle.js
File metadata and controls
48 lines (44 loc) · 1.16 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
// 判断一个有向图是否有环
// 图用以下结构表示:
const node1 = {
value: 1,
};
const node2 = {
value: 2,
};
const node3 = {
value: 3,
};
const node4 = {
value: 4,
};
node1.from = [];
node1.to = [node2];
node2.from = [node1, node4];
node2.to = [node3];
node3.from = [node2];
node3.to = [node4];
node4.from = [node3];
node4.to = [node2];
const nodes = { node1, node2, node3, node4 };
//使用拓扑排序:先删掉入度为0的点,然后遍历该点的出度,把出度对应的点的入度删掉,继续循环,如果最后还存在
//未被删除的点,则说明存在环
function isCircle(nodes) {
while (Object.keys(nodes).length) {
for (const key of Object.keys(nodes)) {
const { from, to } = nodes[key];
// 处理入度为0的点
if (!from.length) {
// 然后遍历该点的出度,把出度对应的点的入度删掉
for (const item of to) {
item.from = item.from.filter((val) => val !== nodes[key]);
}
delete nodes[key];
} else {
return true; //遍历一遍没有入度为0的点,说明存在环
}
}
}
return false;
}
console.log(isCircle(nodes));