Skip to main content

Questions tagged [breadth-first-search]

In graph theory, breadth-first search (BFS) is a graph search algorithm that begins at the root node and explores all the neighboring nodes. Then for each of those nearest nodes, it explores their unexplored neighbor nodes, and so on, until it finds the goal.

Filter by
Sorted by
Tagged with
3 votes
1 answer
256 views

I was playing around with leetcode's Number of Islands. As per the challenge's description: Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the ...
ccot's user avatar
  • 351
-1 votes
2 answers
133 views

there are some problems in this code that I can't think of a solution. It does work for print(real_path,count) but the real thing I want is return real_path,count and for doing this I need to make the ...
tumr's user avatar
  • 53
1 vote
1 answer
149 views

Let's consider a matrix of integers m: [[1,2,3] [4,5,6] [7,8,9]] Which represents a graph: ...
Gameplay's user avatar
  • 171
7 votes
1 answer
137 views

I have implemented a breadth-first search algorithm to find the distance from a point to an edge of a grid. The grid consists of spaces that may have walls between them, effectively blocking certain ...
Andreas Tostrup Christensen's user avatar
4 votes
1 answer
857 views

Task: implement an algorithm to find graph center \$Z(G)\$ given undirected tree \$G\$. This is my first time programming in C++ so any (elementary) feedback is appreciated. The way I did it is: Run ...
average math enjoyer's user avatar
6 votes
2 answers
269 views

I am trying to solve LC126, and got a memory limit exceeded for the following solution. I've seen the answer here -- so I'll try to use Dijkstra as well, but I would like to understand why I get the ...
Mircea's user avatar
  • 322
1 vote
1 answer
279 views

I am writing a Python code to find the connected components of a graph using BFS. The code works but takes a long time to run. Please suggest possible optimizations of my code. The graph is stored as ...
NBK's user avatar
  • 11
1 vote
1 answer
187 views

I am working on Word Ladder - LeetCode Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence from beginWord to endWord, such that: ...
abybaddi009's user avatar
0 votes
1 answer
253 views

The following code is applying the depth-first search and breadth-first search algorithm to find the shortest path from bottom left to top right in a 10 x 10 grid. ...
Mohamed Magdy's user avatar
1 vote
0 answers
170 views

How should I structure this code to create a maze, solve it with breath-first-search (BFS) and to provide basic navigation movements by 1 step within maze with number of moves required to navigate? ...
Matthew's user avatar
  • 11
5 votes
1 answer
441 views

I'm writing a breadth first search algorithm in Haskell that supports pruning. It is guaranteed that the search will never encounter nodes that have been visited before, so I implemented the search as ...
Trebor's user avatar
  • 153
1 vote
1 answer
103 views

For context, I wrote a function, get_servers, for the game Bitburner which is usually played by writing JavaScript code. Instead, I used PureScript to write the ...
nicoty's user avatar
  • 329
3 votes
0 answers
394 views

This is a problem based on the game "NGU Industries" in which the objective is to build a factory. Each tile can hold either a production building or a "beacon" which increases the ...
Mark Green's user avatar
8 votes
2 answers
576 views

I wrote a function to return the first n length sequence which occurs twice in the digits of pi. Along with the repeated sequence, I want the indices of the first and second occurrences. For example, ...
cona's user avatar
  • 83
5 votes
1 answer
189 views

I am creating a program that will be given a text file as input with the board's dimension and the piece ID along with the piece width and length. The goal is to arrange all blocks in the board ...
Mperez's user avatar
  • 59
9 votes
2 answers
1k views

I have been coding 'Pac-Man' with my 16yo. A hopefully not too boring project to help improve his Python coding. We have just moved the 'Ghosts' into a class which was a good first introduction to ...
Kevin W's user avatar
  • 191
5 votes
1 answer
746 views

I tried solving Leetcode 01 Matrix problem. It is running too slow when solved using DFS approach. Given a matrix consists of 0 and 1, find the distance of the nearest 0 for each cell. The distance ...
Akanksha's user avatar
  • 129
4 votes
1 answer
318 views

here below a working implementation that finds the minimal distance between k(set =4 below) clusters in a graph. I have doubts mainly on the implementation of the ...
Giogre's user avatar
  • 515
4 votes
1 answer
304 views

So I have a 12 x 6 2D numpy array for input which consists of 7 possible board objects (characters from 'abcdefg'). From a 2D numpy array I wish to generate all the possible unique 2D arrays, in whose ...
OldGrog's user avatar
  • 41
3 votes
0 answers
241 views

I have the below implementation of the BFS algorithm, in which I've used OrderedDict instead of a list or ...
Saurabh's user avatar
  • 445
1 vote
1 answer
119 views

So after years of teaching myself, I think I'm ready to stand on the shoulders of giants and ask you a good question! I have a tree of nodes. What I want is a function that returns an array of the ...
aName's user avatar
  • 143
2 votes
0 answers
268 views

I was assigned an university project where I had to parse directed graph files coming from SNAP and then convert them into CSR (Compressed Sparse Row) format. Then the client must have the ability to ...
NickDelta's user avatar
  • 165
2 votes
1 answer
79 views

I wrote a program that is supposed to be a breadth-first search algorithm, but I'm very new to search algorithm so I don't know if my method is very effective or if there is a simpler way to do this. ...
IsaacK0's user avatar
  • 29
3 votes
1 answer
339 views

