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 heuristic costs (for the shortest path algorithm) and and optional label
  • Edges from and to nodes with certain costs
  • Adding and deleting nodes
  • Adding and deleting edges

The Graph module is strucutred like so:

  • nodes is a Map that has the node_id (atom) as a key and another Map as value. This Map conatins label and costs as keys that refer to the corresponding value.
  • edges is a Map that has the node_id (atom) as a key and a MapSet as value. The MapSet contains Maps which store the information of the edge. The key to points to the node the adge is connecting and the key costs 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 type costs()
costs() :: non_neg_integer()
Link to this type edge_info()
edge_info() :: %{to: node_id(), costs: costs()}
Link to this type label()
label() :: term()
Link to this type node_id()
node_id() :: atom()
Link to this type t()
t() :: %Graph{edges: %{optional(node_id()) => MapSet.t(edge_info())}, nodes: %{optional(node_id()) => %{label: label(), costs: costs()}}}

Link to this section Functions

Link to this function add_edge(g, from, to, costs)
add_edge(t(), node_id(), node_id(), costs()) :: t()

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}} }

Link to this function add_node(g, node)

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}}}

Link to this function add_node(g, node, opts)

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”}}}

Link to this function delete_edge(g, from, to)
delete_edge(t(), node_id(), node_id()) :: t()

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}} }

Link to this function delete_node(g, node)
Link to this function new()
new() :: t()

Creates a new undirected Graph.

Link to this function shortest_path(g, from, to)

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]