pandrs 0.3.2

A high-performance DataFrame library for Rust, providing pandas-like API with advanced features including SIMD optimization, parallel processing, and distributed computing capabilities
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
//! Shortest path algorithms for weighted graphs
//!
//! This module provides:
//! - Dijkstra's algorithm (non-negative weights)
//! - Bellman-Ford algorithm (handles negative weights)
//! - Floyd-Warshall algorithm (all-pairs shortest paths)
//! - A* search (with heuristic)
//!
//! # Examples
//!
//! ```
//! use pandrs::graph::{Graph, GraphType};
//! use pandrs::graph::path::dijkstra;
//!
//! let mut graph: Graph<&str, f64> = Graph::new(GraphType::Directed);
//! // ... add weighted edges ...
//! ```

use super::core::{Edge, Graph, GraphError, NodeId};
use std::cmp::Ordering;
use std::collections::{BinaryHeap, HashMap, HashSet};
use std::fmt::Debug;

/// Result of a shortest path computation
#[derive(Debug, Clone)]
pub struct ShortestPathResult {
    /// Distance from source to each reachable node
    pub distances: HashMap<NodeId, f64>,
    /// Parent node in the shortest path tree
    pub predecessors: HashMap<NodeId, Option<NodeId>>,
}

impl ShortestPathResult {
    /// Reconstructs the path from source to target
    pub fn path_to(&self, target: NodeId) -> Option<Vec<NodeId>> {
        if !self.predecessors.contains_key(&target) {
            return None;
        }

        let mut path = Vec::new();
        let mut current = target;

        loop {
            path.push(current);
            match self.predecessors.get(&current)? {
                Some(pred) => current = *pred,
                None => break, // Reached source
            }
        }

        path.reverse();
        Some(path)
    }

    /// Returns the distance to a specific node
    pub fn distance_to(&self, target: NodeId) -> Option<f64> {
        self.distances.get(&target).copied()
    }
}

/// State for Dijkstra's priority queue
#[derive(Debug, Clone)]
struct DijkstraState {
    node: NodeId,
    distance: f64,
}

impl PartialEq for DijkstraState {
    fn eq(&self, other: &Self) -> bool {
        self.distance == other.distance
    }
}

impl Eq for DijkstraState {}

