Given **N** and **E**, the number of nodes and edges in a directed graph. The task is to do Breadth First Search of this graph.

**Input:**

The first line of input contains the number of test cases **T**. For each test case, the first line of input contains N and E separated by space, and next line contains 2*E numbers (E pairs as X Y) are given in the next line which represents an edge from X to Y.

**Output:**

For each testcase, print the BFS of the graph starting from 0.

**Note:** The expected output button always produces BFS starting from node 0.

**User Task:**

Since, this is a functional problem, your task is to complete the function **bfs**() which do BFS of the given graph starting from node 0 and prints the nodes in BFS order.

**Constraints:**

1 <= T <= 100

1 <= N <= 100

**Example:
Input:**

2

5 4

0 1 0 2 0 3 2 4

3 2

0 1 0 2

**Output:**

0 1 2 3 4 // BFS from node 0

0 1 2

**Explanation:
Testcase 1:** First line of each test case represent an integer N denoting number of nodes and edges and then in the next line N pairs of values

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

the_coder95 | 465 |

thanuvinu94 | 388 |

adityasuman2025 | 348 |

Senbonzakura_Kageyoshi | 321 |

PrakharBansal1 | 294 |

the_coder95 | 1691 |

RishabhTanwar1 | 1110 |

thanuvinu94 | 1064 |

samay_kam_hai | 726 |

adityasuman2025 | 646 |

blackshadows | 5331 |

Ibrahim Nash | 5219 |

akhayrutdinov | 5111 |

mb1973 | 4897 |

Quandray | 4547 |

Login to report an issue on this page.