A Graph Library for Resolving Dependent Nodes
This package provides functionality for traversing and analyzing a dependency graph. The DependencyGraph struct and
its methods allow for listing dependencies in various orders, including direct, recursive, top-down, and reverse
dependencies.
To use this package, you need to import it into your Go project. Make sure you have the following dependencies in your
go.mod file:
import (
graph "github.com/kdeps/kartographer/graph"
)First, create a new DependencyGraph instance by calling the NewDependencyGraph function with the appropriate parameters.
import (
graph "github.com/kdeps/kartographer/graph"
)
dependencies := map[string][]string{
"A": {"B", "C"},
"B": {"D"},
"C": {"D"},
"D": {},
}
dg := graph.NewDependencyGraph(fs, logger, dependencies)Then you can call any of the methods on the DependencyGraph instance to list dependencies in various orders.
dg.ListDirectDependencies("A")
dg.ListDependencyTree("A")
dg.ListDependencyTreeTopDown("A")
dg.ListReverseDependencies("D")Traverses the dependency graph starting from a given node and prints the paths.
func (dg *DependencyGraph) TraverseDependencyGraph(node string, dependencies map[string][]string, visited map[string]bool)Lists the dependency tree in a top-down order starting from a given node.
func (dg *DependencyGraph) ListDependencyTreeTopDown(node string)Lists all dependencies recursively from a given node and prints each dependency path.
func (dg *DependencyGraph) ListDependenciesRecursive(node string, path []string, visited map[string]bool)Lists all reverse dependencies for a given node.
func (dg *DependencyGraph) ListReverseDependencies(node string)Lists the entire dependency tree for a given node.
func (dg *DependencyGraph) ListDependencyTree(node string)Builds and returns a stack of dependencies starting from a given node.
func (dg *DependencyGraph) BuildDependencyStack(node string, visited map[string]bool) []stringInverts the dependency graph, swapping dependencies with their dependents.
func (dg *DependencyGraph) InvertDependencies() map[string][]stringLists direct dependencies for a given node.
func (dg *DependencyGraph) ListDirectDependencies(node string)The package also includes utility functions for constructing or printing dependency paths.
Construct a given dependency path which returns a string
func (dg *DependencyGraph) ConstructDependencyPath(path []string, dir string) stringPrints a given dependency path.
func (dg *DependencyGraph) PrintDependencyPath(path []string, dir string)This function can be used to format and print paths in a human-readable form.
With this package, you can easily manage and traverse dependency graphs in Go, allowing for complex dependency analysis and visualization.