impl PartialOrd for DijkstraState {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for DijkstraState {
    fn cmp(&self, other: &Self) -> Ordering {
        // Reverse ordering for min-heap
        other
            .distance
            .partial_cmp(&self.distance)
            .unwrap_or(Ordering::Equal)
    }
}

/// Performs Dijkstra's shortest path algorithm
///
/// # Arguments
/// * `graph` - The graph (edge weights must be non-negative)
/// * `source` - The starting node
/// * `weight_fn` - Function to extract weight from edge (returns f64)
///
/// # Returns
/// Shortest path result or error if negative weights encountered
pub fn dijkstra<N, W, F>(
    graph: &Graph<N, W>,
    source: NodeId,
    weight_fn: F,
) -> Result<ShortestPathResult, GraphError>
where
    N: Clone + Debug,
    W: Clone + Debug,
    F: Fn(&Edge<W>) -> f64,
{
    let mut distances: HashMap<NodeId, f64> = HashMap::new();
    let mut predecessors: HashMap<NodeId, Option<NodeId>> = HashMap::new();
    let mut heap: BinaryHeap<DijkstraState> = BinaryHeap::new();
    let mut visited: HashSet<NodeId> = HashSet::new();

    // Initialize source
    distances.insert(source, 0.0);
    predecessors.insert(source, None);
    heap.push(DijkstraState {
        node: source,
        distance: 0.0,
    });

    while let Some(DijkstraState { node, distance }) = heap.pop() {
        if visited.contains(&node) {
            continue;
        }
        visited.insert(node);

        // Skip if we've found a better path already
        if let Some(&best) = distances.get(&node) {
            if distance > best {
                continue;
            }
        }

        // Check all neighbors
        if let Some(neighbors) = graph.neighbors(node) {
            for neighbor in neighbors {
                if let Some(edge) = graph.get_edge_between(node, neighbor) {
                    let weight = weight_fn(edge);

                    if weight < 0.0 {
                        return Err(GraphError::InvalidOperation(
                            "Dijkstra's algorithm requires non-negative weights".to_string(),
                        ));
                    }

                    let new_distance = distance + weight;

                    let is_better = distances
                        .get(&neighbor)
                        .map(|&d| new_distance < d)
                        .unwrap_or(true);

                    if is_better {
                        distances.insert(neighbor, new_distance);
                        predecessors.insert(neighbor, Some(node));
                        heap.push(DijkstraState {
                            node: neighbor,
                            distance: new_distance,
                        });
                    }
                }
            }
        }
    }

    Ok(ShortestPathResult {
        distances,
        predecessors,
    })
}

/// Dijkstra's algorithm with default weight extraction (assumes `Option<f64>` weights)
pub fn dijkstra_default<N>(
    graph: &Graph<N, f64>,
    source: NodeId,
) -> Result<ShortestPathResult, GraphError>
where
    N: Clone + Debug,
{
    dijkstra(graph, source, |edge| edge.weight.unwrap_or(1.0))
}

/// Performs Bellman-Ford shortest path algorithm
///
/// Can handle negative edge weights but will detect negative cycles.
///
/// # Arguments
/// * `graph` - The graph
/// * `source` - The starting node
/// * `weight_fn` - Function to extract weight from edge
///
/// # Returns
/// Shortest path result or error if negative cycle detected
pub fn bellman_ford<N, W, F>(
    graph: &Graph<N, W>,
    source: NodeId,
    weight_fn: F,
) -> Result<ShortestPathResult, GraphError>
where
    N: Clone + Debug,
    W: Clone + Debug,
    F: Fn(&Edge<W>) -> f64,
{
    let n = graph.node_count();
    let mut distances: HashMap<NodeId, f64> = HashMap::new();
    let mut predecessors: HashMap<NodeId, Option<NodeId>> = HashMap::new();

    // Initialize
    for node_id in graph.node_ids() {
        distances.insert(node_id, f64::INFINITY);
        predecessors.insert(node_id, None);
    }
    distances.insert(source, 0.0);

    // Relax edges n-1 times
    for _ in 0..n - 1 {
        let mut changed = false;

        for (_, edge) in graph.edges() {
            let weight = weight_fn(edge);
            let src_dist = distances[&edge.source];

            if src_dist < f64::INFINITY {
                let new_dist = src_dist + weight;
                if new_dist < distances[&edge.target] {
                    distances.insert(edge.target, new_dist);
                    predecessors.insert(edge.target, Some(edge.source));
                    changed = true;
                }
            }
        }

        // Early termination if no changes
        if !changed {
            break;
        }
    }

    // Check for negative cycles
    for (_, edge) in graph.edges() {
        let weight = weight_fn(edge);
        let src_dist = distances[&edge.source];

        if src_dist < f64::INFINITY && src_dist + weight < distances[&edge.target] {
            return Err(GraphError::NegativeWeightCycle);
        }
    }

    Ok(ShortestPathResult {
        distances,
        predecessors,
    })
}

/// Bellman-Ford with default weight extraction
pub fn bellman_ford_default<N>(
    graph: &Graph<N, f64>,
    source: NodeId,
) -> Result<ShortestPathResult, GraphError>
where
    N: Clone + Debug,
{
    bellman_ford(graph, source, |edge| edge.weight.unwrap_or(1.0))
}

/// Result of Floyd-Warshall all-pairs shortest paths
#[derive(Debug, Clone)]
pub struct AllPairsShortestPaths {
    /// Distance matrix: `distances[i][j]` = shortest path from i to j
    pub distances: HashMap<NodeId, HashMap<NodeId, f64>>,
    /// Next hop matrix for path reconstruction
    pub next: HashMap<NodeId, HashMap<NodeId, Option<NodeId>>>,
}

impl AllPairsShortestPaths {
    /// Reconstructs the path between two nodes
    pub fn path(&self, source: NodeId, target: NodeId) -> Option<Vec<NodeId>> {
        if self.distances.get(&source)?.get(&target)? == &f64::INFINITY {
            return None;
        }

        let mut path = vec![source];
        let mut current = source;

        while current != target {
            current = self.next.get(&current)?.get(&target)?.as_ref().copied()?;
            path.push(current);
        }

        Some(path)
    }

