Breadth First Traversal

4

1 votes
Easy
Problem

Given n, i.e. total number of nodes in an undirected graph numbered from 1 to n and an integer e, i.e. total number of edges in the graph. Calculate the Breadth First order of nodes considering node 1 as source node.

Input Format:

First line of input line contains two integers n and e. Next e line will contain two integers u and v meaning that node u and node v are connected to each other in undirected fashion.

Output Format:

For each input graph print the breadth first order of traversal of nodes.

Constraints:

All the input values are well with in the integer range.

Sample Input
5 4
1 2
2 3
2 4
3 5
Sample Output
1 2 3 4 5 
Time Limit: 5
Memory Limit: 256
Source Limit:
Explanation

Constructed graph will look like above hence breadth first order will be 1 2 3 4 5 or 1 2 4 3 5.

Editor Image

?