lose/utils/algorithms/pathing.py
def dijkstra(graph, start, target=None, cost_func=None, include_diagonals=None):
    """Implementation of dijkstra's algorithm as a generator.

    This one uses a priority queue for a stack.

    See: https://en.wikipedia.org/wiki/Dijkstra's_algorithm

    Args:
        graph (list): a set of nodes
        start (node): the starting position
        target (node): the ending position; None means all nodes
        cost_func (callback):  the cost function or distance formula
        include_diagonals (bool):  Only use cardinal directions if False, else include all 8 possibilities

    Yields:
        cost, node
    """
    cost_func = cost_func or log_distance
    queue = []
    heapq.heapify(queue)
    costs = {start: 0}
    heapq.heappush(queue, (costs[start], start))
    yield (costs[start], start)
    while queue:
        node_cost, node = heapq.heappop(queue)
        node_cost = costs[node]
        neighbors = get_neighbors(node, include_diagonals=include_diagonals)
        neighbors = [
            neighbor for neighbor in neighbors
            if neighbor in graph  # short circuit on nodes not available
            if neighbor not in costs  # short circuit on nodes already calculated
        ]
        for neighbor in neighbors:
            neighbor_cost = cost_func(start, neighbor) + node_cost
            yield (neighbor_cost, neighbor)
            costs[neighbor] = neighbor_cost
            heapq.heappush(queue, (neighbor_cost, neighbor))
            if neighbor == target:
                break
Implementation of dijkstra's algorithm as a generator.

    This one uses a priority queue for a stack.

    See: https://en.wikipedia.org/wiki/Dijkstra's_algorithm

    Args:
        graph (list): a set of nodes
        start (node): the starting position
        target (node): the ending position; None means all nodes
        cost_func (callback):  the cost function or distance formula
        include_diagonals (bool):  Only use cardinal directions if False, else include all 8 possibilities

    Yields:
        cost, node
    
Implementation of dijkstra's algorithm as a generator.
This one uses a priority queue for a stack.


Implementation of dijkstra ' s algorithm as a generator . This one uses a priority queue for a stack .
def dijkstra(graph, start, target=None, cost_func=None, include_diagonals=None):
    cost_func = cost_func or log_distance
    queue = []
    heapq.heapify(queue)
    costs = {start: 0}
    heapq.heappush(queue, (costs[start], start))
    yield (costs[start], start)
    while queue:
        node_cost, node = heapq.heappop(queue)
        node_cost = costs[node]
        neighbors = get_neighbors(node, include_diagonals=include_diagonals)
        neighbors = [
            neighbor for neighbor in neighbors
            if neighbor in graph  
            if neighbor not in costs  
        ]
        for neighbor in neighbors:
            neighbor_cost = cost_func(start, neighbor) + node_cost
            yield (neighbor_cost, neighbor)
            costs[neighbor] = neighbor_cost
            heapq.heappush(queue, (neighbor_cost, neighbor))
            if neighbor == target:
                break
def dijkstra ( graph , start , target = None , cost_func = None , include_diagonals = None ) : cost_func = cost_func or log_distance queue = [ ] heapq . heapify ( queue ) costs = { start : 0 } heapq . heappush ( queue , ( costs [ start ] , start ) ) yield ( costs [ start ] , start ) while queue : node_cost , node = heapq . heappop ( queue ) node_cost = costs [ node ] neighbors = get_neighbors ( node , include_diagonals = include_diagonals ) neighbors = [ neighbor for neighbor in neighbors if neighbor in graph if neighbor not in costs ] for neighbor in neighbors : neighbor_cost = cost_func ( start , neighbor ) + node_cost yield ( neighbor_cost , neighbor ) costs [ neighbor ] = neighbor_cost heapq . heappush ( queue , ( neighbor_cost , neighbor ) ) if neighbor == target : break
Implementation of dijkstra's algorithm as a generator.
Implementation of dijkstra ' s algorithm as a generator .
"""Implementation of dijkstra's algorithm as a generator.

    This one uses a priority queue for a stack.

    See: https://en.wikipedia.org/wiki/Dijkstra's_algorithm

    Args:
        graph (list): a set of nodes
        start (node): the starting position
        target (node): the ending position; None means all nodes
        cost_func (callback):  the cost function or distance formula
        include_diagonals (bool):  Only use cardinal directions if False, else include all 8 possibilities

    Yields:
        cost, node
    """ # short circuit on nodes not available # short circuit on nodes already calculated
None
graph
a set of nodes
a set of nodes
start
the starting position
the starting position
target
the ending position; None means all nodes
the ending position ; None means all nodes
cost_func
the cost function or distance formula
the cost function or distance formula
include_diagonals
Only use cardinal directions if False, else include all 8 possibilities
Only use cardinal directions if False else include all 8 possibilities