ExGraphs is an Elixir library for creating, manipulating, and analyzing graphs. It provides a simple and intuitive API for working with graph data structures.
- Graph Creation: Create graphs with vertices and edges
- File I/O: Import graphs from standard
.netfiles - Graph Operations: Add vertices, create edges, check connectivity
- Basic Queries: Check vertex/edge existence, count vertices/edges
Add ex_graphs to your list of dependencies in mix.exs:
def deps do
[
{:ex_graphs, "~> 0.1.0"}
]
endalias ExGraphs.Graph
# Create an empty graph
graph = %Graph{}
# Add vertices
{:ok, graph} = Graph.create_vertex(graph, 1, "A")
{:ok, graph} = Graph.create_vertex(graph, 2, "B")
{:ok, graph} = Graph.create_vertex(graph, 3, "C")
# Create edges between vertices
{:ok, graph, _, _} = Graph.create_edge(graph, 1, 2)
{:ok, graph, _, _} = Graph.create_edge(graph, 2, 3, 2.5) # with weightExGraphs supports reading graphs from .net files:
alias ExGraphs.Utils
# Read from a .net file
{:ok, graph} = Utils.read_graph("path/to/file.net")The .net file should have the following format:
*vertices 3
1 A
2 B
3 C
*edges
1 2 1.0
2 3 2.5
# Check if a vertex exists
Graph.vertex_in_graph?(graph, 1) # => true
# Get vertex info
vertex = Graph.get_vertex(graph, 1)
# Check if an edge exists
Graph.has_edge?(graph, 1, 2) # => true
# Count vertices and edges
Graph.vertices_amount(graph) # => 3
Graph.edges_amount(graph) # => 2Graphs in ExGraphs are composed of:
- Vertices: Each vertex has an index, label, neighbors list, and degree
- Edges: Each edge connects two vertices with an optional weight
mix test