resq_dsa.graph
Graph data structures and shortest path algorithms. This module provides a graph representation with implementations of Breadth-First Search, Dijkstra’s algorithm, and A* pathfinding.heapq
deque
Callable
Graph Objects
_adj- Adjacency list representation._counter- Counter for tiebreaking in priority queue.
g = Graph() g.add_edge(“A”, “B”, 1.0) g.add_edge(“B”, “C”, 2.0) g.bfs(“A”) [‘A’, ‘B’, ‘C’] g.dijkstra(“A”, “C”)
{'path'- [‘A’, ‘B’, ‘C’], ‘cost’: 3.0}
Graph.__init__
Graph.add_edge
from_- Source node.to- Destination node.weight- Edge weight (default: 1.0).
g = Graph() g.add_edge(“start”, “end”, 5.0)
Graph.bfs
start- Starting node.
g = Graph() g.add_edge(“A”, “B”) g.add_edge(“A”, “C”) g.bfs(“A”) [‘A’, ‘B’, ‘C’]
Graph.dijkstra
start- Starting node.end- Target node.
g = Graph() g.add_edge(“A”, “B”, 1.0) g.add_edge(“B”, “C”, 2.0) g.dijkstra(“A”, “C”)
{'path'- [‘A’, ‘B’, ‘C’], ‘cost’: 3.0}
Graph.astar
start- Starting node.end- Target node.h- Heuristic function estimating cost from node to goal.
g = Graph() g.add_edge(“A”, “B”, 1.0) g.add_edge(“B”, “C”, 2.0) h = lambda n, goal: abs(ord(goal) - ord(n)) # Simple heuristic g.astar(“A”, “C”, h)
{'path'- [‘A’, ‘B’, ‘C’], ‘cost’: 3.0}