bfs solutions java

# tree level-by-level traversal. O(n) time/space
def breadthFirstSearch(root):
    q = [root]

    while q:
        current = q.pop(0)
        print(current)
        if current.left is not None: q.append(current.left)
        if current.right is not None: q.append(current.right)

0
0
HMWDesign 85 points

                                    #include<iostream>
#include <list>
 
using namespace std;
 


class Graph
{
    int V;   
 
  
    list<int> *adj;   
public:
    Graph(int V);  
 
    
    void addEdge(int v, int w); 
 
    
    void BFS(int s);  
};
 
Graph::Graph(int V)
{
    this->V = V;
    adj = new list<int>[V];
}
 
void Graph::addEdge(int v, int w)
{
    adj[v].push_back(w); 
}
 
void Graph::BFS(int s)
{
  
    bool *visited = new bool[V];
    for(int i = 0; i < V; i++)
        visited[i] = false;
 
   
    list<int> queue;
 
   
    visited[s] = true;
    queue.push_back(s);
 
   
    list<int>::iterator i;
 
    while(!queue.empty())
    {
       
        s = queue.front();
        cout << s << " ";
        queue.pop_front();
 
      
        for (i = adj[s].begin(); i != adj[s].end(); ++i)
        {
            if (!visited[*i])
            {
                visited[*i] = true;
                queue.push_back(*i);
            }
        }
    }
}
 

int main()
{
    
    Graph g(4);
    g.addEdge(0, 1);
    g.addEdge(0, 2);
    g.addEdge(1, 2);
    g.addEdge(2, 0);
    g.addEdge(2, 3);
    g.addEdge(3, 3);
 
    cout << "Following is Breadth First Traversal "
         << "(starting from vertex 2) \n";
    g.BFS(2);
 
    return 0;
}

0
0
3.75
8
M. Wildy 115 points

                                    package com.javaaid.hackerrank.solutions.tutorials.ctci;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;

class Graph {

  private final int V;
  private int E;
  private ArrayList<Integer>[] adj;

  Graph(int V) {
    adj = (ArrayList<Integer>[]) new ArrayList[V + 1];
    this.V = V;
    this.E = 0;
    for (int v = 1; v <= V; v++) adj[v] = new ArrayList<Integer>(V);
  }

  Graph(Scanner in) {
    this(in.nextInt());
    int E = in.nextInt();
    for (int i = 0; i < E; i++) {
      int v = in.nextInt();
      int w = in.nextInt();
      addEdge(v, w);
    }
  }

  public int V() {
    return V;
  }

  public int E() {
    return E;
  }

  public void addEdge(int v, int w) {
    adj[v].add(w);
    adj[w].add(v);
    E++;
  }

  public Iterable<Integer> adj(int v) {
    return adj[v];
  }
}

class BreadthFirstPaths {

  private int s;
  private boolean marked[];
  private int edgeTo[];

  BreadthFirstPaths(Graph G, int s) {
    marked = new boolean[G.V() + 1];
    this.s = s;
    edgeTo = new int[G.V() + 1];
    bfs(G, s);
  }

  private void bfs(Graph G, int s) {
    Queue<Integer> q = (Queue<Integer>) new LinkedList<Integer>();
    q.add(s);
    while (!q.isEmpty()) {
      int v = q.poll();
      marked[v] = true;
      for (int w : G.adj(v)) {
        if (!marked[w]) {
          marked[w] = true;
          edgeTo[w] = v;
          q.add(w);
        }
      }
    }
  }

  public Iterable<Integer> pathTo(int v) {
    if (!hasPathTo(v)) return null;
    Stack<Integer> path = new Stack<Integer>();
    for (int x = v; x != s; x = edgeTo[x]) path.push(x);
    path.push(s);
    return path;
  }

  public boolean hasPathTo(int v) {
    return marked[v];
  }
}

public class BFSShortestReachInAGraph {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int q = sc.nextInt();
    for (int i = 0; i < q; i++) {
      Graph G = new Graph(sc);
      int s = sc.nextInt();
      BreadthFirstPaths bfp = new BreadthFirstPaths(G, s);
      for (int v = 1; v <= G.V(); v++) {
        if (s != v) {
          if (bfp.hasPathTo(v)) {
            Stack<Integer> st = (Stack<Integer>) bfp.pathTo(v);
            int sum = 0;
            for (int x = 1; x < st.size(); x++) {
              sum += 6;
            }
            System.out.print(sum + " ");
          } else {
            System.out.print(-1 + " ");
          }
        }
      }
      System.out.println();
      sc.close();
    }
  }
}

