From f430a594cec4df1cdb45874a951db5ac71d713cb Mon Sep 17 00:00:00 2001 From: its-sunny <43422638+its-sunny@users.noreply.github.com> Date: Fri, 2 Oct 2020 02:02:14 +0530 Subject: [PATCH] Create bfstraversal.java Given an undirected and disconnected graph G(V, E), print its BFS traversal. Here you need to consider that you need to print BFS path starting from vertex 0 only. V is the number of vertices present in graph G and vertices are numbered from 0 to V-1. E is the number of edges present in graph G. Note : 1. Take graph input in the adjacency matrix. 2. Handle for Disconnected Graphs as well Input Format : Line 1: Two Integers V and E (separated by space) Next 'E' lines, each have two space-separated integers, 'a' and 'b', denoting that there exists an edge between Vertex 'a' and Vertex 'b'. Output Format : BFS Traversal (separated by space) Constraints : 2 <= V <= 1000 1 <= E <= 1000 Sample Input 1: 4 4 0 1 0 3 1 2 2 3 Sample Output 1: 0 1 3 2 --- Graphs 1/bfstraversal.java | 44 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 Graphs 1/bfstraversal.java diff --git a/Graphs 1/bfstraversal.java b/Graphs 1/bfstraversal.java new file mode 100644 index 0000000..ee963ec --- /dev/null +++ b/Graphs 1/bfstraversal.java @@ -0,0 +1,44 @@ +import java.util.Scanner; +import java.util.Queue; +import java.util.*; + +public class Solution { + + public static void printHelper(int edges[][], int sv,boolean visited[]){ + Queue q = new LinkedList<>(); + q.add(sv); + while(q.size()!=0){ + int firstelem = q.remove(); + System.out.print(firstelem+" "); + visited[sv] = true; + for(int i=0; i