Computer Science Canada Help with algorithm in C |
Author: | Panphobia [ Wed Mar 05, 2014 8:34 pm ] | ||
Post subject: | Help with algorithm in C | ||
So I am using ncurses so that is probably why some of the functions will be strange. But basically I am trying to find the shortest path from one point to another on the terminal, and keep track of the path, so I used Dijkstra. Sufficed to say, it isn't working, I checked my code for the queue, and it works perfectly, so I can only think that there is a problem in the logic. But the problem there is that I translated the code to java and it worked perfectly there. So here is my code, can anyone see the problem?
|
Author: | Dreadnought [ Wed Mar 05, 2014 9:16 pm ] | ||||||
Post subject: | Re: Help with algorithm in C | ||||||
Panphobia wrote: Sufficed to say, it isn't working
It might be useful to know how it isn't working? Here's the only "potential problem" I noticed, but its probably nothing. There is an inconsistency in the way you store (x, y) coordinates
Not necessarily an issue, but it might be the cause of some weird behavior. Personally, I would use a struct or at least preprocessor symbols. (#define) On a different note, since the distance between neighboring nodes is always 1, any node you have already visited must have been visited by a shortest path. This means that you could save some time by doing this. (and only have to calculate the length of the path at the end)
As I said not much but maybe this will help. Good luck! |
Author: | Panphobia [ Wed Mar 05, 2014 9:20 pm ] | ||||
Post subject: | RE:Help with algorithm in C | ||||
Yea I have used the seen int/bool array, it didn't do anything but improve the space complexity. Also they aren't (x,y), in ncurses when you're looking on the screen, you have to search (y,x). Well when I say it isn't working, I say it is breaking after one round of en queuing and then dequeuing, so basically if this was the minpath array initialization I = INFINITY
it only does this
|
Author: | Dreadnought [ Wed Mar 05, 2014 9:51 pm ] | ||
Post subject: | Re: Help with algorithm in C | ||
Quote: Also they aren't (x,y), in ncurses when you're looking on the screen, you have to search (y,x).
My point was that in one instance index 0 is X and index 1 is Y and in the second instance it is reversed, but that shouldn't cause a problem. Quote: Yea I have used the seen int/bool array, it didn't do anything but improve the space complexity.
Ya, it doesn't improve complexity, but it seems weird that you check the length of the path each time you look at a vertex except for the last vertex (where you break on the iteration after you reach it). But honestly that's just me quibbling about consistency. Also, shouldn't you initialize to this?
I don't know what infinity + 1 will do, but it might be interesting. Other than that I'm stumped, maybe if you can provide more details of the behavior (does it crash after the first enqueue/dequeue, or does it exit the while loop?), but otherwise I have no idea. Sorry, |
Author: | Panphobia [ Wed Mar 05, 2014 9:57 pm ] |
Post subject: | RE:Help with algorithm in C |
By the way, I know what you're saying, and it first gets initialized to all infinity, but the start node gets set to 0 right before the while loop. Also the neighbors array where I switched it doesn't matter as you said, soooooo, this is bugging me. It doesn't make sense because the same code in Java works PERFECTLY. |
Author: | Dreadnought [ Wed Mar 05, 2014 10:05 pm ] |
Post subject: | Re: Help with algorithm in C |
Well, it shouldn't be too hard to figure out where the problem occurs since its after the first enqueue/dequeue. Put some debug prints in (even better, use a debugger) and figure out where stuff goes wrong. It might not be fun, but debugging is at least half the work (usually much more than half ) |
Author: | Panphobia [ Thu Mar 06, 2014 1:55 am ] | ||
Post subject: | Re: Help with algorithm in C | ||
Yea I know debugging is a big part of coding. I think I got one bug, and a few others. I forgot to malloc the variables in the struct, and also I changed the queue so that it takes in 1D arrays instead of ints. But the algorithm works until the distance from the origin is more than 10, then it breaks down. Like for the distance from (0,0) to (10,10) it printed 414, but when it is (0,0) to (1,1) it says the distance is 2, it doesn't make sense. Here is the revised code, and I put the "isVisited" variable in this time.
|
Author: | Dreadnought [ Thu Mar 06, 2014 9:44 pm ] |
Post subject: | Re: Help with algorithm in C |
Welp, in my boredom I coded your algorithm (in Turing ), as far as I know, it works. If you ask me the problem probably lies in the queue or in p (whatever kind of structure it is), unless I'm missing something. When you say things break when you hit (0,0) to (10,10). what happens for (0,0) to (9,9), (9, 10), (10,9) (9, 11), (11, 9) and basically any nearby case? Good luck. |
Author: | Panphobia [ Thu Mar 06, 2014 10:17 pm ] |
Post subject: | RE:Help with algorithm in C |
The same thing happens for those too, I think it happens when the distance from (0,0) is > 10. I tested my queue A LOT and I know it is not the problem, I even went to my professor and she said it was fine. My structure just holds the minpath,and the prev array. |
Author: | Panphobia [ Sat Mar 08, 2014 6:24 pm ] | ||
Post subject: | Re: Help with algorithm in C | ||
Let me show you the results of a 20 by 20 grid without any walls, this is just a pure shortest path to any point on a 20 by 20 grid.
As you can see, there is an actual problem, and I don't know what it is. The shortest path is accurate for the two leftmost columns, but that is it. |
Author: | Dreadnought [ Sat Mar 08, 2014 10:39 pm ] |
Post subject: | Re: Help with algorithm in C |
Things I might try if I were debugging this: 1- What do p->prev tell you the shortest path is? (does it even make sense?) 2- What happens if you remove the seen array? (do the minpath values change? If they do you might be able to get an idea of what's happening) 3- What happens when you give it a 20x1 grid? (since things seem to go wrong in the horizontal direction) 4- (Probably one of the first things I would have tried, lots of prints (and if they don't tell me anything interesting, I put more!) You might have already tried some of these. I'm assuming you've tried 4 to some extent, but you should consider putting A LOT of prints and writing everything to a file. Then you can look through it and check what happens at important moments (like when you suddenly jump from 1 to 140 and stuff. Again, if I had to guess, I'd say something bad is happening in p. Good luck. |
Author: | Panphobia [ Sat Mar 08, 2014 10:53 pm ] | ||
Post subject: | Re: Help with algorithm in C | ||
If I reduce the rows and the columns to 10 this is the output
So the algorithm works for two columns at a time. |
Author: | Dreadnought [ Sat Mar 08, 2014 11:06 pm ] |
Post subject: | Re: Help with algorithm in C |
What happens if you don't check the seen array? |
Author: | Panphobia [ Sun Mar 09, 2014 12:46 am ] |
Post subject: | RE:Help with algorithm in C |
same thing |
Author: | Panphobia [ Sun Mar 09, 2014 3:47 pm ] |
Post subject: | RE:Help with algorithm in C |
I figured it out, the C compiler I am using is weird, when building a propositional statement like a&&b == b && c you need brackets, (a && b) == (b && c), also I wasn't checking whether the index of the prev array was filled, so there were my problems. Thanks for the help! |
Author: | Dreadnought [ Sun Mar 09, 2014 3:53 pm ] |
Post subject: | Re: Help with algorithm in C |
Panphobia wrote: I figured it out, the C compiler I am using is weird, when building a propositional statement like a&&b == b && c you need brackets, (a && b) == (b && c) That's not your compiler, that's the C standard, (look up operator precedence). Did that come up in the snippet you posted? |
Author: | Insectoid [ Sun Mar 09, 2014 3:56 pm ] |
Post subject: | RE:Help with algorithm in C |
That's not an issue with the compiler, that's part of the C spec. == takes precedence over &&. In your situation, it evaluates b == b first, which of course is true, so your equation simplifies to a&&c. But yeah, the brackets would have fixed it. EDIT: Damnit, Dreadnaught. |
Author: | Panphobia [ Sun Mar 09, 2014 3:59 pm ] |
Post subject: | RE:Help with algorithm in C |
No it was the other way around, a == b && b == c, sorry, as you can see the xEnd = uX && yEnd == uY, so I needed to change it to (xEnd == uX) && (yEnd == uY) |