Giraffe.Graph.Undirected (giraffe v0.1.3)

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.

Detects all maximal cliques in the graph using the Bron-Kerbosch algorithm.

Returns a list of all edges in the graph. Each edge is represented as a tuple {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, :no_path otherwise.

Checks if the graph is acyclic (contains no cycles). Returns true if the graph has no cycles, false otherwise.

Checks if the graph contains any cycles. Returns true if the graph has cycles, false otherwise.

Creates a new empty undirected graph.

Returns a list of vertices that are reachable from any of the given vertices. Includes the starting vertices themselves as paths of length zero are allowed.

Returns a list of all vertices in the graph.

Types

edge()

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

t()

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

vertex()

@type vertex() :: any()

weight()

@type weight() :: number()

Functions

add_edge(graph, vertex1, vertex2, weight)

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

Adds an edge between two vertices with the given weight. If the vertices don't exist, they will be added to the graph.

add_vertex(graph, vertex)

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

Adds a vertex to the graph.

cliques(graph)

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

Detects all maximal cliques in the graph using the Bron-Kerbosch algorithm.

Returns a list of cliques, where each clique is a list of vertices. A clique is a subset of vertices that forms a complete subgraph.

edges(undirected)

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

Returns a list of all edges in the graph. Each edge is represented as a tuple {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(graph, 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, :no_path otherwise.

is_acyclic?(undirected)

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

Checks if the graph is acyclic (contains no cycles). Returns true if the graph has no cycles, false otherwise.

is_cyclic?(graph)

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

Checks if the graph contains any cycles. Returns true if the graph has cycles, false otherwise.

neighbors(undirected, vertex)

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

new()

@spec new() :: t()

Creates a new empty undirected graph.

postorder(undirected)

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

reachable(graph, vertices)

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

Returns a list of vertices that are reachable from any of the given vertices. Includes the starting vertices themselves as paths of length zero are allowed.

Examples

iex> g = Giraffe.Graph.Undirected.new()
...> g = g |> Giraffe.Graph.Undirected.add_edge(1, 2, 1) |> Giraffe.Graph.Undirected.add_edge(2, 3, 1)
...> Giraffe.Graph.Undirected.reachable(g, [1])
[1, 2, 3]

shortest_paths(graph, source)

vertices(undirected)

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

Returns a list of all vertices in the graph.