graphbrewer v0.1.0 Graph
This module serves as a graph library that enables to handle undirected graphs (directed is in the works) in memory. It features simple operations as stated below and also includes a shortest path calculation.
Supported features:
Nodes
with an optional heuristiccosts
(for the shortest path algorithm) and and optionallabel
Edges
from and to nodes with certaincosts
- Adding and deleting
nodes
- Adding and deleting
edges
The Graph
module is strucutred like so:
nodes
is aMap
that has the node_id (atom) as a key and anotherMap
as value. ThisMap
conatinslabel
andcosts
as keys that refer to the corresponding value.edges
is aMap
that has the node_id (atom) as a key and aMapSet
as value. TheMapSet
containsMaps
which store the information of theedge
. The keyto
points to thenode
the adge is connecting and the keycosts
points to the assigned costs of the edge.
Link to this section Summary
Functions
Adds an edge to the given graph from a to b & b to a and assigns the according costs. If the nodes a and / or b do not exist they are created (costs and label of these nodes is set to nil)
Adds a node to the graph without any further info
Adds a node to the graph with the specified information
Deletes an the edge that goes from a to b. The edge is only deleted if it really exists. Isolated nodes are of course not deleted
Creates a new undirected Graph
Find the shortest path from a to b in the given graph
Link to this section Types
Link to this section Functions
Adds an edge to the given graph from a to b & b to a and assigns the according costs. If the nodes a and / or b do not exist they are created (costs and label of these nodes is set to nil).
Example
iex> g = Graph.new |> Graph.add_edge(:a, :b, 5) %Graph{ edges: %{a: #MapSet<[%{costs: 5, to: :b}]>, b: #MapSet<[%{costs: 5, to: :a}]>}, nodes: %{a: %{costs: 0, label: nil}, b: %{costs: 0, label: nil}} }
Adds a node to the graph without any further info.
Example
iex> g = Graph.new |> Graph.add_node(:a) %Graph{edges: %{}, nodes: %{a: %{costs: 0, label: nil}}}
Adds a node to the graph with the specified information.
Example
iex> g = Graph.new |> Graph.add_node(:a, %{label: “This is a”, costs: 2}) %Graph{edges: %{}, nodes: %{a: %{costs: 2, label: “This is a”}}}
Deletes an the edge that goes from a to b. The edge is only deleted if it really exists. Isolated nodes are of course not deleted.
Example
iex> g = Graph.new |> Graph.add_edge(:a, :b, 5) |> Graph.add_edge(:b, :c, 5) %Graph{ edges: %{a: #MapSet<[%{costs: 5, to: :b}]>, b: #MapSet<[%{costs: 5, to: :a}, %{costs: 5, to: :c}]>, c: #MapSet<[%{costs: 5, to: :b}]>}, nodes: %{a: %{costs: 0, label: nil}, b: %{costs: 0, label: nil}, c: %{costs: 0, label: nil}} } iex> g = Graph.delete_edge(g, :b, :c) %Graph{ edges: %{a: #MapSet<[%{costs: 5, to: :b}]>, b: #MapSet<[%{costs: 5, to: :a}]>, c: #MapSet<[]>}, nodes: %{a: %{costs: 0, label: nil}, b: %{costs: 0, label: nil}, c: %{costs: 0, label: nil}} }
Creates a new undirected Graph.
Find the shortest path from a to b in the given graph.
iex> g = Graph.new |> …> Graph.add_edge(:s, :a, 3) |> …> Graph.add_edge(:a, :b, 5) |> …> Graph.add_edge(:b, :c, 10) |> …> Graph.add_edge(:c, :d, 3) |> …> Graph.add_edge(:d, :e, 4) |> …> Graph.add_edge(:b, :e, 5) |> …> Graph.shortest_path(:s, :e) [:s, :a, :b, :e]