03 lab igraph1

Upload: samuel-ramirez

Post on 11-Oct-2015

35 views

Category:

Documents


0 download

TRANSCRIPT

  • Introducing the igraph library

    Course: Complex Network Analysis mit GNU R

    Prof. Dr. Claudia Mller-BirnInstitute for Computer Science, Networked Information Systems

    February 22, 2012

    This chapter is mainly based on the documentation written by Gbor Csrdi, Tams Nepusz: The igraph software package for complex network research. InterJournal Complex Systems, 1695, 2006.

  • February 2012Claudia Mller-Birn, Lecture: Complex Network Analysis mit R

    igraph is a free software package for creating and manipulating undirected and directed graphs

    Contains the implementation of a lot graph algorithms

    Provides a platform for the developing and/or implementing graph algorithms

    Website with documentation and download http://cneurocvs.rmki.kfki.hu/igraph/index.html

    2

    Introduction

  • February 2012Claudia Mller-Birn, Lecture: Complex Network Analysis mit R

    Installation igraph R package

    A recent GNU R version is recommended

    To compile the R package from source, you'll need a C and C++ compiler

    Optional - libxml2 library, for reading GraphML files (included in Windows builds).- GMP library, graph automorphisms (not included in Windows builds).

    Suggested packages: - stats4, rgl, tcltk, RSQLite, digest, graph, Matrix

    3

  • February 2012Claudia Mller-Birn, Lecture: Complex Network Analysis mit R

    Data model

    igraph graph is either directed or undirected and it cannot handle mixed graphs (no direct support for bipartite (two-mode) graphs)

    Directed graphs- An ordered multiset of directed edges and additional meta dat- |V| is the number of vertices, therefore the number of vertices is an integer number

    between zero and |V|-1- Edge ids are integer numbers between zero and |E|-1, |E| is the number of edges

    Undirected graphs- Undirected graph is an ordered multiset of undirected edges, plus the meta data- Undirected igraph edge is a two-element (non-loop) or one-element (loop) set of vertex

    ids

    4

  • February 2012Claudia Mller-Birn, Lecture: Complex Network Analysis mit R

    Data model (cont.)

    5

    Example directed graph Example undirected graph

  • February 2012Claudia Mller-Birn, Lecture: Complex Network Analysis mit R

    Graph objects

    igraph uses graph objects to represent graphs and provides functions to create and manipulate these objects

    Graph objects have a R class igraph Check that an R object is an igraph graph object with the is.igraph

    function

    Some available functions to create graph objects

    Example

    6

    0

    1

    2

    3

    4

    5

    6

    graph()graph.star()graph.tree()graph.adjacency()

    > g

  • Claudia Mller-Birn, Lecture: Complex Network Analysis mit R February 2012

    Introducing the igraph libraryGraph structures, edge lists and adjacency matrices

    7

    This chapter is mainly based on the documentation written by Gbor Csrdi, Tams Nepusz: The igraph software package for complex network research. InterJournal Complex Systems, 1695, 2006.

  • February 2012Claudia Mller-Birn, Lecture: Complex Network Analysis mit R

    Generating graph structures

    Empty graphs

    Full graphs

    Stars and Rings

    Other structures

    8

    0

    1

    2

    3

    4 5

    6

    78

    9

    10

    11

    12

    13

    14

    0

    1

    23

    4

    5

    6

    7 8

    9

    0

    1

    2

    3

    4 5

    6

    7

    8

    9

    > e e f f s s s r r r graph.lattice()> graph.tree()

  • February 2012Claudia Mller-Birn, Lecture: Complex Network Analysis mit R

    Edge lists and adjacency matrices

    Edge list> g1 edgelist graph(t(edgelist))

    inverse operation: > get.edgelist()

    Create adjacency matrix from igraph graph> g get.adjacency(g)

    Create igraph graph from adjacency matrix> graph.adjacency(g)

    9

  • February 2012Claudia Mller-Birn, Lecture: Complex Network Analysis mit R

    Real world example

    Situation: your data are not in an graph format

    Edit data> dat g V(g)$name> E(g)$weight

    10

  • February 2012Claudia Mller-Birn, Lecture: Complex Network Analysis mit R

    Real world example (cont.)

    Your data are saved in CSV formatImport your data > traits rel g g V(g)$name #check

    Reveal names > names ids names(ids)

  • Claudia Mller-Birn, Lecture: Complex Network Analysis mit R February 2012

    Introducing the igraph libraryBasic measures

    12

    This chapter is mainly based on the documentation written by Gbor Csrdi, Tams Nepusz: The igraph software package for complex network research. InterJournal Complex Systems, 1695, 2006.

  • February 2012Claudia Mller-Birn, Lecture: Complex Network Analysis mit R 13

    Functions for exploring the basic structure of a network

    Number of vertices in the graph vcount(g)

    List of all nodes V(g)

    Access the first node V(g)[0]

    List of all vertex attributes names list.vertex.attributes(g)

    Access the attribute 'id' for the first node V(g)[0]$id

    Number of edges in the graph ecount()

    List of all edges/arcs E(g)

    Checks if the graph is directed is.directed()

  • February 2012Claudia Mller-Birn, Lecture: Complex Network Analysis mit R

    Shortest Path

    Shortest Path > shortest.paths(graph, v=V(graph))

    - Arguments- mode: Character constant, gives whether the shortest paths to or from the given vertices should

    be calculated for directed graphs. If out then the shortest paths from the vertex, if in then to it will be considered. If all, the default, then the corresponding undirected graph will be used, ie. not directed paths are searched. This argument is ignored for undirected graphs.

    - from: Numeric constant, the vertex from or to the shortest paths will be calculated. Note that right now this is not a vector of vertex ids, but only a single vertex.

    - to: Numeric vector, only the shortest paths to these vertices will be calculated. De- faults to all vertices.

    - directed: Whether to consider directed paths in directed graphs, this argument is ignored for undirected graphs.

    - unconnected: What to do if the graph is unconnected (not strongly connected if directed paths are considered). If TRUE only the lengths of the existing paths are considered and averaged; if FALSE the length of the missing paths are counted having length vcount(graph), one longer than the longest possible geodesic in the network.

    14

  • February 2012Claudia Mller-Birn, Lecture: Complex Network Analysis mit R

    Degree and degree distribution

    Degree> degree(g)

    Degree distribution> degree.distribution(graph, cumulative = FALSE, ...)> plot(degree.distribution(graph), xlab="degree", ylab="frequency", log="xy", pch=3, col=3)

    Arguments- v: The ids of vertices of which the degree will be calculated.- mode: Character string, out for out-degree, in for in-degree or total for the sum of

    the two. For undirected graphs this argument is ignored.- loops: Logical; whether the loop edges are also counted.- cumulative: Logical; whether the cumulative degree distribution is to be calculated.

    15

  • February 2012Claudia Mller-Birn, Lecture: Complex Network Analysis mit R

    Closeness and Betweenness measure

    Closeness > closeness(graph, v=V(graph), mode = "all")

    - Arguments:- v: The vertices for which closeness will be calculated.- mode: Character string, defined the types of the paths used for measuring the distance in

    directed graphs. in measures the paths to a vertex, out measures paths from a vertex, all uses undirected paths. This argument is ignored for undirected graphs.

    Betweeness> betweenness(graph, v=V(graph), directed = TRUE)

    - Arguments:- v: The vertices for which the vertex betweenness will be calculated.- directed: Logical, whether directed paths should be considered while determining the shortest

    paths.

    16

  • Claudia Mller-Birn, Lecture: Complex Network Analysis mit R February 2012

    Homework assignment 3

    17

  • February 2012Claudia Mller-Birn, Lecture: Complex Network Analysis mit R

    Homework assignment 3: Basic network measures

    1. Install igraph library

    2. Apply the during class learned basic measures on the provided data set. (Please consider the following measures: number of vertices, number of edges, average path length, mean degree). What can you say about the network? Please provide your results in a table.Import the file by using the following function:

    g