-
Notifications
You must be signed in to change notification settings - Fork 19
/
buscaBFS.html
266 lines (239 loc) · 7.59 KB
/
buscaBFS.html
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Visualização de Estratégias de Busca - BFS</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
display: flex;
}
.left-container {
display: flex;
flex-direction: column;
align-items: flex-start;
width: 70%;
}
.right-container {
display: flex;
flex-direction: column;
align-items: center;
width: 30%;
}
.controls {
margin-bottom: 20px;
}
.graph {
display: inline-block;
margin: 0 auto;
}
.node {
fill: #4CAF50;
stroke: #000;
stroke-width: 2px;
}
.node.visited {
fill: #FF5722;
}
.node.expanded {
stroke: #FFD700;
stroke-width: 4px;
}
.link {
stroke: #999;
stroke-opacity: 0.6;
}
#path-display, #fringe-display {
margin-top: 20px;
font-weight: bold;
}
.fringe-container {
display: flex;
align-items: center;
margin-top: 20px;
}
.fringe-label {
margin-right: 10px;
font-weight: bold;
}
.fringe-display {
display: flex;
flex-direction: row;
overflow-x: auto;
white-space: nowrap;
border: 1px solid #000;
padding: 10px;
border-radius: 5px;
background-color: #f4f4f4;
height: 50px;
}
.fringe-item {
margin: 0 5px;
padding: 5px;
border: 1px solid #000;
border-radius: 3px;
background-color: #e0e0e0;
transition: all 0.3s ease;
display: inline-block;
}
</style>
</head>
<body>
<div class="left-container">
<h1>Visualização de Busca em Largura (BFS)</h1>
<div class="controls">
<label for="start-node">Nó de Início:</label>
<select id="start-node">
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
<option value="D">D</option>
<option value="E">E</option>
<option value="F">F</option>
<option value="G">G</option>
<option value="H">H</option>
<option value="I">I</option>
<option value="J">J</option>
</select>
<button onclick="startSearch()">Iniciar Busca</button>
<button id="next-button" onclick="nextStep()" disabled>Next</button>
</div>
<svg class="graph" width="800" height="400"></svg>
<div id="esquerda">
<div id="path-display">Caminho Percorrido: </div>
<div class="fringe-container">
<div class="fringe-label">Fringe (início):</div>
<div id="fringe-display" class="fringe-display"></div>
</div>
</div>
</div>
<script src="https://d3js.org/d3.v7.min.js"></script>
<script>
const nodes = [
{id: 'A', level: 0, x: 400, y: 50},
{id: 'B', level: 1, x: 200, y: 150},
{id: 'C', level: 1, x: 600, y: 150},
{id: 'D', level: 2, x: 100, y: 250},
{id: 'E', level: 2, x: 300, y: 250},
{id: 'F', level: 2, x: 500, y: 250},
{id: 'G', level: 2, x: 700, y: 250},
{id: 'H', level: 3, x: 50, y: 350},
{id: 'I', level: 3, x: 150, y: 350},
{id: 'J', level: 3, x: 250, y: 350},
{id: 'K', level: 3, x: 350, y: 350},
{id: 'L', level: 3, x: 450, y: 350},
{id: 'M', level: 3, x: 550, y: 350},
{id: 'N', level: 3, x: 650, y: 350},
{id: 'O', level: 3, x: 750, y: 350}
];
const links = [
{source: 'A', target: 'B'}, {source: 'A', target: 'C'},
{source: 'B', target: 'D'}, {source: 'B', target: 'E'},
{source: 'C', target: 'F'}, {source: 'C', target: 'G'},
{source: 'D', target: 'H'}, {source: 'D', target: 'I'},
{source: 'E', target: 'J'}, {source: 'E', target: 'K'},
{source: 'F', target: 'L'}, {source: 'F', target: 'M'},
{source: 'G', target: 'N'}, {source: 'G', target: 'O'}
];
const svg = d3.select('.graph');
const width = +svg.attr('width');
const height = +svg.attr('height');
const link = svg.append('g')
.selectAll('line')
.data(links)
.enter().append('line')
.attr('class', 'link')
.attr('x1', d => nodes.find(n => n.id === d.source).x)
.attr('y1', d => nodes.find(n => n.id === d.source).y)
.attr('x2', d => nodes.find(n => n.id === d.target).x)
.attr('y2', d => nodes.find(n => n.id === d.target).y);
const node = svg.append('g')
.selectAll('circle')
.data(nodes)
.enter().append('circle')
.attr('class', 'node')
.attr('r', 20)
.attr('cx', d => d.x)
.attr('cy', d => d.y)
.call(d3.drag()
.on('start', dragstarted)
.on('drag', dragged)
.on('end', dragended));
const label = svg.append('g')
.selectAll('text')
.data(nodes)
.enter().append('text')
.attr('dy', 5)
.attr('x', d => d.x)
.attr('y', d => d.y)
.text(d => d.id);
function dragstarted(event, d) {
if (!event.active) simulation.alphaTarget(0.3).restart();
d.fx = d.x;
d.fy = d.y;
}
function dragged(event, d) {
d.fx = event.x;
d.fy = event.y;
}
function dragended(event, d) {
if (!event.active) simulation.alphaTarget(0);
d.fx = null;
d.fy = null;
}
let searchQueue = [];
let visited = new Set();
let currentPath = [];
function bfs(startNode) {
searchQueue = [startNode];
visited.clear();
currentPath = [];
}
async function nextStep() {
if (searchQueue.length === 0) {
document.getElementById('next-button').disabled = true;
return;
}
let nodeId = searchQueue.shift(); // BFS: Remove o nó do início da fila
if (!visited.has(nodeId)) {
visited.add(nodeId);
currentPath.push(nodeId);
await highlightNode(nodeId, currentPath);
let neighbors = links.filter(l => l.source === nodeId || l.target === nodeId)
.map(l => l.source === nodeId ? l.target : l.source)
.filter(n => !visited.has(n));
searchQueue.push(...neighbors); // BFS: Adiciona vizinhos ao final da fila
// Atualiza a fringe
await updateFringeDisplay();
}
if (searchQueue.length === 0) {
document.getElementById('next-button').disabled = true;
}
}
async function updateFringeDisplay() {
const fringeDisplay = document.getElementById('fringe-display');
const currentItems = searchQueue.map(n => `<div class="fringe-item">${n}</div>`).join('');
fringeDisplay.innerHTML = currentItems;
}
async function highlightNode(nodeId, currentPath) {
svg.selectAll('circle').filter(d => d.id === nodeId)
.classed('visited', true)
.classed('expanded', true);
// Atualiza o display do caminho
document.getElementById('path-display').innerHTML = `Caminho Percorrido: ${currentPath.join(' -> ')}`;
}
function startSearch() {
const startNode = document.getElementById('start-node').value;
bfs(startNode);
document.getElementById('next-button').disabled = false;
// Limpa o gráfico
svg.selectAll('circle').classed('visited', false).classed('expanded', false);
// Atualiza a fringe e o caminho
document.getElementById('path-display').innerHTML = 'Caminho Percorrido: ';
document.getElementById('fringe-display').innerHTML = '';
}
</script>
</body>
</html>