Documentation
¶
Overview ¶
Package graphv provides search algorithm on graphs, where only the vertices are concerned.
For better performance, all functions in this package are unsafe for concurrency unless otherwise specified.
Index ¶
- func BFS[Vertex any](itf AccessVertex[Vertex], initArgs ...any) (vertexFound Vertex, found bool)
- func BFSPath[Vertex any](itf AccessPath[Vertex], initArgs ...any) []Vertex
- func DFS[Vertex any](itf AccessVertex[Vertex], initArgs ...any) (vertexFound Vertex, found bool)
- func DFSPath[Vertex any](itf AccessPath[Vertex], initArgs ...any) []Vertex
- func DLS[Vertex any](itf AccessVertex[Vertex], limit int, initArgs ...any) (vertexFound Vertex, found, more bool)
- func DLSPath[Vertex any](itf AccessPath[Vertex], limit int, initArgs ...any) (pathFound []Vertex, more bool)
- func IDS[Vertex any](itf IDSAccessVertex[Vertex], initLimit int, initArgs ...any) (vertexFound Vertex, found bool)
- func IDSPath[Vertex any](itf IDSAccessPath[Vertex], initLimit int, initArgs ...any) []Vertex
- type AccessPath
- type AccessVertex
- type Common
- type IDSAccessPath
- type IDSAccessVertex
- type IDSSpecific
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BFS ¶ added in v0.5.0
func BFS[Vertex any](itf AccessVertex[Vertex], initArgs ...any) ( vertexFound Vertex, found bool)
BFS finds a vertex in itf using breadth-first search algorithm and returns that vertex.
It also returns an indicator found to report whether the vertex has been found.
initArgs are the arguments to initialize itf.
func BFSPath ¶ added in v0.5.0
func BFSPath[Vertex any](itf AccessPath[Vertex], initArgs ...any) []Vertex
BFSPath is similar to function BFS, except that it returns the path from the root of itf to the vertex found instead of only the vertex.
It returns nil if the vertex is not found.
func DFS ¶ added in v0.5.0
func DFS[Vertex any](itf AccessVertex[Vertex], initArgs ...any) ( vertexFound Vertex, found bool)
DFS finds a vertex in itf using depth-first search algorithm and returns that vertex.
It also returns an indicator found to report whether the vertex has been found.
initArgs are the arguments to initialize itf.
func DFSPath ¶ added in v0.5.0
func DFSPath[Vertex any](itf AccessPath[Vertex], initArgs ...any) []Vertex
DFSPath is similar to function DFS, except that it returns the path from the root of itf to the vertex found instead of only the vertex.
It returns nil if the vertex is not found.
func DLS ¶ added in v0.5.0
func DLS[Vertex any](itf AccessVertex[Vertex], limit int, initArgs ...any) ( vertexFound Vertex, found, more bool)
DLS finds a vertex in itf using depth-limited depth-first search algorithm.
limit is the maximum depth during this search. The depth of the root is 0, of adjacent vertices of the root is 1, and so on.
initArgs are the arguments to initialize itf.
It returns the vertex found and two indicators:
found - to report whether the vertex has been found; more - to report whether there is any undiscovered vertex because of the depth limit.
The indicator more makes sense only when the vertex is not found. When more is false, all the vertices must have been discovered. However, when more is true, it does not guarantee that there must be an undiscovered vertex, because the vertex may be discovered in another search path.
func DLSPath ¶ added in v0.5.0
func DLSPath[Vertex any](itf AccessPath[Vertex], limit int, initArgs ...any) ( pathFound []Vertex, more bool)
DLSPath is similar to function DLS, except that it returns the path from the root of itf to the vertex found instead of only the vertex.
It returns nil for the path if the vertex is not found.
func IDS ¶ added in v0.5.0
func IDS[Vertex any]( itf IDSAccessVertex[Vertex], initLimit int, initArgs ...any, ) (vertexFound Vertex, found bool)
IDS finds a vertex in itf using iterative deepening depth-first search algorithm and returns that vertex.
It also returns an indicator found to report whether the vertex has been found.
initLimit is the depth limit used in the first iteration. The depth of the root is 0, of adjacent vertices of the root is 1, and so on. If initLimit < 1, the depth limit in the first iteration is 1.
initArgs are the arguments to initialize itf.
func IDSPath ¶ added in v0.5.0
func IDSPath[Vertex any]( itf IDSAccessPath[Vertex], initLimit int, initArgs ...any, ) []Vertex
IDSPath is similar to function IDS, except that it returns the path from the root of itf to the vertex found instead of only the vertex.
It returns nil if the vertex is not found.
Types ¶
type AccessPath ¶ added in v0.5.0
type AccessPath[Vertex any] interface { Common[Vertex] // AccessPath examines the path from the search root to the current vertex. // // Its parameter is the path from the search root to // the current vertex to examine. // path must be nonempty. // // It returns two indicators: // found - to report whether the specified vertex is the search goal; // cont - to report whether to continue searching. // // The search algorithm should exit immediately if cont is false. // In this case, the search result may be invalid. // // Sometimes it is also referred to as "visit". AccessPath(path []Vertex) (found, cont bool) }
AccessPath represents a graph used in the graph search algorithm, where only the vertices are concerned.
Its method AccessPath examines the path from the search root to the current vertex.
type AccessVertex ¶ added in v0.5.0
type AccessVertex[Vertex any] interface { Common[Vertex] // AccessVertex examines the specified vertex. // // It has two parameters: // vertex - the vertex to examine; // depth - the search depth from the root to the node. // // It returns two indicators: // found - to report whether the specified vertex is the search goal; // cont - to report whether to continue searching. // // The search algorithm should exit immediately if cont is false. // In this case, the search result may be invalid. // // Sometimes it is also referred to as "visit". AccessVertex(vertex Vertex, depth int) (found, cont bool) }
AccessVertex represents a graph used in the graph search algorithm, where only the vertices are concerned.
Its method AccessVertex examines the current vertex.
type Common ¶ added in v0.5.0
type Common[Vertex any] interface { // Init initializes all states for a new search // with specified arguments args (e.g., set the search goal). // // It will be called once at the beginning of the search functions. Init(args ...any) // Root returns the vertex of the graph where the search algorithm start. // // It returns only one vertex because the search starts at // one specified vertex in most cases. // If there are multiple starting vertices, set the root to a dummy vertex // whose adjacent vertices are the starting vertices. Root() Vertex // Adjacency returns the adjacent vertices of the specified vertex // as an adjacency list. // // The first item in the list is accessed first. Adjacency(vertex Vertex) []Vertex // Discovered reports whether the specified vertex // has been examined by the method AccessVertex or AccessPath. // // Sometimes it is also referred to as "visited". // // It is typically implemented by using a hash table or // associating an attribute "discovered" to each vertex. // // If it is not necessary to record the vertices discovered, // or the graph is too big to record the vertices discovered, // just always return false. // In this case, the search algorithms may access one vertex // multiple times, and may even fall into an infinite loop. Discovered(vertex Vertex) bool }
Common is the interface common to AccessVertex and AccessPath. It represents a graph used in the graph search algorithm, where only the vertices are concerned.
It contains the basic methods required by every graph search algorithm.
type IDSAccessPath ¶ added in v0.5.0
type IDSAccessPath[Vertex any] interface { AccessPath[Vertex] IDSSpecific[Vertex] }
IDSAccessPath extends interface AccessPath for function IDSPath.
It appends a new method ResetSearchState to reset the search state for each iteration.
type IDSAccessVertex ¶ added in v0.5.0
type IDSAccessVertex[Vertex any] interface { AccessVertex[Vertex] IDSSpecific[Vertex] }
IDSAccessVertex extends interface AccessVertex for function IDS.
It appends a new method ResetSearchState to reset the search state for each iteration.
type IDSSpecific ¶ added in v0.5.0
type IDSSpecific[Vertex any] interface { // ResetSearchState resets the search state for the next iteration. // // It will be called before each iteration in functions IDS and IDSPath, // except for the first iteration. // // Its implementation must reset all the vertices to undiscovered, // and can reset any other states associated with each iteration // in this method. ResetSearchState() }
IDSSpecific contains a method ResetSearchState for iterative deepening depth-first search (IDS) algorithms.