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,253
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
  • 94.3k
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
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 ...
Martin R's user avatar
  • 23.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
  • 42.7k
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.5k
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.4k
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

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

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

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 ...
Edward's user avatar
  • 65.3k
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

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,737
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,421
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
  • 17k
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,421
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,495
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

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 : ...
SylvainD's user avatar
  • 29.2k
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. ...
Antot's user avatar
  • 3,637
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 ...
holroy's user avatar
  • 11.5k
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

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,608

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