2

I am learning algorithms. I am stuck in implementing BFS exercise part. There is no clue or solution to exercises on their site.I cannot figure out where am I making mistake?

Someone please help me in understanding where I am doing it wrong.

here is my code.

        /* A Queue object for queue-like functionality over JavaScript arrays. */
    var Queue = function() {
        this.items = [];
    };
    Queue.prototype.enqueue = function(obj) {
        this.items.push(obj);
    };
    Queue.prototype.dequeue = function() {
        return this.items.shift();
    };
    Queue.prototype.isEmpty = function() {
        return this.items.length === 0;
    };

    /*
     * Performs a breadth-first search on a graph
     * @param {array} graph - Graph, represented as adjacency lists.
     * @param {number} source - The index of the source vertex.
     * @returns {array} Array of objects describing each vertex, like
     *     [{distance: _, predecessor: _ }]
     */


          var doBFS = function(graph, source) {
                var bfsInfo = [];

                for (var i = 0; i < graph.length; i++) {
                    bfsInfo[i] = {
                        distance: null,
                        predecessor: null };
                }

                bfsInfo[source].distance = 0;

                var queue = new Queue();
                queue.enqueue(source);

                // Traverse the graph

                // As long as the queue is not empty:
                //  Repeatedly dequeue a vertex u from the queue.
                //  
                //  For each neighbor v of u that has not been visited:
                //     Set distance to 1 greater than u's distance
                //     Set predecessor to u
                //     Enqueue v
                //
                //  Hint:
                //  use graph to get the neighbors,
                //  use bfsInfo for distances and predecessors 

                while(!queue.isEmpty()){
                    var vertex= queue.dequeue();

                for(var i=0; i<vertex.length; i++){
                    var  neighbour = graph[vertex][i];

                    if(bfsInfo[neighbour].distance===null){
                        bfsInfo[neighbour].distance+=1;
                        bfsInfo[neighbour].predecessor=vertex;
                        queue.enqueue(neighbour);
                    }
                }



                }
                return bfsInfo;
            };

            var adjList = [
                [1],
                [0, 4, 5],
                [3, 4, 5],
                [2, 6],
                [1, 2],
                [1, 2, 6],
                [3, 5],
                []
                ];
            var bfsInfo = doBFS(adjList, 3);
            for (var i = 0; i < adjList.length; i++) {
                println("vertex " + i + ": distance = " + bfsInfo[i].distance + ", predecessor = " + bfsInfo[i].predecessor);
            }

Test cases

 Program.assertEqual(bfsInfo[0], {distance: 4, predecessor: 1});
Program.assertEqual(bfsInfo[1], {distance: 3, predecessor: 4});
Program.assertEqual(bfsInfo[2], {distance: 1, predecessor: 3});
Program.assertEqual(bfsInfo[3], {distance: 0, predecessor: null});
Program.assertEqual(bfsInfo[4], {distance: 2, predecessor: 2});
Program.assertEqual(bfsInfo[5], {distance: 2, predecessor: 2});
Program.assertEqual(bfsInfo[6], {distance: 1, predecessor: 3});
Program.assertEqual(bfsInfo[7], {distance: null, predecessor: null});
user2991828
  • 163
  • 4
  • 12
  • please add the code for `Program.assertEqual`. – Nina Scholz Nov 30 '15 at 14:49
  • the only assert code provided by them i have already added above.All the test cases are not passed for me.I am doing something wrong in exercise.for more info here is link https://www.khanacademy.org/computing/computer-science/algorithms/breadth-first-search/p/challenge-implement-breadth-first-search – user2991828 Nov 30 '15 at 15:06

1 Answers1

1

You need to change

  bfsInfo[neighbour].distance+=1;

To something like

  bfsInfo[neighbour].distance = bfsInfo[vertex].distance + 1;

Since the distance is null adding one doesn't make much sense. You want to take the distance to the curent node (vertext) and add one to get the distance to the new node (neighbour).

Also I think you need to iterate through the vertex neighbours(graph[vertex]) not the vertex itself (since that is just a number).

Not fully sure how it works out with javascript. You need to know which one is the current node (vertex) to get the right distance there.

Sorin
  • 11,863
  • 22
  • 26
  • your answer helped me to solve my problem.As you said that distance is in null and it doesn't make sense so i changed it to your implementation and 2nd mistake was that i was iterating on 'vertex.length' condition instead of 'graph[vertex].length' .Thanks – user2991828 Nov 30 '15 at 20:04