|
You last visited: Today at 08:04
Advertisement
Dijkstra`s algorithm for shortest path
Discussion on Dijkstra`s algorithm for shortest path within the CO2 Programming forum part of the Conquer Online 2 category.
05/03/2012, 12:16
|
#1
|
elite*gold: 0
Join Date: Jul 2006
Posts: 2,216
Received Thanks: 794
|
Dijkstra`s algorithm for shortest path
I had this laying around from 10th grade lol, releasing random codes now.
So what is Dijkstra`s algorithm? It calculates the shortest path from one node to all the other nodes in a weighted graph. For node-to-node paths use the A* algorithm instead, if you need the path from every node to every node, use Roy-Floyd or Floyd-Warshall algorithm.
The code reads a weighted graph from a file format that looks like this...
NUMBER_OF_NODES
edgelength(0,0) edgelength(0,1) ... edgelength(0,n)
...
edgelength(n,0) ... edgelength(n,n)
edgelength(x,y) equals the length of the edge if there is an edge between the nodes, and 0 if there isn`t an edge (or x=y)
Here`s a video of how it works:
(Don`t mind the simple code, this was done aaaaaaaaages ago)
PHP Code:
#include <iostream> #include <queue> #include <fstream>
#define MAX 100
using namespace std;
struct Data { int N; int graph[MAX][MAX]; int d[MAX]; int father[MAX]; bool marked[MAX]; };
void getData(Data & data) { ifstream fin("graph.txt"); fin >> data.N; for(int i=0; i<data.N; ++i) { for(int j=0; j<data.N; ++j) { fin >> data.graph[i][j]; } } fin.close(); }
void dijkstra(int start, Data & data) { priority_queue<pair<int,int> > q; pair<int, int> node; for(int i=0; i<data.N; ++i) { data.d[i] = 9999999; // distance = infinity, we cant see the node data.father[i] = -1; // we didnt come here from anywhere... data.marked[i] = false; // we didnt visit this node }
data.d[start] = 0; q.push(pair<int,int> (data.d[start], start)); while(!q.empty()) { node = q.top(); // nem q.front, de miert? o.o q.pop(); int i = node.second; if(!data.marked[i]) { data.marked[i] = true; for(int j=0; j<data.N; ++j) { if(!data.marked[j] && data.graph[i][j] > 0 && data.d[i] + data.graph[i][j] < data.d[j]) { data.d[j] = data.d[i] + data.graph[i][j]; data.father[j] = i; q.push(pair<int, int>(-data.d[j], j)); } } } } }
void printPath(int end, Data & data) { cout << end+1 << ' '; while (data.father[end]!= -1) { cout << data.father[end]+1 << " "; end = data.father[end]; } cout << endl; }
int main() { Data data; getData(data); cout << "Start = "; int start; cin >> start; start--; dijkstra(start, data); for(int i=0; i<data.N; ++i) { if(i!=start) { cout << "Path from " << i+1 << " to " << start+1 << ": "; printPath(i, data); } } cin.get(); cin.get(); return 0; }
|
|
|
02/09/2015, 09:59
|
#2
|
elite*gold: 67
Join Date: Aug 2014
Posts: 1,323
Received Thanks: 928
|
Re-Implemented in C#. Was kinda slow so I had to stackalloc the array.
Using it for Co2 pathfinding from A to B on the same map (not mobs, more like a GPS) and it completes from Warehouse man to Mine Teleporter in 161ms (Twin City) I'm satisfied. It also appears to be very close to the Pathfinding algo used in the client. It's almost always aligned. I'm spawning squamas on each node so I can compare that
|
|
|
02/09/2015, 11:01
|
#3
|
elite*gold: 0
Join Date: Feb 2015
Posts: 21
Received Thanks: 9
|
I program in C ++ so it can help you something?
|
|
|
02/09/2015, 12:10
|
#4
|
elite*gold: 0
Join Date: Jul 2006
Posts: 2,216
Received Thanks: 794
|
Quote:
Originally Posted by Xio.
Re-Implemented in C#. Was kinda slow so I had to stackalloc the array.
Using it for Co2 pathfinding from A to B on the same map (not mobs, more like a GPS) and it completes from Warehouse man to Mine Teleporter in 161ms (Twin City) I'm satisfied. It also appears to be very close to the Pathfinding algo used in the client. It's almost always aligned. I'm spawning squamas on each node so I can compare that 
|
I think that it would be way better if you used this for map-to-map travels (the nodes being the maps), and on the same map A* would probably yield better results. Then again, I`m happy if you`re happy with the results.
|
|
|
02/09/2015, 14:48
|
#5
|
elite*gold: 67
Join Date: Aug 2014
Posts: 1,323
Received Thanks: 928
|
Quote:
Originally Posted by KraHen
I think that it would be way better if you used this for map-to-map travels (the nodes being the maps), and on the same map A* would probably yield better results. Then again, I`m happy if you`re happy with the results. 
|
I used A* before, it's faster for short paths but it tends to have too much "noise" in it.. I don't know if that made sense..
Lets say the path is like
Start-------------------------End (a straight line)
A* would end up having useless 1 node turns in it like:
Start--------___-----_-----------_---End
While Dijkstra does exactly what it should. A straight line:
Start-------------------------End
If i play around with the cost and heuristics it ends up being way slower than Dijkstra's approach in many cases. A* is faster for < 35 nodes but since I expect a min. of 80 nodes for my case of use, I'll stick with Dijkstra for now
Thanks for your suggestion though! <3
|
|
|
02/10/2015, 12:01
|
#6
|
elite*gold: 0
Join Date: Jul 2006
Posts: 2,216
Received Thanks: 794
|
Well, yes, A* is a heuristic search, hence it won`t always yield the absolute very best, but a close approximation, putting more focus on efficiency. Also, is that a problem? I mean you have to split that path in jump points anyway, so does it really matter? Or are you using it for walk/run only (ex. mobs)?
A* will also produce a straight line if you implement tie-breaking.
|
|
|
02/10/2015, 21:33
|
#7
|
elite*gold: 67
Join Date: Aug 2014
Posts: 1,323
Received Thanks: 928
|
Quote:
Originally Posted by KraHen
Well, yes, A* is a heuristic search, hence it won`t always yield the absolute very best, but a close approximation, putting more focus on efficiency. Also, is that a problem? I mean you have to split that path in jump points anyway, so does it really matter? Or are you using it for walk/run only (ex. mobs)?
|
I'm using it to display every step they need to take. It will put a squama on every tile thats in the path. It's used for more complex quests where they have to talk to npcs that may be really far away or on different maps, so it shows them the way. Conquer GPS basically
|
|
|
 |
Similar Threads
|
[Release] AStar Algorithm
03/07/2012 - CO2 PServer Guides & Releases - 1 Replies
Well , I was working on the AStar Algorithm , and am almost finished it.
the way I intended to design it with so I don't need anther struct/class for Node, but anyways it needs some Optimization ("like using Heap (data structure) instead of Dictionary") but so far it works fine.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MapData.Interfaces;
|
L2 enchant algorithm
03/18/2011 - Lineage 2 - 0 Replies
Hello everyone!
I wan't to know the algorithm of the enchanting system on L2j if someone could help!.....I actually wanna know how it works and if there's something i can do to proliferate the chances to enchant a weapon for example!
thanks guys! great forum!
|
Getting blowfish key and using the algorithm
03/17/2011 - SRO Coding Corner - 10 Replies
Good evening:)
I am still busy with the packets of silkroad and I'm now trying to understand on how to decrypt the packets but I'm having some problems with it.
I read drew benton's artikel about the silkroad security but I still don't know on which blowfish key is used to decrypt the packets.
let's say these four packets are the first 4
S -> C
00000 25 00 00 50 00 00 0e d2 a9 27 80 2d c3 23 6c f6 %..P.....'.-.#l.
00016 00 00 00 62 00 00 00 8d ac fe 38 3d d0 9c ef 67 ...
|
Algorithm for Awarding Virtue Points.
03/13/2011 - CO2 Programming - 25 Replies
Does anyone have it? I mean the official algorithm, or as close as possible. If all else fails, I'll spend a while looking through the binaries for it... lol.
|
All times are GMT +1. The time now is 08:05.
|
|