-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuscaEmLargura.java
More file actions
41 lines (36 loc) · 963 Bytes
/
Copy pathBuscaEmLargura.java
File metadata and controls
41 lines (36 loc) · 963 Bytes
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
package Exercicio2;
import java.util.LinkedList;
public class BuscaEmLargura extends Grafo {
public BuscaEmLargura(int v) {
super(v);
}
//metodo que faz a busca em largura
private void BFS(int comeco) {
for (Vertice v : getListaVertices()) {
v.setCor(0);
v.setDistancia(-1);
v.setParente(null);
}
LinkedList<Vertice> fila = new LinkedList<>();
getListaVertices()[comeco].setCor(1);
getListaVertices()[comeco].setDistancia(0);
fila.add(getListaVertices()[comeco]);
while (!fila.isEmpty()) {
Vertice u = fila.removeFirst();
for (Vertice v : u.getAdjacente()) {
if (v.getCor() == 0) {
v.setCor(1);
v.setDistancia(u.getDistancia() + 1);
v.setParente(u);
fila.add(v);
}
}
u.setCor(2);
}
}
//metodo para imprimir quantidade de salas percorridas com a busca
protected int retornaQtdSalasPercorridas(int comeco, int fim) {
BFS(comeco);
return getListaVertices()[fim].getDistancia();
}
}