C
C#9mo ago
Lilac

✅ Problems with Dijkstra Algorithm

So this is my take on Dijkstra in C#. After doing everything. My output is completely wrong. Can someone help me with this?
15 Replies
Lilac
LilacOP9mo ago
BlazeBin - doqdfjzauhan
A tool for sharing your source code with the world!
Lilac
LilacOP9mo ago
U -> V: 0 -> 1 wt 1
U -> V: 0 -> 2 wt 4
U -> V: 0 -> 3 wt 0
U -> V: 0 -> 4 wt 0
U -> V: 0 -> 5 wt 0
U -> V: 0 -> 1 wt 1
U -> V: 0 -> 2 wt 4
U -> V: 0 -> 3 wt 0
U -> V: 0 -> 4 wt 0
U -> V: 0 -> 5 wt 0
and this is my output
Lilac
LilacOP9mo ago
this is what the graph supposed to look like
No description
Keswiik
Keswiik9mo ago
I think your issue is due to how you're selecting the min vertex, but the way you've written this makes it difficult to follow. Personally, I'd rewrite this with a more appropriate structure for the graph (as in, create a graph class with definitions of vertices, their neighbors, and edges), that way you don't have to filter out already-processed data and it should make following pseudo-code for the algorithm more straightforward.
Diceble
Diceble9mo ago
@Lilac I once implemented the A* algorithm in unity (C#) with some help of the videoseries of this guy https://www.youtube.com/watch?v=-L-WgKMFuhE&list=PLFt_AvWsXl0cq5Umv3pMC9SPnKjfp9eGW It's been ages ago but I believe he also talks about Dijkstra Algorithm maybe it helps you
Sebastian Lague
YouTube
A* Pathfinding (E01: algorithm explanation)
Welcome to the first part in a series teaching pathfinding for video games. In this episode we take a look at the A* algorithm and how it works. Some great A* learning resources: http://theory.stanford.edu/~amitp/GameProgramming/ http://www.policyalmanac.org/games/aStarTutorial.htm Source code: https://github.com/SebLague/Pathfinding If you'd...
Lilac
LilacOP9mo ago
oh thanks for your help yeah, I took some reference from a cpp program how can I put all these codes of Dijkstra pathfinder into a winfoms program
Keswiik
Keswiik9mo ago
like, have it visualize the route finding process or something? Or what?
Lilac
LilacOP9mo ago
I just want it to do the route finding process with the given data I don't need to input the data or anything
Graph graph = new Graph(); // Create a new graph object
graph.AddVertex("v0"); graph.AddVertex("v1"); // Add vertices to the graph
graph.AddVertex("v2"); graph.AddVertex("v3");
graph.AddVertex("v4"); graph.AddVertex("v5");
graph.AddEdge(0, 1, 2); graph.AddEdge(0, 2, 3); // Add edges to the graph
graph.AddEdge(1, 2, 2); graph.AddEdge(1, 3, 1);
graph.AddEdge(1, 4, 3); graph.AddEdge(1, 5, 2);
graph.AddEdge(2, 4, 1); graph.AddEdge(3, 4, 2);
graph.AddEdge(3, 5, 1); graph.AddEdge(4, 5, 2);
Graph graph = new Graph(); // Create a new graph object
graph.AddVertex("v0"); graph.AddVertex("v1"); // Add vertices to the graph
graph.AddVertex("v2"); graph.AddVertex("v3");
graph.AddVertex("v4"); graph.AddVertex("v5");
graph.AddEdge(0, 1, 2); graph.AddEdge(0, 2, 3); // Add edges to the graph
graph.AddEdge(1, 2, 2); graph.AddEdge(1, 3, 1);
graph.AddEdge(1, 4, 3); graph.AddEdge(1, 5, 2);
graph.AddEdge(2, 4, 1); graph.AddEdge(3, 4, 2);
graph.AddEdge(3, 5, 1); graph.AddEdge(4, 5, 2);
like this
Keswiik
Keswiik9mo ago
Is this a school assignment or something? Not sure why you'd need this in a winforms app and have no way to interact with it.
Lilac
LilacOP9mo ago
yes it's a school assignment and I have no idea how to code with winforms right now I have not even started OOP yet actually it would be better if my team can find routes with input values but we find it too hard to perform that right now
Keswiik
Keswiik9mo ago
Create a Windows Forms app with C# - Visual Studio (Windows)
Create a Windows Forms app in Visual Studio with C#, add button controls to the form, and run the code to test your application.
Keswiik
Keswiik9mo ago
Is part of the assignment doing this in winforms? If so, I would have expected it to be covered during your class at some point.
Lilac
LilacOP9mo ago
well we were required to use winfomrs but we were only briefly introduced with it in only 1 lesson
Keswiik
Keswiik9mo ago
Then I'd follow the tutorial and start trying to learn.
Lilac
LilacOP9mo ago
okay then I will check out the tutorials
Want results from more Discord servers?
Add your server