# Tutorial

## Compiling programs using igraph

The following short example program demonstrates the basic usage of the `igraph` library.

This example illustrates a couple of points. First, programs using the `igraph` library should include the `igraph.h` header file. Second, `igraph` uses the `igraph_real_t` type for real numbers instead of `double`. Third, `igraph` graph objects are represented by the `igraph_t` data type. Fourth, the `igraph_erdos_renyi_game()` creates a graph and `igraph_destroy()` destroys it, i.e. deallocates the memory associated to it.

For compiling this program you need a C compiler. Optionally, CMake can be used to automate the compilation.

### Compiling with CMake

It is convenient to use CMake because it can automatically discover the necessary compilation flags on all operating systems. Many IDEs support CMake, and can work with CMake projects directly. To create a CMake project for this example program, create a file name `CMakeLists.txt` with the following contents:

    cmake_minimum_required(VERSION 3.16)
    project(igraph_test)

    find_package(igraph REQUIRED)

    add_executable(igraph_test igraph_test.c)
    target_link_libraries(igraph_test PUBLIC igraph::igraph)

To compile the project, create a new directory called `build` in the root of the igraph source tree, and switch to it:

    mkdir build
    cd build

Run CMake to configure the project:

    cmake ..

If igraph was installed at a non-standard location, specify its prefix using the `-DCMAKE_PREFIX_PATH=...` option. The prefix must be the same directory that was specified as the `CMAKE_INSTALL_PREFIX` when compiling igraph.

If configuration has succeeded, build the program using

    cmake --build .

#### C++ has to be enabled in igraph projects

Parts of igraph are implemented in C++; therefore, any CMake target that depends on igraph should use the C++ linker. Furthermore, OpenMP support in igraph works correctly only if C++ is enabled in the CMake project. The script that finds igraph on the host machine will throw an error if C++ support is not enabled in the CMake project.

### Compiling without CMake

On most Unix-like systems, the default C compiler is called `cc`. To compile the test program, you will need a command similar to the following:

    cc igraph_test.c -I/usr/local/include/igraph -L/usr/local/lib -ligraph -o igraph_test

The exact form depends on where `igraph` was installed on your system, whether it was compiled as a shared or static library, and the external libraries it was linked to. The directory after the `-I` switch is the one containing the `igraph.h` file, while the one following `-L` should contain the library file itself, usually a file called `libigraph.a` (static library on macOS and Linux), `libigraph.so` (shared library on Linux), `libigraph.dylib` (shared library on macOS), `igraph.lib` (static library on Windows) or `igraph.dll` (shared library on Windows). If `igraph` was compiled as a static library, it is also necessary to manually link to all of its dependencies.

If your system has the `pkg-config` utility you are likely to get the necessary compile options by issuing the command

    pkg-config --libs --cflags igraph

(if `igraph` was built as a shared library) or

    pkg-config --static --libs --cflags igraph

(if `igraph` was built as a static library).

### Running the program

On most systems, the executable can be run by simply typing its name like this:

    ./igraph_test

If you use dynamic linking and the `igraph` library is not in a standard place, you may need to add its location to the `LD_LIBRARY_PATH` (Linux), `DYLD_LIBRARY_PATH` (macOS) or `PATH` (Windows) environment variables.

## Creating your first graphs

The functions generating graph objects are called graph generators. Stochastic (i.e. randomized) graph generators are called games.

`igraph` can handle directed and undirected graphs. Most graph generators are able to create both types of graphs and most other functions are usually also capable of handling both. E.g. `igraph_shortest_paths()` which (surprisingly) calculates shortest paths from a vertex to other vertices can calculate directed or undirected paths.

`igraph` has sophisticated ways for creating graphs. The simplest graphs are deterministic regular structures like star graphs (`igraph_star()`), ring graphs (`igraph_ring()`), lattices (`igraph_lattice()`) or trees (`igraph_tree()`).

The following example creates an undirected regular circular lattice, adds some random edges to it and calculates the average length of shortest paths between all pairs of vertices in the graph before and after adding the random edges. (The message is that some random edges can reduce path lengths a lot.)

This example illustrates some new points. `igraph` uses `igraph_vector_t` instead of plain C arrays. `igraph_vector_t` is superior to regular arrays in almost every sense. Vectors are created by the `igraph_vector_init()` function and, like graphs, they should be destroyed if not needed any more by calling `igraph_vector_destroy()` on them. A vector can be indexed by the `VECTOR()` function (right now it is a macro). Vectors can be resized, e.g. most `igraph` functions returning the result in a vector resize it to the size of the result.

`igraph_lattice()` takes a vector argument specifying the dimensions of the lattice. In this example we generate a 30x30 two dimensional lattice. See the documentation of `igraph_lattice()` in the reference manual for the other arguments.

The vertices in a graph are identified by an integer number between 0 and N-1, N is the number of vertices in the graph (this can be obtained by `igraph_vcount()`, as in the example).

The `igraph_add_edges()` function simply takes a graph and a vector of vertex ids defining the new edges. The first edge is between the first two vertex ids in the vector, the second edge is between the second two, etc. This way we add ten random edges to the lattice.

Note that in the example it is possible to add loop edges, edges pointing to the same vertex and multiple edges, more than one edge between the same pair of vertices. `igraph_t` can of course represent loops and multiple edges, although some routines expect simple graphs, i.e. graphs without loop and multiple edges, because for example some structural properties are ill-defined for non-simple graphs. Loop edges can be removed by calling `igraph_simplify()`.

## Calculating various properties of graphs

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.

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 to perform operations on a subset of vertices, you can read more about them in one of the following chapters.