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:
...
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 ...
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 ...
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 ...
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
...
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.
...
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
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 ...
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.
...
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 ...
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, ...
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 ...
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 ...
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
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 ...
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 ...
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 ...
5
votes
Accepted
Java Maze Solver (DFS)
API and Reusability
solveMaze takes both a Maze maze and Node start, even though ...
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,...
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 ...
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.
...
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'...
4
votes
Equivalent binary trees (A Tour of Go)
Disclaimer: Not a go programmer
I think Walk could be written in a way such that it handles nil input. Then, this :
...
4
votes
Accepted
Java Recursive Depth First Search
The context of this code example is not very clear, so in my remarks below I will suppose that the code is about a generic implementation of depth-first search.
...
4
votes
Accepted
Finding a loop in a directed graph in Python
Lets first review your code, and then introduce you to one alternative on how to do unit tests in Python.
Adding comments or docstrings is good – In dfs(), not the ...
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 ...
4
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. ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
depth-first-search × 181graph × 74
python × 61
algorithm × 42
java × 37
programming-challenge × 32
breadth-first-search × 32
c++ × 26
tree × 26
c# × 21
performance × 19
python-3.x × 18
recursion × 16
javascript × 13
time-limit-exceeded × 10
interview-questions × 8
backtracking × 8
c × 7
pathfinding × 6
matrix × 5
sudoku × 5
beginner × 4
reinventing-the-wheel × 4
sorting × 3
complexity × 3