Branch and Bound
Tutorial Video
Text description (accessibility)
This video demonstrates the "Branch and Bound" functional Rust example. Difficulty level: Advanced. Key concepts covered: Functional Programming. Backtracking explores the complete search space but doesn't use bounds to prune. Key difference from OCaml: | Aspect | Rust | OCaml |
Tutorial
The Problem
Backtracking explores the complete search space but doesn't use bounds to prune. Branch and bound adds an upper/lower bound computation at each node: if the best possible solution achievable from a partial assignment cannot beat the current best complete solution, prune that entire subtree. This makes branch and bound the algorithm of choice for exact optimization: integer linear programming, traveling salesman problem (with bounds from relaxations), knapsack (with greedy fractional bound), and scheduling optimization. While still exponential in the worst case, good bounds make it practical for real instances that pure backtracking cannot handle.
🎯 Learning Outcomes
Code Example
#![allow(clippy::all)]
//! PlaceholderKey Differences
| Aspect | Rust | OCaml |
|---|---|---|
| Search strategy | DFS (stack-based) or BFS | Same options |
| Best tracking | mut f64 variable | float ref |
| Bounding function | Nested fn bound | Nested let bound or separate |
| Priority queue | BinaryHeap for best-first | Heap module or priority list |
| Sorting | sort_by with partial_cmp | List.sort with comparison |
| Integer variant | u64 items and capacity | int items |
OCaml Approach
OCaml's branch-and-bound uses a priority queue (BFS with best-first search) or a recursive DFS. The bound function mirrors the Rust fractional knapsack. OCaml's List.sort sorts items by ratio. The Queue.t or Heap module implements BFS order. Mutable best ref tracks the current best. OCaml's tail-recursive DFS avoids stack overflow for deep trees. The List.fold_left accumulates the bound computation.
Full Source
#![allow(clippy::all)]
//! PlaceholderDeep Comparison
OCaml vs Rust: Branch And Bound
Overview
See the example.rs and example.ml files for detailed implementations.
Key Differences
| Aspect | OCaml | Rust |
|---|---|---|
| Type system | Hindley-Milner | Ownership + traits |
| Memory | GC | Zero-cost abstractions |
| Mutability | Explicit ref | mut keyword |
| Error handling | Option/Result | Result<T, E> |
See README.md for detailed comparison.
Exercises
BinaryHeap<Reverse<(NotNan<f64>, State)>> for better pruning order.minilp crate for tighter bounds on integer programs.