Giraffe.Graph.Undirected (giraffe v0.2.0)
Implementation of an undirected graph with weighted edges. Vertices can be any term, and edges have numeric weights.
Summary
Functions
Adds an edge between two vertices with the given weight. If the vertices don't exist, they will be added to the graph.
Adds a vertex to the graph with an optional label.
Finds shortest paths from a source vertex using the Bellman-Ford algorithm. Returns nil if a negative cycle is detected.
Detects all maximal cliques in the graph using the Bron-Kerbosch 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, :no_path otherwise.
Returns true if the given vertex exists in the graph.
Checks if the graph is acyclic (contains no cycles).
Checks if the graph contains any cycles.
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 shortest paths using Bellman-Ford algorithm.
Returns the label for the given vertex
Returns a list of all vertices in the graph.
Types
Functions
Adds an edge between two vertices with the given weight. If the vertices don't exist, they will be added to the graph.
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 from a source vertex using the Bellman-Ford algorithm. Returns nil if a negative cycle is detected.
Examples
iex> graph = Giraffe.Graph.Undirected.new()
...> graph = graph |> Giraffe.Graph.Undirected.add_edge(:a, :b, 1)
...> graph |> Giraffe.Graph.Undirected.bellman_ford(:a)
%{a: 0, b: 1}
Detects all maximal cliques in the graph using the Bron-Kerbosch algorithm.
Returns a list of all edges in the graph as tuples of {from, to, weight}.
Examples
iex> graph = Giraffe.Graph.Undirected.new()
...> graph = Giraffe.Graph.Undirected.add_edge(graph, :a, :b, 1)
...> Giraffe.Graph.Undirected.edges(graph)
[{:a, :b, 1}]
Returns a list of all edges for a specific vertex.
Examples
iex> graph = Giraffe.Graph.Undirected.new()
...> graph = Giraffe.Graph.Undirected.add_edge(graph, :a, :b, 1)
...> Giraffe.Graph.Undirected.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.
Finds the shortest path between two vertices using Dijkstra's algorithm. Returns {:ok, path, total_weight} if a path exists, :no_path otherwise.
Returns true if the given vertex exists in the graph.
Checks if the graph is acyclic (contains no cycles).
Checks if the graph contains any cycles.
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"}}
Finds shortest paths using Bellman-Ford algorithm.
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]