shortest_path

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 4, 2022 License: MIT Imports: 7 Imported by: 0

README

shortest_path

Highly optimized implementation of Dijkstra's algorithm as well as several speed-up techniques and the respective preprocessing: Bidirectional Dijkstra's algorith, arc flags, A* search, ALT and some more.

Documentation

Index

Constants

View Source
const DEFAULT_BENCHMARK_SEED = 314159265359
View Source
const MAX_GOROUTINES = 8

Variables

This section is empty.

Functions

func ComputeArcFlags

func ComputeArcFlags[N g.Partitioner, E g.IFlaggedHalfEdge[W], W g.Weight](forwardGraph, transposedGraph g.Graph[N, E], partitionCount int) *g.AdjacencyArrayGraph[N, E]

Parallel implementation of arc flag preprocessing

func ComputeTwoLevelArcFlags

func ComputeTwoLevelArcFlags[N g.TwoLevelPartitioner, E g.ITwoLevelFlaggedHalfEdge[W], W g.Weight](forwardGraph, transposedGraph g.Graph[N, E]) *g.AdjacencyArrayGraph[N, E]

Parallel implementation of two-level arcflag preprocessing

func UniformLandmarks

func UniformLandmarks[N any, E g.IHalfEdge](graph g.Graph[N, E], n int) []g.NodeId

UniformLandmarks chooses n nodes uniformly and at random from the graph.

Types

type AStarPqItem

type AStarPqItem[W g.Weight] struct {
	// ID of the node this item refers to
	Id g.NodeId
	// minimum distance from source node to this node (g-value)
	Distance W
	// priority of this item in the heap (f-value)
	Priority W
	// predecessor node of this item in the search tree
	Predecessor g.NodeId
	// contains filtered or unexported fields
}

Atomic element of the priority queue used in A* Search

type AStarPriorityQueue

type AStarPriorityQueue[W g.Weight] []*AStarPqItem[W]

