How to make the result of a BFS spanning tree is shown in preorder

2.1k views Asked by At

I'm trying to implement a BFS algorithm for homework, I find the spanning tree algorithm with BFS, the problem is that I require that the resulting spanning tree is shown in preorder. Here's my solution code:

#include <stdio.h>
#include<iostream>
#include <vector>
#include <stdlib.h>
using namespace std;
#define MAX 10001
#include <queue>

int BFS_graph[MAX][MAX];
int followOrder [MAX];
queue<int> myQueue;
int visit[MAX];
int V, it, it2=0;
bool first;
int BFS_tree [ MAX ];

void bfs(int root){
     int i, j,it2=0, node;
     memset(visit, 0, sizeof(visit));
     myQueue.push(root);
     while(!myQueue.empty())
     {
          node = myQueue.front();
          myQueue.pop();
          if(visit[node]) continue;
          visit[node] = 1;
         // cout <<" "<<node;
          BFS_tree[it2]=node;
          it2++;
                for(i=0; i<V; i++)
                {
                if( BFS_graph[i][node]) myQueue.push(i);
                if( BFS_graph[node][i]) myQueue.push(i);
                }
     }
}

int main(int argc, char *argv []){
int origin ,destination, i, j, k;
int vertexNumber, EdgesNumber;

    int initial;
    memset(visit, 0, sizeof(visit));

    cin>>vertexNumber>>EdgesNumber;
        V=vertexNumber;


         for (int j=0; j<EdgesNumber; j++){
            cin>>origin>>destination;
            BFS_graph[origin][destination]=1;
            BFS_graph[destination][origin]=1;
        }

        for (int j=0; j<vertexNumber; j++)
        cin>>followOrder[j];

        first = true;
       initial=followOrder[0];

        bfs (initial);
        for (int j=0; j<V; ++j)
        cout<<BFS_tree[j]<<" ";

return 0;
}

for this input:

10 10
0 1
0 3
1 3
0 2
4 1
4 5
6 4
7 2
8 7
7 9
0 1 2 3 4 5 6 7 8 9

my algorithm produces as output: [0 1 2 3 4 7 5 6 8 9]. representing the BFS tree by printing the nodes per level as shown in this image:

enter image description here

But the correct output (in preorder) should be, [0 1 3 4 5 6 2 7 8 9], resulting in traverse the tree in preorder. I need a solution for my code, How can fix my code to show me the solution in preorder?, for not having to use trees, that is, that somehow can be stored in my array BFS_tree the tree directly in preorder. Im stuck with this. I read a similar question here but I can not implement trees for efficiency measures and it is not allowed. How could I do this? is it possible?...Excuse my english.

1

There are 1 answers

0
Dietmar Kühl On

A preorder traversal of a tree consists of processing each parent prior to its children. Depending on how you traverse a tree you get different preorder traversals. Both of the traversals you showed are correct preorder traversals for the tree. The former looks like one of the breadth first trees while the latter looks like a depth first search order. If you are supposed to create the latter order as the result of a breadth first search you need to indicate the tree structure in the graph as a first path and then process the node using a depth first search.