37
votes
Accepted
BFS Implementation in Python 3
sets perform containing checks (w in visited) \$O(1)\$ rather than \$O(n)\$ for lists.
...
14
votes
Read CSV and use bidirectional BFS to find the shortest connections between actors
It looks like you found a pretty nice approach there already.
A few general things stand out:
Using globals
Magical constants and unsafe C-style arrays/raw pointers:
...
9
votes
BFS Implementation in Python 3
I agree with Mathias Ettinger's use of sets and deques, with two changes:
name the set seen ...
9
votes
Binary Search Tree BFS iteratively
Much of the code looks good. Some comments could be clearer, and there are some correctness issues.
In is_leaf(), please strike this redundant remark:
...
9
votes
Accepted
9
votes
Accepted
Find the first repeating sequence in digits of pi
How do I make it simpler and faster?
A more efficient approach:
Iterate on pi with a sliding window of size n.
Save each ...
8
votes
Number of Islands in a 2d grid
One way to simplify the problem is a shift of datastructure to hold your islands. By converting the grid to a set containing only the "land" parts, you can speed up ...
8
votes
Accepted
Iterator for BFS binary tree traversal
I'll ignore the 'PluralSight' code.
Note that you have implemented a breadth-first traversal. As far as I'm concerned, a spec which doesn't specify how the tree is traversed is insufficient, but you ...
7
votes
Path sum in binary tree
If recursion has reached a leaf node then either you have accumulated
the correct sum or not, but in no case it is necessary to check the
left or right child node. Therefore I would change the first
...
7
votes
Accepted
Breadth First Search using adjacency list
Will be fixed soon
Advice 1
In BFS, you don't really need colors. (See alternative implementation.)
Advice 2 (Exposing internals)
...
7
votes
Accepted
Perform BFS on a Binary Tree
PEP-0008: your method names should follow snake_case style, and there should be a space between arguments. So change your method singature to:
...
6
votes
Accepted
Shortest path on a maze with keys and doors
Treatment of Keys
You're doing a whole lot of extra work - both in terms of actual operations and code - the way you're treating keys. You're copying the entire maze everytime you get a key, and ...
6
votes
Accepted
Breadth first search implementation (also includes functions for finding shortest path and number of connected components)
First, Queue is an unnecessary abstraction. JS arrays already provide methods to make it act like a queue. Use them directly instead.
Next is that your logic is ...
6
votes
C++ Graph Implementation
Pointer to vector?
You have two members: a pointer to a vector (which you new, but never delete, which leads to several other ...
6
votes
Accepted
Cows with walkie-talkies (USACO Silver Prob. #3; December '16 )
I'd say that this code has the right amount of verbosity - not too terse, not too wordy. It's very readable, with one exception I'll mention below. In order to assess the performance, I'd probably ...
6
votes
Accepted
Number of Islands in a 2d grid
Complexity
Each cell will be accessed at most 5 times (once to check itself, once per each neighbour that are on land, as part of their BFS search).
This means your overall complexity is O(n) (n ...
6
votes
Number of Islands in a 2d grid
It seems that someone has copied @MathiasEttinger's answer claiming it as their own and posted another question on CR asking for feedback on "their" solution. So here are my improvements to @...
6
votes
Accepted
Degrees of Wikipedia
I think you can potentially remove the impact of the network altogether by using offline Wikipedia dumps. Wikipedia itself asks to not crawl them:
Suppose you are building a piece of software that ...
5
votes
C++ Graph Implementation
First of all, why do you use a raw pointer for
std::vector<int>* Adj;
you can simply use
...
5
votes
Graph theory using C++ STL
I see some things that may help you improve your program, but first let me thank you for asking a good question with a complete sample program. It makes things much easier to review and provides ...
5
votes
Accepted
Recursive Breadth First Search for Knights Tour
I see a number of things that could help you improve your code.
typedef struct is not needed in C++
In KMBaseData.h, the code ...
5
votes
SPOJ solution of GRAVITY
I cannot review the content of your code because it is very, very unreadable, but I just wanted to point a few things out.
According to the signature, bfs is ...
5
votes
SPOJ solution of GRAVITY
This #define here:
#define ull unsigned long long
Should be changed to a typedef instead, ...
5
votes
Accepted
Generalized Missionaries and Cannibals in Java
main
long tb = System.currentTimeMillis();
System.out.println("Duration: " + (tb - ta) + " milliseconds.");
...
5
votes
Cows with walkie-talkies (USACO Silver Prob. #3; December '16 )
A word on programming competitions
I realize that when solving a problem from a programming competition, you aren't going to end up with the nicest code. It's more about getting something that works ...
5
votes
Accepted
Find the shortest path through a maze with a twist: you can knock down one wall
So here is my version of your code. Changes made:
1. pep8:
I changed the code to more closely conform to pep8. This is important when sharing code as the consistent style makes it much easier for ...
5
votes
Accepted
Binary Search Tree BFS iteratively
[Caveat: This review is premised on a rigorous technical interview context. It takes the specification literally.]
High order bit
To me, the code does not really meet the specification.
The ...
5
votes
Accepted
C++ breadth-first search using adjacency matrix
A bug
The path printed by main (1, 3, 4, 6, 7) is not shortest. The culprit is the printPath: it needs to know, for each node \...
5
votes
Accepted
Breadth First Search implementation in Python 3 to find path between two given nodes
Comments
Provide a docstring, detailing what the code does and returns
Pep-8
When writing Python, it is advised to follow the styleguide. This means snake_case ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
breadth-first-search × 180graph × 56
python × 54
algorithm × 50
c++ × 39
java × 37
performance × 33
depth-first-search × 32
tree × 25
programming-challenge × 22
python-3.x × 21
pathfinding × 21
c# × 20
javascript × 11
time-limit-exceeded × 10
interview-questions × 7
object-oriented × 6
haskell × 6
c++11 × 5
queue × 5
memory-optimization × 5
sliding-tile-puzzle × 5
beginner × 4
c × 4
python-2.x × 4