| igraph Reference Manual |
|---|
In our next example we will calculate various centrality measures in a friendship graph. The friendship graph is from the famous Zachary karate club study. (Web search on 'Zachary karate' if you want to know more about this.) Centrality measures quantify how central is the position of individual vertices in the graph.
#include <igraph.h>
int main(void) {
igraph_t graph;
igraph_vector_t v;
igraph_vector_t result;
igraph_real_t edges[] = { 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_view(&v, edges, sizeof(edges)/sizeof(double));
igraph_create(&graph, &v, 0, IGRAPH_UNDIRECTED);
igraph_vector_init(&result, 0);
igraph_degree(&graph, &result, igraph_vss_all(), IGRAPH_ALL,
IGRAPH_LOOPS);
printf("Maximum degree is %10i, vertex %2i.\n",
(int)igraph_vector_max(&result), (int)igraph_vector_which_max(&result));
igraph_closeness(&graph, &result, igraph_vss_all(), IGRAPH_ALL);
printf("Maximum closeness is %10f, vertex %2i.\n",
(double)igraph_vector_max(&result), (int)igraph_vector_which_max(&result));
igraph_betweenness(&graph, &result, igraph_vss_all(),
IGRAPH_UNDIRECTED);
printf("Maximum betweenness is %10f, vertex %2i.\n",
(double)igraph_vector_max(&result), (int)igraph_vector_which_max(&result));
igraph_vector_destroy(&result);
igraph_destroy(&graph);
return 0;
}
This example reflects some new features. First of all, it shows a
way to define a graph simply as defining a C array with its edges.
Function igraph_vector_view()
creates a view of a C
array. It does not copy any data, this also means that you should not
call igraph_vector_destroy()
on a vector created this way. This vector is then used to create the
undirected graph.
Then the degree, closeness and betweenness centrality of the vertices
is calculated and the highest values are printed. Note that the vector
(result) which returns the result from these
functions has to be initialized first, and also that the functions resize
it to be able to hold the result.
The igraph_vss_all() argument tells the functions to
calculate the property for every vertex in the graph, it is shorthand
for a vertex selector (igraph_vs_t).
Vertex selectors help performing operations on a subset of vertices,
you can read more about them in one
of the following chapters.
| << 2. Lesson 2. Creating your first graphs. | Chapter 4. About igraph graphs, the basic interface >> |