Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ruby-ist/dijkstra_trace
A ruby gem for dijkstra Algorithm implementation
https://github.com/ruby-ist/dijkstra_trace
dijkstra-shortest-path ruby rubygems
Last synced: about 2 months ago
JSON representation
A ruby gem for dijkstra Algorithm implementation
- Host: GitHub
- URL: https://github.com/ruby-ist/dijkstra_trace
- Owner: ruby-ist
- Created: 2022-07-23T10:34:10.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-01-10T05:52:19.000Z (almost 2 years ago)
- Last Synced: 2024-10-31T14:47:10.862Z (2 months ago)
- Topics: dijkstra-shortest-path, ruby, rubygems
- Language: Ruby
- Homepage: https://rubygems.org/gems/dijkstra_trace
- Size: 7.81 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Dijkstra Trace
[![Gem Version](https://badge.fury.io/rb/dijkstra_trace.svg)](https://badge.fury.io/rb/dijkstra_trace)
A simple Dijkstra algorithm implementation to find the shortest path between any two points in the undirected graph.
## Installation
Install the gem by adding the following line in your Gemfile and run `bundle install`
```ruby
gem 'dijkstra_trace'
```
or simply run `bundle add dijkstra_trace`
## UsageCreate an object for `Dijkstra::Trace` class with the graph edges as its argument.
```ruby
require 'dijkstra_trace'edges = [
[0,1,55],
[1,2,100],
[0,3,75],
[3,4,60],
[1,4,75],
[3,6,50],
[4,7,50],
[6,7,65],
[2,5,95],
[5,8,65],
[7,8,40],
[8,9,40],
[9,10,40],
[9,11,50],
[10,12,75],
[11,13,110],
[12,13,50]
]graph = Dijkstra::Trace.new(edges)
path = graph.path(2,13)puts "The shortest distance between #{path.starting_point} and #{path.ending_point} is #{path.distance} units"
puts "The shortest path: #{path.path}"```
### Output
```
The shortest distance between 2 and 13 is 360 units
The shortest path: [2, 5, 8, 9, 11, 13]
```The Vertices is not only limited to numbers, it can be any type as long as it can be used as a key for an `Hash`
```ruby
edges = [
["City A", "City B", 200],
["City B", "City C", 60],
["City A", "City C", 270],
]
graph = Dijkstra::Trace.new(edges)
path = graph.path("City A", "City C")
pp path.path
```
### Output
```
["City A", "City B", "City C"]
```
The Graph matrix and the adjacent matrix can also be viewed by calling the methods `graph_matrix` and `adjacent_matrix` on the `Dijkstra::Trace` class object.Graph Matrix for the first output:
```
irb(main):001:0> pp graph.graph_matrix[[0, 55, 0, 75, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[55, 0, 100, 0, 75, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 100, 0, 0, 0, 95, 0, 0, 0, 0, 0, 0, 0, 0],
[75, 0, 0, 0, 60, 0, 50, 0, 0, 0, 0, 0, 0, 0],
[0, 75, 0, 60, 0, 0, 0, 50, 0, 0, 0, 0, 0, 0],
[0, 0, 95, 0, 0, 0, 0, 0, 65, 0, 0, 0, 0, 0],
[0, 0, 0, 50, 0, 0, 0, 65, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 50, 0, 65, 0, 40, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 65, 0, 40, 0, 40, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 40, 0, 40, 50, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 40, 0, 0, 75, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 50, 0, 0, 0, 110],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 75, 0, 0, 50],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 110, 50, 0]]
```