Socket
Book a DemoInstallSign in
Socket

github.com/mcustiel/go-graph

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/mcustiel/go-graph

Source
Go
Version
v0.0.0-20210221101108-8b69a0f66fde
Version published
Created
Source

go-graph

Implements a general graph ADT in Go

This project is part of my "learn go" projects

Types

  • graph.Node
  • graph.Graph

Functions

  • graph.New(*Node) *Graph - Creates a new graph using the given node as start point for traversing methods.
  • graph.NewNode(interface{}) *Node - Creates a new node contained a given value.

Methods

Node

  • (*Node) AddAdyacent(*Node) - Makes the given node adyacent to the current one
  • (*Node) RemoveAdyacent(*Node) - Removes the given node from the current one. (TODO: return the removed node)
  • (*Node) GetIterator() func() *Node - Returns an iterator function that will return the next adyacent node each time is called until nil.
  • (*Node) Value() interface{} - Returns the value stored in the current node. You should convert it to the expected type.
  • (*Node) Adyacents() *list.List - Returns the adyacents node as a Go's double linked list.

Graph

  • (*Graph) Walk(func(*Node)) - Walks all the graph nodes without specific order and executes the given function for each of them.
  • (*Graph) Bfs(func(*Node)) - Traverses the graph using BFS algorithm and executes the given function for each node.
  • (*Graph) Dfs(func(*Node)) - Traverses the graph using DFS algorithm and executes the given function for each node.

Example

Saves a directories tree into a graph:

// GetDirectoryContentents(dirName) should be implemented returning []os.FileInfo

func ScanDirToTree(dirName string) *graph.Graph {
	var function func(string) *graph.Node
	function = func(curDir string) *graph.Node {
		node := graph.NewNode(curDir)
		for _, file := range GetDirectoryContents(curDir) {
			if file.IsDir() {
				node.AddAdyacent(function(curDir + string(os.PathSeparator) + file.Name()))
			}
		}
		return node
	}
	return graph.New(function(dirName))
}

Displaying the folders in the graph using BFS:

graphInstance := ScanDirToTree("some/directory/path")
graphInstance.Bfs(func(node *graph.Node) {
	fmt.Println(node.Value().(string))
})

FAQs

Package last updated on 21 Feb 2021

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