-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.ts
More file actions
128 lines (104 loc) · 1.97 KB
/
Copy pathindex.ts
File metadata and controls
128 lines (104 loc) · 1.97 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { DirectedGraph, UndigraphGraph } from "./data-struct/graph/graph";
const graph = new DirectedGraph();
// 添加节点
graph.insertNode(0, "A");
graph.insertNode(1, "B");
graph.insertNode(3, "D");
graph.insertNode(2, "C");
// 设置边
graph.addEdge(3, 1); // B A->B, D->B
graph.addEdge(0, 1);
graph.addEdge(0, 2);
graph.addEdge(3, 2);
graph.addEdge(2, 3);
graph.addEdge(2, 0);
graph.addEdge(3, 0);
let res = graph.display();
let jsonRes = JSON.stringify(res, null, 4);
console.log(`%c${jsonRes}`, "color:red");
/*
console.log(res);
res ->
[
{
"input": [
"D->A",
"C->A"
],
"output": [
"A->B",
"A->C"
],
"name": "A"
},
{
"input": [
"A->B",
"D->B"
],
"output": [],
"name": "B"
},
{
"input": [
"C->D"
],
"output": [
"D->B",
"D->A",
"D->C"
],
"name": "D"
},
{
"input": [
"A->C",
"D->C"
],
"output": [
"C->D",
"C->A"
],
"name": "C"
}
]
*/
/*
graph.deleteNode(0)
res = graph.display();
jsonRes = JSON.stringify(res, null, 4);
console.log(jsonRes);
graph.deleteEdge(3, 1)
res = graph.display();
jsonRes = JSON.stringify(res, null, 4);
console.log(jsonRes);
*/
let s = ''
graph.bfs(graph.getNode(2), n => {
s += n.data
})
console.log(s);
s = '';
graph.dfs(graph.getNode(2), n => {
s += n.data;
})
console.log(s);
s = '';
graph.dfsStack(graph.getNode(2), n => {
s += n.data;
})
console.log(s);
const ung = new UndigraphGraph();
ung.insertNode(0, "A");
ung.insertNode(1, "B");
ung.insertNode(2, "C")
ung.insertNode(3, "D")
ung.insertNode(4, "E");
ung.addEdge(0, 1)
ung.addEdge(0, 3)
ung.addEdge(1, 4)
ung.addEdge(2, 1)
ung.addEdge(2, 4)
ung.addEdge(2, 3)
let di = ung.display();
console.log(JSON.stringify(di, null, 4));