Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding degree centrality function #1145

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
30 changes: 30 additions & 0 deletions rustworkx-core/src/centrality.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,36 @@ where
betweenness
}

/// Computes the degree centrality of nodes in a graph.
///
/// Degree centrality is defined as the number of edges incident upon a node
/// normalized by the maximum possible degree in a simple graph (n - 1, where n is the number of nodes).
///
/// Arguments:
/// * `graph` - The graph object to compute degree centrality for.
///
/// Returns:
/// A `Vec<f64>` containing the degree centrality of each node in the graph.
pub fn degree_centrality<G>(graph: G) -> Result<Option<Vec<f64>>, E>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not compile because you never defined E

where
G: IntoNodeIdentifiers + NodeCount,
{
let num_nodes = graph.node_count() as f64;
let mut centrality_values = vec![0.0; graph.node_count()];
{
let degree = graph.edges(node).count() as f64;
let centrality = if num_nodes <= 1.0 {
0.0
} else {
degree / (num_nodes - 1.0)
};
centrality_values[graph.to_index(node)] = centrality;
}

Ok(Some(centrality_values))
}


/// Compute the edge betweenness centrality of all edges in a graph.
///
/// The algorithm used in this function is based on:
Expand Down
20 changes: 20 additions & 0 deletions src/centrality.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,26 @@ pub fn graph_betweenness_centrality(
}
}

/// A Python function wrapper for degree_centrality

fn degree_centrality_py(
py: Python,
graph: &digraph::PyDiGraph,
) -> PyResult<CentralityMapping> {
// Convert Python object to Rust type
let graph_rust = match graph.extract::<SomeRustGraphType>() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also will not compile

Ok(g) => g,
Err(e) => return Err(e.into()),
};
// Call the Rust function
match degree_centrality(graph_rust) {
Ok(Some(values)) => Ok(Some(values)),
Ok(None) => Ok(None),
Err(e) => Err(e.into()),
}
}


/// Compute the betweenness centrality of all nodes in a PyDiGraph.
///
/// Betweenness centrality of a node :math:`v` is the sum of the
Expand Down
Loading