3.75 (8 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
bfs tree in c++ python graph breadth first search breadth first search python implmementation breadth first search pyton list breadth first search python Python code that implements BFS (Breadth-First Search) bfs and dfs cpp c++ bfs algorithm bfs in graph in c++ breadth first python breadth search algorithm python BFS of graph program in cpp BFS progarm in cpp bfs binary tree c++ c++ program for BFS code bfs on frid cpp code for finding bfs in c++ breadth first search python library breadth first algorithm python bfsutil cpp bfs on graph c++ bfs class c++ write a program to implement breadth first search using python stack python using breadth first search implementation of bfs in graph in c++ bfs code java BFS in cp bfs on tree c++ bfs algorithm program c++ bfs tree cpp bfs algorithm in cpp bfs algorithm cpp bfs program in c plus plus code implementation of BFS algorithm in c++ BFS and DFS in c++ code for bfs in c++ breadth first search using python bfs c++ tree bfs code c++ implementing breadth first search in python breadth first search bfs program in python bfs data structure code in c++ a) breadth first search BFS in graph c++ bfs program in java graph bfs c++ program in cpp to bfs python breadth-first search breadth first search source code in python bfs implementation in java python code for breadth first search bfs algorithm java¨ BFS search C++ bfs in tree in c++ implementing a breadth first search in python java bfs implementation program to implement BFS algorithm in C++ c++ bfs code bfs graph c++ bfs code for c++ breath first search in ython bfs tree c++ bfs in tree c++ breadth first search using class python bfs c++ display bfs c++ github Breadth-first Search python code bfs c++ using class bfs algorithm c++ breadth first search path python BFS CP algorithms bfs code in c++ breadth first implementation in python bfs in c++ using array bfs in c++ code Breadth First Search algorithm on graph python implementation Breadth First Search algorithm python implementation bfs and dfs program in cpp c++ graph bfs bfs c++ stl bfc cpp bfs vector c++ bfs code c++ stl bfs using java bfs algorithm in c++ bfs example bfs algorithm in c++ bfs java bfs on graph cpp breadth first search python RECU breadth search python python breadth first seach python breadth first search iterative how to implement BFS in Java breadth first search example python bfs with solution java breadth-first search in python bfs algorithm java simple bfs code in c++ bfs implementation java two way breadth first search python how to implement breadth first search in python how to make algorithm bfs in cpp methode bfs in cpp python breadth first search breadfirst search breadth first search pythn breadth first search python code bfs in graph in cpp breadth first search implementation python bfs program in data structure using c++ bfs using stl in c++ what is breadth first search python bfs java geesk bfs java code bfs code in java tree breadth first search python bfs in binary tree c++ breadth first search algorithm python example breadth first search algorithm python breadth first search python implementation bfs traversal gfg java BFS breath first search breadth first searcg python breadthfirst search pyhon bfs and dfs c++ a* graph search example java bfs and dfs in python breadth first search graph java bfs in undirected graph bfs functiojn in python breadth first search python module explain bfs in python breadht first search java breadth for search python bfs cpp bfs in data structure java Breadth first search on the grahp using linked list ... processing java best implementation for BFS traversing graph bfs c++ code breadth first search tree python bfs with adjacency list breadth first search in JAVA bfs traversal of graph breadh first search how to do bfs in java bfs implementation cpp bfs in java bfs template python breadth first search algorithm java bfs code graph bfs in python why is bfs implemented using queue bfs in graph bfs algorithm example bfs code gfg breadth first search explained bfs of following graph starting from vertex A bfs of following graph vertex A Breadth-First Graph Search g.LSC_BF() c++ depth first search and breadth first search use of breadth first search bfs in python bfs graph traversal example BFS implementation in bfs in python in graph Given an undirected and disconnected graph G(V, E), print its BFS traversal. python Given an undirected and disconnected graph G(V, E), print its BFS traversal. BFS of graph bfs with queue java BFS of graph gfg dfs bfs java bfs dfs java bfs algorithm python breath first search cpp breath first search python bfs algorithm in python breath first search in graph examle bfs of graph code breadth first search in python bfs graph code in python breadth first traversal bfs of graph in cpp Implement a breadth First Search breadth first search python bfs bfs in cpp bfs python breadth first search algorithm python code DFS BFS implementation in c++ implement bfs in java breadth first traversal of a graph breadth first search cpp create Breadth-First Search tree python graph breadth first search bfs implementation in cpp breadth first traversal graph bfs implementation in python what is breadth first search java bfs and dfs in cpp bfs graph python bsf in python pyhtom bfs alogorithm list breadth first search python breadth search in python node bfs stl recursive Explain Breadth-first search and Depth-first search. bfs solutions java python depth first search python breadth first search binary tree python breadth first search adjacency list bfs c++ breadth first search and depth first search in graphs bredth first search java graph bfs bfs function c++ breadth first search algorithm bfs queue java c++ code of bfs bfs on graph java bfs in c++ using stl bfs using set c++ bfs traversal in graph bfs implementation using c++ bfs using queue in c++ graph create using list and queue graph creation using breadth first search bfs implementation in java example java bfs algorithm java breadth first graph using queue c++ Program to create a graph and use breath First Search porogram of bfsin cpp bfs in c++ using queue breadth first search in c++ using adjacency list BFS approachable nodes from a given source of a given graph using queue as an intermediate data structure (BFS). java breadth-first traverses bfs code in cpp breadth-first search python (C) Bredth first Search and Depth first bfs java implementation algorithm java bfs Write a program to find approachable nodes from a given source of a given graph using queue as an intermediate data structure (BFS). (Only code) bfs algorithm using geeksforgeeks Program for Breadth First Search (BFS) for graph traversal IN CPP cpp adjacency list bfs algo cpp adjacency list bfs find out the breardh first tree automatically form the graph Breadth-First Search c++ breadth first search java breath first search of a graph python geeksforgeeks bfs dfs breadth first search c++ how to do a breadth first search java BFS in c++
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source