17 votes

C++ implementation of depth-first search

What further improvements can I make to this code? The code is rather bad - the site you read it from tends to have lots of bad/amateur articles. The graph class: ...
ALX23z's user avatar
  • 2,328
12 votes
Accepted

Hacker Rank: Journey to the Moon (Graph Theory)

The big problem The reason your program runs so slowly is that your dfs() function takes the edge list as a parameter by value instead of by reference. That means ...
JS1's user avatar
  • 28.5k
12 votes

C++ implementation of depth-first search

Disagreement I have to disagree with @ALX23z blanket statement that using std::map is bad idea. There is a lot more to it than that (if the graph was sparse the map ...
Martin York's user avatar
  • 95.1k
10 votes

C++ implementation of depth-first search

I've assumed here the OP is performing DFS on undirected graphs - if not only my second point is valid. There are several other answers here suggesting better coding practices and more efficient ...
Chaos's user avatar
  • 201
8 votes
Accepted

DFS in a binary tree

This code is pretty much the most efficient it can be, so I will add some pointers on the coding style and convention. Instead of calling your Binary Search Tree class a BST, define it as ...
Anshul Goyal's user avatar
8 votes

Generating the powerset in C

I'll just work through this from the top: #include <stdio.h> #include <stdlib.h> #include <string.h> Looks good; we need these. ...
Toby Speight's user avatar
  • 75.1k
7 votes
Accepted

Depth First Search Using Stack in Python

Lists in Python are already stacks. It would be better if you used a raw list as people are more familiar with lists then a ...
Peilonrayz's user avatar
  • 43.3k
6 votes

2D grid Hamiltonian path puzzle solver

1. Review Code is easier to test (and measure the performance) if it's organized into functions or classes. In this case you have persistent data (W, ...
Gareth Rees's user avatar
  • 49.6k
6 votes
Accepted

C++ Maze Generator

OOPs int maze_size[2]; int start_axis; int start_side; std::vector< std::vector< int > > dfs_path; Global variables should be avoided, as they can ...
mdfst13's user avatar
  • 21.6k
6 votes

Make Depth First Search program more time efficient

Style I understand that this is a quick and dirty piece of code to earn some points on some code-challenge website. I will still offer some comments on style. ...
Emily L.'s user avatar
  • 16.6k
6 votes
Accepted

Leetcode 17. Letter Combinations of a Phone Number

To use only static methods increases complexity of your code (because you need more parameters for function calls). In this review I won't change this to do not go to far away from your original code ...
Adriano Repetti's user avatar
5 votes

Equivalent binary trees (A Tour of Go)

This is a nice answer to a fun challenge. There are a couple ways I can see to improve it. This solution only works for arrays of up to 10 values due to the ...
Jason Mooberry's user avatar
5 votes

Find the largest continuous descending sum in a 2d matrix

get_largest_neighbour is a big confusing function. Let's try strip it down to avoid rewriting code. First I would create a loop to run on each of the four ...
SuperBiasedMan's user avatar
5 votes
Accepted

DFS implementation in Rust

Before I begin, you should be aware tha petgraph has already implemented most of the graph algorithms people may need. Use well-tested existing code whenever you can. Cleanup General Run rustfmt. ...
Shepmaster's user avatar
  • 8,638
5 votes

DFS in a binary tree

this code looks quite good already, especially if you integrate the tips of the other answer return value One problem I see though is that if you want to use your tree for something else than ...
Maarten Fabré's user avatar
5 votes

Generating the powerset in C

I subscribe to everything @TobySpeight said. I also believe that you could make your code a lot clearer (and your coding style better) by obeying a few important rules: When in doubt, choose the ...
papagaga's user avatar
  • 5,757
5 votes
Accepted

Java Maze Solver (DFS)

API and Reusability solveMaze takes both a Maze maze and Node start, even though ...
VisualMelon's user avatar
  • 7,431
5 votes
Accepted

Return all valid paren strings with a given length

I think your implicit question was "how may I improve runtime?", in terms of CPU cycles, and in terms of memory read stalls. The only relevant timings to pay attention to seem to be the optimized runs,...
J_H's user avatar
  • 25.5k
5 votes
Accepted

LeetCode: Add and Search Word C#

Short review: not a whole lot to say. I don't think breaking any of the method up any further would really help matters, and none of them should be combined, so the overall design looks reasonable to ...
VisualMelon's user avatar
  • 7,431
5 votes
Accepted

Sudoku solver using backtracting and depth-first-search

Overall it looks fine for a beginner-level program, you keep the functions small and separate different parts of the logic between them, which is good. ...
Lundin's user avatar
  • 4,660
5 votes

C++ implementation of depth-first search

std::map<int, bool> visited; This is useless as a member variable. It means nothing before or after you call DFS, so it'...
trent's user avatar
  • 1,064
4 votes
Accepted

Recursive Depth First Search for finding valid sudoku boards in Python 3

Style Your indentation is variable, some times 2 spaces, some times 4. I suggest you stick to 4 as per PEP8 conventions. continues on the same line than the ...
301_Moved_Permanently's user avatar
4 votes
Accepted

Searching a maze using DFS and BFS in Python 3

Kill the noise It's great that the solution works, but it's full of elements that seem to serve no purpose, which makes it confusing and hard to read. ...
janos's user avatar
  • 111k
4 votes
Accepted

Find the longest connect cells region per color

Looks fine overall, but there are a few oddities here and there, and there are some opportunities for significant performance improvements: Since you're going to visit every node in the given ...
Pieter Witvoet's user avatar
4 votes

(Leetcode) Minesweeper game

An M field is never pushed. It may only happen as a result of an unfortunate click. Testing for board[r][c] == 'M' in the loop ...
vnp's user avatar
  • 56.4k
4 votes
Accepted

(Leetcode) Minesweeper game

Instead of repeating 0 <= dr < m and 0 <= dc < n tests in the mine counting loop and the recursive search step, ...
AJNeufeld's user avatar
  • 33.5k
4 votes
Accepted

Javascript Tree Class

This is overall well-thought-out, well-organized, and well-written. There's at least one bug, and one oversight that I would consider a bug: In traverse your ...
Scott Sauyet's user avatar

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