Skip to content
This repository was archived by the owner on Jun 11, 2023. It is now read-only.

Initial release of dijkstra_graph

Choose a tag to compare

@msayson msayson released this 28 Apr 02:06
· 9 commits to master since this release

DijkstraGraph::Graph API

# Add directed edge (source, destination) to the graph with given weight
# Requires that weight is a positive number
add_edge(source, destination, weight)

# Add undirected edge (vertex_a, vertex_b) to the graph with given weight
# Requires that weight is a positive number
add_undirected_edge(vertex_a, vertex_b, weight)

# Remove directed edge (source, destination) from the graph
remove_edge(source, destination)

# Remove undirected edge (vertex_a, vertex_b) from the graph
remove_undirected_edge(vertex_a, vertex_b)

# Return true iff the graph contains directed edge (source, destination)
contains_edge?(source, destination)

# Returns the weight of directed edge (source, destination),
# or returns Float::INFINITY if no such edge exists
get_edge_weight(source, destination)

# Returns the set of vertices v_i where edge (source, v_i) is in the graph
get_adjacent_vertices(source)

# Use Dijkstra's algorithm to find the shortest distances
# from the start vertex to each of the other vertices
#
# Returns a hash of form { 'start' => 0, 'a' => 3, 'b' => 4 },
# where result[v] indicates the shortest distance from start to v
shortest_distances(start)

# Use Dijkstra's algorithm to find the shortest paths
# from the start vertex to each of the other vertices
#
# Returns a hash of form { 'c' => ['a', 'b', 'c'] }, where
# result[v] indicates the shortest path from start to v
shortest_paths(start)

# Use Dijkstra's algorithm to find the shortest path
# from the start vertex to the destination vertex
#
# Returns an array of vertices along the shortest path
# of form ['a', 'b', 'c'], or [] if no such path exists
shortest_path(start, destination)