-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathe3.js
115 lines (95 loc) · 1.34 KB
/
e3.js
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
const dijkstras = require('./dijkstras');
var g = dijkstras.createGraph();
g.addVertex('A', {
B: 8,
D: 5,
E: 4,
});
g.addVertex('B', {
A: 8,
C: 3,
E: 12,
F: 4,
});
g.addVertex('C', {
B: 3,
G: 11,
F: 9,
});
g.addVertex('D', {
A: 5,
E: 9,
H: 6,
});
g.addVertex('E', {
A: 4,
B: 12,
D: 9,
F: 3,
J: 5,
I: 8,
});
g.addVertex('F', {
B: 4,
C: 9,
E: 3,
G: 1,
K: 8,
});
g.addVertex('G', {
C: 11,
F: 1,
K: 8,
L: 7,
});
g.addVertex('H', {
I: 2,
D: 6,
M: 7,
});
g.addVertex('I', {
M: 6,
J: 10,
E: 8,
H: 2,
});
g.addVertex('J', {
E: 5,
K: 6,
N: 9,
I: 10,
});
g.addVertex('K', {
P: 7,
L: 5,
G: 8,
F: 8,
J: 6,
});
g.addVertex('L', {
P: 6,
K: 5,
G: 7,
});
g.addVertex('M', {
N: 2,
I: 6,
H: 7,
});
g.addVertex('N', {
P: 12,
J: 9,
M: 2,
});
g.addVertex('P', {
L: 6,
K: 7, // foi mesmo
N: 12,
});
// operações
var pathStart = 'A';
var pathEnd = 'P';
var shortestPath = g.shortestPath(pathStart, pathEnd);
var distance = g.calcDistance(shortestPath);
console.log('Caminho mais curto de ' + pathStart + ' à ' + pathEnd + ': [ ' + shortestPath.toString().replace(/\,/g, ' => ') + ' ]');
console.log('A distância/peso do caminho é: ' + distance);