Socket
Book a DemoInstallSign in
Socket

github.com/baburkin/graphs

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/baburkin/graphs

Source
Go
Version
v0.3.1
Version published
Created
Source

Simple Golang library for graph processing

Introduction

This library provides simple API (data structures and set of algorithms) to use in programs that run algorithms on graphs.

Sample Usage

package main

// This is a sample usage of topological sort algorithm

import (
	"fmt"
	"os"

	"github.com/baburkin/graphs"
)

func main() {
	// Initialize a directed graph from a file
	graph, err := graphs.LoadDirectedGraph("examples/directed_7.txt")
	if err != nil {
		fmt.Printf("Got an error: %v\n", err)
		os.Exit(2)
	}
	fmt.Printf("Initial graph: %v\n", graph)

	// Get a reversed graph
	reversedGraph := graph.Reverse()
	fmt.Printf("Reversed graph: %v\n", reversedGraph)

	// Try to topologically sort the graph
	if order, err := graphs.TopoSort(graph); err != nil {
		fmt.Printf("Cannot do topological sort: %v\n", err)
	} else {
		fmt.Printf("Topologically sorted graph (vertex order): %v\n", order)
	}

	// Try to topologically sort the reversed graph
	if order, err := graphs.TopoSort(reversedGraph); err != nil {
		fmt.Printf("Cannot do topological sort for reversed graph: %v\n", err)
	} else {
		fmt.Printf("Topologically sorted reversed graph (vertex order): %v\n", order)
	}
}

FAQs

Package last updated on 26 Nov 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts