Giraffe.Graph (giraffe v0.2.1)

Public interface for working with graphs. Delegates to specific implementations based on whether the graph is directed or undirected.

Summary

Functions

Adds an edge between two vertices with optional weight.

Adds a new vertex to the graph with optional label.

Finds shortest paths from source vertex using Bellman-Ford algorithm.

Gets the shortest path between two vertices using Dijkstra's algorithm.

Returns a list of all edges in the graph.

Returns a list of all edges connected to the given vertex.

Returns a list of edges between two vertices.

Gets the label associated with a vertex.

Gets all paths between two vertices.

Gets the shortest path between two vertices.

Returns true if the vertex exists in the graph.

Returns true if the graph is acyclic.

Returns true if the graph contains cycles.

Returns a list of neighboring vertices.

Creates a new graph.

Returns the total number of edges in the graph.

Returns the total number of vertices in the graph.

Returns a list of vertices reachable from the given vertices.

Sets a label for an existing vertex.

Returns the labels for the given vertex.

Returns a list of all vertices in the graph.

Types

edge()

@type edge() :: {vertex(), vertex(), weight()}

label()

@type label() :: any()

t()

@type t() :: %Giraffe.Graph{
  impl: Giraffe.Graph.Directed.t() | Giraffe.Graph.Undirected.t(),
  type: :directed | :undirected
}

vertex()

@type vertex() :: any()

weight()

@type weight() :: number()

Functions

add_edge(graph, from, to, weight \\ 1)

@spec add_edge(t(), vertex(), vertex(), weight()) :: t()

Adds an edge between two vertices with optional weight.

Examples

iex> graph = Giraffe.Graph.new(type: :directed)
iex> Giraffe.Graph.add_edge(graph, :a, :b, 2.5)
%Giraffe.Graph{impl: %Giraffe.Graph.Directed{vertices: MapSet.new([:a, :b]), edges: %{a: %{b: 2.5}}}, type: :directed}

add_vertex(graph, vertex, label \\ nil)

@spec add_vertex(t(), vertex(), label()) :: t()

Adds a new vertex to the graph with optional label.

Examples

iex> graph = Giraffe.Graph.new(type: :directed)
iex> Giraffe.Graph.add_vertex(graph, :a, "vertex A")
%Giraffe.Graph{impl: %Giraffe.Graph.Directed{vertices: MapSet.new([:a]), edges: %{}, labels: %{a: "vertex A"}}, type: :directed}

bellman_ford(graph, source)

@spec bellman_ford(t(), vertex()) :: %{required(vertex()) => number()} | nil

Finds shortest paths from source vertex using Bellman-Ford algorithm.

Examples

iex> graph = Giraffe.Graph.new(type: :directed) |> Giraffe.Graph.add_edge(:a, :b, 1)
iex> Giraffe.Graph.bellman_ford(graph, :a)
%{a: 0, b: 1}

dijkstra(graph, a, b)

@spec dijkstra(t(), vertex(), vertex()) :: {:ok, [vertex()], number()} | :no_path

Gets the shortest path between two vertices using Dijkstra's algorithm.

Examples

iex> graph = Giraffe.Graph.new(type: :directed) |> Giraffe.Graph.add_edge(:a, :b, 1)
iex> Giraffe.Graph.dijkstra(graph, :a, :b)
{:ok, [:a, :b], 1}

edges(graph)

@spec edges(t()) :: [edge()]

Returns a list of all edges in the graph.

Examples

iex> graph = Giraffe.Graph.new(type: :directed) |> Giraffe.Graph.add_edge(:a, :b, 1)
iex> Giraffe.Graph.edges(graph)
[{:a, :b, 1}]

edges(graph, v)

@spec edges(t(), vertex()) :: [edge()]

Returns a list of all edges connected to the given vertex.

Examples

iex> graph = Giraffe.Graph.new(type: :directed) |> Giraffe.Graph.add_edge(:a, :b, 1)
iex> Giraffe.Graph.edges(graph, :a)
[{:a, :b, 1}]

edges(graph, v1, v2)

@spec edges(t(), vertex(), vertex()) :: [edge()]

Returns a list of edges between two vertices.

get_label(graph, vertex)

@spec get_label(t(), vertex()) :: label() | nil

Gets the label associated with a vertex.

Examples

iex> graph = Giraffe.Graph.new(type: :directed) |> Giraffe.Graph.add_vertex(:a, "label A")
iex> Giraffe.Graph.get_label(graph, :a)
"label A"

get_paths(graph, start, finish)

@spec get_paths(t(), vertex(), vertex()) :: [{[vertex()], number()}]

Gets all paths between two vertices.

Examples

