Giraffe.Graph.Directed (giraffe v0.2.1)
Implementation of a directed graph with weighted edges. Vertices can be any term, and edges have numeric weights.
Examples
iex> graph = Giraffe.Graph.Directed.new()
...> graph = graph |> Giraffe.Graph.Directed.add_edge(:a, :b, 1)
...> graph |> Giraffe.Graph.Directed.edges()
[{:a, :b, 1}]
Summary
Functions
Adds a weighted edge between two vertices in the graph. The vertices will be added to the graph if they don't already exist.
Adds a vertex to the graph with an optional label.
Finds shortest paths using Bellman-Ford algorithm
Finds all maximal cliques in the graph. A clique is a subset of vertices where every vertex is connected to every other vertex. Only considers bidirectional edges.
Gets the shortest path between a and b using Dijkstra's algorithm
Returns a list of all edges in the graph as tuples of {from, to, weight}.
Returns a list of all edges for a specific vertex.
Gets the label associated with a vertex.
Finds all possible paths between two vertices. Returns a list of tuples containing the path and its total weight.
Finds the shortest path between two vertices using Dijkstra's algorithm. Returns {:ok, path, total_weight} if a path exists, or :no_path if no path exists.
Returns true if the given vertex exists in the graph.
Checks if the graph is acyclic.
Checks if the graph is cyclic.
Returns a sorted list of all vertices that are neighbors of the given vertex. Includes both incoming and outgoing edges.
Creates a new empty graph.
Returns the number of edges in the graph
Returns the number of vertices in the graph
Returns vertices in post-order DFS traversal order.
Returns a list of all vertices reachable from the given starting vertices.
Sets a label for an existing vertex. Returns unchanged graph if vertex doesn't exist.
Finds the shortest paths from a source vertex to all other vertices using the Bellman-Ford algorithm.
Returns the label for the given vertex
Returns a list of all vertices in the graph.
Types
Functions
Adds a weighted edge between two vertices in the graph. The vertices will be added to the graph if they don't already exist.
Examples
iex> graph = Giraffe.Graph.Directed.new()
...> graph = Giraffe.Graph.Directed.add_edge(graph, :a, :b, 1)
...> Giraffe.Graph.Directed.edges(graph)
[{:a, :b, 1}]
Adds a vertex to the graph with an optional label.
Examples
iex> graph = Giraffe.Graph.Directed.new()
iex> Giraffe.Graph.Directed.add_vertex(graph, :a, "vertex A")
%Giraffe.Graph.Directed{vertices: MapSet.new([:a]), edges: %{}, labels: %{a: "vertex A"}}
Finds shortest paths using Bellman-Ford algorithm
Finds all maximal cliques in the graph. A clique is a subset of vertices where every vertex is connected to every other vertex. Only considers bidirectional edges.
Examples
iex> graph = Giraffe.Graph.Directed.new()
...> graph = graph |> Giraffe.Graph.Directed.add_edge(:a, :b, 1)
...> graph = graph |> Giraffe.Graph.Directed.add_edge(:b, :a, 1)
...> Giraffe.Graph.Directed.cliques(graph)
[[:a, :b]]
Gets the shortest path between a and b using Dijkstra's algorithm
Returns a list of all edges in the graph as tuples of {from, to, weight}.
Examples
iex> graph = Giraffe.Graph.Directed.new()
...> graph = Giraffe.Graph.Directed.add_edge(graph, :a, :b, 1)
...> Giraffe.Graph.Directed.edges(graph)
[{:a, :b, 1}]
Returns a list of all edges for a specific vertex.
Examples
iex> graph = Giraffe.Graph.Directed.new()
...> graph = Giraffe.Graph.Directed.add_edge(graph, :a, :b, 1)
...> Giraffe.Graph.Directed.edges(graph, :a)
[{:a, :b, 1}]
Gets the label associated with a vertex.
Examples
iex> graph = Giraffe.Graph.Directed.new() |> Giraffe.Graph.Directed.add_vertex(:a, "vertex A")
iex> Giraffe.Graph.Directed.get_label(graph, :a)
"vertex A"
Finds all possible paths between two vertices. Returns a list of tuples containing the path and its total weight.
Examples
iex> graph = Giraffe.Graph.Directed.new()
...> graph = graph |> Giraffe.Graph.Directed.add_edge(:a, :b, 1)
...> Giraffe.Graph.Directed.get_paths(graph, :a, :b)
[{[:a, :b], 1.0}]
Finds the shortest path between two vertices using Dijkstra's algorithm. Returns {:ok, path, total_weight} if a path exists, or :no_path if no path exists.
Examples
iex> graph = Giraffe.Graph.Directed.new()
...> graph = graph |> Giraffe.Graph.Directed.add_edge(:a, :b, 1)
...> Giraffe.Graph.Directed.get_shortest_path(graph, :a, :b)
{:ok, [:a, :b], 1}
Returns true if the given vertex exists in the graph.
Checks if the graph is acyclic.
Examples
iex> graph = Giraffe.Graph.Directed.new()
...> graph = graph |> Giraffe.Graph.Directed.add_edge(:a, :b, 1)
...> Giraffe.Graph.Directed.is_acyclic?(graph)
true
Checks if the graph is cyclic.
Examples
iex> graph = Giraffe.Graph.Directed.new()
...> graph = graph |> Giraffe.Graph.Directed.add_edge(:a, :b, 1)
...> graph = graph |> Giraffe.Graph.Directed.add_edge(:b, :a, 1)
...> Giraffe.Graph.Directed.is_cyclic?(graph)
true
Returns a sorted list of all vertices that are neighbors of the given vertex. Includes both incoming and outgoing edges.
Examples
iex> graph = Giraffe.Graph.Directed.new()
...> graph = graph |> Giraffe.Graph.Directed.add_edge(:a, :b, 1)
...> Giraffe.Graph.Directed.neighbors(graph, :a)
[:b]
Creates a new empty graph.
Examples
iex> Giraffe.Graph.Directed.new()
%Giraffe.Graph.Directed{vertices: MapSet.new(), edges: %{}, labels: %{}}
@spec num_edges(t()) :: non_neg_integer()
Returns the number of edges in the graph
@spec num_vertices(t()) :: non_neg_integer()
Returns the number of vertices in the graph
Returns vertices in post-order DFS traversal order.
Examples
iex> graph = Giraffe.Graph.Directed.new()
...> |> Giraffe.Graph.Directed.add_edge(:a, :b, 1)
...> |> Giraffe.Graph.Directed.add_edge(:b, :c, 1)
iex> Giraffe.Graph.Directed.postorder(graph)
[:c, :b, :a]
Returns a list of all vertices reachable from the given starting vertices.
Examples
iex> graph = Giraffe.Graph.Directed.new()
...> |> Giraffe.Graph.Directed.add_edge(:a, :b, 1)
...> |> Giraffe.Graph.Directed.add_edge(:b, :c, 1)
iex> Giraffe.Graph.Directed.reachable(graph, [:a])
[:a, :b, :c]
Sets a label for an existing vertex. Returns unchanged graph if vertex doesn't exist.
Examples
iex> graph = Giraffe.Graph.Directed.new() |> Giraffe.Graph.Directed.add_vertex(:a)
iex> Giraffe.Graph.Directed.set_label(graph, :a, "new label")
%Giraffe.Graph.Directed{vertices: MapSet.new([:a]), edges: %{}, labels: %{a: "new label"}}
@spec shortest_paths(t(), vertex()) :: {:ok, %{required(vertex()) => weight()}} | {:error, :negative_cycle}
Finds the shortest paths from a source vertex to all other vertices using the Bellman-Ford algorithm.
Examples
iex> graph = Giraffe.Graph.Directed.new()
...> graph = graph |> Giraffe.Graph.Directed.add_edge(:a, :b, 1)
...> Giraffe.Graph.Directed.shortest_paths(graph, :a)
{:ok, %{a: 0, b: 1}}
Returns the label for the given vertex
Returns a list of all vertices in the graph.
Examples
iex> graph = Giraffe.Graph.Directed.new() |> Giraffe.Graph.Directed.add_vertex(:a)
iex> Giraffe.Graph.Directed.vertices(graph)
[:a]