GraphNode

public protocol GraphNode : Graph, Hashable

A simplified version of the Graph protocol that allows optimal path finding between nodes without the need of a containing Graph type.

Use this protocol instead of Graph if your nodes store their outgoing edges.

When your nodes do not store edge information, consider using the Graph protocol.

  • In a GraphNode Graph the nodes are of type GraphNode.

    Declaration

    Swift

    associatedtype Node = Self

Optimal pathfinding requirements

  • List of other graph nodes that this node has an edge leading to.

    Declaration

    Swift

    var connectedNodes: Set<Self> { get }
  • Returns the estimated/heuristic cost to reach the indicated node from this node

    Declaration

    Swift

    func estimatedCost(to node: Self) -> Float

    Parameters

    node

    the end point of the edge who’s cost is to be estimated

    Return Value

    the heuristic cost

  • Declaration

    Swift

    func cost(to node: Self) -> Float

    Parameters

    node

    the destination node

    Return Value

    the actual cost to reach the indicated node from this node

Available where Node == Self

A* Implementation

  • findPath(to:) Extension method

    Attempts to find the optimal path between this node and the indicated goal node. If such a path exists, it is returned in start to end order. If it doesn’t exist, the returned array will be empty.

    Declaration

    Swift

    public func findPath(to goalNode: Self) -> [Self]

    Parameters

    goalNode

    the goal node of the pathfinding attempt

    Return Value

    the optimal path between this node and the indicated goal node

  • findPath(from:) Extension method

    As with findPathToNode: except this node is the goal node and a startNode is specified

    Declaration

    Swift

    public func findPath(from startNode: Self) -> [Self]

    Parameters

    startNode

    the start node of the pathfinding attempt

    Return Value

    the optimal path between the indicated start node and this node