Giter Club home page Giter Club logo

graph's Introduction

graph

Go

A simple graph library for Go.

Supported Algorithms

  • Bellman-Ford Algorithm
  • Dijkstras Algorithm
  • Breadth-first search
  • Depth-first search
  • Topological Sort
  • Cycle Detection
  • Prim Algorithm (MST)
  • Floyd-Warshall Algorithm
  • Kosaraju Algorithm
  • Laplacian Matrix
  • Hamiltonian Path Detection (Via DP)

Create a graph

There are multiple ways to create a graph. The first and straight forward option is to pass in an EdgeList

edgeList := []graph.Edge{
	{0, 1, 1},
	{1, 2, 1},
	{2, 0, 1},
}
g := graph.FromEdgeList(edgeList)

or you can first create the graph and then add Edges to it. The nodes will be created automatically

g2 := graph.NewGraph()
g2.AddEdge(0, 1, 1)
g2.AddEdge(1, 2, 1)
g2.AddEdge(2, 0, 1)

or you can pass the Adjaceny Matrix to create a graph, the float values of the matrix represent the weights of the edges. This Method returns an error if the matrix is not square.

g, err := graph.FromAdjMat([]float64{
    0, 2, 1, 0, 0, 0,
    3, 0, 0, 1, 1, 0,
    1, 0, 0, 0, 1, 0,
    0, 9, 0, 0, 1, 1,
    0, 1, 1, 1, 0, 1,
    0, 0, 0, 1, 1, 0,
})

Internally the Graph uses its Adjacency List as a data structure, so you can just create a new graph from an Adjacency List

g3 := graph.FromAdjList(graph.AdjList{
    0: []graph.WeightTuple{{1, 1}, {2, 1}},
    1: []graph.WeightTuple{{0, 3}},
    2: []graph.WeightTuple{{1, 2}},
})

Installing

go get github.com/timHau/[email protected]

Example usage

go run example/main.go

Testing

go test -v .

graph's People

Contributors

timhau avatar

Watchers

 avatar

graph's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.