Problem with DFS variation - C #

0

I have a problem with a variation of a DFS which has ALL VERTICES TO A DISTANCE GIVEN FROM VERTICE ORIGIN, for this I used a DFS algorithm which looks for all possible paths given a source vertex and a destination vertex, to recycle code I place the origin in the section of the destination paramentro and with a traverse all the vertices placing them as the origin parameter in the method, to this I add a sum of distances and when it meets the two conditions it shows a result that is when it reaches the destination + x distance returns that result, my problem is that sometimes does not give the correct result and I do not know why ...

 static void DFS_S2(vertice origen, vertice destino, grafo graf,float distancia, float suma, string path)
    {
        path = (path == String.Empty ? path : $"{path},") + origen.nombre;

        if (origen.nombre == destino.nombre&distancia==suma)
        {
            string[] datos2 = path.Split(new char[] { ',' });

           Console.WriteLine(datos2[0]);
        }

        else
        {
            for (int con = 0; con < origen.adyacencias.Length; con++)
            {
                var visitado = Regex.IsMatch(path, $@"\b{graf.vertices[con].nombre}\b");
                if (origen.adyacencias[con] > 0 && !visitado)
                {
                    suma = suma + origen.adyacencias[con];
                    DFS_S2(graf.vertices[con], destino, graf, distancia, suma, path);
                }
            }
        }
    }
    
asked by Shiki 17.02.2018 в 01:22
source

1 answer

0

In the code it seems to me that in the line where you make the condition:

if (origen.nombre == destino.nombre&distancia==suma)

Instead of a single & (which performs bitwise AND) you should use double & , which would be:

if (origen.nombre == destino.nombre && distancia==suma)

Although you should also consider that if you want to find all the vertices at a given distance from a vertex v it is more advisable to use BFS since it gives you the minimum distance to which are all the vertices of a graph of a vertex v of the graph itself.

    
answered by 10.03.2018 в 18:19