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

Add Zachary's Karate Club #1280

Open
wants to merge 27 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
f21aef6
Karate club draft
IvanIsCoding Sep 8, 2024
559844a
Add Zachary's data
IvanIsCoding Sep 9, 2024
8c75e64
Consider weight for Karate Club
IvanIsCoding Sep 9, 2024
5a340da
Add tests
IvanIsCoding Sep 9, 2024
592a2d1
Add karate_club_graph signature
IvanIsCoding Sep 9, 2024
a00a645
Fix clippy
IvanIsCoding Sep 9, 2024
5e28875
Update rustworkx-core/src/generators/karate_club.rs
IvanIsCoding Sep 9, 2024
7554049
Add labels and documentation
IvanIsCoding Sep 14, 2024
a090e51
Merge remote-tracking branch 'origin/karate-club' into karate-club
IvanIsCoding Sep 14, 2024
f4e47ba
Test node labels
IvanIsCoding Sep 14, 2024
d0ae8cb
Add simple Rust docstring
IvanIsCoding Sep 14, 2024
90dc34c
Add release notes
IvanIsCoding Sep 14, 2024
a2eb3cc
Merge remote-tracking branch 'upstream/main' into karate-club
IvanIsCoding Sep 14, 2024
6b26741
Merge branch 'main' into karate-club
IvanIsCoding Sep 19, 2024
5f31a69
Merge branch 'main' into karate-club
IvanIsCoding Oct 1, 2024
5ae0b7e
Merge branch 'main' into karate-club
IvanIsCoding Oct 3, 2024
7267d46
Merge branch 'main' into karate-club
IvanIsCoding Oct 4, 2024
667de7e
Black changes
IvanIsCoding Oct 5, 2024
268c0d6
Update test to be independent
IvanIsCoding Oct 7, 2024
8ed085f
Apply suggestions from code review
IvanIsCoding Oct 7, 2024
9ccfe4a
Format and u8
IvanIsCoding Oct 7, 2024
3c73a8b
Ignore ruff for that file
IvanIsCoding Oct 7, 2024
128008c
Use adjacency list
IvanIsCoding Oct 8, 2024
a4c1492
Add some documentation
IvanIsCoding Oct 8, 2024
5891bc8
Merge branch 'main' into karate-club
IvanIsCoding Oct 11, 2024
b24acb4
Merge branch 'main' into karate-club
IvanIsCoding Oct 14, 2024
1fa2f74
Merge branch 'main' into karate-club
IvanIsCoding Oct 16, 2024
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
14 changes: 14 additions & 0 deletions releasenotes/notes/karate-club-35708b3838689a0b.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
features:
- |
Added a new function, :func:`~rustworkx.generators.karate_club_graph` that
returns Zachary's Karate Club graph, commonly found in social network examples.

.. jupyter-execute::

import rustworkx.generators
from rustworkx.visualization import mpl_draw

graph = rustworkx.generators.karate_club_graph()
layout = rustworkx.circular_layout(graph)
mpl_draw(graph, pos=layout)
126 changes: 126 additions & 0 deletions rustworkx-core/src/generators/karate_club.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.

use std::hash::Hash;

use petgraph::data::{Build, Create};
use petgraph::visit::{Data, NodeIndexable};

/// Generates Zachary's Karate Club graph.
///
/// Zachary's Karate Club graph is a well-known social network that represents
/// the relations between 34 members of a karate club.
IvanIsCoding marked this conversation as resolved.
Show resolved Hide resolved
/// Arguments:
///
/// * `default_node_weight` - A callable that will receive a boolean, indicating
/// if a node is part of Mr Hi's faction (True) or the Officer's faction (false).
/// It shoudl return the node weight according to the desired type.
/// * `default_edge_weight` - A callable that will receive the integer representing
/// the strenght of the relation between two nodes. It should return the edge
/// weight according to the desired type.
///
pub fn karate_club_graph<G, T, F, H, M>(mut default_node_weight: F, mut default_edge_weight: H) -> G
where
G: Build + Create + Data<NodeWeight = T, EdgeWeight = M> + NodeIndexable,
F: FnMut(bool) -> T,
H: FnMut(usize) -> M,
G::NodeId: Eq + Hash,
{
const N: usize = 34;
const M: usize = 78;
let mr_hi_members: [u8; 17] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 13, 16, 17, 19, 21];
let membership: std::collections::HashSet<u8> = mr_hi_members.into_iter().collect();

let adjacency_list: Vec<Vec<(usize, usize)>> = vec![
vec![],
vec![(0, 4)],
vec![(0, 5), (1, 6)],
vec![(0, 3), (1, 3), (2, 3)],
vec![(0, 3)],
vec![(0, 3)],
vec![(0, 3), (4, 2), (5, 5)],
vec![(0, 2), (1, 4), (2, 4), (3, 3)],
vec![(0, 2), (2, 5)],
vec![(2, 1)],
vec![(0, 2), (4, 3), (5, 3)],
vec![(0, 3)],
vec![(0, 1), (3, 3)],
vec![(0, 3), (1, 5), (2, 3), (3, 3)],
vec![],
vec![],
vec![(5, 3), (6, 3)],
vec![(0, 2), (1, 1)],
vec![],
vec![(0, 2), (1, 2)],
vec![],
vec![(0, 2), (1, 2)],
vec![],
vec![],
vec![],
vec![(23, 5), (24, 2)],
vec![],
vec![(2, 2), (23, 4), (24, 3)],
vec![(2, 2)],
vec![(23, 3), (26, 4)],
vec![(1, 2), (8, 3)],
vec![(0, 2), (24, 2), (25, 7), (28, 2)],
vec![
(2, 2),
(8, 3),
(14, 3),
(15, 3),
(18, 1),
(20, 3),
(22, 2),
(23, 5),
(29, 4),
(30, 3),
(31, 4),
],
vec![
(8, 4),
(9, 2),
(13, 3),
(14, 2),
(15, 4),
(18, 2),
(19, 1),
(20, 1),
(23, 4),
(26, 2),
(27, 4),
(28, 2),
(29, 2),
(30, 3),
(31, 4),
(32, 5),
(22, 3),
],
];

