c++ - Dijkstra's algorithm pseudocode -


i'm trying write dijkstra's algorithm in c++, , there countless examples on internet can't seem grasp how examples work. i'd rather in way makes sense me can understand algorithm better. know how algorithm should work, , have written code. wondering if point out flaw in thought process. i'm choosing represent graph edge list. i'll write in pseudocode because actual code huge mess:

class node{    vector<node> linkvector;           //links other nodes, generated @ random    int cost;                     //randomly generated constructor    bool visited = false; }  vector<node> edgelist;           //contains nodes   int main(){    create n nodes    add nodes edgelist    each node {       randomly add weight nodes linkvector    } findpath(initialnode) }  int findpath(node start, node want, int cost=0){   //returns cost src dest if(start==want) return cost; if(every node has been visited) return 0;        //this in case of failure node result = getminimumcost()    //finds node in linkvector least cost result.visited(true)  //so don't stuck in loop findpath(result, want, result.getcost() + cost);  //recursive call } 

via recursion, i'm trying explore nodes until find 1 i'm looking for, return , ride cascading returns top of function call stack, while adding total cost.

performance doesn't matter, if using recursion makes harder needs be, open rewriting code.

dijkstra's algorithm isn't recursive. recursive algorithm end being depth-first whereas dijkstra's algorithm breadth-first search.

the central idea have priority queue of unvisited nodes. each iteration pull node shortest distance off of front of queue , visit it. update distances each of unvisited neighbors.

a queue-based algorithm such not lend recursive implementation. search not explore 1 path exhaustion before tries alternate path depth-first search do. explores many paths simultaneously, exploring them long cheapest path. current path no longer cheapest moves on path. recursion doesn't let "jump" path path.

you can see behavior in graphic wikipedia article.

enter image description here


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -