ConnectivityOptimized

com.phasmidsoftware.gryphon.adjunct.ConnectivityOptimized
See theConnectivityOptimized companion object

Concrete disjoint-set implementation using "Weighted Quick Union with Path Compression."

Extends ConnectivityLazy with path compression: during every findAndCompress traversal to the root, all nodes along the path are rewired to point directly to the root. This flattens the tree lazily, giving amortized near-O(1) performance (formally O(α(n)), where α is the inverse Ackermann function).

Path compression is implemented purely functionally: the rewired nodes are accumulated into a new immutable Map which is returned alongside the root as a fresh ConnectivityOptimized instance. No mutation occurs.

Note that after path compression, rank (tracked via ParentSize.size) becomes an upper bound on depth rather than the true depth — this is the standard behaviour of union-by-rank with path compression, and is why the field is called size (a weaker proxy) rather than depth.

unit and put return ConnectivityOptimized[V] directly, with no overrides required, courtesy of the F-bounded type parameter.

Type parameters

V

the underlying object type.

Value parameters

map

the component map.

Attributes

Companion
object
Graph
Supertypes
trait Serializable
trait Product
trait Equals
trait Connectivity[V]
trait Connected[V]
class Object
trait Matchable
class Any
Show all

Members list

Value members

Concrete methods

def findAndCompress(key: V): (V, ConnectivityOptimized[V])

Finds the root of key's component and simultaneously compresses the path, rewiring every node visited to point directly to the root.

Finds the root of key's component and simultaneously compresses the path, rewiring every node visited to point directly to the root.

The traversal collects the path from key to the root into a List[V], then folds over it to produce an updated Map in which every node on the path (except the root itself) has its parent set directly to the root. A fresh ConnectivityOptimized wrapping the updated map is returned alongside the root — no mutation occurs at any point.

Value parameters

key

the element whose root is sought.

Attributes

Returns

a pair (root, compressed) where root is the representative of key's component and compressed is the path-compressed instance.

def put(key: V): ConnectivityOptimized[V]

Adds the specified key to the disjoint set data structure and initializes it as a new singleton set if it does not already exist.

Adds the specified key to the disjoint set data structure and initializes it as a new singleton set if it does not already exist.

Value parameters

key

the element to be added to the disjoint set.

Attributes

Returns

the updated disjoint set containing the newly added key.

def unit(map: Map[V, ParentSize[V]]): ConnectivityOptimized[V]

Constructs a new instance of the concrete subtype from the given map. Subclasses must implement this to return This.

Constructs a new instance of the concrete subtype from the given map. Subclasses must implement this to return This.

Attributes

Inherited methods

override def connect(v1: V, v2: V): ConnectivityOptimized[V]

Merges the components containing v1 and v2. Short-circuits (returning this) if v1 == v2. Delegates to findAndCompress for both arguments so that subclasses which override it get compression applied during every merge automatically. The asInstanceOf here is safe: this is always a This.

Merges the components containing v1 and v2. Short-circuits (returning this) if v1 == v2. Delegates to findAndCompress for both arguments so that subclasses which override it get compression applied during every merge automatically. The asInstanceOf here is safe: this is always a This.

Attributes

Definition Classes
Inherited from:
AbstractDisjointSetWithCompression
def depth(v: V): Int

Returns the depth of v in the component tree (root has depth 1).

Returns the depth of v in the component tree (root has depth 1).

Attributes

Inherited from:
AbstractDisjointSet
def get(key: V): Option[ParentSize[V]]

Returns the W value associated with key, if present.

Returns the W value associated with key, if present.

Attributes

Inherited from:
AbstractDisjointSet
def getDisjointSet(key: V): V

Returns the root of the component containing key.

Returns the root of the component containing key.

Attributes

Inherited from:
AbstractDisjointSet
def isConnected(v1: V, v2: V): Boolean

True if v1 and v2 belong to the same disjoint set.

True if v1 and v2 belong to the same disjoint set.

Attributes

Inherited from:
AbstractDisjointSet
def isRoot(v: V): Boolean

True if v is the root of its own component (i.e. has no parent).

True if v is the root of its own component (i.e. has no parent).

Attributes

Inherited from:
AbstractDisjointSet
def iterator: Iterator[(V, ParentSize[V])]

Returns an iterator over all (V, W) entries.

Returns an iterator over all (V, W) entries.

Attributes

Inherited from:
AbstractDisjointSet
def maxDepth: Double

The maximum path length to root across all objects.

The maximum path length to root across all objects.

Attributes

Inherited from:
AbstractDisjointSet
def meanDepth: Double

The mean path length to root across all objects.

The mean path length to root across all objects.

Attributes

Inherited from:
AbstractDisjointSet
def parent(v: V): Option[V]

Returns the optional parent of v.

Returns the optional parent of v.

Attributes

Inherited from:
AbstractDisjointSet
def productElementNames: Iterator[String]

An iterator over the names of all the elements of this product.

An iterator over the names of all the elements of this product.

Attributes

Inherited from:
Product
def productIterator: Iterator[Any]

An iterator over all the elements of this product.

An iterator over all the elements of this product.

Attributes

Returns

in the default implementation, an Iterator[Any]

Inherited from:
Product
def remove(key: V): ConnectivityOptimized[V]

Removes key from this disjoint set.

Removes key from this disjoint set.

Attributes

Inherited from:
AbstractDisjointSet
def size: Int

Returns the number of disjoint sets (roots).

Returns the number of disjoint sets (roots).

Attributes

Inherited from:
AbstractDisjointSet
def updated[W1 >: ParentSize[V]](key: V, value: W1): Map[V, W1]

Returns an updated map with key -> value. Note: Map.updated is a purely functional operation on an immutable Map — it returns a new Map and does not mutate the receiver.

Returns an updated map with key -> value. Note: Map.updated is a purely functional operation on an immutable Map — it returns a new Map and does not mutate the receiver.

Attributes

Inherited from:
AbstractDisjointSet