bfs solutions java

Solutions on MaxInterview for bfs solutions java by the best coders in the world

showing results for - "bfs solutions java"
Swann
15 Jun 2018
1# tree level-by-level traversal. O(n) time/space
2def breadthFirstSearch(root):
3    q = [root]
4
5    while q:
6        current = q.pop(0)
7        print(current)
8        if current.left is not None: q.append(current.left)
9        if current.right is not None: q.append(current.right)
Salvatore
22 Jun 2019
1package com.javaaid.hackerrank.solutions.tutorials.ctci;
2
3import java.util.ArrayList;
4import java.util.LinkedList;
5import java.util.Queue;
6import java.util.Scanner;
7import java.util.Stack;
8
9class Graph {
10
11  private final int V;
12  private int E;
13  private ArrayList<Integer>[] adj;
14
15  Graph(int V) {
16    adj = (ArrayList<Integer>[]) new ArrayList[V + 1];
17    this.V = V;
18    this.E = 0;
19    for (int v = 1; v <= V; v++) adj[v] = new ArrayList<Integer>(V);
20  }
21
22  Graph(Scanner in) {
23    this(in.nextInt());
24    int E = in.nextInt();
25    for (int i = 0; i < E; i++) {
26      int v = in.nextInt();
27      int w = in.nextInt();
28      addEdge(v, w);
29    }
30  }
31
32  public int V() {
33    return V;
34  }
35
36  public int E() {
37    return E;
38  }
39
40  public void addEdge(int v, int w) {
41    adj[v].add(w);
42    adj[w].add(v);
43    E++;
44  }
45
46  public Iterable<Integer> adj(int v) {
47    return adj[v];
48  }
49}
50
51class BreadthFirstPaths {
52
53  private int s;
54  private boolean marked[];
55  private int edgeTo[];
56
57  BreadthFirstPaths(Graph G, int s) {
58    marked = new boolean[G.V() + 1];
59    this.s = s;
60    edgeTo = new int[G.V() + 1];
61    bfs(G, s);
62  }
63
64  private void bfs(Graph G, int s) {
65    Queue<Integer> q = (Queue<Integer>) new LinkedList<Integer>();
66    q.add(s);
67    while (!q.isEmpty()) {
68      int v = q.poll();
69      marked[v] = true;
70      for (int w : G.adj(v)) {
71        if (!marked[w]) {
72          marked[w] = true;
73          edgeTo[w] = v;
74          q.add(w);
75        }
76      }
77    }
78  }
79
80  public Iterable<Integer> pathTo(int v) {
81    if (!hasPathTo(v)) return null;
82    Stack<Integer> path = new Stack<Integer>();
83    for (int x = v; x != s; x = edgeTo[x]) path.push(x);
84    path.push(s);
85    return path;
86  }
87
88  public boolean hasPathTo(int v) {
89    return marked[v];
90  }
91}
92
93public class BFSShortestReachInAGraph {
94
95  public static void main(String[] args) {
96    Scanner sc = new Scanner(System.in);
97    int q = sc.nextInt();
98    for (int i = 0; i < q; i++) {
99      Graph G = new Graph(sc);
100      int s = sc.nextInt();
101      BreadthFirstPaths bfp = new BreadthFirstPaths(G, s);
102      for (int v = 1; v <= G.V(); v++) {
103        if (s != v) {
104          if (bfp.hasPathTo(v)) {
105            Stack<Integer> st = (Stack<Integer>) bfp.pathTo(v);
106            int sum = 0;
107            for (int x = 1; x < st.size(); x++) {
108              sum += 6;
109            }
110            System.out.print(sum + " ");
111          } else {
112            System.out.print(-1 + " ");
113          }
114        }
115      }
116      System.out.println();
117      sc.close();
118    }
119  }
120}
121
Alecia
24 Aug 2017
1#include<iostream>
2#include <list>
3 
4using namespace std;
5 
6
7
8class Graph
9{
10    int V;   
11 
12  
13    list<int> *adj;   
14public:
15    Graph(int V);  
16 
17    
18    void addEdge(int v, int w); 
19 
20    
21    void BFS(int s);  
22};
23 
24Graph::Graph(int V)
25{
26    this->V = V;
27    adj = new list<int>[V];
28}
29 
30void Graph::addEdge(int v, int w)
31{
32    adj[v].push_back(w); 
33}
34 
35void Graph::BFS(int s)
36{
37  
38    bool *visited = new bool[V];
39    for(int i = 0; i < V; i++)
40        visited[i] = false;
41 
42   
43    list<int> queue;
44 
45   
46    visited[s] = true;
47    queue.push_back(s);
48 
49   
50    list<int>::iterator i;
51 
52    while(!queue.empty())
53    {
54       
55        s = queue.front();
56        cout << s << " ";
57        queue.pop_front();
58 
59      
60        for (i = adj[s].begin(); i != adj[s].end(); ++i)
61        {
62            if (!visited[*i])
63            {
64                visited[*i] = true;
65                queue.push_back(*i);
66            }
67        }
68    }
69}
70 
71
72int main()
73{
74    
75    Graph g(4);
76    g.addEdge(0, 1);
77    g.addEdge(0, 2);
78    g.addEdge(1, 2);
79    g.addEdge(2, 0);
80    g.addEdge(2, 3);
81    g.addEdge(3, 3);
82 
83    cout << "Following is Breadth First Traversal "
84         << "(starting from vertex 2) \n";
85    g.BFS(2);
86 
87    return 0;
88}
queries leading to this page
c 2b 2b program for bfs codebreadth first search python librarylinear breadth first search algorithm pythonbfs in cpbreadth first search algorithm on graph python implementationbfs implementation in javabreadth search in python nodefind out the breardh first tree automatically form the graphbreadth first traversalbfs program in javacode implementation of bfs algorithm in c 2b 2bbfs and dfs in c 2b 2bbreadth first searchbfs java geeskimplementing breadth first search in pythonbfs of graphjava bfs algorithmbfs graph c 2b 2bhow to implement bfs in javabfs on graph c 2b 2bbreadth first search on the grahp using linked list code for bfs in c 2b 2bbfs with queue javabfs code cppbfs traversal gfgbreadth for search pythonbreath first search pythoncpp adjacency list bfs algobreadth search pythonbfs tree c 2b 2bbreadth first search tree python code a 29 breadth first searchpython code for breadth first searchbreadth first search pythonhow to make algorithm bfs in cpppython depth first searchbfs queue javag lsc bf 28 29 c 2b 2bbfs c 2b 2b codejava breadth firstprogram to create a graph and use breath first searchgraph creation using breadth first searchbreadth first search python modulebreadth first pythonbfs class c 2b 2bpython breadth first searchbfs c 2b 2b githubbfs on tree c 2b 2bdfs bfs javabreadth first search pythonbfsbfs examplea 2a graph search example javabfs algorithm using geeksforgeeksbreath first search in graph examlebfs progarm in cppbreadth first search python implmementation bfs codebfs c 2b 2b treepython code that implements bfs 28breadth first search 29breadth first graph searchbreadth first search implementation pythonbfs method javabfs implementation in java examplehow to do a breadth first search javabfs of graph codebfs code in javabfs c 2b 2b displaybfs in graphimplement a breadth first searchbfs c 2b 2b using classbfs program in data structure using c 2b 2bbfs using stl in c 2b 2bbfs algorithm in cppbfs search c 2b 2bbfs algorithm in c 2b 2bbfs using javabfs of following graph starting from vertex abfs algorithm examplewrite a program to find approachable nodes from a given source of a given graph using queue as an intermediate data structure 28bfs 29 28only code 29breadthfirst search pyhongraph bfs c 2b 2b algorithm java bfsbfs code c 2b 2b stlbreadth first search python recubfs binary tree c 2b 2bexplain bfs in pythonwhy is bfs implemented using queuebfs in pythonpython breadth first search binary treegraph bfsgraph create using list and queuewrite a program to implement breadth first search using pythonpython display breadth first searchbfs code c 2b 2bbfs in python in graphprocessing java best implementation for bfs traversing graphbfs of graph in cppbreadth first search javabreadth first implementation in pythonbfs in data structure javagraph bfs in pythonpyhtom bfs alogorithm listbfs algorithm cppbreadth first search using class pythonbreadth first search for node to node pythonbfs and dfs in cppbfs code in cppbfs in javabreadth first search python codebfs algorithm java c2 a8breadth first search source code in pythonbfs on graph javabfs algorithm in c 2b 2bbfs algorithm javahow to implement breadth first search in pythonbfs traversal of graphpython breadth first search iterativeuse of breadth first searchjava breadth first traversesexplain breadth first search and depth first search 28c 29 bredth first search and depth firstbfs implementation using c 2b 2bbfsutil cppbreadth first search algorithm pythonbfs traversal in graphbfs in c 2b 2bbfs and dfs program in cppprogram for breadth first search 28bfs 29 for graph traversal in cppbreath first search of a graph pythonbfs java implementationbredth first search javapython breadth first seachbfs algorithm pythonprogram in cpp to bfsdfs bfs implementation in c 2b 2bbreadth first search explainedbfs template pythonbfs algorithm in pythonbreadth first search program in pythonbfs on graph cppprogram to implement bfs algorithm in c 2b 2b approachable nodes from a given source of a given graph using queue as an intermediate data structure 28bfs 29 bfs and dfs cppbreath first search in ythonhow to do bfs in javabfs of a tree c 2b 2bbreadth first search python implementationbreadth first search python programbfs with adjacency listjava bfs implementationbfs implementation in pythonbfs function c 2b 2bdepth first search and breadth first searchbfs in c 2b 2b using queuebfs implementation in cppbfs implementation cppbreadth first search algorithm javabreadth first search and depth first search in graphsbreadth first search path pythonbfs in c 2b 2b codebreadth first search c 2b 2bbfs algorithm c 2b 2bbfs and dfs c 2b 2bbfs java codemethode bfs in cppbreadth first search 28bfs 29 program pythonbreath first search cppbreadth first search in javabfs in binary tree c 2b 2blist breadth first search pythonbfs in c 2b 2b using arraytwo way breadth first search pythonbfc cpptree breadth first search pythonsimple bfs code in c 2b 2bbfs pythonpython breadth first search breadfirst searchbreadth first search bfs program in pythonbreadth first search algorithm python examplebreadth first search graph javabfs solutions javawrite a program for breadth first search 28bfs 29 in python breadth first search tree pythonbfs code for c 2b 2bbreadth first search algorithm python codecpp adjacency list bfsbfs with solution javabfs on frid cppbfs in graph in cppbreadth first search example pythonbfs in undirected graphbreath first searchbfs in cppbreadth first search in c 2b 2b using adjacency listbfs graph pythonpython breadth first searchc 2b 2b bfs codebfs data structure code in c 2b 2b breadth first search algorithm python implementationbfs of following graph vertex astack python using breadth first searchbreadth first search code python bfs and dfs in pythonbreadh first searchbfs using queue in c 2b 2bbreadth first search algorithmbinary tree bfs c 2b 2bbfs stl recursivebfs implementation in given an undirected and disconnected graph g 28v 2c e 29 2c print its bfs traversal pythonwhat is breadth first search pythonbfs in graph c 2b 2bbfs c 2b 2b stlpython bfsadjacency list bfs c 2b 2bbfs code in c 2b 2bcode for finding bfs in c 2b 2bc 2b 2b bfs algorithmbfs cppbreadth first search cppbfs program in c plus plusbreadth first search python codepython traversre breadth first searchbfs vector c 2b 2bbfs implementation javacreate breadth first search tree pythongraph breadth first searchimplementation of bfs in graph in c 2b 2bbfs functiojn in pythonbfs code gfgimplement bfs in javabreadth first algorithm pythonbfs graph traversal examplebreadth first traversal of a graphgeeksforgeeks bfs dfsbreadth first search code in pythonbfs code javabreadth first search pytongiven an undirected and disconnected graph g 28v 2c e 29 2c print its bfs traversal bfs javabfs in graph in c 2b 2bbfs in c 2b 2b using stlbfs tree cppbfs in tree in c 2b 2bjava bfsbreadth first searcg pythonbreadth search algorithm pythonbreadth first search using pythonbreadth first search in pythonbsf in pythongraph using queue c 2b 2bgraph breadth first pythonbreadth first traversal graphbfs cp algorithmsc 2b 2b code of bfsbreadht first search javabreadth first search in pythonbfs of graph gfgbfs dfs javapython graph breadth first searchbfs using set c 2b 2bbreadth first search pythngraph breadth first search in pythonbfs graph code in pythonimplementing a breadth first search in pythonbfs in tree c 2b 2bbfs algorithm program c 2b 2bc 2b 2b graph bfsbfs tree in c 2b 2bwhat is breadth first search javaporogram of bfsin cppbreadth first search c 2b 2bbfs of graph program in cppbfs solutions java