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 29 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
29 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
cdf4776
Merge branch 'main' into karate-club
IvanIsCoding Oct 31, 2024
812e1e1
Merge branch 'main' into karate-club
IvanIsCoding Nov 10, 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
89 changes: 89 additions & 0 deletions rustworkx-core/src/generators/karate_club.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// 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};

use super::InvalidInputError;

// Adapted from NetworkX, licensend under the MIT license
IvanIsCoding marked this conversation as resolved.
Show resolved Hide resolved
// https://github.com/networkx/networkx/blob/409979eff35f02eff54f4eea3731736bd431dc2e/networkx/generators/social.py#L49
const ZACHARY: &str = "\
IvanIsCoding marked this conversation as resolved.
Show resolved Hide resolved
0 4 5 3 3 3 3 2 2 0 2 3 2 3 0 0 0 2 0 2 0 2 0 0 0 0 0 0 0 0 0 2 0 0\n\
4 0 6 3 0 0 0 4 0 0 0 0 0 5 0 0 0 1 0 2 0 2 0 0 0 0 0 0 0 0 2 0 0 0\n\
5 6 0 3 0 0 0 4 5 1 0 0 0 3 0 0 0 0 0 0 0 0 0 0 0 0 0 2 2 0 0 0 3 0\n\
3 3 3 0 0 0 0 3 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 0 0 0 0 0 3 0 0 0 0 0 3 3 0 0 1 0 3 0 2 5 0 0 0 0 0 4 3 4 0 5\n\
0 0 0 0 0 0 0 0 4 2 0 0 0 3 2 4 0 0 2 1 1 0 3 4 0 0 2 4 2 2 3 4 5 0";

pub fn karate_club_graph<G, T, F, H, M>(
mut default_node_weight: F,
mut default_edge_weight: H,
) -> Result<G, InvalidInputError>
IvanIsCoding marked this conversation as resolved.
Show resolved Hide resolved
where
G: Build + Create + Data<NodeWeight = T, EdgeWeight = M> + NodeIndexable,
F: FnMut() -> T,
H: FnMut(usize) -> M,
G::NodeId: Eq + Hash,
{
let mut graph = G::with_capacity(0, 0);
IvanIsCoding marked this conversation as resolved.
Show resolved Hide resolved
let mut node_indices = Vec::new();
IvanIsCoding marked this conversation as resolved.
Show resolved Hide resolved
for (row, line) in ZACHARY.split('\n').enumerate() {
let node_id = graph.add_node(default_node_weight());
node_indices.push(node_id);
let this_row: Vec<usize> = line
.split_whitespace()
.map(|b| b.parse::<usize>().unwrap())
.collect();

for (col, entry) in this_row.iter().enumerate() {
if *entry >= 1 && row > col {
graph.add_edge(
node_indices[col],
node_indices[row],
default_edge_weight(*entry),
);
}
}
}
Ok(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 @@ -131,3 +131,4 @@ def directed_complete_graph(
multigraph: bool = ...,
) -> PyDiGraph: ...
def dorogovtsev_goltsev_mendes_graph(n: int) -> PyGraph: ...
def karate_club_graph(multigraph: bool = ...) -> PyGraph: ...
21 changes: 21 additions & 0 deletions src/generators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1715,6 +1715,26 @@ pub fn dorogovtsev_goltsev_mendes_graph(py: Python, n: usize) -> PyResult<graph:
})
}

#[pyfunction]
#[pyo3(
signature=(multigraph=true),
)]
pub fn karate_club_graph(py: Python, multigraph: bool) -> PyResult<graph::PyGraph> {
let default_node_fn = || py.None();
let default_edge_fn = |w: usize| (w as f64).to_object(py);
let graph: StablePyGraph<Undirected> =
match core_generators::karate_club_graph(default_node_fn, default_edge_fn) {
Ok(graph) => graph,
Err(_) => return Err(PyValueError::new_err("error generating Karate club graph")),
};
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 +1764,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(())
}
34 changes: 34 additions & 0 deletions tests/graph/test_karate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# 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.

import unittest

import rustworkx as rx
import networkx as nx


class TestKarate(unittest.TestCase):
def test_isomorphic_to_networkx(self):
IvanIsCoding marked this conversation as resolved.
Show resolved Hide resolved
graph = rx.generators.karate_club_graph()
nx_graph = rx.networkx_converter(nx.karate_club_graph())

def edge_matcher(a, b):
if isinstance(a, dict):
a, b, = (
b,
a,
)
return a == b["weight"]

self.assertTrue(
rx.is_isomorphic(graph, nx_graph, node_matcher=None, edge_matcher=edge_matcher)
)