Abstract

Weighted search is an essential component of many fundamental and useful algorithms. Despite this, it is relatively under explored as a computational effect, receiving not nearly as much attention as either depth- or breadth-first search. This paper explores the algebraic underpinning of weighted search, and demonstrates how to implement it as a monad transformer. The development first explores breadth-first search, which can be expressed as a polynomial over semirings. These polynomials are generalised to the free semimodule monad to capture a wide range of applications, including probability monads, polynomial monads, and monads for weighted search. Finally, a monad transformer based on the free semimodule monad is introduced. Applying optimisations to this type yields an implementation of pairing heaps, which is then used to implement Dijkstra's algorithm and efficient probabilistic sampling. The construction is formalised in Cubical Agda and implemented in Haskell.

Highlights

  • Functional programmers often describe a problem with multiple solutions by a nondeterministic program where sequential composition counts how many steps it takes to produce a solution, and parallel composition offers the choice between solutions

  • This paper has shown that there are similar connections to be found between the free semiring and the Levels type for breadth-first search, and between free semimodules and the Weighted type for weighted search

  • Working at a high level of abstraction has made it possible to use the Cayley transformation to enable the implementation of optimised datastructures for these searches, where the Queue type is as an efficienct version of Levels, and the Heap type is an efficient version of Weighted

Read more

Summary

INTRODUCTION

Functional programmers often describe a problem with multiple solutions by a nondeterministic program where sequential composition counts how many steps it takes to produce a solution, and parallel composition offers the choice between solutions. In this paper we demonstrate that that the structure of weight-first strategies is the free semimodule, this corresponds to a free semiring with an additional scalar multiplication, where this scalar allows weights to be accounted for To understand this construction, we use breadth-first search and its representation as a free semiring as a starting point. This construction can be understood by the language of polynomials (over a semiring), and by generalizing the exponents in this semiring from natural numbers to arbitrary monoids, the structure for weighted search emerges Working at this level of abstraction means that the results can be applied in different contexts: we show how reservoir sampling, shortest subset sum, and Dijkstra’s algorithm are all instances of this theory.

BREADTH-FIRST SEARCH IS POLYNOMIAL
Levels
Polynomials in N
Polynomials in Alternative
Polynomials in Applicative
Cayley Transforms
THE LEVELS TRANSFORMER
The Levels Transformer
An Algebra of Costs
Generalising from Monus to Semiring
Defining a Monad for Least-Cost Search
Paramorphism-Based Recursion Schemes for Quotiented Data Types
The Monad
The Free Semimodule
OPTIMISING WEIGHTED SEARCH
Improving Efficiency
Implementing a Transformer
Algorithms
Performance
RELATED WORK
CONCLUSION
Full Text
Published version (Free)

Talk to us

Join us for a 30 min session where you can share your feedback and ask us any queries you have

Schedule a call