Skip to main content

second_zagreb_coindex

Function second_zagreb_coindex 

Source
pub fn second_zagreb_coindex(graph: &Graph) -> IgraphResult<i64>
Expand description

Compute the second Zagreb coindex.

\bar{M}_2(G) = Σ_{(u,v)∉E, u≠v} d(u) · d(v)

Uses the identity: \bar{M}_2 = 2m² - M_2 where M_2 = Σ_{(u,v)∈E} d(u)·d(v).

§Examples

use rust_igraph::{Graph, second_zagreb_coindex};

// K_3: M₂ = 3·(2·2)=12, m=3 → bar_M₂ = 2·9-12 = 6
// But K_3 has no non-edges, so bar_M₂ = 0. Let's check:
// 2m² = 18, M₂ = 12 → 18-12 = 6? No, for K_3 there are no non-edges.
// The identity is: Σ_{u<v} d(u)d(v) = M₂ + bar_M₂
// Σ_{u<v} d(u)d(v) = (Σ d(v))²/2 - Σ d(v)²/2 = (2m)²/2 - M₁/2
// = 2m² - M₁/2. So bar_M₂ = 2m² - M₁/2 - M₂.
// For path 0-1-2: m=2, M₁=6, M₂=2+2=4
// bar_M₂ = 2·4 - 6/2 - 4 = 8-3-4 = 1
// Non-edge (0,2): d(0)·d(2) = 1·1 = 1. ✓
let g = Graph::from_edges(&[(0,1),(1,2)], false, Some(3)).unwrap();
assert_eq!(second_zagreb_coindex(&g).unwrap(), 1);