Graph algorithms and data structures

Your basic graph GoDoc

Golang library of basic graph algorithms

Topological ordering

Topological ordering, image by David Eppstein, CC0 1.0.

This library offers efficient and well-tested algorithms for

  • breadth-first and depth-first search,
  • topological ordering,
  • strongly and weakly connected components,
  • bipartion,
  • shortest paths,
  • maximum flow,
  • Euler walks,
  • and minimum spanning trees.

The algorithms can be applied to any graph data structure implementing the two Iterator methods: Order, which returns the number of vertices, and Visit, which iterates over the neighbors of a vertex.

All algorithms operate on directed graphs with a fixed number of vertices, labeled from 0 to n-1, and edges with integer cost. An undirected edge {v, w} of cost c is represented by the two directed edges (v, w) and (w, v), both of cost c. A self-loop, an edge connecting a vertex to itself, is both directed and undirected.

Graph data structures

The type Mutable represents a directed graph with a fixed number of vertices and weighted edges that can be added or removed. The implementation uses hash maps to associate each vertex in the graph with its adjacent vertices. This gives constant time performance for all basic operations.

The type Immutable is a compact representation of an immutable graph. The implementation uses lists to associate each vertex in the graph with its adjacent vertices. This makes for fast and predictable iteration: the Visit method produces its elements by reading from a fixed sorted precomputed list.

Virtual graphs

The subpackage graph/build offers a tool for building graphs of type Virtual.

In a virtual graph no vertices or edges are stored in memory, they are instead computed as needed. New virtual graphs are constructed by composing and filtering a set of standard graphs, or by writing functions that describe the edges of a graph.

The following standard graphs are predefined:

  • empty graphs,
  • complete graphs and complete bipartite graphs,
  • grid graphs and complete k-ary trees,
  • cycle graphs and circulant graphs,
  • and hypergraphs.

The following operations are supported:

  • adding and deleting sets of edges,
  • adding cost functions,
  • filtering graphs by edge functions,
  • complement, intersection and union,
  • subgraphs,
  • connecting graphs at a single vertex,
  • joining two graphs by a set of edges,
  • matching two graphs by a set of edges,
  • cartesian product and tensor product.

Non-virtual graphs can be imported, and used as building blocks, by the Specific function. Virtual graphs don't need to be “exported‬”; they implement the Iterator interface and hence can be used directly by any algorithm in the graph package.

Installation

Once you have installed Go, run this command to install the graph package:

go get github.com/yourbasic/graph

Documentation

There is an online reference for the package at godoc.org/github.com/yourbasic/graph.

Roadmap

  • The API of this library is frozen.
  • Bug fixes and performance enhancement can be expected.
  • New functionality might be included.
  • Version numbers adhere to semantic versioning.

The only accepted reason to modify the API of this package is to handle issues that can't be resolved in any other reasonable way.

New features and performance enhancements are limited to basic algorithms and data structures, akin to the ones that you might find in a computer science textbook.

Stefan Nilsson – korthaj

Owner
Algorithms to Go
Code should be correct, clear and efficient. Prefer simple. Avoid clever.
Algorithms to Go
Similar Resources

Graph algorithms written in Go

Graph Algorithms in Go This repository contains implementations of various graph algorithms written in Go. I’ve written them to learn about these algo

Dec 26, 2022

Some algorithms in go: maxflow(min-cuts or graph-cuts), edit-distance.

Algorithms In this repository, some algorithms are implemented in go language. GoDoc link: ed maxflow About Max-flow problem: A flow network is repres

Sep 8, 2022

Graphoscope: a solution to access multiple independent data sources from a common UI and show data relations as a graph

Graphoscope: a solution to access multiple independent data sources from a common UI and show data relations as a graph

Graphoscope A solution to access multiple independent data sources from a common UI and show data relations as a graph: Contains a list of by default

May 26, 2022

Tutorial code for my video Learn to Use Basic Data Structures - Slices, Structs and Maps in Golang

Learn to Use Basic Data Structures - Slices, Structs and Maps in Golang Read text from a file and split into words. Introduction to slices / lists. Co

Jan 26, 2022

Data structure and relevant algorithms for extremely fast prefix/fuzzy string searching.

Trie Data structure and relevant algorithms for extremely fast prefix/fuzzy string searching. Usage Create a Trie with: t := trie.New() Add Keys with:

Dec 27, 2022

Data Structure Libraries and Algorithms implementation

Algorithms Data Structure Libraries and Algorithms implementation in C++ Disclaimer This repository is meant to be used as a reference to learn data s

Dec 8, 2022

A tree like tool help you to explore data structures in your redis server

 A tree like tool help you to explore data structures in your redis server

Redis-view is a tree like tool help you explore data structures in your redis server

Mar 17, 2022

Probabilistic data structures for processing continuous, unbounded streams.

Boom Filters Boom Filters are probabilistic data structures for processing continuous, unbounded streams. This includes Stable Bloom Filters, Scalable