let mut graph = G::with_capacity(N, M);

let mut node_indices = Vec::with_capacity(N);
for (row, neighbors) in adjacency_list.into_iter().enumerate() {
let node_id = graph.add_node(default_node_weight(membership.contains(&(row as u8))));
node_indices.push(node_id);

for (neighbor, weight) in neighbors.into_iter() {
graph.add_edge(
node_indices[neighbor],
node_indices[row],
default_edge_weight(weight),
);
}
}
graph
}
2 changes: 2 additions & 0 deletions rustworkx-core/src/generators/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ mod grid_graph;
mod heavy_hex_graph;
mod heavy_square_graph;
mod hexagonal_lattice_graph;
mod karate_club;
mod lollipop_graph;
mod path_graph;
mod petersen_graph;
Expand Down Expand Up @@ -55,6 +56,7 @@ pub use grid_graph::grid_graph;
pub use heavy_hex_graph::heavy_hex_graph;
pub use heavy_square_graph::heavy_square_graph;
pub use hexagonal_lattice_graph::{hexagonal_lattice_graph, hexagonal_lattice_graph_weighted};
pub use karate_club::karate_club_graph;
pub use lollipop_graph::lollipop_graph;
pub use path_graph::path_graph;
pub use petersen_graph::petersen_graph;
Expand Down
1 change: 1 addition & 0 deletions rustworkx/generators/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,4 @@ def directed_complete_graph(
multigraph: bool = ...,
) -> PyDiGraph: ...
def dorogovtsev_goltsev_mendes_graph(n: int) -> PyGraph: ...
def karate_club_graph(multigraph: bool = ...) -> PyGraph: ...
54 changes: 54 additions & 0 deletions src/generators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1715,6 +1715,59 @@ pub fn dorogovtsev_goltsev_mendes_graph(py: Python, n: usize) -> PyResult<graph:
})
}

/// Generates Zachary's Karate Club graph.
///
/// Zachary's Karate Club graph is a well-known social network that represents
/// the relations between 34 members of a karate club, as described in the
/// paper by Zachary [1]_.
///
/// The graph is undirected and contains 34 nodes (members) and 78 edges
/// (interactions). Each node represents a club member, and each edge represents
/// a relationship between two members.
IvanIsCoding marked this conversation as resolved.
Show resolved Hide resolved
///
/// :param bool multigraph: When set to ``False`` the output
/// :class:`~rustworkx.PyGraph` object will not be not be a multigraph and
/// won't allow parallel edges to be added. Instead
/// calls which would create a parallel edge will update the existing edge.
///
/// :returns: The undirected graph representing the Karate club graph.
/// Each node is labeled according to its assigned faction.
///
/// :rtype: PyGraph
///
/// .. jupyter-execute::
///
/// import rustworkx.generators
/// from rustworkx.visualization import mpl_draw
///
/// graph = rustworkx.generators.karate_club_graph()
/// layout = rustworkx.circular_layout(graph)
/// mpl_draw(graph, pos=layout)
///
/// .. [1] Zachary, Wayne W.
/// "An information flow model for conflict and fission in small groups"
/// Journal of Anthropological Research, 33, 452-473.
/// https://doi.org/10.1086/jar.33.4.3629752
#[pyfunction]
#[pyo3(
signature=(multigraph=true),
)]
pub fn karate_club_graph(py: Python, multigraph: bool) -> PyResult<graph::PyGraph> {
let default_node_fn = |w: bool| match w {
true => "Mr. Hi".to_object(py),
false => "Officer".to_object(py),
};
let default_edge_fn = |w: usize| (w as f64).to_object(py);
let graph: StablePyGraph<Undirected> =
core_generators::karate_club_graph(default_node_fn, default_edge_fn);
Ok(graph::PyGraph {
graph,
node_removed: false,
multigraph,
attrs: py.None(),
})
}

#[pymodule]
pub fn generators(_py: Python, m: &Bound<PyModule>) -> PyResult<()> {
m.add_wrapped(wrap_pyfunction!(cycle_graph))?;
Expand Down Expand Up @@ -1744,5 +1797,6 @@ pub fn generators(_py: Python, m: &Bound<PyModule>) -> PyResult<()> {
m.add_wrapped(wrap_pyfunction!(complete_graph))?;
m.add_wrapped(wrap_pyfunction!(directed_complete_graph))?;
m.add_wrapped(wrap_pyfunction!(dorogovtsev_goltsev_mendes_graph))?;
m.add_wrapped(wrap_pyfunction!(karate_club_graph))?;
Ok(())
}
Loading