graph indices
Graph Indices
- graphcalc.graphs.invariants.graph_indices.abc_index(G)[source]
Compute the Atom–Bond Connectivity (ABC) index of a graph \(G\).
The ABC index is
\[\mathrm{ABC}(G) \;=\; \sum_{\{u,v\}\in E(G)} \sqrt{\frac{d(u)+d(v)-2}{d(u)\,d(v)}}\,,\]where \(d(\cdot)\) denotes vertex degree.
- Parameters:
G (networkx.Graph) – The input graph. Intended for finite simple undirected graphs.
Notes
For a pendant edge with \(d(u)=d(v)=1\) (which can only occur in \(K_2\)), the summand is \(\sqrt{0/1}=0\).
In a simple graph, every edge has endpoints of degree at least 1, so the denominator is positive.
- Returns:
The Atom–Bond Connectivity index \(\mathrm{ABC}(G)\).
- Return type:
float
Examples
>>> import networkx as nx >>> import graphcalc.graphs as gc >>> gc.abc_index(nx.complete_graph(2)) # single edge with degrees 1,1 0.0
For \(P_3\) (degrees 1-2-1), each edge contributes \(\sqrt{1/2}\), so the total is \(\sqrt{2}\):
>>> import networkx as nx >>> import graphcalc.graphs as gc >>> round(gc.abc_index(nx.path_graph(3)), 6) 1.414214
A 4-cycle has 4 edges with degree-pair (2,2), so each contributes \(\sqrt{(2)/(4)}=\sqrt{1/2}\):
>>> import networkx as nx >>> import graphcalc.graphs as gc >>> round(gc.abc_index(nx.cycle_graph(4)), 6) 2.828427
- graphcalc.graphs.invariants.graph_indices.augmented_zagreb_index(G)[source]
Compute the Augmented Zagreb index \(AZI(G)\) of a graph \(G\) (with a total-function convention).
The usual definition is
\[AZI(G) \;=\; \sum_{\{u,v\}\in E(G)} \left(\frac{d(u)\,d(v)}{d(u)+d(v)-2}\right)^3,\]where \(d(\cdot)\) denotes vertex degree.
Convention / edge cases
The denominator \(d(u)+d(v)-2\) is zero exactly when \(d(u)=d(v)=1\), which in a simple graph occurs only for \(G=K_2\). Different sources either treat \(AZI(K_2)\) as undefined or define a special value.
This implementation uses a total-function convention: any edge with \(d(u)+d(v)-2 \le 0\) contributes 0 to the sum. In particular, this yields \(AZI(K_2)=0\).
- param G:
The input graph. Intended for finite simple undirected graphs.
- type G:
networkx.Graph
Notes
For all other edges in a simple graph, \(d(u)+d(v)-2 \ge 1\), so the summand is well-defined.
- returns:
The Augmented Zagreb index \(AZI(G)\) under the convention above.
- rtype:
float
Examples
By convention, \(AZI(K_2)=0\):
>>> import networkx as nx >>> import graphcalc.graphs as gc >>> gc.augmented_zagreb_index(nx.complete_graph(2)) 0.0
For \(P_3\), degrees are 1-2-1, and each edge contributes \(((2)/(1))^3 = 8\), so \(AZI(P_3)=16\):
>>> import networkx as nx >>> import graphcalc.graphs as gc >>> gc.augmented_zagreb_index(nx.path_graph(3)) 16.0
For \(C_4\), every edge has degrees (2,2), so each contributes \(((4)/(2))^3 = 8\), hence \(AZI(C_4)=32\):
>>> import networkx as nx >>> import graphcalc.graphs as gc >>> gc.augmented_zagreb_index(nx.cycle_graph(4)) 32.0
- graphcalc.graphs.invariants.graph_indices.ga_index(G)[source]
Compute the Geometric–Arithmetic (GA) index of a graph \(G\).
The GA index is the degree-based topological index
\[\mathrm{GA}(G) \;=\; \sum_{\{u,v\}\in E(G)} \frac{2\sqrt{d(u)\,d(v)}}{d(u)+d(v)},\]where \(d(\cdot)\) denotes vertex degree.
- Parameters:
G (networkx.Graph) – The input graph. Intended for finite simple undirected graphs.
Notes
For a simple graph, \(d(u),d(v)\ge 1\) on every edge, and \(d(u)+d(v)\ge 2\), so the expression is well-defined.
Each summand lies in \((0,1]\) by AM–GM, with equality 1 iff \(d(u)=d(v)\).
- Returns:
The GA index \(\mathrm{GA}(G)\).
- Return type:
float
Examples
For \(K_2\), the single edge has degrees (1,1), so GA = 1:
>>> import networkx as nx >>> import graphcalc.graphs as gc >>> gc.ga_index(nx.complete_graph(2)) 1.0
For \(P_3\), each edge has degrees (1,2), contributing \(2\sqrt{2}/3\):
>>> import networkx as nx >>> import graphcalc.graphs as gc >>> round(gc.ga_index(nx.path_graph(3)), 6) 1.885618
For \(C_4\), all edges have degrees (2,2), so each term is 1 and GA = 4:
>>> import networkx as nx >>> import graphcalc.graphs as gc >>> gc.ga_index(nx.cycle_graph(4)) 4.0
- graphcalc.graphs.invariants.graph_indices.harmonic_index(G: Graph) float[source]
Returns the harmonic index of a graph.
The harmonic index of a graph is defined as:
\[H(G) = \sum_{uv \in E(G)} \frac{2}{d(u) + d(v)}\]where: - \(E(G)\) is the edge set of the graph \(G\). - \(d(u)\) is the degree of vertex \(u\).
The harmonic index is commonly used in mathematical chemistry and network science to measure structural properties of molecular and network graphs.
- Parameters:
G (nx.Graph) – The graph.
- Returns:
The harmonic index of the graph.
- Return type:
float
Examples
>>> import graphcalc.graphs as gc >>> from graphcalc.graphs.generators import path_graph, complete_graph
>>> G = path_graph(4) # Path graph with 4 vertices >>> gc.harmonic_index(G) 1.8333333333333333
>>> H = complete_graph(3) # Complete graph with 3 vertices >>> gc.harmonic_index(H) 1.5
Notes
The harmonic index assumes all edge weights are equal to 1. If you want to consider weighted graphs, modify the function to account for edge weights.
The harmonic index is symmetric with respect to the graph’s structure, making it invariant under graph isomorphism.
References
S. Klavžar and I. Gutman, A comparison of the Schultz molecular topological index and the Wiener index. Journal of Chemical Information and Computer Sciences, 33(6), 1006-1009 (1993).
- graphcalc.graphs.invariants.graph_indices.hyper_zagreb_index(G)[source]
Compute the Hyper Zagreb index \(HM(G)\) of a graph \(G\).
The Hyper Zagreb index is
\[HM(G) \;=\; \sum_{\{u,v\}\in E(G)} \bigl(d(u)+d(v)\bigr)^2,\]where \(d(\cdot)\) denotes vertex degree.
- Parameters:
G (networkx.Graph) – The input graph. Intended for finite simple undirected graphs.
- Returns:
The Hyper Zagreb index \(HM(G)\) (integer-valued for simple graphs).
- Return type:
int
Examples
For \(P_4\), edge degree-sums are 3, 4, 3, so \(HM(P_4) = 3^2 + 4^2 + 3^2 = 34\):
>>> import networkx as nx >>> import graphcalc.graphs as gc >>> gc.hyper_zagreb_index(nx.path_graph(4)) 34
For \(K_4\), all degrees are 3 and each of the 6 edges has sum 6, so \(HM(K_4) = 6 \cdot 6^2 = 216\):
>>> import networkx as nx >>> import graphcalc.graphs as gc >>> gc.hyper_zagreb_index(nx.complete_graph(4)) 216
A graph with no edges has Hyper Zagreb index 0:
>>> import networkx as nx >>> import graphcalc.graphs as gc >>> gc.hyper_zagreb_index(nx.empty_graph(5)) 0
- graphcalc.graphs.invariants.graph_indices.randic_index(G, alpha=-0.5)[source]
Compute the generalized Randić index \(R_\alpha(G)\) of a graph \(G\).
For a real parameter \(\alpha\), the generalized Randić index (also called the generalized connectivity index) is
\[R_\alpha(G) \;=\; \sum_{\{u,v\}\in E(G)} \bigl(d(u)\,d(v)\bigr)^{\alpha},\]where \(d(u)\) denotes the degree of \(u\).
The classical Randić index is the special case \(\alpha=-\tfrac12\):
\[R(G) \;=\; \sum_{\{u,v\}\in E(G)} \frac{1}{\sqrt{d(u)\,d(v)}}.\]- Parameters:
G (networkx.Graph) – The input graph. Intended for finite simple undirected graphs.
alpha (float, default=-0.5) – The exponent \(\alpha\).
Notes
Only edges contribute, so isolated vertices do not affect the value.
In a simple graph, every edge has endpoints of degree at least 1, so \(\alpha<0\) causes no division-by-zero issues.
For multigraphs, degrees count multiplicity and parallel edges are summed repeatedly; this matches the literal formula over the multiset of edges, which may differ from some chemistry conventions.
- Returns:
The value \(R_\alpha(G)\).
- Return type:
float
Examples
A path on 4 vertices has degrees \((1,2,2,1)\), so
\[R(G)=\tfrac{1}{\sqrt{2}}+\tfrac{1}{2}+\tfrac{1}{\sqrt{2}}.\]>>> import networkx as nx >>> import graphcalc.graphs as gc >>> G = nx.path_graph(4) >>> round(gc.randic_index(G), 6) 1.914214
A complete graph \(K_n\) has all degrees \(n-1\), so \(R_{-1/2}(K_n) = |E|/(n-1) = n/2\).
>>> import networkx as nx >>> import graphcalc.graphs as gc >>> gc.randic_index(nx.complete_graph(6)) 3.0
Changing \(\alpha\) changes the weighting:
>>> import networkx as nx >>> import graphcalc.graphs as gc >>> G = nx.path_graph(4) >>> gc.randic_index(G, alpha=1) # sum of degree products over edges 8
- graphcalc.graphs.invariants.graph_indices.reciprocal_augmented_zagreb_index(G)[source]
Compute a reciprocal variant of the Augmented Zagreb index.
This per-edge reciprocal variant is
\[RAZI(G) \;=\; \sum_{\{u,v\}\in E(G)} \left(\frac{d(u)+d(v)-2}{d(u)\,d(v)}\right)^3,\]which is the reciprocal of the usual AZI edge fraction (inside the cube).
- Parameters:
G (networkx.Graph) – The input graph. Intended for finite simple undirected graphs.
Notes
For \(K_2\), the single edge has \(d(u)=d(v)=1\), so the summand is 0 and \(RAZI(K_2)=0\).
For simple graphs, \(d(u),d(v)\ge 1\) on every edge, so the expression is well-defined.
- Returns:
The reciprocal Augmented Zagreb index \(RAZI(G)\).
- Return type:
float
Examples
>>> import networkx as nx >>> import graphcalc.graphs as gc >>> gc.reciprocal_augmented_zagreb_index(nx.complete_graph(2)) 0.0
For \(P_3\), each edge has degrees (1,2), so each contributes \(((1)/(2))^3 = 1/8\) and the total is 1/4:
>>> import networkx as nx >>> import graphcalc.graphs as gc >>> gc.reciprocal_augmented_zagreb_index(nx.path_graph(3)) 0.25
- graphcalc.graphs.invariants.graph_indices.reciprocal_ga_index(G)[source]
Compute the reciprocal Geometric–Arithmetic index of a graph \(G\).
This “reciprocal” variant sums the reciprocals of the GA edge terms:
\[\mathrm{RGA}(G) \;=\; \sum_{\{u,v\}\in E(G)} \frac{d(u)+d(v)}{2\sqrt{d(u)\,d(v)}}.\]For simple graphs, degrees on an edge are at least 1, so this is well-defined.
- Parameters:
G (networkx.Graph) – The input graph. Intended for finite simple undirected graphs.
- Returns:
The reciprocal GA index \(\mathrm{RGA}(G)\).
- Return type:
float
Examples
For \(K_2\), RGA = 1:
>>> import networkx as nx >>> import graphcalc.graphs as gc >>> gc.reciprocal_ga_index(nx.complete_graph(2)) 1.0
For \(C_4\), every edge has degrees (2,2), so each term is 1 and RGA = 4:
>>> import networkx as nx >>> import graphcalc.graphs as gc >>> gc.reciprocal_ga_index(nx.cycle_graph(4)) 4.0
- graphcalc.graphs.invariants.graph_indices.reciprocal_hyper_zagreb_index(G)[source]
Compute the reciprocal Hyper Zagreb index \(RHM(G)\) of a graph \(G\).
This “reciprocal” variant sums the reciprocals of the Hyper Zagreb edge terms:
\[RHM(G) \;=\; \sum_{\{u,v\}\in E(G)} \frac{1}{\bigl(d(u)+d(v)\bigr)^2}.\]For simple graphs, every edge satisfies \(d(u)+d(v)\ge 2\), so the expression is well-defined.
- Parameters:
G (networkx.Graph) – The input graph. Intended for finite simple undirected graphs.
- Returns:
The reciprocal Hyper Zagreb index \(RHM(G)\).
- Return type:
float
Examples
For \(K_2\), the single edge has degree-sum 2, so \(RHM=1/4\):
>>> import networkx as nx >>> import graphcalc.graphs as gc >>> gc.reciprocal_hyper_zagreb_index(nx.complete_graph(2)) 0.25
For \(P_4\), edge degree-sums are 3, 4, 3, so \(RHM(P_4)=1/3^2 + 1/4^2 + 1/3^2\):
>>> import networkx as nx >>> import graphcalc.graphs as gc >>> round(gc.reciprocal_hyper_zagreb_index(nx.path_graph(4)), 6) 0.284722
- graphcalc.graphs.invariants.graph_indices.reciprocal_sombor_index(G)[source]
Compute the reciprocal Sombor index \(RSO(G)\) of a graph \(G\).
This “reciprocal” variant sums the reciprocals of the per-edge Sombor terms:
\[RSO(G) \;=\; \sum_{\{u,v\}\in E(G)} \frac{1}{\sqrt{d(u)^2 + d(v)^2}}.\]For simple graphs, every edge has endpoints of degree at least 1, so each denominator is at least \(\sqrt{2}\) and the expression is well-defined.
- Parameters:
G (networkx.Graph) – The input graph. Intended for finite simple undirected graphs.
- Returns:
The reciprocal Sombor index \(RSO(G)\).
- Return type:
float
Examples
>>> import networkx as nx >>> import graphcalc.graphs as gc >>> round(gc.reciprocal_sombor_index(nx.complete_graph(2)), 6) 0.707107
>>> import networkx as nx >>> import graphcalc.graphs as gc >>> round(gc.reciprocal_sombor_index(nx.path_graph(3)), 6) # 2 edges, each 1/sqrt(5) 0.894427
- graphcalc.graphs.invariants.graph_indices.reciprocal_zagreb_1(G)[source]
Compute the reciprocal first Zagreb index \(RM_1(G)\).
A common “reciprocal” variant replaces \(d(v)^2\) by its reciprocal:
\[RM_1(G) = \sum_{v \in V(G)} \frac{1}{d(v)^2}.\]Isolated vertices (degree 0) contribute nothing in this implementation.
- Parameters:
G (networkx.Graph) – The input graph. Intended for finite simple undirected graphs.
- Returns:
The reciprocal first Zagreb index \(RM_1(G)\).
- Return type:
float
Examples
>>> import networkx as nx >>> import graphcalc.graphs as gc >>> G = nx.path_graph(4) # degrees 1,2,2,1 >>> gc.reciprocal_zagreb_1(G) 2.5 >>> H = nx.empty_graph(3) # all isolated -> contributes 0 by convention here >>> gc.reciprocal_zagreb_1(H) 0.0
- graphcalc.graphs.invariants.graph_indices.reciprocal_zagreb_2(G)[source]
Compute the reciprocal second Zagreb index \(RM_2(G)\).
A common “reciprocal” variant replaces \(d(u)d(v)\) by its reciprocal:
\[RM_2(G) = \sum_{\{u,v\} \in E(G)} \frac{1}{d(u)\,d(v)}.\]For simple graphs, every edge has endpoints of degree at least 1, so no division-by-zero occurs.
- Parameters:
G (networkx.Graph) – The input graph. Intended for finite simple undirected graphs.
- Returns:
The reciprocal second Zagreb index \(RM_2(G)\).
- Return type:
float
Examples
>>> import networkx as nx >>> import graphcalc.graphs as gc >>> G = nx.path_graph(4) # edge degree-products: 2,4,2 -> reciprocals: 1/2+1/4+1/2 >>> gc.reciprocal_zagreb_2(G) 1.25 >>> gc.reciprocal_zagreb_2(nx.complete_graph(4)) # 6 edges, each 1/(3*3) 0.6666666666666666
- graphcalc.graphs.invariants.graph_indices.sombor_index(G)[source]
Compute the Sombor index \(SO(G)\) of a graph \(G\).
The Sombor index is the degree-based topological index
\[SO(G) \;=\; \sum_{\{u,v\}\in E(G)} \sqrt{d(u)^2 + d(v)^2},\]where \(d(\cdot)\) denotes vertex degree.
- Parameters:
G (networkx.Graph) – The input graph. Intended for finite simple undirected graphs.
- Returns:
The Sombor index \(SO(G)\).
- Return type:
float
Examples
For \(K_2\), degrees are (1,1), so \(SO(K_2)=\sqrt{2}\):
>>> import networkx as nx >>> import graphcalc.graphs as gc >>> round(gc.sombor_index(nx.complete_graph(2)), 6) 1.414214
For \(P_3\), degrees are 1-2-1, so each edge contributes \(\sqrt{1^2+2^2}=\sqrt{5}\):
>>> import networkx as nx >>> import graphcalc.graphs as gc >>> round(gc.sombor_index(nx.path_graph(3)), 6) 4.472136
For \(C_4\), all degrees are 2, so each edge contributes \(\sqrt{8}\):
>>> import networkx as nx >>> import graphcalc.graphs as gc >>> round(gc.sombor_index(nx.cycle_graph(4)), 6) 11.313708
- graphcalc.graphs.invariants.graph_indices.sum_connectivity_index(G, alpha=-0.5)[source]
Compute the generalized sum-connectivity index \(SC_\alpha(G)\) of a graph \(G\).
For a real parameter \(\alpha\), the generalized sum-connectivity index is
\[SC_\alpha(G) \;=\; \sum_{\{u,v\}\in E(G)} \bigl(d(u)+d(v)\bigr)^{\alpha},\]where \(d(\cdot)\) denotes vertex degree.
The classical sum-connectivity index is the special case \(\alpha=-\tfrac12\).
- Parameters:
G (networkx.Graph) – The input graph. Intended for finite simple undirected graphs.
alpha (float, default=-0.5) – The exponent \(\alpha\).
Notes
Only edges contribute, so isolated vertices do not affect the value.
In a simple graph, every edge satisfies \(d(u)+d(v)\ge 2\), so negative \(\alpha\) causes no division-by-zero issues.
- Returns:
The value \(SC_\alpha(G)\).
- Return type:
float
Examples
For \(K_2\), the single edge has degrees (1,1), so \(SC_{-1/2} = 2^{-1/2}\):
>>> import networkx as nx >>> import graphcalc.graphs as gc >>> round(gc.sum_connectivity_index(nx.complete_graph(2)), 6) 0.707107
For \(P_4\), edge degree-sums are 3, 4, 3, so \(SC_{-1/2} = 3^{-1/2} + 4^{-1/2} + 3^{-1/2}\):
>>> import networkx as nx >>> import graphcalc.graphs as gc >>> round(gc.sum_connectivity_index(nx.path_graph(4)), 6) 1.654701
With \(\alpha=1\), this is the sum of degree-sums over edges:
>>> import networkx as nx >>> import graphcalc.graphs as gc >>> gc.sum_connectivity_index(nx.path_graph(4), alpha=1) 10
- graphcalc.graphs.invariants.graph_indices.zagreb_1(G)[source]
Compute the first Zagreb index \(M_1(G)\).
The first Zagreb index is
\[M_1(G) = \sum_{v \in V(G)} d(v)^2,\]where \(d(v)\) is the degree of \(v\).
- Parameters:
G (networkx.Graph) – The input graph. Intended for finite simple undirected graphs.
- Returns:
The first Zagreb index \(M_1(G)\).
- Return type:
int
Examples
>>> import networkx as nx >>> import graphcalc.graphs as gc >>> gc.zagreb_1(nx.path_graph(4)) # degrees 1,2,2,1 10 >>> gc.zagreb_1(nx.complete_graph(4)) # degrees all 3 36
- graphcalc.graphs.invariants.graph_indices.zagreb_2(G)[source]
Compute the second Zagreb index \(M_2(G)\).
The second Zagreb index is
\[M_2(G) = \sum_{\{u,v\} \in E(G)} d(u)\,d(v),\]where \(d(\cdot)\) denotes vertex degree.
- Parameters:
G (networkx.Graph) – The input graph. Intended for finite simple undirected graphs.
- Returns:
The second Zagreb index \(M_2(G)\).
- Return type:
int
Examples
>>> import networkx as nx >>> import graphcalc.graphs as gc >>> gc.zagreb_2(nx.path_graph(4)) # edge degree-products: 1*2 + 2*2 + 2*1 8 >>> gc.zagreb_2(nx.complete_graph(4)) # 6 edges, each contributes 3*3 54