使用队列在图中进行广度优先搜索

izj3ouym  于 2021-08-20  发布在  Java
关注(0)|答案(0)|浏览(159)

我认为我的代码有错误,我不知道原因。
图表是:这是图表
输出为:
bfs的输出
输出为:1->2->4->5
但这是不正确的,因为正确的答案是:
1->2->4->5->3
我的代码是用java编写的,我有类nodog(表示图的节点)、grafo(表示图)和principal(我在其中执行操作)。
此外,我不考虑边缘的权重,只考虑相邻节点在各自节点中引入的顺序。
例如:
1->2,权重3:我先写
1->4和重量10:我把它放在第二位
1->5和重量12:我把它放在第三位
我会处理每个节点。但是,如果节点没有邻居,我不会将其作为数据写入。

public class NodoG {
    private int dato;
    boolean visitado;
    List<NodoG> adyacentes;

    public NodoG(int dato){
        this.dato = dato;
        this.visitado = false;
        this.adyacentes = new ArrayList<>();
    }
    public int getDato() {
        return dato;
    }

    public void setDato(int dato) {
        this.dato = dato;
    }

    public boolean isVisitado() {
        return visitado;
    }

    public void setVisitado(boolean visitado) {
        this.visitado = visitado;
    }

    public List<NodoG> getAdyacentes() {
        return adyacentes;
    }

    public void setAdyacentes(ArrayList<NodoG> adyacentes) {
        this.adyacentes = adyacentes;
    }

    @Override
    public String toString(){
        return (dato + "->");
    } 
 }

我的另一门课:格拉福

import java.util.ArrayList;
    import java.util.LinkedList;
    import java.util.List;
    import java.util.Queue;
    import java.util.Scanner;

    public class Grafo {
        private List<NodoG> nodoGrafo;

        public Grafo(){
            nodoGrafo = new ArrayList<NodoG>();
        }

        public List<NodoG> getNodoGrafo() {
            return nodoGrafo;
        }

        public void setNodoGrafo(ArrayList<NodoG> nodoGrafo) {
            this.nodoGrafo = nodoGrafo;
        }

        public void insertarNodoG(){
            System.out.println("Write data: ");
            Scanner entrada = new Scanner(System.in);
            System.out.print("->");
            int valor = entrada.nextInt();
            NodoG nodo = new NodoG(valor);
            nodoGrafo.add(nodo);       
        }

        public NodoG buscarNodoGrafo(int n1){
            NodoG nodo = null;
            for (NodoG nodoGrafo1 : nodoGrafo) {
                if(nodoGrafo1.getDato() == n1){
                    nodo = nodoGrafo1;
                }
            }
            return nodo;
        }

        public void insertarAdyacente(){
            System.out.println("Write the data of the graph's node: ");
            Scanner entrada = new Scanner(System.in);
            System.out.print("->");
            int valor = entrada.nextInt();
            NodoG nodo = buscarNodoGrafo(valor);
            if(nodo!=null){
                System.out.println("Write the data of the adjacent: ");
                System.out.print("->");
                int dato = entrada.nextInt();
                NodoG nuevo = new NodoG(dato);
                nodo.getAdyacentes().add(nuevo);
            }
            else{
                System.out.println("->This node doesn't exist.");
            }
        }

        public void busquedaAmplitud(){
            System.out.println("Write the initial node: ");
            Scanner entrada = new Scanner(System.in);
            int dato = entrada.nextInt();
            System.out.println("-----------------------");
            NodoG inicio = buscarNodoGrafo(dato);
            if(inicio!=null){
                Queue<NodoG> cola = new LinkedList<NodoG>();

                cola.add(inicio);
                inicio.setVisitado(true);
                while(!cola.isEmpty()){
                  //  System.out.println("size: "+cola.size());
                    NodoG elem = cola.poll();
                    //System.out.println("--------------");
                    System.out.print("->" + elem.getDato());
                    //System.out.println("--------------");
                    List<NodoG> ady = elem.getAdyacentes();
                    for (int i = 0;i<ady.size();i++) {
                        NodoG ady1 = ady.get(i);
                        if(ady1!= null && !ady1.isVisitado()){
                            cola.add(ady1);
                            ady1.setVisitado(true);
                        }
                    }
                }
            }
            else{
                System.out.println("This node doesn't exist.");
            }
        }

    }

我的最后一堂课:校长

import java.util.Scanner;
public class Principal {
    public static void main(String[] args) {
        Grafo G = new Grafo();
        int opc;
        System.out.println("=========================");
        System.out.println("BFS");
        System.out.println("=========================");
        System.out.println("¿How many nodes does the graph have?: ");
        Scanner entrada = new Scanner(System.in);
        int cant = entrada.nextInt();
        System.out.println("---------------------------------");
        System.out.println("Write the data of the nodes: ");
        for(int i=0;i<cant;i++){
            G.insertarNodoG();
        }
        System.out.println("==================================");
        System.out.println("Adjacent of the node");
        System.out.println("===================================");
        do{
            System.out.println("Enter the node you want to write an adjacent:");
            G.insertarAdyacente();
            System.out.println("-----------------------------------------");
    System.out.print("¿Do you want to write another adjacent?: (Yes: 1) (No: Any number) -> ");
            opc = entrada.nextInt();
            System.out.println("-----------------------------------");
        }while(opc == 1);
        do{
            System.out.println("==========================");
            System.out.println("BFS: ");
            System.out.println("---------------------------");
            G.busquedaAmplitud();
            System.out.println("\n---------------------------");
            //restartnodes() //To restar the boolean values of the nodes
        System.out.println("¿Do you want to use another node? (Yes: 1) (No: Any number)->");
            opc = entrada.nextInt();
        }while(opc==1);
    }
}

拜托,有人能帮我吗?另外,我需要知道如何重新启动节点的布尔值。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题