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
@type label() :: any()
@type t() :: %Giraffe.Graph{ impl: Giraffe.Graph.Directed.t() | Giraffe.Graph.Undirected.t(), type: :directed | :undirected }
@type vertex() :: any()
@type weight() :: number()
Functions
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}
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}
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}
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}
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}]
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}]
Returns a list of edges between two vertices.
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"
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}]
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}
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
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
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
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]
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}
@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
@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
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]
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}
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]
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]