Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Breadth First Search in Javascript

function BFS_search(data, start, ende){
    var queue = new Array();
    var steps = 0;
    for(var x=0; x<data.Adjazenzen.length-1; x++){  //push start Element in Queue
        if(data.Adjazenzen[x] == start){
            queue.push(data.Adjazenzen[x]);
            steps++;
            break;
        }
    }

    while(queue.length != 0) {
        var t = queue.pop();
        if(t.Von == ende) {
            return t.Von;
        }
        if(t.Nach == ende){
            return t.Nach;
        }
        else{
            queue.push(data.Adjazenzen[0]);
            data.Adjazenzen.shift();
            steps++;
            break;
        }
    }
    return "keine Ergebnis";
}

This code snippet should calculate the best route between two points from a given JSON Object and also add all Points between the start and end node. Basically it should be a navigation-system using BFS.

The JSON Object looks like this:

{"Adjazenzen":[
{"Von":"A1.02","Nach":"G-A3-1","X":"5778","Y":"223","Z":"3","Treppe":"0","Gebaeude":"A","Level":"0"},
    {"Von":"A1.04","Nach":"G-A3-1","X":"5025","Y":"223","Z":"3","Treppe":"0","Gebaeude":"A","Level":"0"},
    {"Von":"A1.05","Nach":"G-A3-1","X":"4212","Y":"223","Z":"3","Treppe":"0","Gebaeude":"A","Level":"0"},
    {"Von":"A1.06","Nach":"G-A3-1","X":"3016","Y":"223","Z":"3","Treppe":"0","Gebaeude":"A","Level":"0"},
    {"Von":"A1.07","Nach":"G-A3-1","X":"2354","Y":"223","Z":"3","Treppe":"0","Gebaeude":"A","Level":"0"},
    {"Von":"A1.08","Nach":"G-A3-1","X":"1856","Y":"223","Z":"3","Treppe":"1","Gebaeude":"A","Level":"0"},
    {"Von":"A1.12","Nach":"G-A3-1","X":"1313","Y":"223","Z":"3","Treppe":"0","Gebaeude":"A","Level":"0"},
    {"Von":"B1.01","Nach":"G-B3-1","X":"6572","Y":"223","Z":"3","Treppe":"1","Gebaeude":"B","Level":"0"},
    {"Von":"G-A3-1","Nach":"G-A3-2","X":"906","Y":"223","Z":"3","Treppe":{},"Gebaeude":{},"Level":{}},
    {"Von":"G-A3-1","Nach":"B1.01","X":"6572","Y":"223","Z":"3","Treppe":"1","Gebaeude":"B","Level":"0"},
    {"Von":"G-A3-2","Nach":"A1.14","X":"1066","Y":"1333","Z":"3","Treppe":"0","Gebaeude":"A","Level":"0"},
    {"Von":"G-A3-2","Nach":"G-A3-3","X":"1444","Y":"4016","Z":"3","Treppe":{},"Gebaeude":{},"Level":{}},
    {"Von":"G-A3-2","Nach":"A1.15","X":"1207","Y":"2340","Z":"3","Treppe":"0","Gebaeude":"A","Level":"0"},
    {"Von":"G-A3-2","Nach":"A1.18","X":"1444","Y":"4016","Z":"3","Treppe":"0","Gebaeude":"A","Level":"0"},
    {"Von":"G-A3-2","Nach":"A1.16","X":"1327","Y":"3188","Z":"3","Treppe":"0","Gebaeude":"A","Level":"0"},
    {"Von":"G-A3-2","Nach":"A1.13","X":"933","Y":"383","Z":"3","Treppe":"0","Gebaeude":"A","Level":"0"},
    {"Von":"G-A3-2","Nach":"A1.17","X":"1425","Y":"3884","Z":"3","Treppe":"0","Gebaeude":"A","Level":"0"},
    {"Von":"G-A3-3","Nach":"A1.19","X":"2691","Y":"3977","Z":"3","Treppe":"1","Gebaeude":"A","Level":"0"},
    {"Von":"G-A3-3","Nach":"A1.22","X":"2946","Y":"3969","Z":"3","Treppe":"0","Gebaeude":"A","Level":"0"},
    {"Von":"G-A3-3","Nach":"A1.23","X":"2946","Y":"3969","Z":"3","Treppe":"0","Gebaeude":"A","Level":"0"},
    {"Von":"G-A3-3","Nach":"A1.20","X":"1990","Y":"4002","Z":"3","Treppe":"0","Gebaeude":"A","Level":"0"}]}

You can ignore everything except Von (From) and Nach (To). The problem is that my BFS is not 100% working and I don't know how to fix it.

An Example: If you want to calculate from A1.04 to A1.15 it should add: A1.04, G-A3-1, G-A3-2, A1.15

And if you want from A1.04 to A1.20: A1.04, G-A3-1, G-A3-2, G-A3-3, A1.20

I hope you can help me to solve this problem.

like image 694
Daniel Schott Avatar asked Jul 19 '26 11:07

Daniel Schott


1 Answers

The first thing I notice is that queue.push(...) and queue.pop() will not work has you expect, because what you have then is a stack, which means that you're doing a depth-first search and not a breadth-first search.

Recall that a queue is a FIFO container. So, use queue.push(...) (which adds an element to the end of the array) and queue.shift() (which removes an element from the front of the array). Basically, shift() is similar to the dequeue() operation on a queue.

like image 168
Vivin Paliath Avatar answered Jul 22 '26 00:07

Vivin Paliath