    /// Gets the distance between two nodes
    pub fn distance(&self, source: NodeId, target: NodeId) -> Option<f64> {
        self.distances.get(&source)?.get(&target).copied()
    }
}

/// Performs Floyd-Warshall all-pairs shortest paths algorithm
///
/// # Arguments
/// * `graph` - The graph
/// * `weight_fn` - Function to extract weight from edge
pub fn floyd_warshall<N, W, F>(
    graph: &Graph<N, W>,
    weight_fn: F,
) -> Result<AllPairsShortestPaths, GraphError>
where
    N: Clone + Debug,
    W: Clone + Debug,
    F: Fn(&Edge<W>) -> f64,
{
    let nodes: Vec<NodeId> = graph.node_ids().collect();
    let n = nodes.len();

    // Initialize distance and next matrices
    let mut dist: HashMap<NodeId, HashMap<NodeId, f64>> = HashMap::new();
    let mut next: HashMap<NodeId, HashMap<NodeId, Option<NodeId>>> = HashMap::new();

    for &i in &nodes {
        let mut dist_row: HashMap<NodeId, f64> = HashMap::new();
        let mut next_row: HashMap<NodeId, Option<NodeId>> = HashMap::new();

        for &j in &nodes {
            if i == j {
                dist_row.insert(j, 0.0);
                next_row.insert(j, None);
            } else {
                dist_row.insert(j, f64::INFINITY);
                next_row.insert(j, None);
            }
        }

        dist.insert(i, dist_row);
        next.insert(i, next_row);
    }

    // Initialize direct edges
    for (_, edge) in graph.edges() {
        let weight = weight_fn(edge);
        dist.get_mut(&edge.source)
            .expect("operation should succeed")
            .insert(edge.target, weight);
        next.get_mut(&edge.source)
            .expect("operation should succeed")
            .insert(edge.target, Some(edge.target));
    }

    // Main Floyd-Warshall loop
    for &k in &nodes {
        for &i in &nodes {
            for &j in &nodes {
                let ik = dist[&i][&k];
                let kj = dist[&k][&j];

                if ik < f64::INFINITY && kj < f64::INFINITY {
                    let through_k = ik + kj;
                    if through_k < dist[&i][&j] {
                        dist.get_mut(&i)
                            .expect("operation should succeed")
                            .insert(j, through_k);
                        let k_next = next[&i][&k];
                        next.get_mut(&i)
                            .expect("operation should succeed")
                            .insert(j, k_next);
                    }
                }
            }
        }
    }

    // Check for negative cycles (diagonal should be 0)
    for &i in &nodes {
        if dist[&i][&i] < 0.0 {
            return Err(GraphError::NegativeWeightCycle);
        }
    }

    Ok(AllPairsShortestPaths {
        distances: dist,
        next,
    })
}

/// Floyd-Warshall with default weight extraction
pub fn floyd_warshall_default<N>(graph: &Graph<N, f64>) -> Result<AllPairsShortestPaths, GraphError>
where
    N: Clone + Debug,
{
    floyd_warshall(graph, |edge| edge.weight.unwrap_or(1.0))
}

/// A* search algorithm state
#[derive(Debug, Clone)]
struct AStarState {
    node: NodeId,
    f_score: f64, // f = g + h
    g_score: f64, // Actual cost from start
}

impl PartialEq for AStarState {
    fn eq(&self, other: &Self) -> bool {
        self.f_score == other.f_score
    }
}

impl Eq for AStarState {}

impl PartialOrd for AStarState {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for AStarState {
    fn cmp(&self, other: &Self) -> Ordering {
        other
            .f_score
            .partial_cmp(&self.f_score)
            .unwrap_or(Ordering::Equal)
    }
}

/// Performs A* search algorithm
///
/// # Arguments
/// * `graph` - The graph
/// * `source` - The starting node
/// * `target` - The target node
/// * `weight_fn` - Function to extract weight from edge
/// * `heuristic` - Function to estimate remaining distance to target
///
/// # Returns
/// The path and its total cost, or None if no path exists
pub fn astar<N, W, F, H>(
    graph: &Graph<N, W>,
    source: NodeId,
    target: NodeId,
    weight_fn: F,
    heuristic: H,
) -> Option<(Vec<NodeId>, f64)>
where
    N: Clone + Debug,
    W: Clone + Debug,
    F: Fn(&Edge<W>) -> f64,
    H: Fn(NodeId) -> f64,
{
    let mut open_set: BinaryHeap<AStarState> = BinaryHeap::new();
    let mut g_score: HashMap<NodeId, f64> = HashMap::new();
    let mut came_from: HashMap<NodeId, NodeId> = HashMap::new();
    let mut closed_set: HashSet<NodeId> = HashSet::new();

    g_score.insert(source, 0.0);
    open_set.push(AStarState {
        node: source,
        f_score: heuristic(source),
        g_score: 0.0,
    });

    while let Some(AStarState {
        node: current,
        g_score: current_g,
        ..
    }) = open_set.pop()
    {
        if current == target {
            // Reconstruct path
            let mut path = vec![current];
            let mut node = current;
            while let Some(&prev) = came_from.get(&node) {
                path.push(prev);
                node = prev;
            }
            path.reverse();
            return Some((path, current_g));
        }

        if closed_set.contains(&current) {
            continue;
        }
        closed_set.insert(current);

        if let Some(neighbors) = graph.neighbors(current) {
            for neighbor in neighbors {
                if closed_set.contains(&neighbor) {
                    continue;
                }

                if let Some(edge) = graph.get_edge_between(current, neighbor) {
                    let weight = weight_fn(edge);
                    let tentative_g = current_g + weight;

                    let is_better = g_score
                        .get(&neighbor)
                        .map(|&g| tentative_g < g)
                        .unwrap_or(true);

                    if is_better {
                        came_from.insert(neighbor, current);
                        g_score.insert(neighbor, tentative_g);

                        let f = tentative_g + heuristic(neighbor);
                        open_set.push(AStarState {
                            node: neighbor,
                            f_score: f,
                            g_score: tentative_g,
                        });
                    }
                }
            }
        }
    }

    None // No path found
}

/// Finds all shortest paths between source and target (for unweighted graphs)
pub fn all_shortest_paths<N, W>(
    graph: &Graph<N, W>,
    source: NodeId,
    target: NodeId,
) -> Vec<Vec<NodeId>>
where
    N: Clone + Debug,
    W: Clone + Debug,
{
    use super::traversal::bfs;

    let result = bfs(graph, source);

    // Check if target is reachable
    if !result.distance.contains_key(&target) {
        return vec![];
    }

    let target_dist = result.distance[&target];

    // Use BFS to find all paths of minimum length
    let mut all_paths: Vec<Vec<NodeId>> = Vec::new();
    let mut current_paths: Vec<Vec<NodeId>> = vec![vec![source]];

    for _ in 0..target_dist {
        let mut next_paths: Vec<Vec<NodeId>> = Vec::new();

        for path in current_paths {
            let last = *path.last().expect("operation should succeed");

            if let Some(neighbors) = graph.neighbors(last) {
                for neighbor in neighbors {
                    // Only extend if it's on a shortest path
                    if result.distance.get(&neighbor) == Some(&(result.distance[&last] + 1)) {
                        let mut new_path = path.clone();
                        new_path.push(neighbor);

                        if neighbor == target {
                            all_paths.push(new_path);
                        } else {
                            next_paths.push(new_path);
                        }
                    }
                }
            }
        }

        current_paths = next_paths;
    }

    all_paths
}

/// Computes the k-shortest paths between source and target
/// Uses Yen's algorithm
pub fn k_shortest_paths<N, W, F>(
    graph: &Graph<N, W>,
    source: NodeId,
    target: NodeId,
    k: usize,
    weight_fn: F,
) -> Vec<(Vec<NodeId>, f64)>
where
    N: Clone + Debug,
    W: Clone + Debug,
    F: Fn(&Edge<W>) -> f64 + Clone,
{
    let mut result: Vec<(Vec<NodeId>, f64)> = Vec::new();
    let mut candidates: Vec<(Vec<NodeId>, f64)> = Vec::new();

    // Find first shortest path
    if let Ok(sp_result) = dijkstra(graph, source, &weight_fn) {
        if let Some(path) = sp_result.path_to(target) {
            let cost = sp_result.distance_to(target).unwrap_or(f64::INFINITY);
            result.push((path, cost));
        }
    }

    if result.is_empty() {
        return result;
    }

    while result.len() < k {
        let (last_path, _) = &result[result.len() - 1];

        for i in 0..last_path.len() - 1 {
            let spur_node = last_path[i];
            let root_path: Vec<NodeId> = last_path[..=i].to_vec();

            // Create a modified graph by removing edges
            let mut removed_edges: Vec<(NodeId, NodeId)> = Vec::new();

            for (path, _) in &result {
                if path.len() > i && path[..=i] == root_path {
                    removed_edges.push((path[i], path[i + 1]));
                }
            }

            // Calculate spur path using modified graph (without actually modifying)
            // This is a simplified version - full implementation would modify graph
            if let Ok(sp_result) = dijkstra(graph, spur_node, &weight_fn) {
                if let Some(spur_path) = sp_result.path_to(target) {
                    // Check if we need to skip removed edges
                    let mut valid = true;
                    if !spur_path.is_empty() {
                        let first_edge = (spur_node, spur_path[1]);
                        if removed_edges.contains(&first_edge) {
                            valid = false;
                        }
                    }

                    if valid && spur_path.len() > 1 {
                        let mut total_path = root_path.clone();
                        total_path.pop(); // Remove spur node (it's already in spur_path)
                        total_path.extend(spur_path);

                        let total_cost = total_path
                            .windows(2)
                            .map(|w| {
                                graph
                                    .get_edge_between(w[0], w[1])
                                    .map(|e| weight_fn(e))
                                    .unwrap_or(f64::INFINITY)
                            })
                            .sum();

                        // Check if this path is already in candidates or result
                        let path_exists = candidates.iter().any(|(p, _)| *p == total_path)
                            || result.iter().any(|(p, _)| *p == total_path);

                        if !path_exists {
                            candidates.push((total_path, total_cost));
                        }
                    }
                }
            }
        }

        if candidates.is_empty() {
            break;
        }

        // Find the candidate with minimum cost
        candidates.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(Ordering::Equal));
        let best = candidates.remove(0);
        result.push(best);
    }