Dec 30, 2022
Comments
  • Memory Improvement

    Memory Improvement

    This PR contain memory improvements to the following functions:

    • Acyclic
    • BFS
    • Bipartition
    • Components
    • Connected
    • EulerDirected
    • EulerUndirected
    • MST
    • ShortestPath
    • ShortestPaths
    • Sort
    • TopSort
    • Transpose

    The technique to use less memory is equal in all the functions. I declared the closures (from g.Visit(v, closure)) outside the loop (related: #8), the result is the closure is allocated only one time and not allocated in each loop iteration.

    I changed ShortestPath to stop when finding the target vertex.

    I added the benchmark results in the testdata directory (it can be removed). The benchmarks were done in my machine with background processes running so the speed results aren't very reliable, but the memory results should be.

    Obs: I didn't change the StrongComponents because it uses a recursive function, so the technique didn't fit well, and I didn't change MaxFlow because the improvement in memory was tiny.

    Benchmark Results:

    name       old time/op    new time/op    delta
    Acyclic-8     128µs ± 0%      66µs ± 0%  -47.95%
    
    name       old alloc/op   new alloc/op   delta
    Acyclic-8    52.0kB ± 0%    12.4kB ± 0%  -76.18%
    
    name       old allocs/op  new allocs/op  delta
    Acyclic-8     1.17k ± 0%     0.01k ± 0%  -98.89%
    
    name   old time/op    new time/op    delta
    BFS-8     269µs ± 0%     206µs ± 0%  -23.36%
    
    name   old alloc/op   new alloc/op   delta
    BFS-8    86.8kB ± 0%    23.2kB ± 0%  -73.27%
    
    name   old allocs/op  new allocs/op  delta
    BFS-8     1.01k ± 0%     0.02k ± 0%  -98.33%
    
    name           old time/op    new time/op    delta
    Bipartition-8    1.88µs ± 0%    1.40µs ± 0%  -25.44%
    
    name           old alloc/op   new alloc/op   delta
    Bipartition-8    3.62kB ± 0%    2.79kB ± 0%  -22.79%
    
    name           old allocs/op  new allocs/op  delta
    Bipartition-8      26.0 ± 0%      14.0 ± 0%  -46.15%
    
    name          old time/op    new time/op    delta
    Components-8     196µs ± 0%     149µs ± 0%  -24.01%
    
    name          old alloc/op   new alloc/op   delta
    Components-8    86.8kB ± 0%    54.8kB ± 0%  -36.83%
    
    name          old allocs/op  new allocs/op  delta
    Components-8     1.23k ± 0%     0.23k ± 0%  -80.96%
    
    name         old time/op    new time/op    delta
    Connected-8     151µs ± 0%     104µs ± 0%  -31.26%
    
    name         old alloc/op   new alloc/op   delta
    Connected-8    40.2kB ± 0%     8.3kB ± 0%  -79.45%
    
    name         old allocs/op  new allocs/op  delta
    Connected-8     1.00k ± 0%     0.01k ± 0%  -99.50%
    
    name             old time/op    new time/op    delta
    EulerDirected-8    7.28µs ± 0%    2.00µs ± 0%  -72.44%
    
    name             old alloc/op   new alloc/op   delta
    EulerDirected-8    5.71kB ± 0%    0.96kB ± 0%  -83.19%
    
    name             old allocs/op  new allocs/op  delta
    EulerDirected-8       103 ± 0%         4 ± 0%  -96.12%
    
    name               old time/op    new time/op    delta
    EulerUndirected-8    7.07µs ± 0%    2.39µs ± 0%  -66.21%
    
    name               old alloc/op   new alloc/op   delta
    EulerUndirected-8    5.72kB ± 0%    0.97kB ± 0%  -83.08%
    
    name               old allocs/op  new allocs/op  delta
    EulerUndirected-8       104 ± 0%         5 ± 0%  -95.19%
    
    name   old time/op    new time/op    delta
    MST-8     249µs ± 0%     190µs ± 0%  -23.85%
    
    name   old alloc/op   new alloc/op   delta
    MST-8    96.9kB ± 0%    33.0kB ± 0%  -65.99%
    
    name   old allocs/op  new allocs/op  delta
    MST-8     1.01k ± 0%     0.01k ± 0%  -99.20%
    
    
    name            old time/op    new time/op    delta
    ShortestPath-8     141µs ± 0%      60µs ± 0%  -57.23%
    
    name            old alloc/op   new alloc/op   delta
    ShortestPath-8    79.9kB ± 0%    39.8kB ± 0%  -50.23%
    
    name            old allocs/op  new allocs/op  delta
    ShortestPath-8       856 ± 0%        21 ± 0%  -97.55%
    
    name             old time/op    new time/op    delta
    ShortestPaths-8     121µs ± 0%      80µs ± 0%  -34.06%
    
    name             old alloc/op   new alloc/op   delta
    ShortestPaths-8    79.2kB ± 0%    39.7kB ± 0%  -49.85%
    
    name             old allocs/op  new allocs/op  delta
    ShortestPaths-8       841 ± 0%        19 ± 0%  -97.74%
    
    name    old time/op    new time/op    delta
    Sort-8     608µs ± 0%     500µs ± 0%  -17.79%
    
    name    old alloc/op   new alloc/op   delta
    Sort-8     249kB ± 0%     201kB ± 0%  -19.25%
    
    name    old allocs/op  new allocs/op  delta
    Sort-8     6.03k ± 0%     5.03k ± 0%  -16.57%
    
    name       old time/op    new time/op    delta
    TopSort-8     135µs ± 0%      69µs ± 0%  -48.49%
    
    name       old alloc/op   new alloc/op   delta
    TopSort-8    56.1kB ± 0%    16.5kB ± 0%  -70.62%
    
    name       old allocs/op  new allocs/op  delta
    TopSort-8     1.18k ± 0%     0.02k ± 0%  -98.14%
    
    
    name         old time/op    new time/op    delta
    Transpose-8     404µs ± 0%     401µs ± 0%   -0.72%
    
    name         old alloc/op   new alloc/op   delta
    Transpose-8     218kB ± 0%     170kB ± 0%  -22.04%
    
    name         old allocs/op  new allocs/op  delta
    Transpose-8     5.00k ± 0%     4.00k ± 0%  -19.98%
    
  • Detect cycles

    Detect cycles

    I apologies if your pkg can already do this, but I was perusing your docs and didn't see an easy way to. I'd like to tell which vertices contain cycles or paths back to themselves.

    I do know that you can tell me quickly if it is Acyclic, but I'd like to know which nodes have cycles.

    Is that possible and if not how would on add that?

    Thank you!

  • ShortestPath memory improvement

    ShortestPath memory improvement

    Hi Korthaj, when i was using the ShortestPath function i noticed that it was using a significant amount of memory (I was processing a large graph). It seems that the closure is allocated (or a pointer to it) at each iteration of the loop which makes the function use more memory.

    name             old time/op    new time/op    delta
    ShortestPaths-8     154µs ± 0%      85µs ± 0%  -45.02%
    
    name             old alloc/op   new alloc/op   delta
    ShortestPaths-8    81.0kB ± 0%    41.1kB ± 0%  -49.26%
    
    name             old allocs/op  new allocs/op  delta
    ShortestPaths-8       849 ± 0%        17 ± 0%  -98.00%
    

    This package and your blog are awsome.

    -Rodrigo

  • Make ShortestPath faster

    Make ShortestPath faster

    I noticed that the performance of ShortestPath can be substantially improved. Instead of finding all the shortest paths from the initial vertex, we could terminate the search as soon as we found the path to the destination vertex.

    I've opened a pull request because I already have a patch.

    1. No API changes whatsoever;
    2. This performance enhancement is based on the original E. W. Dijkstra's paper (it's what should be found in a CS textbook).

    old/new comparison:

    benchmark                       old ns/op     new ns/op     delta
    BenchmarkShortestPath250-4      55089         31078         -43.59%
    BenchmarkShortestPath500-4      121956        51563         -57.72%
    BenchmarkShortestPath1000-4     206507        79512         -61.50%
    
