# Print the lexicographically smallest BFS of the graph starting from 1

Given a connected graph with N vertices and M edges. The task is to print the lexicographically smallest BFS traversal of the graph starting from 1.

Note: The vertices are numbered from 1 to N.

Examples:

```Input: N = 5, M = 5
Edges:
1 4
3 4
5 4
3 2
1 5
Output: 1 4 3 2 5
Start from 1, go to 4, then to 3 and then to 2 and to 5.

Input: N = 3, M = 2
Edges:
1 2
1 3
Output: 1 2 3
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: Instead of doing a normal BFS traversal on the graph, we can use a priority queue(min heap) instead of a simple queue. When a node is visited add its adjacent nodes into the priority queue. Every time, we visit a new node, it will be the one with the smallest index in the priority queue. Print the nodes when every time we visit them starting from 1.

Below is the implementation of the above approach:

 `// C++ program to print the lexcicographically ` `// smallest path starting from 1 ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to print the smallest lexicographically ` `// BFS path starting from 1 ` `void` `printLexoSmall(vector<``int``> adj[], ``int` `n) ` `{ ` `    ``// Visited array ` `    ``bool` `vis[n + 1]; ` `    ``memset``(vis, 0, ``sizeof` `vis); ` ` `  `    ``// Minimum Heap ` `    ``priority_queue<``int``, vector<``int``>, greater<``int``> > Q; ` ` `  `    ``// First one visited ` `    ``vis = ``true``; ` `    ``Q.push(1); ` ` `  `    ``// Iterate till all nodes are visited ` `    ``while` `(!Q.empty()) { ` ` `  `        ``// Get the top element ` `        ``int` `now = Q.top(); ` ` `  `        ``// Pop the element ` `        ``Q.pop(); ` ` `  `        ``// Print the current node ` `        ``cout << now << ``" "``; ` ` `  `        ``// Find adjacent nodes ` `        ``for` `(``auto` `p : adj[now]) { ` ` `  `            ``// If not visited ` `            ``if` `(!vis[p]) { ` ` `  `                ``// Push ` `                ``Q.push(p); ` ` `  `                ``// Mark as visited ` `                ``vis[p] = ``true``; ` `            ``} ` `        ``} ` `    ``} ` `} ` ` `  `// Function to insert edges in the graph ` `void` `insertEdges(``int` `u, ``int` `v, vector<``int``> adj[]) ` `{ ` `    ``adj[u].push_back(v); ` `    ``adj[v].push_back(u); ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `n = 5, m = 5; ` `    ``vector<``int``> adj[n + 1]; ` ` `  `    ``// Insert edges ` `    ``insertEdges(1, 4, adj); ` `    ``insertEdges(3, 4, adj); ` `    ``insertEdges(5, 4, adj); ` `    ``insertEdges(3, 2, adj); ` `    ``insertEdges(1, 5, adj); ` ` `  `    ``// Function call ` `    ``printLexoSmall(adj, n); ` ` `  `    ``return` `0; ` `} `

Output:

```1 4 3 2 5
```

My Personal Notes arrow_drop_up Striver(underscore)79 at Codechef and codeforces D

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.

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.