|
| 1 | +fn line() { |
| 2 | + use graphina::core::types::Graph; |
| 3 | + |
| 4 | + use graphina::core::paths::dijkstra_path_f64; |
| 5 | + |
| 6 | + let mut graph = Graph::new(); |
| 7 | + let ids = (0..5).map(|i| graph.add_node(i)).collect::<Vec<_>>(); |
| 8 | + let edges = [(0, 1, 1.0), (1, 2, 1.0), (2, 3, 2.0), (3, 4, 1.0)]; |
| 9 | + for (s, d, w) in edges { |
| 10 | + graph.add_edge(ids[s], ids[d], w); |
| 11 | + } |
| 12 | + |
| 13 | + let (cost, trace) = dijkstra_path_f64(&graph, ids[0], None).unwrap(); |
| 14 | + |
| 15 | + println!("cost : {:?}", cost); |
| 16 | + println!("trace: {:?}", trace); |
| 17 | + // cost : [Some(0.0), Some(1.0), Some(2.0), Some(4.0), Some(5.0)] |
| 18 | + // trace: [None, Some(NodeId(NodeIndex(0))), Some(NodeId(NodeIndex(1))), Some(NodeId(NodeIndex(2))), Some(NodeId(NodeIndex(3)))] |
| 19 | +} |
| 20 | + |
| 21 | +fn flight() { |
| 22 | + use graphina::core::types::Digraph; |
| 23 | + |
| 24 | + use graphina::core::paths::dijkstra_path_impl; |
| 25 | + |
| 26 | + let mut graph: Digraph<String, (f64, String)> = Digraph::new(); |
| 27 | + // ^^^^^^^^^^^^^ |
| 28 | + // L arbitrary type as edge |
| 29 | + |
| 30 | + let cities = ["ATL", "PEK", "LHR", "HND", "CDG", "FRA", "HKG"]; |
| 31 | + |
| 32 | + let ids = cities |
| 33 | + .iter() |
| 34 | + .map(|s| graph.add_node(s.to_string())) |
| 35 | + .collect::<Vec<_>>(); |
| 36 | + |
| 37 | + let edges = [ |
| 38 | + // |
| 39 | + ("ATL", "PEK", (900.0, "boeing")), |
| 40 | + ("ATL", "LHR", (500.0, "airbus")), |
| 41 | + ("ATL", "HND", (700.0, "airbus")), |
| 42 | + // |
| 43 | + ("PEK", "LHR", (800.0, "boeing")), |
| 44 | + ("PEK", "HND", (100.0, "airbus")), |
| 45 | + ("PEK", "HKG", (100.0, "airbus")), |
| 46 | + // |
| 47 | + ("LHR", "CDG", (100.0, "airbus")), |
| 48 | + ("LHR", "FRA", (200.0, "boeing")), |
| 49 | + ("LHR", "HND", (600.0, "airbus")), |
| 50 | + // |
| 51 | + ("HND", "ATL", (700.0, "airbus")), |
| 52 | + ("HND", "FRA", (600.0, "airbus")), |
| 53 | + ("HND", "HKG", (100.0, "airbus")), |
| 54 | + // |
| 55 | + ]; |
| 56 | + |
| 57 | + for (s, d, w) in edges { |
| 58 | + let depart = cities.iter().position(|city| s == *city).unwrap(); |
| 59 | + let destin = cities.iter().position(|city| d == *city).unwrap(); |
| 60 | + graph.add_edge(ids[depart], ids[destin], (w.0, w.1.to_string())); |
| 61 | + } |
| 62 | + |
| 63 | + // function for evaluating possible cost for the edge |
| 64 | + // Some(f64) for cost |
| 65 | + // None for impassable |
| 66 | + let eval_cost = |(price, manufactuer): &(f64, String)| match manufactuer.as_str() { |
| 67 | + "boeing" => None, // avoid boeing plane |
| 68 | + _ => Some(*price), // return price as the cost |
| 69 | + }; |
| 70 | + |
| 71 | + let (cost, trace) = dijkstra_path_impl(&graph, ids[0], Some(1000.0), eval_cost).unwrap(); |
| 72 | + |
| 73 | + println!("cost : {:?}", cost); |
| 74 | + println!("trace: {:?}", trace); |
| 75 | + // cost : [Some(0.0), None, Some(500.0), Some(700.0), Some(600.0), None, Some(800.0)] |
| 76 | + // trace: [None, None, Some(NodeId(NodeIndex(0))), Some(NodeId(NodeIndex(0))), Some(NodeId(NodeIndex(2))), None, Some(NodeId(NodeIndex(3)))] |
| 77 | +} |
| 78 | + |
| 79 | +macro_rules! run_examples { |
| 80 | + ($($func:ident),* $(,)?) => { |
| 81 | + $( |
| 82 | + println!("<{}>", stringify!($func)); |
| 83 | + $func(); |
| 84 | + println!(); |
| 85 | + )* |
| 86 | + }; |
| 87 | +} |
| 88 | + |
| 89 | +fn main() { |
| 90 | + run_examples!(line, flight); |
| 91 | +} |
0 commit comments