Skip to main content

example_3

Function example_3 

Source
pub fn example_3() -> Result<KarateCentralities>
Expand description

Lesson 3: degree, closeness and betweenness centrality in Zachary’s karate club.

Creates the undirected friendship graph from ZACHARY_KARATE_EDGES and returns, for each of the three centrality measures, its maximum and the first vertex attaining it:

  • degree (all neighbors, loops counted): 17, the administrator (vertex 33);
  • closeness (non-normalized, 1 / Σ distances): 1/58 ≈ 0.0172414, the instructor (vertex 0);
  • betweenness (non-normalized): ≈ 231.071, the instructor again.

It translates examples/tutorial/tutorial3.c of the third lesson.

§C code

#include <igraph.h>

int main(void) {
    igraph_t graph;
    igraph_vector_int_t result;
    igraph_vector_t result_real;
    igraph_int_t edges_array[] = {
        0,1, 0,2, 0,3, 0,4, 0,5, 0,6, 0,7, 0,8,
        0,10, 0,11, 0,12, 0,13, 0,17, 0,19, 0,21, 0,31,
        1, 2, 1, 3, 1, 7, 1,13, 1,17, 1,19, 1,21, 1,30,
        2, 3, 2, 7, 2,27, 2,28, 2,32, 2, 9, 2, 8, 2,13,
        3, 7, 3,12, 3,13, 4, 6, 4,10, 5, 6, 5,10, 5,16,
        6,16, 8,30, 8,32, 8,33, 9,33, 13,33, 14,32, 14,33,
        15,32, 15,33, 18,32, 18,33, 19,33, 20,32, 20,33,
        22,32, 22,33, 23,25, 23,27, 23,32, 23,33, 23,29,
        24,25, 24,27, 24,31, 25,31, 26,29, 26,33, 27,33,
        28,31, 28,33, 29,32, 29,33, 30,32, 30,33, 31,32,
        31,33, 32,33
    };
    igraph_vector_int_t edges =
        igraph_vector_int_view(edges_array, sizeof(edges_array) / sizeof(edges_array[0]));

    /* Initialize the library. */
    igraph_setup();

    igraph_create(&graph, &edges, 0, IGRAPH_UNDIRECTED);

    igraph_vector_int_init(&result, 0);
    igraph_vector_init(&result_real, 0);

    igraph_degree(&graph, &result, igraph_vss_all(), IGRAPH_ALL, IGRAPH_LOOPS);
    printf("Maximum degree is      %10" IGRAPH_PRId ", vertex %2" IGRAPH_PRId ".\n",
           igraph_vector_int_max(&result),
           igraph_vector_int_which_max(&result));

    igraph_closeness(&graph, &result_real, NULL, NULL, igraph_vss_all(),
                     IGRAPH_ALL, /* weights= */ NULL, /* normalized= */ false);
    printf("Maximum closeness is   %10g, vertex %2" IGRAPH_PRId ".\n",
           (double) igraph_vector_max(&result_real),
           igraph_vector_which_max(&result_real));

    igraph_betweenness(&graph, /* weights= */ NULL, &result_real, igraph_vss_all(),
                       IGRAPH_UNDIRECTED, /* normalized= */ false);
    printf("Maximum betweenness is %10g, vertex %2" IGRAPH_PRId ".\n",
           (double) igraph_vector_max(&result_real),
           igraph_vector_which_max(&result_real));

    igraph_vector_int_destroy(&result);
    igraph_vector_destroy(&result_real);
    igraph_destroy(&graph);

    return 0;
}

§Rust translation

The body of this function, step by step: the result vectors are plain Vecs returned by the methods, and Maximum::of plays the role of igraph_vector_max() plus igraph_vector_which_max().

use igraph::prelude::*;
use igraph::tutorial::{Maximum, ZACHARY_KARATE_EDGES};

// `0` vertices: igraph infers the vertex count from the largest id.
let graph = Graph::from_flat_edges(&ZACHARY_KARATE_EDGES, 0, false)?;

let degree = graph.degree(VertexSelector::All, NeighborMode::All, Loops::Twice)?;
let max_degree = Maximum::of(&degree).unwrap();
assert_eq!(max_degree, Maximum { value: 17, vertex: 33 });

let closeness = graph.closeness(VertexSelector::All, NeighborMode::All, None, false)?;
let max_closeness = Maximum::of(&closeness).unwrap();
assert_eq!(max_closeness.vertex, 0);
assert!((max_closeness.value - 1.0 / 58.0).abs() < 1e-15);

let betweenness = graph.betweenness(None, VertexSelector::All, false, false)?;
let max_betweenness = Maximum::of(&betweenness).unwrap();
assert_eq!(max_betweenness.vertex, 0);
assert!((max_betweenness.value - 231.0714285714286).abs() < 1e-9);

// ... which is what `example_3` returns.
let res = igraph::tutorial::example_3()?;
assert_eq!(res.degree, max_degree);
assert_eq!(res.to_string(), "\
Maximum degree is              17, vertex 33.
Maximum closeness is    0.0172414, vertex  0.
Maximum betweenness is    231.071, vertex  0.");

§Errors

Propagates the errors of the igraph calls (none are expected).