    result
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::graph::core::GraphType;

    fn create_weighted_graph() -> Graph<&'static str, f64> {
        let mut graph = Graph::new(GraphType::Directed);

        let a = graph.add_node("A");
        let b = graph.add_node("B");
        let c = graph.add_node("C");
        let d = graph.add_node("D");
        let e = graph.add_node("E");

        // Create weighted edges
        //     A --1--> B --2--> C
        //     |        |        |
        //     4        1        1
        //     v        v        v
        //     D --1--> E <------+
        graph
            .add_edge(a, b, Some(1.0))
            .expect("operation should succeed");
        graph
            .add_edge(b, c, Some(2.0))
            .expect("operation should succeed");
        graph
            .add_edge(a, d, Some(4.0))
            .expect("operation should succeed");
        graph
            .add_edge(b, e, Some(1.0))
            .expect("operation should succeed");
        graph
            .add_edge(c, e, Some(1.0))
            .expect("operation should succeed");
        graph
            .add_edge(d, e, Some(1.0))
            .expect("operation should succeed");

        graph
    }

    #[test]
    fn test_dijkstra() {
        let graph = create_weighted_graph();
        let a = NodeId(0);
        let e = NodeId(4);

        let result = dijkstra_default(&graph, a).expect("operation should succeed");

        // Shortest path A -> B -> E = 1 + 1 = 2
        assert!((result.distance_to(e).expect("operation should succeed") - 2.0).abs() < 1e-6);

        let path = result.path_to(e).expect("operation should succeed");
        assert_eq!(path.len(), 3); // A, B, E
    }

