Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,19 @@

## [Unreleased]

### Added

- `WeightedGraph::stored_weights(node)`, a defaulted method that yields every
stored outgoing weight of a node. The default reads `edge_weight` for each
entry of `neighbors`; `AdjacencyMatrix` overrides it to return the full row.

### Fixed

- `pagerank_weighted_checked` and `pagerank_weighted_checked_run` now reject
negative and NaN weights in an `AdjacencyMatrix`. The validator walked
`neighbors`, which keeps only `w > 0.0`, so those entries were never seen
and the call succeeded. Other algorithms are unchanged.

- `clustering_coefficients` and `global_clustering_coefficient` ignore
self-loops, as networkx does. A self-loop counted toward the degree, so a
triangle with a self-loop on node 0 gave C(0) = 1/3 instead of 1.
Expand Down
18 changes: 18 additions & 0 deletions src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ pub trait WeightedGraph: Graph {
/// absent — callers treat zero as "no edge", not as a zero-probability
/// edge, to keep random-walk transition matrices well-defined.
fn edge_weight(&self, source: usize, target: usize) -> f64;

/// Every stored outgoing weight of `node`, including entries that
/// [`Graph::neighbors`] filters out (negative, NaN). Used by
/// `pagerank_weighted_checked` to validate input.
///
/// The default yields `edge_weight(node, v)` for each `v` in
/// `neighbors(node)`. Override it when `neighbors` hides stored entries.
fn stored_weights(&self, node: usize) -> Vec<f64> {
self.neighbors(node)
.into_iter()
.map(|v| self.edge_weight(node, v))
.collect()
}
}

/// Weighted graph view returning **borrowed** `(neighbors, weights)` slice pairs.
Expand Down Expand Up @@ -139,6 +152,11 @@ impl<'a> WeightedGraph for AdjacencyMatrix<'a> {
fn edge_weight(&self, source: usize, target: usize) -> f64 {
self.0[source][target]
}

/// The full row, so negative and NaN entries are visible to validation.
fn stored_weights(&self, node: usize) -> Vec<f64> {
self.0[node].clone()
}
}

#[cfg(feature = "petgraph")]
Expand Down
28 changes: 26 additions & 2 deletions src/pagerank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,7 @@ pub fn pagerank_weighted_checked<G: WeightedGraph>(
fn validate_weights<G: WeightedGraph>(graph: &G) -> Result<()> {
let n = graph.node_count();
for u in 0..n {
for v in graph.neighbors(u) {
let w = graph.edge_weight(u, v);
for w in graph.stored_weights(u) {
if !w.is_finite() {
return Err(Error::InvalidParameter(
"edge weights must be finite".to_string(),
Expand Down Expand Up @@ -598,4 +597,29 @@ mod tests {
prop_assert!(res.is_err());
}
}

#[test]
fn weighted_checked_rejects_negative_and_nan_in_adjacency_matrix() {
// `AdjacencyMatrix::neighbors` keeps only positive entries, so the
// validator must read the full row to see these.
let negative = vec![
vec![0.0, 1.0, -1.0],
vec![0.0, 0.0, 1.0],
vec![1.0, 0.0, 0.0],
];
let nan = vec![
vec![0.0, 1.0, f64::NAN],
vec![0.0, 0.0, 1.0],
vec![1.0, 0.0, 0.0],
];
let config = PageRankConfig::default();
assert!(pagerank_weighted_checked(&AdjacencyMatrix(&negative), config).is_err());
assert!(pagerank_weighted_checked_run(&AdjacencyMatrix(&nan), config).is_err());
let ok = vec![
vec![0.0, 1.0, 0.0],
vec![0.0, 0.0, 1.0],
vec![1.0, 0.0, 0.0],
];
assert!(pagerank_weighted_checked(&AdjacencyMatrix(&ok), config).is_ok());
}
}
Loading