Skip to main content
37 votes
Accepted

BFS Implementation in Python 3

sets perform containing checks (w in visited) \$O(1)\$ rather than \$O(n)\$ for lists. ...
301_Moved_Permanently's user avatar
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: ...
sehe's user avatar
  • 1,322
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 ...
sesotek's user avatar
  • 191
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: ...
J_H's user avatar
  • 32.7k
9 votes
Accepted

Solve 8-tile sliding puzzle

Here's the code you want to speed up: ...
Quuxplusone's user avatar
  • 19.4k
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 ...
Marc's user avatar
  • 5,614
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 ...
301_Moved_Permanently's user avatar
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 ...
VisualMelon's user avatar
  • 7,431
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) ...
coderodde's user avatar
  • 29.1k
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: ...
Anshul Goyal's user avatar
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 ...
user1118321's user avatar
  • 11.7k
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 ...
MrBrushy's user avatar
  • 1,228
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 @...
Bailey Parker's user avatar
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 ...
alecxe's user avatar
  • 17.3k
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 ...
Stephen Rauch's user avatar
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 ...
JS1's user avatar
  • 28.6k
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 ...
ben rudgers's user avatar
  • 2,663
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 \...
coderodde's user avatar
  • 29.1k
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 ...
Maarten Fabré's user avatar
5 votes

Iterator for BFS binary tree traversal

Bug There's a bug in your Reset method. public void Reset() { _current = null; } It should also be resetting the ...
t3chb0t's user avatar
  • 44.3k
5 votes
Accepted

Flood Fill using unordered_map which has user-defined type as key

I'll ignore questions 1 and 5 regarding hashing, because you don't need a hash function at all. Storing image data A std::vector<std::vector<int>> is, ...
Cris Luengo's user avatar
  • 6,217
5 votes
Accepted

Breadth-first search(BFS) Implementation in Python 3

There are a few PEP8 issues: In Python3, class definitions have been overhauled a bit, and one can simply do class Node: instead of ...
Cyril D.'s user avatar
  • 456
5 votes
Accepted

Find center of an undirected graph

Don't use <bits/stdc++.h> The header file <bits/stdc++.h> is not part of the C++ standard, and there is no guarantee ...
G. Sliepen's user avatar
  • 60.1k
4 votes

Create a complete binary tree and do level order traversal on it

using namespace std; using namespace std; Why is “using namespace std;” considered bad practice? The particular problem I ...
mdfst13's user avatar
  • 21.7k
4 votes
Accepted

Minimum number of flight segments using breadth-first search

1. Review There's no docstring. What does this function do? What arguments does it take? What does it return? The code uses a list to represent the queue of nodes to be visited. But if you look at ...
Gareth Rees's user avatar
  • 49.7k
4 votes
Accepted

Storing configurations of states to a database - Breadth First Search

Prefer interfaces to implementations public static ArrayList<State> findNeighbours(State currentState) { Consider ...
mdfst13's user avatar
  • 21.7k
4 votes
Accepted

Hackerrank: KnightL on a Chessboard

Main routine can be simplified There are a few things about your CalculateStepsFromLeftTopToBottomRight() function that I feel could be simplified: No need to use <...
JS1's user avatar
  • 28.6k
4 votes
Accepted

Word Ladder JavaScript Implementation

Naming I suggest renaming getShortestPathBetweenWords() to findShortestLadderDistance() for the following reasons: get is ...
le_m's user avatar
  • 1,925
4 votes
Accepted

Performance of BFS in Python 3

1. Review The bfs function relies on a global variable distance. This make it hard to test (you have to remember to set up the <...
Gareth Rees's user avatar
  • 49.7k

Only top scored, non community-wiki answers of a minimum length are eligible