    #[test]
    fn test_bellman_ford() {
        let graph = create_weighted_graph();
        let a = NodeId(0);
        let e = NodeId(4);

        let result = bellman_ford_default(&graph, a).expect("operation should succeed");

        // Should give same result as Dijkstra for this graph
        assert!((result.distance_to(e).expect("operation should succeed") - 2.0).abs() < 1e-6);
    }

    #[test]
    fn test_floyd_warshall() {
        let graph = create_weighted_graph();

        let apsp = floyd_warshall_default(&graph).expect("operation should succeed");

        let a = NodeId(0);
        let e = NodeId(4);

        assert!((apsp.distance(a, e).expect("operation should succeed") - 2.0).abs() < 1e-6);

        let path = apsp.path(a, e).expect("operation should succeed");
        assert_eq!(path.len(), 3);
    }

    #[test]
    fn test_astar() {
        let graph = create_weighted_graph();
        let a = NodeId(0);
        let e = NodeId(4);

        // Simple heuristic (always 0, degenerates to Dijkstra)
        let result = astar(&graph, a, e, |edge| edge.weight.unwrap_or(1.0), |_| 0.0);

        assert!(result.is_some());
        let (path, cost) = result.expect("operation should succeed");
        assert!((cost - 2.0).abs() < 1e-6);
        assert_eq!(path.len(), 3);
    }