Given an N x N matrix of positive integers, I need to find the shortest path from the first cell of matrix to the last cell of matrix. I can move exactly x steps from any cell, where x is the value of ...
User3000's user avatar
1 vote
2 answers
645 views

I have implemented a BFS. Does this look like a proper implementation of BFS in Python 3? And if you think something needs to be improved, say it. ...
Brijesh Kalkani's user avatar
4 votes
1 answer
718 views

https://leetcode.com/problems/rotting-oranges/ Please review for coding style in 40 minutes job interview. In a given grid, each cell can have one of three values: the value 0 representing an ...
Gilad's user avatar
  • 5,443
9 votes
1 answer
2k views

This maze solver is a continuation to the maze generator I posted here recently Maze generator & animator in Python This code takes an image containing a 2-color maze as input and solves the maze ...
user avatar
3 votes
2 answers
672 views

This is Leetcode problem 139: "Wordbreak" Problem: Given a non-empty string s and a dictionary wordDict containing a list ...
neet's user avatar
  • 101
3 votes
1 answer
78 views

This question is the second version of the code here. I'm writing a general tree class. Specifically, each node should have oen parent, some number of children, and hold a value. I'm looking for ...
Alex F's user avatar
  • 443
4 votes
2 answers
1k views

I got caught in the trap of implementing my own data structure. I've created a very general tree structure, where each node gets one parent, some children, and holds a value. I'm mostly looking for ...
Alex F's user avatar
  • 443
1 vote
1 answer
96 views

The input graph to the bfs function is in the form of edge list representation. ...
Your IDE's user avatar
  • 111
4 votes
1 answer
789 views

This is a Leetcode problem - On an \$N\$ x \$N\$ board, the numbers from 1 to N * N are ...
Justin's user avatar
  • 2,619
5 votes
2 answers
976 views

This is a Leetcode problem - In a 1 million by 1 million grid, the coordinates of each grid square are (x, y) with \$0\$ \$<=\$ ...
Justin's user avatar
  • 2,619
2 votes
1 answer
411 views

This is a Leetcode problem - Strings A and B are K-similar (for some non-negative integer ...
Justin's user avatar
  • 2,619
4 votes
1 answer
3k views

This is a Leetcode problem: On a 2 x 3 board, there are 5 tiles represented by the integers 1 through ...
Justin's user avatar
  • 2,619
4 votes
1 answer
155 views

https://leetcode.com/problems/find-bottom-left-tree-value/ Given a binary tree, find the leftmost value in the last row of the tree. Example 1: Input: ...
Gilad's user avatar
  • 5,443
2 votes
1 answer
171 views

You are given a data structure of employee information, which includes the employee's unique id, his importance value and his direct subordinates' id. For example, employee 1 is the leader of ...
Gilad's user avatar
  • 5,443
3 votes
1 answer
908 views

I have this Java implementation of a Rubik's cube's state. My primary concern is DRYness of my code: RubiksCubeNode.java ...
coderodde's user avatar
  • 32.3k
2 votes
0 answers
114 views

I have improved my BFS in Java according to vnp's suggestions. Again, we wish to find shortest paths in directed unweighted graphs using BFS, competitive style (no ...
coderodde's user avatar
  • 32.3k
3 votes
2 answers
475 views

https://leetcode.com/problems/n-ary-tree-level-order-traversal/ Given an n-ary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example, ...
Gilad's user avatar
  • 5,443
6 votes
3 answers
973 views

https://leetcode.com/problems/maximum-depth-of-n-ary-tree/ Given a n-ary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the ...
Gilad's user avatar
  • 5,443
1 vote
1 answer
429 views

Here my attempt was to write a graph algorithm the way a competitive programmer would write under time pressure. The problem to solve was to find a shortest path in a directed, unweighted graph using ...
coderodde's user avatar
  • 32.3k
4 votes
1 answer
358 views

My goal is to write a GUI application where a user can create a maze and select an algorithm. The passage of the algorithm from a selected start and end point should be visualized. The following ...
Roman's user avatar
  • 2,923
0 votes
0 answers
7k views

I need to find shortest path between two points in a grid given an obstacles.. Given a 2 dimensional matrix where some of the elements are filled with 1 and rest of the elements are filled. Here X ...
user5447339's user avatar
4 votes
1 answer
121 views

I have been studying graphs/BFS/DFS. working on wrapping my head around the following problem Given a grid [m,n] where each cell has a value denotes a color, find the longest connected region that ...
Rami Alshareef's user avatar
3 votes
1 answer
3k views

Please review my Breadth-first search algorithm implementation in Java for finding the shortest path on a 2D grid map with obstacles. The findPath() method ...
Ilya Gazman's user avatar
6 votes
1 answer
1k views

I am really excited that I just wrote my Haskell maze solution in bfs. This is my very first haskell (and I still haven't tried to build a maze in haskell so that maze is hard coded xD) ANY ...
Aries_is_there's user avatar
5 votes
1 answer
2k views

the question is taken from Pramp(really cool site!) there is a more straight forward solution. using recursion. but I thought trying it using BFS. please review only the code of GetCheapestCost ...
Gilad's user avatar
  • 5,443
1 vote
1 answer
358 views

I'm implementing a BFS traversal in a grid-like structure. Because I wanted to experiment with ES6 Generators for a long time, I thought I'd implement the search using that abstraction. I am sure ...
Sunyatasattva's user avatar
3 votes
1 answer
307 views

I have an implementation of the flood fill algorithm. Assume that an image is provided as a vector of ...
skr's user avatar
  • 539