algorithms_edu/problems/graph/tsp/
brute_force.rs1use crate::algo::graph::WeightedAdjacencyMatrix;
2use crate::algo::misc::permutations::*;
3
4pub fn tsp(g: &WeightedAdjacencyMatrix, start: usize) -> (f64, Vec<usize>) {
5 let n = g.node_count();
6 let permutations = (0..n)
7 .filter(|&i| i != start)
8 .collect::<Vec<_>>()
9 .permutations();
10 let mut tour = vec![];
11 let mut best_tour_cost = f64::INFINITY;
12 for perm in permutations {
13 let perm = unsafe { &*perm };
14 let cost = compute_tour_cost(g, perm, start);
15 if cost < best_tour_cost {
16 best_tour_cost = cost;
17 tour = perm.to_owned();
18 }
19 }
20 tour.insert(0, start);
21 (best_tour_cost, tour)
22}
23
24fn compute_tour_cost(g: &WeightedAdjacencyMatrix, tour: &[usize], start: usize) -> f64 {
25 tour.windows(2)
26 .fold(0., |cost, step| cost + g[step[0]][step[1]])
27 + g[start][*tour.first().unwrap()]
28 + g[*tour.last().unwrap()][start]
29}
30
31