search for depth and width using graphs [closed]

-1

I need a code that does a search in width and depth using graphs for the shortest path search, analyzing these aspects mentioned, but I do not know where to start, I would like someone to help me, Github serves also if someone know it.

    
asked by anonymous 13.02.2016 / 19:48

1 answer

3

I'm going to put just one, to help you but it does not seem right to copy, if it's for a college job or whatever.

WIDTH

public static int BuscaLargura(int ini,int fim){

        Fila f = new Fila(ini);

        int cor[] = new int[vertices];

        Largura = new int[vertices];

        for(int i=0;i<vertices;i++)
            Largura[i]=-1;

        int t = 0;

        while(!f.isEmpty())
        {
            int leitor[] = f.rem();

            for(int i=0;i<leitor.length;i++)
            {
                cor[leitor[i]] = Black;
                Largura[leitor[i]] = t;
                if(leitor[i]==fim)
                    output=t;

            }

            for(int i=0;i<ArestasIni.size();i++)
                for(int j=0;j<leitor.length;j++)
                    if((int)ArestasIni.get(i)==leitor[j] && cor[(int)ArestasFim.get(i)]==White)
                    {
                        cor[(int)ArestasFim.get(i)]=Gray;
                        f.add((int)ArestasFim.get(i));
                    }
            t++;

        }

    }

The color indicates that the node was visited, Black is already visited. This algorithm is what does the search for all nodes using a stack. It accumulates all the next paths from the nodes that are already in this stack, Only the nodes that are not painted can be added in the stack. Searching for depth is much easier and you also use color, but you do not need to use a stack, you go by the way you can go and if the color is already Black you do not go through it. 'Passing' means executing the algorithm recursively on that node. You need to pass the number of steps per parameter on recursive calls with (i + 1). The best algorithm for the shortest path is Dijkstra, but the PRIM algorithm is simpler. The problem with the PRIM algorithm is that it finds the 'best place'. That's good, but there are better results.

    
13.02.2016 / 23:10