    #[test]
    fn test_negative_weights_dijkstra() {
        let mut graph: Graph<&str, f64> = Graph::new(GraphType::Directed);
        let a = graph.add_node("A");
        let b = graph.add_node("B");

        graph
            .add_edge(a, b, Some(-1.0))
            .expect("operation should succeed");

        let result = dijkstra_default(&graph, a);
        assert!(result.is_err());
    }

    #[test]
    fn test_negative_weights_bellman_ford() {
        let mut graph: Graph<&str, f64> = Graph::new(GraphType::Directed);
        let a = graph.add_node("A");
        let b = graph.add_node("B");
        let c = graph.add_node("C");

        graph
            .add_edge(a, b, Some(1.0))
            .expect("operation should succeed");
        graph
            .add_edge(b, c, Some(-1.0))
            .expect("operation should succeed");

        // Should work fine - negative weight but no negative cycle
        let result = bellman_ford_default(&graph, a);
        assert!(result.is_ok());
    }

    #[test]
    fn test_negative_cycle_detection() {
        let mut graph: Graph<&str, f64> = Graph::new(GraphType::Directed);
        let a = graph.add_node("A");
        let b = graph.add_node("B");
        let c = graph.add_node("C");

        graph
            .add_edge(a, b, Some(1.0))
            .expect("operation should succeed");
        graph
            .add_edge(b, c, Some(-2.0))
            .expect("operation should succeed");
        graph
            .add_edge(c, b, Some(0.5))
            .expect("operation should succeed"); // Creates negative cycle B -> C -> B

        let result = bellman_ford_default(&graph, a);
        assert!(matches!(result, Err(GraphError::NegativeWeightCycle)));
    }
}