Ha, path finding time!
Great
I read your post, and I think you're going too fast for yourself. You have a mathy description of Dijkstra and want to jump directly to pathfinding over tiles in the game. That means you're getting everything on your plate at the same time:
1. What data structures do I need
2. How do you perform the operations used in the math (I am always fond of phrases like "find pick the vertex, v, in U with the shortest path to s" )
3. What the heck is this relaxation thingie?
4. I have different costs.
5. How does a node relate to a tile.
6. What about objects in the tile?
7. What is the "main" loop now, the math just lists things you do.
then you added Internet
8. People use generic cost matrices, templates, generics, factories, and I don't know what, for X, Y, and Z language.
9. People make nice animations (fyi I made such a thing too, and believe me, making such a thing does not help in understanding the algorithm, the GUI code is more complicated)
10. People make videos.
11. People use different terminology for perhaps the same things, or maybe not?
When you spend time on something, and you're just running into walls, you took a too big bite.
If I recognize this happening, I always stop what I am doing (it doesn't work), and completely forget about the problem for some time (a few days).
Then, I take a step back and try to come up with a plan how to do this.
In general, you can either go around the problem (which fails here, as any path finding algorithm is going to be mostly the same), or you go (hopefully) through the problem in smaller steps (since my bite was too big, I need several smaller bites to swallow instead).
It seems the only direction available is to split up the problem.
Vision (or draw) a line from start to end. At the start you have the math description, at the end you have your beautiful solution, blinking fast, with all the bells and whistles you can think of. Next, find the problems you're wrestling with, and place them on the line. When do you need to decide about it, does one problem come before or after another problem?
Can you find other steps that need to be done at some point? Where on the line should they be? If a problem seems to be big, can you break it down in more smaller problems?
(In essence, try to inject some structure in your problem, so you can focus on fewer things at the same time.)
I tend to do this in my head while doing things that don't need deep thought, like walking, doing dishes, showering, in bed, somewhat awake and waiting for the alarm clock to go off, and so on. Other people do this differently, experiment to find out what works for you.
Keep in mind that your line with steps is just a plan, likely it is broken, incomplete, and perhaps just plain nonsense. Change it when you find better ways to do things.
Now back to your problem.
You seem to have major problems with implementation in data structures and/or in tile/game context.
I think you're underestimating the importance of truly understanding the algorithm. If you really understand the algorithm, you can list the operations you must perform as function calls (just the calls, how to implement the function is a more difficult problem to do later). You can write the main loop.
From the operations and decisions you must make, you get ideas how to structure your data. At some point you find all the pieces of puzzle fit, and you get a eureka moment, or as Hannibal Smith used to say "I love it when a plan comes together" (yep, I am getting old )
My list with mathy things is usually
1. Understand the math
2. Implement the math
3. Map math onto real problem
I know 3 is waaay too big, but so far you cannot manage 2, and perhaps not even 1. In addition, once you have 1 and 2, you can use the new information to break down 3 in more manageable pieces. We're doing small bites, right? One step at a time.
Ok, about 1.
The point with math is that these things are designed to describe the steps you must do in a compact and unambiguous way, without assuming a certain context, like using a computer to solve it. "find pick the vertex, v, in U with the shortest path to s" says exactly what you must do, it just does not explain how to do it (since this depends on your context).
Secondly, its assumption (Given a graph, G, with edges E of the form (v1, v2) and vertices V, and a source vertex, s) is its world view. [I seem to be missing some cost function here, that got lost somewhere?]
Reading the description, and saying, "I understand every step" is the first item. You seem to have done that.
I then always run the math manually, on a white board or a piece of paper. Take a random graph, from your lesson, the Dijkstra wikipedia page ( https://en.wikipedia.org/wiki/Dijkstra's_algorithm ) or the video above, and DO the algorithm. Don't take shortcuts, simply do each step that it says you must do. (Be a math computer, stupidly do what the 'code' says you must do.)
Play with the algorithm. Try another graph, or other costs. Does it provide correct answers? Can you see patterns in its steps? Can you attach a meaning to set V and U? Can you invent edge cases that would break the 'code'? (Try them!) When does this relaxation take place exactly? What effect does it have? Can you understand why it's there? Does the name make sense?
The goal is to *deeply* understand the math, to 'live' it. Don't try to computerize it yet (a natural tendency for everybody doing programming). You're doing manually what your computer should do. You'll have to explain the computer how to do it. If you don't understand yourself what it should do in every situation, you are not going to be able to explain the algorithm to a computer.
About 2.
You asked for real code, but as a general rule, I don't give solutions to people that are learning. I believe giving a solution defeats the learning goal. Finding and programming a solution yourself is much better. Being able to program it is the proof that you truly understand it. Also, I don't want to take away your Eureka moment when you realize your solution actually works. The feeling you get when you cracked a hard problem is just too good to take away from you.
The goal here is to implement the math. Not your game, but the math. Not efficient, just the math. Not fancy, just the math.
Concentrate on the steps you did in "1". Do everything else with minimal effort to make it work. Take a simple and straight forward data structure, that closely matches the math. Hard-code an example graph into it. (Your aim is coding the algorithm, not making beautiful setup or output code.)
I'd say a list of nodes, like
struct Node {
int distance;
bool in_U;
};
/* assuming your example has 10 nodes. */
#define NUM_NODES 10
#define INFTY 1000000
struct Node nodes[10];
void init() {
/* initialize the nodes[] for the example graph */
for (int i = 0; i < NUM_NODES; i++) nodes[i].distance = INFTY;
// etc
}
int cost(int from, int to) {
if (from == 0 and to == 1) return 1; // path from node 0 to node 1 has cost 1.
return INFTY;
}
void dijkstra() {
bool done = false;
while (!done) {
// code the step you did in "1".
}
}
int main() {
init();
dijkstra();
// That's all folks!
return 0;
}
hope this helps