Giraffe.Graph.Directed (giraffe v0.1.2)

Implementation of a directed graph with weighted edges. Vertices can be any term, and edges have numeric weights.

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.

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.

Returns a list of all edges in the graph as tuples of {from, to, weight}.

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.

Checks if the graph is acyclic.

Checks if the graph is cyclic.

Creates a new empty directed graph.

Finds the shortest paths from a source vertex to all other vertices using the Bellman-Ford algorithm. Returns {:ok, distances} with distances from source to all reachable vertices, or {:error, :negative_cycle} if a negative cycle is detected.

Returns a list of all vertices in the graph.

Types

edge()

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

t()

@type t() :: %Giraffe.Graph.Directed{
  edges: %{required(vertex()) => %{required(vertex()) => weight()}},
  vertices: MapSet.t()
}

vertex()

@type vertex() :: any()

weight()

@type weight() :: number()

Functions

add_edge(graph, from, to, weight)

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

Adds a weighted edge between two vertices in the graph. The vertices will be added to the graph if they don't already exist.

add_vertex(graph, vertex)

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

Adds a vertex to the graph.

cliques(directed)

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

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.

edges(directed)

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

Returns a list of all edges in the graph as tuples of {from, to, weight}.

get_paths(graph, start, finish)

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

Finds all possible paths between two vertices. Returns a list of tuples containing the path and its total weight.

get_shortest_path(directed, start, finish)

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

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.

is_acyclic?(directed)

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

Checks if the graph is acyclic.

Returns true if the graph is acyclic, otherwise false.

is_cyclic?(graph)

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

Checks if the graph is cyclic.

Returns true if the graph contains cycles, otherwise false.

neighbors(directed, vertex)

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

new()

@spec new() :: t()

Creates a new empty directed graph.

postorder(directed)

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

shortest_paths(graph, source)

Finds the shortest paths from a source vertex to all other vertices using the Bellman-Ford algorithm. Returns {:ok, distances} with distances from source to all reachable vertices, or {:error, :negative_cycle} if a negative cycle is detected.

vertices(directed)

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

Returns a list of all vertices in the graph.