# Check if the given permutation is a valid BFS of a given Tree

Given a tree with N nodes numbered from 1 to N and an permutation array of numbers from 1 to N. Check if it is possible to obtain the given permutation array by applying BFS (Breadth First Traversal) on the given tree.

Note: Traversal will always start from 1.

Example:

Input: arr[] = { 1 5 2 3 4 6 }
Edges of the given tree:
1 – 2
1 – 5
2 – 3
2 – 4
5 – 6
Output: No
Explanation:
There is no such traversal which is same as the given permutation. The valid traversals are:
1 2 5 3 4 6
1 2 5 4 3 6
1 5 2 6 3 4
1 5 2 6 4 3

Input: arr[] = { 1 2 3 }
Edges of the given tree:
1 – 2
2 – 3
Output: Yes
Explanation:
The given permutation is a valid one.

Approach: To solve the problem mentioned above we have to follow the steps given below:

• In BFS we visit all the neighbour of the current node and push their children in the queue in order and repeat this process until the queue is not empty.
• Suppose there are two children of root: A and B. We are free to choose which of them to visit first. Let’s say we visit A first, but now we will have to push children of A in the queue and we cannot visit children of B before A.
• So basically we can visit the children of a particular node in any order but the order in which the children of 2 different nodes should be visited is fixed i.e. if A if visited before B, then all the children of A should be visited before all the children of B.
• We will do the same. We will make a queue of sets and in each set, we will push the children of a particular node and traverse the permutation alongside. If the current element of permutation is found in the set at the top of the queue, then we will proceed otherwise, we will return false.

Below is the implementation of above approach:

 `// C++ implementation to check if the ` `// given permutation is a valid ` `// BFS of a given tree ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// map for storing the tree ` `map<``int``, vector<``int``> > tree; ` ` `  `// map for marking ` `// the nodes visited ` `map<``int``, ``int``> vis; ` ` `  `// Function to check if ` `// permutation is valid ` `bool` `valid_bfs(vector<``int``>& v) ` `{ ` `    ``int` `n = (``int``)v.size(); ` `    ``queue > q; ` `    ``set<``int``> s; ` `    ``s.insert(1); ` ` `  `    ``/*inserting the root in  ` `     ``the front of queue.*/` `    ``q.push(s); ` `    ``int` `i = 0; ` ` `  `    ``while` `(!q.empty() && i < n) { ` ` `  `        ``// If the current node ` `        ``// in a permutation ` `        ``// is already visited ` `        ``// then return false ` `        ``if` `(vis.count(v[i])) { ` `            ``return` `0; ` `        ``} ` `        ``vis[v[i]] = 1; ` ` `  `        ``// if all the children of previous ` `        ``// nodes are visited then pop the ` `        ``// front element of queue. ` `        ``if` `(q.front().size() == 0) { ` `            ``q.pop(); ` `        ``} ` ` `  `        ``// if the current element of the ` `        ``// permutation is not found ` `        ``// in the set at the top of queue ` `        ``// then return false ` `        ``if` `(q.front().find(v[i]) ` `            ``== q.front().end()) { ` `            ``return` `0; ` `        ``} ` `        ``s.clear(); ` ` `  `        ``// push all the children of current ` `        ``// node in a set and then push ` `        ``// the set in the queue. ` `        ``for` `(``auto` `j : tree[v[i]]) { ` `            ``if` `(vis.count(j)) { ` `                ``continue``; ` `            ``} ` `            ``s.insert(j); ` `        ``} ` `        ``if` `(s.size() > 0) { ` `            ``set<``int``> temp = s; ` `            ``q.push(temp); ` `        ``} ` `        ``s.clear(); ` ` `  `        ``// erase the current node from ` `        ``// the set at the top of queue ` `        ``q.front().erase(v[i]); ` ` `  `        ``// increment the index ` `        ``// of permutation ` `        ``i++; ` `    ``} ` ` `  `    ``return` `1; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``tree[1].push_back(2); ` `    ``tree[2].push_back(1); ` `    ``tree[1].push_back(5); ` `    ``tree[5].push_back(1); ` `    ``tree[2].push_back(3); ` `    ``tree[3].push_back(2); ` `    ``tree[2].push_back(4); ` `    ``tree[4].push_back(2); ` `    ``tree[5].push_back(6); ` `    ``tree[6].push_back(5); ` ` `  `    ``vector<``int``> arr ` `        ``= { 1, 5, 2, 3, 4, 6 }; ` ` `  `    ``if` `(valid_bfs(arr)) ` `        ``cout << ``"Yes"` `<< endl; ` ` `  `    ``else` `        ``cout << ``"No"` `<< endl; ` ` `  `    ``return` `0; ` `} `

Output:

```No
```

Time complexity: O(N * log N)

Similar articles: Check if the given permutation is a valid DFS of graph

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.