Algorithms and Data Structures Solved in Golang

Algorithms and Data Structures Solved in Golang Hi! I'm Bruno Melo and this repository contains a lot challenges solved on many plataforms using go as

Oct 20, 2022
Some data structures and algorithms using golang

Some data structures and algorithms using golang

Aug 13, 2022
Data structures and algorithms implementation from ZeroToMastery course

ZeroToMastery Data Structures & Algorithms course This repo includes all the data structure and algorithm exercises solutions and implementations. Ins

Jul 4, 2022
Practice-dsa-go - Data Structures and Algorithms for Interview Preparation in Go

Data Structures and Algorithms for Interview Preparation in Go Data Structures K

Jul 3, 2022
Implementation of various data structures and algorithms in Go
Implementation of various data structures and algorithms in Go

GoDS (Go Data Structures) Implementation of various data structures and algorithms in Go. Data Structures Containers Lists ArrayList SinglyLinkedList

Jan 25, 2022
Data Structures and Algorithms implementation in Go

Data Structures and Algorithms Clean and simple implementation in Go Implementation There are several data structures and algorithms implemented in th

Jan 2, 2023
Data Structures & Algorithms in Go

Data Structures and Algorithms with Go The aim of this repository is to provide Gophers with how data structures and algorithms are implemented in the

Dec 28, 2021
Grokking-algorithms-go - Solutions to common Data Structures problems

This is a repository dedicated to study, learn and solve Data Structure algorith

Apr 4, 2022
Golang string comparison and edit distance algorithms library, featuring : Levenshtein, LCS, Hamming, Damerau levenshtein (OSA and Adjacent transpositions algorithms), Jaro-Winkler, Cosine, etc...

Go-edlib : Edit distance and string comparison library Golang string comparison and edit distance algorithms library featuring : Levenshtein, LCS, Ham

Dec 20, 2022
Go translations of the algorithms and clients in the textbook Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.

Overview Go translations of the Java source code for the algorithms and clients in the textbook Algorithms, 4th Edition by Robert Sedgewick and Kevin

Dec 13, 2022