WeightedTraversal

com.phasmidsoftware.gryphon.traverse.WeightedTraversal
abstract class WeightedTraversal[V, E, R <: Edge[V, E]] extends GraphTraversal[V, E, R]

Abstract base class for weighted graph traversals using a tuple (E, V) frontier.

Encapsulates the shared structure of Dijkstra and Prim: both use an IndexedPrioQueue[(E, V)] frontier, CostUpdate with decreaseKey, and mutable pred/bestCost maps owned exclusively by CostUpdate.

The context bound is E: {Zero, Ordering} — both algorithms need a seed cost (Zero.identity) and a cost ordering (Ordering). Only Dijkstra additionally needs Monoid.combine for path cost accumulation; Prim never accumulates costs and so extends this class directly with E: {Zero, Ordering}. DijkstraTraversal extends this with the additional Monoid bound.

Concrete subclasses differ in exactly three ways:

  • edgeCost — how the frontier cost is computed from an edge: cumulative (Monoid.combine) for Dijkstra; edge weight only for Prim.
  • destination — how the destination vertex is extracted from an edge: always e.black for directed; e.other(v) for undirected.
  • filterEdge — which edges are admitted and what concrete R type they have: only AttributedDirectedEdge for Dijkstra; all edges for Prim.

NOTE on lazy evaluation: ev._1 and ev._2 must be extracted via strict val with explicit type ascriptions inside Neighbours. Using tuple pattern matching val (accCost, v) = ev can generate a lazy binding that captures incorrectly when the resulting Iterator is consumed lazily, causing edgeCost to receive zero instead of the actual accumulated cost (a Heisenbug).

Type parameters

E

the edge-weight type; must have Zero and Ordering.

R

the result type; must be a subtype of Edge[V, E].

V

the vertex type.

Attributes

Graph
Supertypes
trait GraphTraversal[V, E, R]
class Object
trait Matchable
class Any
Known subtypes
class DijkstraTraversal[V, E]
class PrimTraversal[V, E]

Members list

Value members

Abstract methods

protected def destination(v: V, e: Edge[V, E]): V

Extracts the destination vertex from edge e, viewed from vertex v.

Extracts the destination vertex from edge e, viewed from vertex v.

Dijkstra: always e.black. Prim: e.other(v) for undirected edges, e.black for directed.

Attributes

protected def edgeCost(accCost: E, e: Edge[V, E], v: V): E

Computes the frontier cost for a neighbour reachable via e from a node settled at accCost.

Computes the frontier cost for a neighbour reachable via e from a node settled at accCost.

Dijkstra: Monoid[E].combine(accCost, e.attribute) — cumulative path cost. Prim: e.attribute — edge weight only.

Attributes

protected def filterEdge(e: Edge[V, E]): Option[R]

Admits an edge and casts it to the concrete result type R, or rejects it.

Admits an edge and casts it to the concrete result type R, or rejects it.

Dijkstra: admits only AttributedDirectedEdge instances. Prim: admits all edges.

Attributes

Concrete methods

def run(graph: Traversable[V])(start: V)(using random: Random = ...): TraversalResult[V, R]

Runs the traversal from start on graph, returning a TraversalResult.

Runs the traversal from start on graph, returning a TraversalResult.

Value parameters

graph

the graph to traverse.

random

controls adjacency ordering.

start

the starting vertex.

Attributes

Returns

a TraversalResult[V, R] mapping each visited vertex to its result.