quando eu compilo este programa a busca em largura so imprime x a b c e eu não consigo imprimir o resto.
e como eu altero o X do primeiro AddNo para uma palavra exemplo (empresa)
public class Grafo {
private No VetorNos[];
private int MatrizAdj[][];
private int NroNos;
private final int MaxNos = 10;
private Pilha minhaPilha;
private Fila minhaFila;
public Grafo(){
this.VetorNos = new No[MaxNos];
this.MatrizAdj = new int[MaxNos][MaxNos];
this.NroNos = 0;
this.minhaPilha = new Pilha(MaxNos);
this.minhaFila = new Fila(MaxNos);
for(int i=0; i<MaxNos; i++){
for(int j=0; j<MaxNos ;j++)
MatrizAdj[i][j] = 0;
}
}
public void AddNo(char Label){
VetorNos[NroNos] = new No(Label);
NroNos++;
}
public void AddAresta(int ini, int fim){
MatrizAdj[ini][fim] = 1;
MatrizAdj[fim][ini] = 1;
}
public void MostraNo(int nro){
System.out.println (VetorNos[nro].Label);
}
// busca em profundidade
public void Profundidade()
{
VetorNos[0].FoiVisitado = true;
MostraNo(0);
minhaPilha.push(0);
while (!minhaPilha.isEmpty())
{
int v = getNoAdjNaoVisitado( minhaPilha.top());
if (v == -1)
{
minhaPilha.pop();
}
else
{
VetorNos[v].FoiVisitado = true;
MostraNo(v);
minhaPilha.push(v);
}
}
// pilha vazia, chegou ao final da profundidade
for (int j = 0; j < NroNos; j++)
{
VetorNos[j].FoiVisitado = false;
}
}
private int getNoAdjNaoVisitado (int v)
{
for (int j = 0; j < NroNos; j++)
{
if ((MatrizAdj[v][j] == 1) && (VetorNos[j].FoiVisitado == false))
return j;
}
return -1;
}
//busca em largura
public void Largura()
{
VetorNos[0].FoiVisitado = true;
MostraNo(0);
minhaFila.insere(0);
int v2;
while (!minhaFila.isempty())
{
int v1 = minhaFila.remove();
while ((v2 = getNoAdjNaoVisitado(v1)) != -1)
{
VetorNos[v2].FoiVisitado = true;
MostraNo(v2);
}
}
}
}
public class SegundoT2 {
public static void main(String[] args) {
Grafo g = new Grafo ();
g.AddNo('x');/* x == empresa*/
g.AddNo('A');
g.AddNo('B');
g.AddNo('C');
g.AddNo('D');
g.AddNo('E');
g.AddNo('F');
g.AddNo('G');
g.AddNo('H');
g.AddAresta(0 , 1);
g.AddAresta(0 , 2);
g.AddAresta(0 , 3);
g.AddAresta(1 , 0);
g.AddAresta(1 , 4);
g.AddAresta(1 , 5);
g.AddAresta(2 , 0);
g.AddAresta(2 , 6);
g.AddAresta(3 , 0);
g.AddAresta(3 , 7);
g.AddAresta(3 , 8);
g.AddAresta(4 , 1);
g.AddAresta(5 , 1);
g.AddAresta(6 , 2);
g.AddAresta(7 , 3);
g.AddAresta(8 , 3);
System.out.println("Itinerario das motocicleta de tipo 1, 3 e 5.");
g.Profundidade();
System.out.println("\n\n");
System.out.println("Itinerario das motocicleta de tipo 2 e 4.");
g.Largura();
}
}