1h
comment Breadth-first algorithm on adjacency matrix: premature ending of search, returns queue of size 1?
If you want to know the shortest path once you end, instead of returning Q, it may be better to define int visited[] = new int[num], use -1 as meaning not-visited, and set visited[temp] = element in the inner loop. Once you finish, the shortest path can be found following visited[end], visited[visited[end]] ... until you reach start
5h
comment Depth-first/breadth-first algorithm printing all nodes; how do I get it to only print nodes in the path?
@riista - you are not supposed to change questions on the fly (except to clarify). The right thing is to ask a new question
7h
comment Depth-first/breadth-first algorithm printing all nodes; how do I get it to only print nodes in the path?
@riista DFS is the wrong tool to find shortest paths. That being said, iteratively-deepening BFS is not much worse than DFS: add a "maxDepth" parameter, initially set to 0, and terminate BFS if your depth reaches 0 before finding the goal. If goal not found, increment maxDepth and try again. The first time you find the goal, maxDepth will be the smallest depth at which it can be found, and therefore the path will be optimal.
7h
comment Depth-first/breadth-first algorithm printing all nodes; how do I get it to only print nodes in the path?
@Carcigenicate probably -> provably. One way of looking at it is that Dijkstra is optimal, and if all edges have the same costs, BFS is Dijkstra.
11h
comment Depth-first/breadth-first algorithm printing all nodes; how do I get it to only print nodes in the path?
@riista imagine a 100x100, fully-empty map, with start at top-left corner and end at center. BFS can decide to "spiral in", painting everything; or in the best case paint half the map; in either case, the returned paths would be long and windy. Breadth-first would only paint half the map, and return the optimum path.
11h
comment Depth-first/breadth-first algorithm printing all nodes; how do I get it to only print nodes in the path?
note that, if you want to find the shortest path, a BFS would be even better.
11h
comment Depth-first/breadth-first algorithm printing all nodes; how do I get it to only print nodes in the path?
Nice full program. Also, I had never seen Arrays.asList used for single elements
11h
revised Depth-first/breadth-first algorithm printing all nodes; how do I get it to only print nodes in the path?
fixed missing return; and the->then typo
11h
comment Depth-first/breadth-first algorithm printing all nodes; how do I get it to only print nodes in the path?
Also note that this returns paths in reverse order
11h
answered Depth-first/breadth-first algorithm printing all nodes; how do I get it to only print nodes in the path?
12h
comment Extract JavaScript String from HTML page with Java
To clarify - this code is extracting the requested string from the HTML page, but does not care at all that it is "JavaScript" or otherwise interpret it in any way. To OP: tags 'JavaScript' and 'Object' are not needed.
13h
comment Scenarios where we force to use Pointers in c++
@Giorgi if you need to keep around N structs of size S each, and N is large and S is small (say, <100KB), then you can avoid pointers in your code, and rely on the heap being used correctly within the containers.
1d
comment Scenarios where we force to use Pointers in c++
Containers such as vector, set or map store their items in heap (transparently for the end-user). Unless individual items are very large, you can avoid news in your code.
1d
revised Scenarios where we force to use Pointers in c++
formatting and added linked data structures
1d
answered Scenarios where we force to use Pointers in c++
Jul
2
reviewed Approve suggested edit on simple way to build multi-page website with A LOT of data?
Jun
30
answered How to rank connection between people in a group?
Jun
29
comment Fastest algorithm to find factors of 2 and 5
@user448810 that algorithm needs O(n^2), where n is the number of digits in the input number, to generate the next-largest power of 2 (because adding two n-digit strings is O(n), and you need to do that O(n) times). Finding the GCD is also O(n^2) (O(n) steps, with an n-digit string subtraction in each) . Therefore, with the OP's representation, this algorithm is not competitive with other O(n^2) algorithms.
Jun
29
comment Fastest algorithm to find factors of 2 and 5
@user448810 please provide an algorithm for computing the next power-of-2 to a given number within the constraints of "a number is a string in base 10". In base-2, it is indeed trivial; but I understand that OP is doing things the hard way.
Jun
29
comment Fastest algorithm to find factors of 2 and 5
The GCD-based algorithm is interesting, but you would need a lot of multiplications to build the initial "next-biggest-power-of-two" and "next-biggetst-power-of-five". Calculating the GCD of a pair of large numbers is also O(n^2) substractions, with n the length of the smallest of the pair.
1 2 3 4 5