A priority queue implementation for A* Search and other algorithms incorporating a heuristic. The priority is sorted according to the f-value: f = g + h, where f is the priority, g the minimum distance and h a heurisitc. An admissible heuristic estimates the distance from a node to the target without overestimating the true distance. Implements heap.Interface (https://pkg.go.dev/container/heap)

func (AStarPriorityQueue[W]) Len

func (pq AStarPriorityQueue[W]) Len() int

Len implements heap.Interface

func (AStarPriorityQueue[W]) Less

func (pq AStarPriorityQueue[W]) Less(i, j int) bool

Less implements heap.Interface

func (*AStarPriorityQueue[W]) Pop

func (pq *AStarPriorityQueue[W]) Pop() interface{}

Pop implements heap.Interface

func (*AStarPriorityQueue[W]) Push

func (pq *AStarPriorityQueue[W]) Push(item interface{})

Push implements heap.Interface

func (AStarPriorityQueue[W]) Swap

func (pq AStarPriorityQueue[W]) Swap(i, j int)

Swap implements heap.Interface

type AStarRouter added in v0.2.0

type AStarRouter[N any, E g.IWeightedHalfEdge[W], W g.Weight] struct {
	Graph     g.Graph[N, E]
	Heuristic Heuristic[W]
}

AStarRouter implements the Router interface and provides A* search, a lower-bounding variant of Dijkstra's algorithm.

func (AStarRouter[N, E, W]) Route added in v0.2.0

func (r AStarRouter[N, E, W]) Route(source, target g.NodeId, recordSearchSpace bool) ShortestPathResult[W]

A* with feasible heruistic is a lower-bounding algorithm

func (AStarRouter[N, E, W]) String added in v0.2.0

func (r AStarRouter[N, E, W]) String() string

String implements fmt.Stringer

type AltHeuristic

type AltHeuristic[W g.Weight] struct {
	LandmarkDistancesCollection map[g.NodeId]LandmarkDistances[W]

	// dynamic attributes
	Source g.NodeId // source node of the current search: updated via Init
	Target g.NodeId // target node of the current search: updated via Init

	ActiveLandmarks []LandmarkDistances[W]
}

Heuristic for ALT algorithm (A*, Landmarks and Triangular Inequalities)

func NewAltHeurisitc

func NewAltHeurisitc[N any, E g.IWeightedHalfEdge[W], W g.Weight](graph, transpose g.Graph[N, E], landmarks []g.NodeId) *AltHeuristic[W]

func (AltHeuristic[W]) Evaluate

func (ah AltHeuristic[W]) Evaluate(id g.NodeId) W

Evaluate implements Heuristic.Evaluate

func (*AltHeuristic[W]) Init

func (ah *AltHeuristic[W]) Init(source g.NodeId, target g.NodeId)

Init implements Heuristic.Init

type ArcFlagAStarRouter added in v0.4.0

type ArcFlagAStarRouter[N g.Partitioner, E g.IFlaggedHalfEdge[W], W g.Weight] struct {
	Graph     g.Graph[N, E]
	Transpose g.Graph[N, E]

	Heuristic Heuristic[W]
}

ArcFlagAStarRouter implements combines A* search with bidirectional arc flags.

func (ArcFlagAStarRouter[N, E, W]) Route added in v0.4.0

func (r ArcFlagAStarRouter[N, E, W]) Route(source, target g.NodeId, recordSearchSpace bool) ShortestPathResult[W]

A* with feasible heruistic is a lower-bounding algorithm

func (ArcFlagAStarRouter[N, E, W]) String added in v0.4.0

func (r ArcFlagAStarRouter[N, E, W]) String() string

String implements fmt.Stringer

type ArcFlagRouter added in v0.2.0

type ArcFlagRouter[N g.Partitioner, E g.IFlaggedHalfEdge[W], W g.Weight] struct {
	Graph g.Graph[N, E]
}

ArcFlagRouter implements the Router interface and improves Dijkstra's algorithm by incorporating arc flags.

func (ArcFlagRouter[N, E, W]) Route added in v0.2.0

func (r ArcFlagRouter[N, E, W]) Route(source, target g.NodeId, recordSearchSpace bool) ShortestPathResult[W]

Implementation of Dijkstra's Algorithm with arc flags

func (ArcFlagRouter[N, E, W]) String added in v0.2.0

func (r ArcFlagRouter[N, E, W]) String() string

String implements fmt.Stringer

type BenchmarkResult added in v0.3.0

type BenchmarkResult struct {
	TimeDistribution   []float64 `json:"times"`
	PqPopsDistribution []int     `json:"pq-pops"`
}

BenchmarkResult reports details of a benchmark, i.e. the distribution of runtimes and pq-Pops.

func NewBenchmarkResult added in v0.3.0

func NewBenchmarkResult() *BenchmarkResult

func (*BenchmarkResult) Add added in v0.3.0

func (br *BenchmarkResult) Add(time float64, pqPops int)

Add adds a new observation to the benchmark.

func (BenchmarkResult) Summarize added in v0.3.0

func (br BenchmarkResult) Summarize() BenchmarkSummary

Summarize builds a summary of the benchmark.

type BenchmarkSummary added in v0.3.1

type BenchmarkSummary struct {
	Runs   int     // number of executions
	Time   float64 // average execution time [ms]
	PqPops int     // average number of Pop() operations on priority queue
}

BenchmarkSummary summarizes the result of a benchmark.

func (BenchmarkSummary) String added in v0.3.1

func (s BenchmarkSummary) String() string

type Benchmarker added in v0.3.0

type Benchmarker[W g.Weight] struct {
	NodeRange g.NodeId
	Router    Router[W]
	Result    BenchmarkResult
}

func NewBenchmarker added in v0.3.0

func NewBenchmarker[W g.Weight](router Router[W], nodeCount int) *Benchmarker[W]

func (*Benchmarker[W]) Run added in v0.3.0

func (b *Benchmarker[W]) Run(n int) BenchmarkSummary

type BiDijkstraRouter added in v0.2.0

type BiDijkstraRouter[N any, E g.IWeightedHalfEdge[W], W g.Weight] struct {
	Graph     g.Graph[N, E]
	Transpose g.Graph[N, E]

	MaxInitializerValue W
}

BiDijkstraRouter implements the Router interface and features bidirectional search with Dijkstra's algorithm.

Caveat: Always set the MaxInitializerValue to the maximum value of the generic type W, e.g. math.MaxInt in case of int.

func (BiDijkstraRouter[N, E, W]) Route added in v0.2.0

func (r BiDijkstraRouter[N, E, W]) Route(source, target g.NodeId, recordSearchSpace bool) ShortestPathResult[W]

Bidirectional Dijkstra runs a forward search from the source node to the target node in parallel with a backward search in the backward graph (transpose) from the target node to the source node.

Bidirectional search requires both the forward graph and its transpose (backward graph) as input parameters. In case of undirected graphs, the same argument may be passed for both parameters.

Reference: https://www.homepages.ucl.ac.uk/~ucahmto/math/2020/05/30/bidirectional-dijkstra.html

func (BiDijkstraRouter[N, E, W]) String added in v0.2.0

func (r BiDijkstraRouter[N, E, W]) String() string

String implements fmt.Stringer

type BidirectionalAStarRouter added in v0.2.0

type BidirectionalAStarRouter[N any, E g.IWeightedHalfEdge[W], W g.Weight] struct {
	Graph     g.Graph[N, E]
	Transpose g.Graph[N, E]

	ForwardHeuristic  Heuristic[W]
	BackwardHeuristic Heuristic[W]

	MaxInitializerValue W
}

BidirectionalAStarRouter implements the Router interface and provides bidirectional A* search, a lower-bounding variant of Dijkstra's algorithm.

func (BidirectionalAStarRouter[N, E, W]) Route added in v0.2.0

func (r BidirectionalAStarRouter[N, E, W]) Route(source, target g.NodeId, recordSearchSpace bool) ShortestPathResult[W]

Bidirectional implementation of the lower-bounding A* algorithm following the symmetric approach by I. Pohl: "Bi-directional Search", 1971 cf. Goldberg et al.: "Computing the Shortest Path: A* Search meets Graph Theory", 2004

func (BidirectionalAStarRouter[N, E, W]) String added in v0.2.0

func (r BidirectionalAStarRouter[N, E, W]) String() string

String implements fmt.Stringer

type BidirectionalArcFlagRouter added in v0.2.0

type BidirectionalArcFlagRouter[N g.Partitioner, E g.IFlaggedHalfEdge[W], W g.Weight] struct {
	Graph     g.Graph[N, E]
	Transpose g.Graph[N, E]

	MaxInitializerValue W
}

BidirectionalArcFlagRouter implements the Router interface and improves bidirectional Dijkstra's algorithm by incorporating arc flags.

func (BidirectionalArcFlagRouter[N, E, W]) Route added in v0.2.0

func (r BidirectionalArcFlagRouter[N, E, W]) Route(source, target g.NodeId, recordSearchSpace bool) ShortestPathResult[W]

Bidirectional Dijkstra runs a forward search from the source node to the target node in parallel with a backward search in the backward graph (transpose) from the target node to the source node.

Bidirectional search requires both the forward graph and its transpose (backward graph) as input parameters. In case of undirected graphs, the same argument may be passed for both parameters.

Reference: https://www.homepages.ucl.ac.uk/~ucahmto/math/2020/05/30/bidirectional-dijkstra.html

func (BidirectionalArcFlagRouter[N, E, W]) String added in v0.2.0

func (r BidirectionalArcFlagRouter[N, E, W]) String() string

String implements fmt.Stringer

type DijkstraPqItem

type DijkstraPqItem[W g.Weight] struct {
	// ID of the node this item refers to
	Id g.NodeId
	// priority of this item in the heap
	Priority W
	// predecessor node of this item in the search tree
	Predecessor g.NodeId
	// contains filtered or unexported fields
}

Atomic element of the priority queue used in Dijkstra's algorithm

type DijkstraPriorityQueue

type DijkstraPriorityQueue[W g.Weight] []*DijkstraPqItem[W]

A priority queue implementation for Dijkstra's algorithm and other shortest path algorithms. Implements heap.Interface (https://pkg.go.dev/container/heap)

func (DijkstraPriorityQueue[W]) Len

func (pq DijkstraPriorityQueue[W]) Len() int

Len implements heap.Interface

func (DijkstraPriorityQueue[W]) Less

func (pq DijkstraPriorityQueue[W]) Less(i, j int) bool

Less implements heap.Interface

func (*DijkstraPriorityQueue[W]) Pop

func (pq *DijkstraPriorityQueue[W]) Pop() interface{}

Pop implements heap.Interface

func (*DijkstraPriorityQueue[W]) Push

func (pq *DijkstraPriorityQueue[W]) Push(item interface{})

Push implements heap.Interface

func (DijkstraPriorityQueue[W]) Swap

func (pq DijkstraPriorityQueue[W]) Swap(i, j int)

Swap implements heap.Interface

type DijkstraRouter added in v0.2.0

type DijkstraRouter[N any, E g.IWeightedHalfEdge[W], W g.Weight] struct {
	Graph g.Graph[N, E]
}

func (DijkstraRouter[N, E, W]) Route added in v0.2.0

func (r DijkstraRouter[N, E, W]) Route(source, target g.NodeId, recordSearchSpace bool) ShortestPathResult[W]

Efficient implementation of Dijkstra's Algorithm for finding the shortest path between a source and a target node in the graph. Implementation is based on a priority queue.

func (DijkstraRouter[N, E, W]) String added in v0.2.0

func (r DijkstraRouter[N, E, W]) String() string

String implements fmt.Stringer

type Heuristic

type Heuristic[W g.Weight] interface {
	// Init initializes the heuristic with the source and the target node id of the next search.
	// The Init method must be called before any search by the search algorithm.
	Init(source g.NodeId, target g.NodeId)
	// Evaluate computes the value of the heuristic at node with ID=id.
	Evaluate(id g.NodeId) W
}

Heuristic interface for A* Search

type LandmarkDistances

type LandmarkDistances[W g.Weight] struct {
	Landmark g.NodeId // node ID of the landmark
	From     []W      // stores the length of shortest paths from L to every node
	To       []W      // stores the length of shortest paths from every node to L
}

Stores the lenghts of shortest path from/to a landmark L to/from every node

type Router added in v0.2.0

type Router[W g.Weight] interface {
	// Route computes the shortest path from the source node to the target node of the underlying graph.
	//
	// The search space of the algorithm's execution is reported iff recordSearchSpace is true.
	// Note that recording the search space will decrease the performance of Route significantly.
	Route(source, target g.NodeId, recordSearchSpace bool) ShortestPathResult[W]
}

Router is the interface that wraps the basic Route method of a shortest path algorithm.

type ShortestPathResult

type ShortestPathResult[W g.Weight] struct {
	// Length stores the length of the shortest path between the source and the target node.
	// The value of Length is set to -1 iff such a path does not exist.
	Length W
	// Path is a slice of NodeId values that describe the shortest path from the source to the target node.
	// The slice is empty iff such a path does not exist.
	Path []g.NodeId
	// PqPops reports the number of Pop() operations on the priority queue during the shortest path computation.
	PqPops int
	// SearchSpace reports the search space of the algorithm by enumerating all processed node IDs.
	// The slice is ordered by the time the nodes were settled.
	// Shortest path algorithms should usually not record the search space for performance reasons.
	// SearchSpace is 'nil' iff the algorithm has been instructed not to record the search space.
	SearchSpace []g.NodeId
}

Encapsulates the output of a shortest path algorithm.

type ShortestPathToAllResult

type ShortestPathToAllResult[W g.Weight] struct {
	// Lengths stores the length of the shortest path from the source node to each node and -1 if such a path does not exist.
	Lengths []W
	// Predecessers store the predecessor node of each node on a shortest path starting at the source node and -1 if such a path does not exist.
	Predecessors []g.NodeId
	// PqPops reports the number of Pop() operations on the priority queue during the one-to-all shortest path computation.
	PqPops int
}

Encapsulates the output of a one-to-all search of a shortest path algorithm.

func DijkstraOneToAll

func DijkstraOneToAll[N any, E g.IWeightedHalfEdge[W], W g.Weight](graph g.Graph[N, E], source g.NodeId) ShortestPathToAllResult[W]

Efficient implementation of Dijkstra's Algorithm for finding the shortest path from a source to every other node in the graph. Implementation is based on a priority queue.

type ShortestPathTreeNode

type ShortestPathTreeNode struct {
	// Id is the root element of this search tree and refers to a Node ID in the graph on which Dijkstra's algorithm has been applied.
	Id g.NodeId
	// Children is a list of pointers to subtrees, each referring to a node that is reached on a shortest path from the source node via this node (ID=Id).
	Children []*ShortestPathTreeNode
	// Visited is a boolean flag to mark nodes that have already been visited while traversing the search graph.
	// Caution: It is not possible to make use of this flag for more than one traversal (without resetting it).
	Visited bool
}

ShortestPathTreeNode encapsulates the directed, acyclic search graph (tree) spanned by Dijkstra's algorithm.

func ShortestPathTree

func ShortestPathTree[N any, E g.IWeightedHalfEdge[W], W g.Weight](graph g.Graph[N, E], source g.NodeId) ShortestPathTreeNode

Dijkstra's algorithm spans a directed, acyclic search graph (tree) of all nodes being reachable from the source node (root of this search). This is due to the fact that multiple shortest paths to the same node might exist.

Note: The term 'search tree' refers to the output of the search and is used to avoid confusions with the input graph, i.e. the graph on which the search is conducted.

type ShortestPathTreePqItem

type ShortestPathTreePqItem[W g.Weight] struct {
	// ID of the node this item refers to
	Id g.NodeId
	// priority of this item in the heap
	Priority W
	// list of predecessor nodes of this item in the search tree
	Predecessors []g.NodeId
	// contains filtered or unexported fields
}

Atomic element of the priority queue used in the shortest path tree algorithm. Unlike DijkstraPqItem, this PqItem allows to store multiple predecessors of a node in case multiple shortest paths exist.

type ShortestPathTreePriorityQueue

type ShortestPathTreePriorityQueue[W g.Weight] []*ShortestPathTreePqItem[W]

A priority queue implementation for the shortest path tree algorithm. Implements heap.Interface (https://pkg.go.dev/container/heap)

func (ShortestPathTreePriorityQueue[W]) Len

func (pq ShortestPathTreePriorityQueue[W]) Len() int

Len implements heap.Interface

func (ShortestPathTreePriorityQueue[W]) Less

func (pq ShortestPathTreePriorityQueue[W]) Less(i, j int) bool

Less implements heap.Interface

func (*ShortestPathTreePriorityQueue[W]) Pop

func (pq *ShortestPathTreePriorityQueue[W]) Pop() interface{}

Pop implements heap.Interface

func (*ShortestPathTreePriorityQueue[W]) Push

func (pq *ShortestPathTreePriorityQueue[W]) Push(item interface{})

Push implements heap.Interface

func (ShortestPathTreePriorityQueue[W]) Swap

func (pq ShortestPathTreePriorityQueue[W]) Swap(i, j int)

Swap implements heap.Interface

type TwoLevelArcFlagRouter added in v0.2.0

type TwoLevelArcFlagRouter[N g.TwoLevelPartitioner, E g.ITwoLevelFlaggedHalfEdge[W], W g.Weight] struct {
	Graph g.Graph[N, E]
}

TwoLevelArcFlagRouter implements the Router interface and improves Dijkstra's algorithm by incorporating two-level arc flags.

func (TwoLevelArcFlagRouter[N, E, W]) Route added in v0.2.0

func (r TwoLevelArcFlagRouter[N, E, W]) Route(source, target g.NodeId, recordSearchSpace bool) ShortestPathResult[W]

Implementation of Dijkstra's Algorithm with two-level arc flags

func (TwoLevelArcFlagRouter[N, E, W]) String added in v0.2.0

func (r TwoLevelArcFlagRouter[N, E, W]) String() string

String implements fmt.Stringer

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL