"If you lie to the computer, it will get you."
Favorite Granny's Wisdom Pearl
"If you lie to the computer, it will get you."
Favorite Granny's Wisdom Pearl
Junilu Lacar wrote:I may find out later on that this is still not a good enough separation of concerns and refactor further
"If you lie to the computer, it will get you."
Favorite Granny's Wisdom Pearl
"If you lie to the computer, it will get you."
Favorite Granny's Wisdom Pearl
"If you lie to the computer, it will get you."
Favorite Granny's Wisdom Pearl
D.J. Quavern wrote:I managed to build the algorithm with crap tests but now it's way too slow. And I cannot figure out where since it's O(n)
"If you lie to the computer, it will get you."
Favorite Granny's Wisdom Pearl
"If you lie to the computer, it will get you."
Favorite Granny's Wisdom Pearl
There are three kinds of actuaries: those who can count, and those who can't.
Piet Souris wrote:hi DJ,
I gave your latest code a look, and don't worry: it is easy to follow code!
I haven't tested it yet, but there are three things that I wonder if you took these into consideration:
1) even when every node has a zero in-out difference, it does not mean there is a path. For instance, if we have the edges 0-1, 1-0, 2-3, 3-2, then the graph is not connected and so has no solution.
2) And in dfs, if at has an outgoing = 0, that does not mean the dfs is over! For instance: if we have the edges 0-1, 1-0. 1-2, 2-1 and we go from 0 to 1 and from 1 to 0, then 0 has no more outgoings. However, the solution is: 1-0-1-2-1. There is an algo, O(N), that says if your loop gets stuck, and not all edges have been used, then look for a vertex in that loop that has still some outgoings, then go on with that vertex, and merge the loops.
3) As I recall, the constraints may be 50.000 edges. That means in the worst case a recursion depth of 50.000. And that may fail.
"If you lie to the computer, it will get you."
Favorite Granny's Wisdom Pearl
There are three kinds of actuaries: those who can count, and those who can't.
Piet Souris wrote:hi DJ,
these drawings of yours are always of the highest quality! It would make Picasso very jealous, if you ask me.
For your points:
1) yes, your routine will discover that, but maybe only after a while, while you could test for it in the inputstage. But it is fine as it is.
2) If you look at your drawing, you see that 1 has neighbors 0 and 2. Since you pick the last one, you go from 1 to 2, and your dfs succeeds. However, if in the input the edges 1-0 and 1-2 were switched, then 1's neighbors would have been 2 and 0, and then you would get the sequence 0-1-0.
3) suppose you have a status, say an enum {FINISHED, STILL_BUSY}, and that you add two parameters to your dfs method: currentDepth and maxDepth. The dfs can then return for two reasons: either it is finished, and before returning, it changes the status to FINISHED, or it has reached maxDepth, and it returns with the status unchanged (i.e. STILL_BUSY).
From your main, you can have something like;
"If you lie to the computer, it will get you."
Favorite Granny's Wisdom Pearl
There are three kinds of actuaries: those who can count, and those who can't.
"If you lie to the computer, it will get you."
Favorite Granny's Wisdom Pearl
There are three kinds of actuaries: those who can count, and those who can't.
Piet Souris wrote:good afternoon, DJ,
no, reaching maxDepth says nothing about there being a solution or not. If the number of edges is larger than maxDepth, then the rourine simply isn't over yet when reaching maxDepth. And if the number of edges is smaller than maxDepth, then maxDepth will never be reached.
Have you thought about a non-recursive solution, that does not suffer from a too deep recursion level?
Still have to perform my test, I'm in a pretty lazy mood these days (months)
"If you lie to the computer, it will get you."
Favorite Granny's Wisdom Pearl
There are three kinds of actuaries: those who can count, and those who can't.
There are three kinds of actuaries: those who can count, and those who can't.
"If you lie to the computer, it will get you."
Favorite Granny's Wisdom Pearl
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |