Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 5725 |

@blackshadows | 5685 |

@akhayrutdinov | 5111 |

@mb1973 | 4989 |

@Quandray | 4908 |

@saiujwal13083 | 4156 |

@sanjay05 | 3762 |

@marius_valentin_dragoi | 3511 |

@sushant_a | 3453 |

@KshamaGupta | 3318 |

@the_coder95 | 3302 |

Complete Leaderboard | |

Handle | Score |

@shashi1iitk | 398 |

@Suniti Jain | 114 |

@ankish2000nayak | 104 |

@gopalsethlnct | 94 |

@senthil1185 | 90 |

@esmadhan | 90 |

@gyanadipta07 | 89 |

@satwikdash2000 | 85 |

@varunmishra3040 | 83 |

@gamec1634 | 82 |

@pj_1721 | 80 |

Complete Leaderboard |

Given a directed graph. The task is to do Breadth First Search of this graph.

**Input:**

The first line of the input contains an integer **'T'** denoting the number of test cases. Then **'T'** test cases follow. Each test case consists of two lines. Description of testcases is as follows: The First line of each test case contains two integers **'N' **and **'E' ** which denotes the no of vertices and no of edges respectively. The Second line of each test case contains **'E' ** space separated pairs **u** and **v** denoting that there is a edge from **u** to **v** .

**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:**

You don't need to read input or print anything. Your task is to complete the function **bfs**() takes the Graph and the number of vertices as its input and returns a list containing the BFS traversal of the graph starting from the 0th vertex.

**Expected Time Complexity: **O(V + E).

**Expected Auxiliary Space: **O(V).

**Constraints:**

1 <= T <= 100

2 <= N <= 10^{4}

1 <= E <= (N*(N-1))/2

Graph doesn't contain multiple edges and self loops.

**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:**

0 is connected to 1 , 2 , 3

2 is connected to 4

so starting from 0 , bfs will be 0 1 2 3 4.

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes
BFS of graph

...

Please choose 'ReadOnlyMode' if you needn't to 'Edit' the problem e.g. viewing OJ's solution, TestCase Files (TCFs), TimeLimit etc.