Giter VIP home page Giter VIP logo

dijkstraalgorithm's Introduction

Dijkstra's Path Finding Algorithm in C++

Dijkstra's Algorithm is used to find the best path between the nodes of a directed graph. This could be used to find directions between places on a map, routing network traffic or to direct AI controlled entities in video games.

There are faster ways to calculate paths (such as the A* algorithm) but they are variations on this Algorithm, so if you can understand this then you should not have too much trouble adapting it to better Algorithms.

Source Code

This code is a very crude example of Dijkstra's path finding Algorithm in C++ (Visual Studio 2010 / CodeBlocks GCC Projects)

  • cGraph - The graph containing all nodes
  • cNode - A node in the Graph
  • cEdge - An edge that joins one node to another with a "cost" value

Program output

The program creates a simple graph in main.cpp and calculate a few paths between some of the nodes in the graph. It will then print out the path to get to that node backwards.

`

-= Example using Dijkstra's Algorithm to calculate paths between connected nodes =
----------------
Calculated path from A to D
D <- A
----------------
----------------
Calculated path from D to A
A <- C <- D
----------------
----------------
Calculated path from A to E
NOT REACHABLE
----------------
----------------
Calculated path from A to A
A
----------------

`

Implementation notes

This is implemented quite crudely but serves as a simple and easy to follow example of how to implement this Algorithm in code.

Tested it compiling and running as a console app on Win32 in Visual Studio 2010 or CodeBlocks IDE using GCC compiler on Linux Ubuntu.

  • It doesn't maintain a "Fringe" list of nodes to check, it just checks all unvisted nodes every time
  • It's not thread safe as it's storing path values in the cNode class
  • The resulting path is simply printed out backwards, you could implement a stack to push it onto it if you wanted
  • Isn't robust - doesn't do any checking for null parameters to functions

Links

dijkstraalgorithm's People

Contributors

davepoo avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.