iex> graph = Giraffe.Graph.new(type: :directed) |> Giraffe.Graph.add_edge(:a, :b, 1)
iex> Giraffe.Graph.get_paths(graph, :a, :b)
[{[:a, :b], 1.0}]

get_shortest_path(graph, start, finish)

@spec get_shortest_path(t(), vertex(), vertex()) ::
  {:ok, [vertex()], number()} | :no_path

Gets the shortest path between two vertices.

Examples

iex> graph = Giraffe.Graph.new(type: :directed) |> Giraffe.Graph.add_edge(:a, :b, 1)
iex> Giraffe.Graph.get_shortest_path(graph, :a, :b)
{:ok, [:a, :b], 1}

has_vertex?(graph, vertex)

@spec has_vertex?(t(), vertex()) :: boolean()

Returns true if the vertex exists in the graph.

Examples

iex> graph = Giraffe.Graph.new(type: :directed) |> Giraffe.Graph.add_vertex(:a)
iex> Giraffe.Graph.has_vertex?(graph, :a)
true

is_acyclic?(graph)

@spec is_acyclic?(t()) :: boolean()

Returns true if the graph is acyclic.

Examples

iex> graph = Giraffe.Graph.new(type: :directed) |> Giraffe.Graph.add_edge(:a, :b, 1)
iex> Giraffe.Graph.is_acyclic?(graph)
true

is_cyclic?(graph)

@spec is_cyclic?(t()) :: boolean()

Returns true if the graph contains cycles.

Examples

iex> graph = Giraffe.Graph.new(type: :directed) |> Giraffe.Graph.add_edge(:a, :a, 1)
iex> Giraffe.Graph.is_cyclic?(graph)
true

neighbors(graph, vertex)

@spec neighbors(t(), vertex()) :: [vertex()]

Returns a list of neighboring vertices.

Examples

iex> graph = Giraffe.Graph.new(type: :directed) |> Giraffe.Graph.add_edge(:a, :b, 1)
iex> Giraffe.Graph.neighbors(graph, :a)
[:b]

new(list)

@spec new([{:type, :directed}]) :: t()
@spec new([{:type, :undirected}]) :: t()

Creates a new graph.

Options

  • :type - The type of graph. Can be :directed or :undirected. Defaults to :directed.

Examples

iex> Giraffe.Graph.new(type: :directed)
%Giraffe.Graph{impl: %Giraffe.Graph.Directed{vertices: MapSet.new([]), edges: %{}}, type: :directed}

iex> Giraffe.Graph.new(type: :undirected)
%Giraffe.Graph{impl: %Giraffe.Graph.Undirected{vertices: MapSet.new([]), edges: %{}}, type: :undirected}

num_edges(graph)

@spec num_edges(t()) :: non_neg_integer()

Returns the total number of edges in the graph.

Examples

iex> graph = Giraffe.Graph.new(type: :directed) |> Giraffe.Graph.add_edge(:a, :b, 1)
iex> Giraffe.Graph.num_edges(graph)
1

num_vertices(graph)

@spec num_vertices(t()) :: non_neg_integer()

Returns the total number of vertices in the graph.

Examples

iex> graph = Giraffe.Graph.new(type: :directed) |> Giraffe.Graph.add_vertex(:a)
iex> Giraffe.Graph.num_vertices(graph)
1

reachable(graph, vertices)

@spec reachable(t(), [vertex()]) :: [vertex()]

Returns a list of vertices reachable from the given vertices.

Examples

iex> graph = Giraffe.Graph.new(type: :directed) |> Giraffe.Graph.add_edge(:a, :b, 1)
iex> Giraffe.Graph.reachable(graph, [:a])
[:a, :b]

set_label(graph, vertex, label)

@spec set_label(t(), vertex(), label()) :: t()

Sets a label for an existing vertex.

Examples

iex> graph = Giraffe.Graph.new(type: :directed) |> Giraffe.Graph.add_vertex(:a)
iex> Giraffe.Graph.set_label(graph, :a, "new label")
%Giraffe.Graph{impl: %Giraffe.Graph.Directed{vertices: MapSet.new([:a]), edges: %{}, labels: %{a: "new label"}}, type: :directed}

vertex_labels(graph, vertex)

@spec vertex_labels(t(), vertex()) :: [label()]

Returns the labels for the given vertex.

Examples

iex> graph = Giraffe.Graph.new(type: :directed) |> Giraffe.Graph.add_vertex(:a, [:label])
iex> Giraffe.Graph.vertex_labels(graph, :a)
[:label]

vertices(graph)

@spec vertices(t()) :: [vertex()]

Returns a list of all vertices in the graph.

Examples

iex> graph = Giraffe.Graph.new(type: :directed) |> Giraffe.Graph.add_vertex(:a)
iex> Giraffe.Graph.vertices(graph)
[:a]