#python

2 articles tagged “python”.

A weighted graph of six nodes with the shortest-path tree from A highlighted and each node labelled with its distance
AlgorithmsAdvanced

Dijkstra's Algorithm: Shortest Paths with a Priority Queue

Implement Dijkstra's shortest-path algorithm in Python with heapq, trace it step by step, understand why it's correct, and learn exactly when it breaks: negative edges, ties in the heap and dense graphs.

7 min read
A sorted row of numbers with the search range shrinking by half at each step until it lands on the first 23
AlgorithmsIntermediate

Binary Search Without Off-by-One Bugs

Binary search is easy to explain and surprisingly easy to get wrong. Learn one loop invariant that makes it correct every time, find first and last positions, and use binary search on answers instead of arrays.

7 min read