-
Notifications
You must be signed in to change notification settings - Fork 177
Add Zachary's Karate Club #1280
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
30 commits
Select commit
Hold shift + click to select a range
f21aef6
Karate club draft
IvanIsCoding 559844a
Add Zachary's data
IvanIsCoding 8c75e64
Consider weight for Karate Club
IvanIsCoding 5a340da
Add tests
IvanIsCoding 592a2d1
Add karate_club_graph signature
IvanIsCoding a00a645
Fix clippy
IvanIsCoding 5e28875
Update rustworkx-core/src/generators/karate_club.rs
IvanIsCoding 7554049
Add labels and documentation
IvanIsCoding a090e51
Merge remote-tracking branch 'origin/karate-club' into karate-club
IvanIsCoding f4e47ba
Test node labels
IvanIsCoding d0ae8cb
Add simple Rust docstring
IvanIsCoding 90dc34c
Add release notes
IvanIsCoding a2eb3cc
Merge remote-tracking branch 'upstream/main' into karate-club
IvanIsCoding 6b26741
Merge branch 'main' into karate-club
IvanIsCoding 5f31a69
Merge branch 'main' into karate-club
IvanIsCoding 5ae0b7e
Merge branch 'main' into karate-club
IvanIsCoding 7267d46
Merge branch 'main' into karate-club
IvanIsCoding 667de7e
Black changes
IvanIsCoding 268c0d6
Update test to be independent
IvanIsCoding 8ed085f
Apply suggestions from code review
IvanIsCoding 9ccfe4a
Format and u8
IvanIsCoding 3c73a8b
Ignore ruff for that file
IvanIsCoding 128008c
Use adjacency list
IvanIsCoding a4c1492
Add some documentation
IvanIsCoding 5891bc8
Merge branch 'main' into karate-club
IvanIsCoding b24acb4
Merge branch 'main' into karate-club
IvanIsCoding 1fa2f74
Merge branch 'main' into karate-club
IvanIsCoding cdf4776
Merge branch 'main' into karate-club
IvanIsCoding 812e1e1
Merge branch 'main' into karate-club
IvanIsCoding 911faff
Update tests/graph/test_karate.py
mtreinish 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,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) |
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,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. | ||
/// 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 | ||
} |
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
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
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
Oops, something went wrong.
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.