egemen-dev / shortest_path_algorithm

Dijkstra’s shortest path algorithm.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Shortest Path Algorithm

Dijkstra’s Algorithm is a pathfinding algorithm that finds the shortest path from the given source node to every other node in the graph.

I had practiced BFS algorithm with the Binary Tree and Knight Travails projects before. However, those graph projects were not involving weighted edges.

This project is all about weighted edges (e.g., vertices are not equally distanced, or cost of reaching the next node varies) which also means it's way more challenging.

Edit 1: I was able to come up with a logic but stil far away from the solution. I was thinking of using the objects in this project and I might be taking the wrong approach as I'm just using arrays.

Edit 2: Completed the Dijkstra's algorithm.

How it works?

  • Mark all nodes/vertices unvisited.
  • Mark all nodes/vertices with ∞ infinite tdistance except the source node.
  • Repeat the following V times:
    1. Select the minimum value node which is the unprocessed/unvisited, initially it's the source node.
    2. Mark selected node as processed (put it in processed array).
    3. Update all adjacent vertices.

How to select the minimum value node which is the unvisited?

  1. Check the processed array and get the indexes of false values from the array. It means from the indexes of false values we haven't visited those nodes in the graph.
  2. Store the indexes in an array in my case indexes.
  3. Each value in indexes array is a representation of the nodes we can find minimum amongst.
  4. Find the minimum value.
  5. Most important part, find the index of the new minimum value from the value array.
  6. Retrun it.

How to update adjacent vertices?

If there is a new shortest path from U --> V like so cost[u] + c[u,v] < cost[v], then cost[v] = cost[u] + c[u,v] executes to update the cost of the V.

About the implementation

  • Because I used arrays to store the necessary information to keep track of the nodes e.g., current minimum value from the source, and parent of the visited node, this only works with sequential labels (0 to N).
  • In the example below where node Memphis is labeled with 0, New Orleans with 1 and there is a connection at the cost of $3.
  • To store the edge between Memphis and New Orleans, add_edge(adacency, 0, 1, 3)
  • Finds the shortest path from source (Memphis) to every other node in the graph.
  • If you want to find the shortest path from for example Atlanta, you have to relabel the graph and make Atlanta the source node at the index of '0'.

After the creation of the graph -> dijkstra(adacency_list, 0, 5)

Printed Output:

 Memphis makes an edge with
  * Memphis with a cost of: $0
  * Neworelans with a cost of: $3
  * Mobile with a cost of: $7
  * Atlanta with a cost of: $10
  * Nashvile with a cost of: $15

 Neworelans makes an edge with
  * Neworelans with a cost of: $0
  * Memphis with a cost of: $3
  * Mobile with a cost of: $3

 Mobile makes an edge with
  * Mobile with a cost of: $0
  * Memphis with a cost of: $7
  * Neworelans with a cost of: $3
  * Atlanta with a cost of: $2
  * Savannah with a cost of: $6

 Atlanta makes an edge with
  * Atlanta with a cost of: $0
  * Memphis with a cost of: $10
  * Nashvile with a cost of: $1
  * Mobile with a cost of: $2
  * Savannah with a cost of: $1

 Nashvile makes an edge with
  * Nashvile with a cost of: $0
  * Memphis with a cost of: $15
  * Atlanta with a cost of: $1

 Savannah makes an edge with
  * Savannah with a cost of: $0
  * Mobile with a cost of: $6
  * Atlanta with a cost of: $1

Cheapest journey from Memphis to Savannah:

  Total number of cities: 5
  Total cost: $9
 
  1 > Memphis
  2 > Neworelans
  3 > Mobile
  4 > Atlanta
  5 > Savannah

About

Dijkstra’s shortest path algorithm.


Languages

Language:Ruby 100.0%