-
Notifications
You must be signed in to change notification settings - Fork 65
implemented Erdos-Renyi model generation #2253
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| //! Generates a graph using the Erdős-Rényi model | ||
| //! | ||
| //! # Examples | ||
| //! | ||
| //! ``` | ||
| //! use raphtory::graphgen::erdos_renyi::erdos_renyi; | ||
| //! let graph = erdos_renyi(1000, 0.1, None).unwrap(); | ||
| //! ``` | ||
|
|
||
| use crate::{ | ||
| db::{ | ||
| api::{mutation::AdditionOps, view::*}, | ||
| graph::graph::Graph, | ||
| }, errors::GraphError, prelude::{NO_PROPS, NodeStateOps} | ||
| }; | ||
| use rand::{rngs::StdRng, Rng, SeedableRng}; | ||
| use raphtory_api::core::storage::timeindex::AsTime; | ||
| use raphtory_core::entities::GID; | ||
| use tracing::error; | ||
|
|
||
| /// Generates an Erdős-Rényi random graph and returns it. | ||
| /// | ||
| /// The Erdős-Rényi model creates a random graph by connecting each pair of nodes | ||
| /// with a given probability. This implementation creates an undirected graph. | ||
| /// | ||
| /// # Arguments | ||
| /// * `nodes_to_add` - Number of nodes to create in the graph. | ||
| /// * `p` - Probability of edge creation between any two nodes (0.0 = no edges, 1.0 = fully connected). | ||
| /// * `seed` - Optional 64-bit seed for deterministic random generation. If `None`, uses entropy. | ||
| /// | ||
| /// # Returns | ||
| /// * `Result<Graph, GraphError>` - A new graph with the generated nodes and edges. | ||
| /// | ||
| /// # Behavior | ||
| /// - Creates a new graph and adds `nodes_to_add` nodes with sequential u64 IDs (0, 1, 2, ...). | ||
| /// - For each pair of distinct nodes, adds an undirected edge with probability `p`. | ||
| /// - Uses the provided seed for reproducible random generation if given. | ||
| /// - All nodes and edges are timestamped with incrementing time values. | ||
| /// | ||
| /// # Example | ||
| /// ``` | ||
| /// use raphtory::graphgen::erdos_renyi::erdos_renyi; | ||
| /// | ||
| /// // Create a random graph with 10 nodes and 20% edge probability | ||
| /// let graph = erdos_renyi(10, 0.2, Some(42)).unwrap(); | ||
| /// ``` | ||
| pub fn erdos_renyi(nodes_to_add: usize, p: f64, seed: Option<u64>) -> Result<Graph, GraphError> { | ||
| let graph = Graph::new(); | ||
| let mut rng; | ||
| if let Some(seed_value) = seed { | ||
| rng = StdRng::seed_from_u64(seed_value); | ||
| } else { | ||
| rng = StdRng::from_entropy(); | ||
| } | ||
| let mut latest_time = graph.latest_time().map_or(0, |t| t.t()); | ||
| for i in 0..nodes_to_add { | ||
| let id = GID::U64(i as u64); | ||
| latest_time += 1; | ||
| graph | ||
| .add_node(latest_time, &id, NO_PROPS, None)?; | ||
| } | ||
| for i in 0..nodes_to_add { | ||
| let source_id = GID::U64(i as u64); | ||
| for j in (i + 1)..nodes_to_add { | ||
| let dst_id = GID::U64(j as u64); | ||
| let create_edge = rng.gen_bool(p); | ||
| if create_edge { | ||
| latest_time += 1; | ||
| graph.add_edge(latest_time, &source_id, &dst_id, NO_PROPS, None)?; | ||
DanielLacina marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| graph.add_edge(latest_time, &dst_id, &source_id, NO_PROPS, None)?; | ||
| } | ||
| } | ||
| } | ||
| Ok(graph) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use crate::graphgen::erdos_renyi::erdos_renyi; | ||
|
|
||
| #[test] | ||
| fn test_erdos_renyi_half_probability() { | ||
| let n_nodes = 20; | ||
| let p = 0.5; | ||
| let seed = Some(42); | ||
| let graph = erdos_renyi(n_nodes, p, seed).unwrap(); | ||
| let node_count = graph.nodes().id().iter_values().count(); | ||
| let edge_count = graph.edges().into_iter().count(); | ||
| assert_eq!(node_count, n_nodes); | ||
| assert!(edge_count > 0); | ||
| assert!(edge_count <= n_nodes * (n_nodes - 1)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_erdos_renyi_zero_probability() { | ||
| let n_nodes = 20; | ||
| let p = 0.0; | ||
| let seed = Some(42); | ||
| let graph = erdos_renyi(n_nodes, p, seed).unwrap(); | ||
| let edge_count = graph.edges().into_iter().count(); | ||
| let node_count = graph.nodes().id().iter_values().count(); | ||
| assert_eq!(node_count, n_nodes); | ||
| assert_eq!(edge_count, 0); | ||
DanielLacina marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| #[test] | ||
| fn test_erdos_renyi_full_probability() { | ||
| let n_nodes = 20; | ||
| let p = 1.0; | ||
| let seed = Some(42); | ||
| let graph = erdos_renyi(n_nodes, p, seed).unwrap(); | ||
| let edge_count = graph.edges().into_iter().count(); | ||
| let node_count = graph.nodes().id().iter_values().count(); | ||
| assert_eq!(node_count, n_nodes); | ||
| assert_eq!(edge_count, (n_nodes * (n_nodes - 1))); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.