### igraph Installation Prompt Source: https://r.igraph.org/articles/installation-troubleshooting.html This is an example of the prompt you might see when a binary version is available but a newer source version exists. Respond 'no' to install the binary version. ```text > install.packages('igraph') There is a binary version available but the source version is later: binary source needs_compilation igraph 1.2.7 1.2.8 TRUE ``` -------------------------------- ### Install All Dependencies Source: https://r.igraph.org/AGENTS.html Installs all necessary dependencies for the project using the pak package manager. Use this command to set up your development environment. ```R pak::pak() ``` -------------------------------- ### List Vertex Attribute Names Source: https://r.igraph.org/reference/vertex_attr_names.html This example demonstrates how to create a graph, set vertex attributes, and then retrieve their names using vertex_attr_names. Ensure the igraph package is installed and loaded. ```R g <- make_ring(10) %>% set_vertex_attr("name", value = LETTERS[1:10]) %>% set_vertex_attr("color", value = rep("green", 10)) vertex_attr_names(g) #> [1] "name" "color" plot(g) ``` -------------------------------- ### Create a tree and get its Prüfer sequence Source: https://r.igraph.org/reference/make_from_prufer.html This example demonstrates how to create a tree graph and then convert it into its Prüfer sequence using `make_tree` and `to_prufer`. ```R g <- make_tree(13, 3) to_prufer(g) #> [1] 2 2 2 1 3 3 3 1 4 4 4 ``` -------------------------------- ### Infomap Community Finding Example Source: https://r.igraph.org/reference/cluster_infomap.html This example demonstrates how to use `cluster_infomap` to find communities in Zachary's karate club graph. It shows how to get the membership vector and the communities themselves. ```R ## Zachary's karate club g <- make_graph("Zachary") imc <- cluster_infomap(g) membership(imc) #> [1] 1 1 1 1 2 2 2 1 3 1 2 1 1 1 3 3 2 1 3 1 3 1 3 3 3 3 3 3 3 3 3 3 3 3 communities(imc) #> $`1` #> [1] 1 2 3 4 8 10 12 13 14 18 20 22 #> #> $`2` #> [1] 5 6 7 11 17 #> #> $`3` #> [1] 9 15 16 19 21 23 24 25 26 27 28 29 30 31 32 33 34 #> ``` -------------------------------- ### Install build-essential and gfortran on Fedora Linux Source: https://r.igraph.org/articles/installation-troubleshooting.html Use this command to install the necessary C, C++, and Fortran compilers and essential build tools on Fedora Linux. ```bash yum install gcc gcc-c++ gcc-gfortran ``` -------------------------------- ### Example Vertex Sequences Source: https://r.igraph.org/reference/weighted_cliques.html These examples show different vertex sequences that can be generated or returned by igraph functions, illustrating subsets of vertices. ```R #> ── 2/34 · from 142fc3f ─────────────────────────────────────── #> [1] 1 2 ``` ```R #> [[129]] #> ── 3/34 · from 142fc3f ─────────────────────────────────────── #> [1] 1 2 3 ``` ```R #> [[130]] #> ── 4/34 · from 142fc3f ─────────────────────────────────────── #> [1] 1 2 3 4 ``` ```R #> [[131]] #> ── 5/34 · from 142fc3f ─────────────────────────────────────── #> [1] 1 2 3 4 14 ``` ```R #> [[132]] #> ── 5/34 · from 142fc3f ─────────────────────────────────────── #> [1] 1 2 3 4 8 ``` ```R #> [[133]] #> ── 4/34 · from 142fc3f ─────────────────────────────────────── #> [1] 1 2 3 14 ``` ```R #> [[134]] #> ── 4/34 · from 142fc3f ─────────────────────────────────────── #> [1] 1 2 3 8 ``` ```R #> [[135]] #> ── 3/34 · from 142fc3f ─────────────────────────────────────── #> [1] 1 2 4 ``` ```R #> [[136]] #> ── 4/34 · from 142fc3f ─────────────────────────────────────── #> [1] 1 2 4 14 ``` ```R #> [[137]] #> ── 4/34 · from 142fc3f ─────────────────────────────────────── #> [1] 1 2 4 8 ``` ```R #> [[138]] #> ── 3/34 · from 142fc3f ─────────────────────────────────────── #> [1] 1 2 14 ``` ```R #> [[139]] #> ── 3/34 · from 142fc3f ─────────────────────────────────────── #> [1] 1 2 18 ``` ```R #> [[140]] #> ── 3/34 · from 142fc3f ─────────────────────────────────────── #> [1] 1 2 20 ``` ```R #> [[141]] #> ── 3/34 · from 142fc3f ─────────────────────────────────────── #> [1] 1 2 22 ``` ```R #> [[142]] #> ── 3/34 · from 142fc3f ─────────────────────────────────────── #> [1] 1 2 8 ``` ```R #> [[143]] #> ── 2/34 · from 142fc3f ─────────────────────────────────────── #> [1] 1 3 ``` ```R #> [[144]] #> ── 3/34 · from 142fc3f ─────────────────────────────────────── #> [1] 1 3 4 ``` ```R #> [[145]] #> ── 4/34 · from 142fc3f ─────────────────────────────────────── #> [1] 1 3 4 14 ``` ```R #> [[146]] #> ── 4/34 · from 142fc3f ─────────────────────────────────────── #> [1] 1 3 4 8 ``` ```R #> [[147]] #> ── 3/34 · from 142fc3f ─────────────────────────────────────── #> [1] 1 3 14 ``` ```R #> [[148]] #> ── 3/34 · from 142fc3f ─────────────────────────────────────── #> [1] 1 3 9 ``` ```R #> [[149]] #> ── 3/34 · from 142fc3f ─────────────────────────────────────── #> [1] 1 3 8 ``` ```R #> [[150]] #> ── 2/34 · from 142fc3f ─────────────────────────────────────── #> [1] 1 4 ``` ```R #> [[151]] #> ── 3/34 · from 142fc3f ─────────────────────────────────────── #> [1] 1 4 14 ``` ```R #> [[152]] #> ── 3/34 · from 142fc3f ─────────────────────────────────────── #> [1] 1 4 13 ``` ```R #> [[153]] #> ── 3/34 · from 142fc3f ─────────────────────────────────────── #> [1] 1 4 8 ``` ```R #> [[154]] #> ── 2/34 · from 142fc3f ─────────────────────────────────────── #> [1] 1 14 ``` ```R #> [[155]] #> ── 2/34 · from 142fc3f ─────────────────────────────────────── #> [1] 1 5 ``` ```R #> [[156]] #> ── 3/34 · from 142fc3f ─────────────────────────────────────── #> [1] 1 5 11 ``` ```R #> [[157]] #> ── 3/34 · from 142fc3f ─────────────────────────────────────── #> [1] 1 5 7 ``` ```R #> [[158]] #> ── 2/34 · from 142fc3f ─────────────────────────────────────── #> [1] 1 12 ``` ```R #> [[159]] #> ── 2/34 · from 142fc3f ─────────────────────────────────────── #> [1] 1 6 ``` ```R #> [[160]] #> ── 3/34 · from 142fc3f ─────────────────────────────────────── #> [1] 1 6 11 ``` ```R #> [[161]] #> ── 3/34 · from 142fc3f ─────────────────────────────────────── #> [1] 1 6 7 ``` ```R #> [[162]] #> ── 2/34 · from 142fc3f ─────────────────────────────────────── #> [1] 1 11 ``` ```R #> [[163]] #> ── 2/34 · from 142fc3f ─────────────────────────────────────── #> [1] 1 7 ``` ```R #> [[164]] #> ── 2/34 · from 142fc3f ─────────────────────────────────────── #> [1] 1 13 ``` ```R #> [[165]] #> ── 2/34 · from 142fc3f ─────────────────────────────────────── #> [1] 1 18 ``` ```R #> [[166]] #> ── 2/34 · from 142fc3f ─────────────────────────────────────── #> [1] 1 20 ``` ```R #> [[167]] #> ── 2/34 · from 142fc3f ─────────────────────────────────────── #> [1] 1 22 ``` ```R #> [[168]] #> ── 2/34 · from 142fc3f ─────────────────────────────────────── #> [1] 1 32 ``` ```R #> [[169]] #> ── 2/34 · from 142fc3f ─────────────────────────────────────── #> [1] 1 9 ``` ```R #> [[170]] #> ── 2/34 · from 142fc3f ─────────────────────────────────────── #> [1] 1 8 ``` -------------------------------- ### DrL Layout Example Source: https://r.igraph.org/reference/layout_with_drl.html An example demonstrating how to use the `layout_with_drl` function to generate a layout for a sample graph and then plot it. The example customizes the 'simmer.attraction' option. ```R g <- as_undirected(sample_pa(100, m = 1)) l <- layout_with_drl(g, options = list(simmer.attraction = 0)) plot(g, layout = l, vertex.size = 3, vertex.label = NA) ``` -------------------------------- ### Install build-essential and gfortran on Debian-based Linux Source: https://r.igraph.org/articles/installation-troubleshooting.html Use this command to install the necessary C, C++, and Fortran compilers and essential build tools on Debian-based Linux distributions. ```bash sudo apt install build-essential gfortran ``` -------------------------------- ### Example of using cluster_louvain Source: https://r.igraph.org/reference/cluster_louvain.html Demonstrates how to use the cluster_louvain function to detect communities in a graph. The example creates a graph by duplicating full graphs and adding specific edges, then applies the Louvain algorithm. ```R g <- make_full_graph(5) %du% make_full_graph(5) %du% make_full_graph(5) g <- add_edges(g, c(1, 6, 1, 11, 6, 11)) cluster_louvain(g) #> IGRAPH clustering multi level, groups: 3, mod: 0.58 #> + groups: #> $`1` #> [1] 1 2 3 4 5 #> #> $`2` #> [1] 6 7 8 9 10 #> #> $`3` #> [1] 11 12 13 14 15 #> ``` -------------------------------- ### Install GLPK library on Debian-based Linux Source: https://r.igraph.org/articles/installation-troubleshooting.html Install the GLPK runtime library on Debian-based systems when encountering 'libglpk.so.40: cannot open shared object file' errors. ```bash sudo apt install libglpk40 ``` -------------------------------- ### Example Usage of cluster_fluid_communities Source: https://r.igraph.org/reference/cluster_fluid_communities.html This example demonstrates how to use the cluster_fluid_communities function. It first creates a graph using make_graph and then applies the algorithm to find 2 communities. ```r g <- make_graph("Zachary") comms <- cluster_fluid_communities(g, 2) ``` -------------------------------- ### Installing igraph Package Source: https://r.igraph.org/articles/installation-troubleshooting.html Use this command to install the igraph package. It will automatically select a binary version if available, which is recommended over compiling from source. ```r install.packages('igraph') ``` -------------------------------- ### Example: Two Vertex Types Preferring Self-Connections Source: https://r.igraph.org/reference/sample_traits_callaway.html Demonstrates generating a graph with 1000 nodes and 2 types, where each type only connects to vertices of the same type. This setup is useful for modeling segregated communities. ```R g1 <- sample_traits_callaway(1000, 2, pref.matrix = matrix(c(1, 0, 0, 1), ncol = 2)) g2 <- sample_traits(1000, 2, k = 2, pref.matrix = matrix(c(1, 0, 0, 1), ncol = 2)) ``` -------------------------------- ### Install GLPK and libxml2 development packages on Debian-based Linux Source: https://r.igraph.org/articles/installation-troubleshooting.html Install the development packages for the GLPK (GNU Linear Programming Kit) and libxml2 libraries on Debian-based Linux systems. ```bash sudo apt install libglpk-dev libxml2-dev ``` -------------------------------- ### Compute Local Scan Statistics (Examples) Source: https://r.igraph.org/reference/local_scan.html These examples demonstrate how to use the `local_scan()` function to compute local scan statistics. They cover both 'us' and 'them' variants, with different neighborhood sizes (k=0 and k=1), and also show how to use pre-computed neighborhoods. ```R pair <- sample_correlated_gnp_pair(n = 10^3, corr = 0.8, p = 0.1) local_0_us <- local_scan(graph.us = pair$graph1, k = 0) local_1_us <- local_scan(graph.us = pair$graph1, k = 1) ``` ```R local_0_them <- local_scan( graph.us = pair$graph1, graph.them = pair$graph2, k = 0 ) local_1_them <- local_scan( graph.us = pair$graph1, graph.them = pair$graph2, k = 1 ) ``` ```R Neigh_1 <- neighborhood(pair$graph1, order = 1) local_1_them_nhood <- local_scan( graph.us = pair$graph1, graph.them = pair$graph2, neighborhoods = Neigh_1 ) ``` -------------------------------- ### Install Development R/igraph from GitHub using pak Source: https://r.igraph.org/index.html Install the development version of R/igraph directly from its GitHub repository using the 'pak' package manager. ```R pak::pak("igraph/rigraph") ``` -------------------------------- ### De Bruijn graph example with output Source: https://r.igraph.org/reference/make_de_bruijn_graph.html Demonstrates the creation of a De Bruijn graph with m=2 and n=2, showing the resulting graph structure, attributes, and edges. This example illustrates the output format and content. ```R #> ── De-Bruijn graph 2-2 ───────────────────────────────────── e76594b ── #> #> ℹ directed #> ℹ 4 vertices · 8 edges #> #> ── Attributes ────────────────────────────────────────────────────────────────── #> → graph: name , m , n #> #> ── Edges ─────────────────────────────────────────────────────────────────────── #> #> [1] 1 → 1 1 → 2 2 → 3 2 → 4 3 → 1 3 → 2 4 → 3 4 → 4 make_line_graph(g) #> ── Line graph ────────────────────────────────────────────── 85a1930 ── #> ℹ directed #> ℹ 4 vertices · 8 edges #> #> ── Attributes ────────────────────────────────────────────────────────────────── #> → graph: name #> #> ── Edges ─────────────────────────────────────────────────────────────────────── #> [1] 1 → 1 3 → 1 1 → 2 3 → 2 2 → 3 4 → 3 2 → 4 4 → 4 ``` -------------------------------- ### Create a 3D lattice graph using length and dim Source: https://r.igraph.org/reference/make_lattice.html This example shows how to create a 3D lattice graph where each dimension has a length of 5, specified using the `length` and `dim` arguments. The output is similar to the `dimvector` example, detailing the graph's properties. ```R make_lattice(length = 5, dim = 3) ``` -------------------------------- ### Get Neighbors of a Vertex Source: https://r.igraph.org/reference/neighbors.html Demonstrates how to find the neighbors of a specific vertex in a graph. This example shows how to get neighbors for vertex 1 and vertex 34, and then find the common neighbors between them. ```R g <- make_graph("Zachary") n1 <- neighbors(g, 1) n34 <- neighbors(g, 34) intersection(n1, n34) ``` -------------------------------- ### Create a Turán graph with 10 vertices and 3 partitions Source: https://r.igraph.org/reference/make_turan.html This example demonstrates how to create a Turán graph with a specified number of vertices and partitions. It also shows how to visualize the resulting graph and inspect the distribution of vertices across partitions. ```R g <- make_turan(10, 3) plot(g) ``` ```R table(V(g)$type) ``` -------------------------------- ### Create a graph and display its attributes Source: https://r.igraph.org/reference/without_attr.html This example demonstrates creating a ring graph with 10 vertices and then displaying its attributes to show the initial state. ```r g1 <- make_ring(10) g1 #> ── Ring graph ────────────────────────────────────────────── 2a15ace ── #> ℹ undirected #> ℹ 10 vertices · 10 edges #> #> ── Attributes ────────────────────────────────────────────────────────────────── #> → graph: name , mutual , circular #> #> ── Edges ─────────────────────────────────────────────────────────────────────── #> [1] 1 ─ 2 2 ─ 3 3 ─ 4 4 ─ 5 5 ─ 6 6 ─ 7 7 ─ 8 8 ─ 9 9 ─ 10 #> [10] 1 ─ 10 ``` -------------------------------- ### List available vertex shapes Source: https://r.igraph.org/reference/shapes.html Call `shapes()` without arguments to get a character vector of all installed vertex shape names. ```R shapes() ``` -------------------------------- ### Sample from Stochastic Block Model (SBM) Source: https://r.igraph.org/reference/sample_.html Demonstrates how to create a graph using the Stochastic Block Model (SBM) with a predefined preference matrix and block sizes. This example uses the generic `sample_()` function. ```R pref_matrix <- cbind(c(0.8, 0.1), c(0.1, 0.7)) blocky <- sample_(sbm( n = 20, pref.matrix = pref_matrix, block.sizes = c(10, 10) )) ``` -------------------------------- ### Get Edge IDs from Incident Vertices Source: https://r.igraph.org/reference/get_edge_ids.html Finds the IDs of edges in a graph given their incident vertices. This example demonstrates basic usage with a ring graph. ```R g <- make_ring(10) ei <- get_edge_ids(g, c(1, 2, 4, 5)) E(g)[ei] ``` -------------------------------- ### Create a full graph with loops Source: https://r.igraph.org/reference/without_loops.html This example demonstrates creating a complete graph with 5 vertices, explicitly allowing loop edges. The output shows the initial graph structure including self-loops. ```R make_(full_graph(5, loops = TRUE)) ``` -------------------------------- ### Create a multipartite graph with partitions of size 2, 3, and 4 Source: https://r.igraph.org/reference/make_full_multipartite.html This example demonstrates creating an undirected multipartite graph with partitions of specified sizes. The resulting graph has a vertex attribute 'type' indicating the partition of each vertex. ```R g <- make_full_multipartite(c(2, 3, 4)) plot(g) ``` -------------------------------- ### Create a ring graph using make_ Source: https://r.igraph.org/reference/make_.html Demonstrates creating a basic ring graph with 10 vertices using the make_() function and the ring() constructor. ```R r <- make_(ring(10)) ``` -------------------------------- ### Delete edges using get_edge_ids Source: https://r.igraph.org/reference/delete_edges.html This example demonstrates how to dynamically get edge IDs based on vertex pairs and then use those IDs to delete edges from the graph. It's useful when edge IDs are not known beforehand. ```R g <- make_ring(5) g <- delete_edges(g, get_edge_ids(g, c(1, 5, 4, 5))) g ``` -------------------------------- ### Create a lattice graph using make_ Source: https://r.igraph.org/reference/make_.html Demonstrates creating a 3D lattice graph using the make_() function and the lattice() constructor. ```R l <- make_(lattice(c(3, 3, 3))) ``` -------------------------------- ### Create a 3D lattice graph and plot it Source: https://r.igraph.org/reference/rglplot.html This example demonstrates how to create a 3D lattice graph and then plot it using the rglplot function with specified 3D coordinates. The plotting is conditional on interactive mode and the rgl package being installed. ```R g <- make_lattice(c(5, 5, 5)) coords <- layout_with_fr(g, dim = 3) if (FALSE) { # interactive() && rlang::is_installed("rgl") rglplot(g, layout = coords) } ``` -------------------------------- ### Setting and Querying Edge Attributes Source: https://r.igraph.org/reference/edge_attr.html This example demonstrates how to create a graph, set multiple edge attributes ('weight' and 'color'), and then query these attributes. It also shows how to visualize the graph with edge widths determined by the 'weight' attribute. ```R g <- make_ring(10) %>% set_edge_attr("weight", value = 1:10) %>% set_edge_attr("color", value = "red") g #> #> ── Ring graph ────────────────────────────────────────────── b9ccc2e ── #> ℹ undirected · weighted #> ℹ 10 vertices · 10 edges #> #> ── Attributes ────────────────────────────────────────────────────────────────── #> → graph: name , mutual , circular #> → edge: weight , color #> #> ── Edges ─────────────────────────────────────────────────────────────────────── #> [1] 1 ─ 2 2 ─ 3 3 ─ 4 4 ─ 5 5 ─ 6 6 ─ 7 7 ─ 8 8 ─ 9 9 ─ 10 #> [10] 1 ─ 10 plot(g, edge.width = E(g)$weight) ``` -------------------------------- ### Remove duplicate edges from an edge sequence Source: https://r.igraph.org/reference/unique.igraph.es.html Demonstrates how to use the unique function to remove duplicate edges from an igraph edge sequence. The example first creates a ring graph, then an edge sequence with several duplicate edges, and finally applies the unique function to get a sequence with only unique edges. ```R g <- make_(ring(10), with_vertex_(name = LETTERS[1:10])) E(g)[1, 1:5, 1:10, 5:10] #> ── 22/10 · vertex names · from fab6d9a ───────────────────────── #> [1] A ─ B A ─ B B ─ C C ─ D D ─ E E ─ F A ─ B B ─ C C ─ D D ─ E #> [11] E ─ F F ─ G G ─ H H ─ I I ─ J A ─ J E ─ F F ─ G G ─ H H ─ I #> [21] I ─ J A ─ J E(g)[1, 1:5, 1:10, 5:10] %>% unique() #> ── 10/10 · vertex names · from fab6d9a ───────────────────────── #> [1] A ─ B B ─ C C ─ D D ─ E E ─ F F ─ G G ─ H H ─ I I ─ J A ─ J ``` -------------------------------- ### Install Build Dependencies Source: https://r.igraph.org/AGENTS.html Installs only the build-specific dependencies required for compiling the igraph package. This is a subset of all project dependencies. ```R pak::pak(dependencies = "Config/Needs/build") ``` -------------------------------- ### Getting and setting graph attributes (shortcut) Source: https://r.igraph.org/reference Provides a shortcut syntax using `$` for getting and setting graph attributes. ```APIDOC ## Graph Attribute Shortcut ### Description Shortcut for getting and setting graph attributes using the `$` operator. ### Method Not applicable (function call) ### Endpoint Not applicable (function call) ### Parameters Not specified in source. ### Request Example Not specified in source. ### Response Not specified in source. ``` -------------------------------- ### Example Graph Creation and min_separators Source: https://r.igraph.org/reference/min_separators.html Demonstrates creating a graph using graph_from_literal and then applying the min_separators function to find minimum separators. ```R camp <- graph_from_literal( Harry:Steve:Don:Bert - Harry:Steve:Don:Bert, Pam:Brazey:Carol:Pat - Pam:Brazey:Carol:Pat, Holly - Carol:Pat:Pam:Jennie:Bill, Bill - Pauline:Michael:Lee:Holly, Pauline - Bill:Jennie:Ann, Jennie - Holly:Michael:Lee:Ann:Pauline, Michael - Bill:Jennie:Ann:Lee:John, Ann - Michael:Jennie:Pauline, Lee - Michael:Bill:Jennie, Gery - Pat:Steve:Russ:John, Russ - Steve:Bert:Gery:John, John - Gery:Russ:Michael ) min_separators(camp) ``` -------------------------------- ### Create a random graph with degree sequence using make_ and sample_ Source: https://r.igraph.org/reference/make_.html Demonstrates creating a random graph from a degree sequence using sample_() and then simplifying it using the simplified() modifier, with the result checked for degree and simplicity. ```R ran <- sample_(degseq(c(3, 3, 3, 3, 3, 3), method = "configuration"), simplified()) degree(ran) #> [1] 3 3 3 1 3 3 is_simple(ran) #> [1] TRUE ``` -------------------------------- ### Install R/igraph Dependencies on Fedora Source: https://r.igraph.org/index.html Install the necessary development packages for glpk and libxml2 on Fedora systems when compiling R/igraph from source. ```bash yum install glpk-devel libxml2-devel ``` -------------------------------- ### Specify attribute combination for 'weight' and others Source: https://r.igraph.org/reference/igraph-attribute-combination.html This example shows how to specify attribute combination methods for different attributes. 'weight' is summed, while other attributes are ignored. ```R list(weight="sum", "ignore") ``` -------------------------------- ### Example of Sampling from SBM Source: https://r.igraph.org/reference/sample_sbm.html Demonstrates how to use sample_sbm to create a graph with two groups and specified connection probabilities between them. The generated graph is then printed. ```R pm <- cbind(c(0.1, 0.001), c(0.001, 0.05)) g <- sample_sbm(1000, pref.matrix = pm, block.sizes = c(300, 700)) g ``` -------------------------------- ### consensus_tree Source: https://r.igraph.org/reference/consensus_tree.html Creates a consensus tree from several fitted hierarchical random graph models using phylogeny methods. If the hrg argument is given and start is set to TRUE, then it starts sampling from the given HRG. Otherwise it optimizes the HRG log-likelihood first, and then samples starting from the optimum. ```APIDOC ## consensus_tree() ### Description Creates a consensus tree from several fitted hierarchical random graph models using phylogeny methods. If the `hrg()` argument is given and `start` is set to `TRUE`, then it starts sampling from the given HRG. Otherwise it optimizes the HRG log-likelihood first, and then samples starting from the optimum. ### Usage ```R consensus_tree(graph, hrg = NULL, start = FALSE, num.samples = 10000) ``` ### Arguments * **graph** (igraph) - The graph the models were fitted to. * **hrg** (igraphHRG or NULL) - A hierarchical random graph model, in the form of an `igraphHRG` object. `consensus_tree()` allows this to be `NULL` as well, then a HRG is fitted to the graph first, from a random starting point. * **start** (logical) - Whether to start the fitting/sampling from the supplied `igraphHRG` object, or from a random starting point. * **num.samples** (numeric) - Number of samples to use for consensus generation or missing edge prediction. ### Value `consensus_tree()` returns a list of two objects. The first is an `igraphHRGConsensus` object, the second is an `igraphHRG` object. The `igraphHRGConsensus` object has the following members: * **parents** (integer vector) - For each vertex, the ID of its parent vertex is stored, or zero, if the vertex is the root vertex in the tree. The first n vertex IDs (from 0) refer to the original vertices of the graph, the other IDs refer to vertex groups. * **weights** (numeric vector) - Counts the number of times a given tree split occurred in the generated network samples, for each internal vertices. The order is the same as in the `parents` vector. ### Related documentation in the C library `hrg_consensus()` ### See also Other hierarchical random graph functions: `fit_hrg()`, `hrg()`, `hrg-methods`, `hrg_tree()`, `predict_edges()`, `print.igraphHRG()`, `print.igraphHRGConsensus()`, `sample_hrg()` ``` -------------------------------- ### Example of a Non-Graphical Degree Sequence Source: https://r.igraph.org/reference/sample_degseq.html Illustrates a degree sequence that may not form a valid graph. This example is conditional and typically run interactively. ```R if (FALSE) { # rlang::is_interactive() exponential_degrees <- c( 1L, 1L, 2L, 1L, 1L, 7L, 1L, 1L, 5L, 1L, 1L, 2L, 5L, 4L, 3L, 2L, 2L, 1L, 1L, 2L, 1L, 3L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 4L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 3L, 1L, 4L, 3L, 1L, 2L, 4L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 4L, 1L, 2L, 1L, 3L, 1L, 2L, 3L, 1L, 1L, 2L, 1L, 2L, 3L, 2L, 2L, 1L, 6L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 4L, 2L, 1L, 3L, 4L, 1L, 1L, 3L, 1L, 2L, 4L, 1L, 3L, 1L, 2L, 1L ) } ``` -------------------------------- ### Aligning a Vertex Layout Source: https://r.igraph.org/reference/align_layout.html This example demonstrates how to use align_layout to center and align a force-directed layout. It compares the original layout with the aligned layout by plotting both. ```R g <- make_lattice(c(3, 3)) l1 <- layout_with_fr(g) l2 <- align_layout(g,l1) plot(g, layout = l1) plot(g, layout = l2) ``` -------------------------------- ### Simulate and count edges in G(n,p) graphs Source: https://r.igraph.org/reference/gsize.html This example demonstrates simulating multiple G(n,p) graphs and visualizing the distribution of their edge counts using a histogram. It uses vapply to efficiently extract the edge count from each simulated graph. ```R # Number of edges in a G(n,p) graph replicate(100, sample_gnp(10, 1 / 2), simplify = FALSE) %>% vapply(gsize, 0) %>% hist() ``` -------------------------------- ### Install R/igraph Dependencies on Debian/Ubuntu Source: https://r.igraph.org/index.html Install the necessary development packages for glpk and libxml2 on Debian or Ubuntu systems when compiling R/igraph from source. ```bash apt install libglpk-dev libxml2-dev ``` -------------------------------- ### Get Number of Communities Source: https://r.igraph.org/reference/communities.html Returns the number of communities found in the 'communities' object. This is a standard S3 method for getting the length of community structures. ```R length(x) ``` -------------------------------- ### Example: Kamada-Kawai Layout on a Ring Graph Source: https://r.igraph.org/reference/layout_with_kk.html Demonstrates how to apply the Kamada-Kawai layout algorithm to a ring graph. Edge weights are assigned and displayed as labels on the plot. ```R g <- make_ring(10) E(g)$weight <- rep(1:2, length.out = ecount(g)) plot(g, layout = layout_with_kk, edge.label = E(g)$weight) ``` -------------------------------- ### Get code length of communities Source: https://r.igraph.org/reference/code.length.html The `code.length()` function was renamed to `code_len()` for API consistency. Use this function to get the length of detected communities. ```R code.length(communities) ``` -------------------------------- ### Create a directed multipartite graph Source: https://r.igraph.org/reference/make_full_multipartite.html This example shows how to create a directed multipartite graph with edges pointing from partitions with lower indices to those with higher indices. The 'mode' parameter controls the directionality of edges in directed graphs. ```R g2 <- make_full_multipartite(c(2, 2, 2), directed = TRUE, mode = "out") plot(g2) ``` -------------------------------- ### Install Stable R/igraph from CRAN Source: https://r.igraph.org/index.html Use this command to install the latest stable version of the R/igraph package from the Comprehensive R Archive Network (CRAN). ```R install.packages("igraph") ``` -------------------------------- ### Example Usage of sample_motifs Source: https://r.igraph.org/reference/sample_motifs.html Demonstrates how to use `sample_motifs` to estimate the number of motifs of size 3 in a generated graph. It also shows the output of `motifs` and `count_motifs` for comparison. ```R g <- sample_pa(100) motifs(g, 3) #> [1] NA NA 268 NA 83 0 0 0 0 0 0 0 0 0 0 0 count_motifs(g, 3) #> [1] 351 sample_motifs(g, 3) #> [1] 10 ``` -------------------------------- ### Get tail vertices of edges in a graph Source: https://r.igraph.org/reference/tail_of.html Use tail_of() to get the tail vertices of specified edges. For undirected graphs, it returns incident vertices. ```R tail_of(graph, es) ``` -------------------------------- ### Get head of edges in a graph Source: https://r.igraph.org/reference/head_of.html Use `head_of()` to get the head vertices of specified edges in a graph. For undirected graphs, it returns vertices incident to the edges. ```R head_of(graph, es) ``` -------------------------------- ### Inspect Graph Properties Source: https://r.igraph.org/reference/knn.html This example shows how to inspect basic properties of a generated random graph, such as the number of vertices and edges. It also demonstrates how to calculate and display the degree distribution of the graph. ```R vcount(g) ecount(g) degree(g) ``` -------------------------------- ### Get vertex IDs of a named graph Source: https://r.igraph.org/reference/V.html Shows how to get vertex IDs when vertices have been assigned names using set_vertex_attr(). The output displays the names of the vertices. ```R g2 <- make_ring(10) %>% set_vertex_attr("name", value = letters[1:10]) V(g2) ``` -------------------------------- ### Fast Greedy Community Detection Example Source: https://r.igraph.org/reference/cluster_fast_greedy.html Demonstrates how to use `cluster_fast_greedy` to find communities in a graph. It shows how to create a graph, apply the algorithm, and then extract the membership and sizes of the detected communities. ```r g <- make_full_graph(5) %du% make_full_graph(5) %du% make_full_graph(5) g <- add_edges(g, c(1, 6, 1, 11, 6, 11)) fc <- cluster_fast_greedy(g) membership(fc) #> [1] 3 3 3 3 3 1 1 1 1 1 2 2 2 2 2 sizes(fc) #> Community sizes #> 1 2 3 #> 5 5 5 ``` -------------------------------- ### Maximum Cardinality Search Example 2 Source: https://r.igraph.org/reference/max_cardinality.html Applies `max_cardinality` to a different graph structure and checks its chordality. This example illustrates the function's behavior on a chordal graph. ```R g2 <- graph_from_literal( A - B:E, B - A:E:F:D, C - E:D:G, D - B:F:E:C:G, E - A:B:C:D:F, F - B:D:E, G - C:D:H:I, H - G:I:J, I - G:H:J, J - H:I ) max_cardinality(g2) #> $alpha #> [1] 10 8 9 6 7 5 4 2 3 1 #> #> $alpham1 #> ── 10/10 · named · from 59ee730 ────────────────────────────── #> [1] J H I G C F D B E A #> is_chordal(g2, fillin = TRUE) #> $chordal #> [1] TRUE #> #> $fillin #> numeric(0) #> #> $newgraph #> NULL #> ``` -------------------------------- ### Walktrap Community Detection Example Source: https://r.igraph.org/reference/communities.html Demonstrates how to use the `cluster_walktrap` function to detect communities in a graph and calculate its modularity. The example uses the Zachary karate club graph. ```r karate <- make_graph("Zachary") wc <- cluster_walktrap(karate) modularity(wc) #> [1] 0.3532216 membership(wc) #> [1] 1 1 2 1 5 5 5 1 2 2 5 1 1 2 3 3 5 1 3 1 3 1 3 4 4 4 3 4 2 3 2 2 3 3 plot(wc, karate) ``` -------------------------------- ### Stationary Distribution Example Source: https://r.igraph.org/reference/random_walk.html Demonstrates how to use `random_walk()` to approximate the stationary distribution of a Markov chain and compares it with eigenvector and PageRank centralities. ```R ## Stationary distribution of a Markov chain g <- make_ring(10, directed = TRUE) %u% make_star(11, center = 11) + edge(11, 1) ec <- eigen_centrality(g, directed = TRUE)$vector pg <- page_rank(g, damping = 0.999)$vector w <- random_walk(g, start = 1, steps = 10000) ## These are similar, but not exactly the same cor(table(w), ec) #> [1] 0.962221 ## But these are (almost) the same cor(table(w), pg) #> [1] 0.9999313 ``` -------------------------------- ### Install Development R/igraph from R-universe Source: https://r.igraph.org/index.html Configure your R session to use the R-universe repository for igraph and then install the development version. Ensure CRAN is also listed for other dependencies. ```R options( repos = c( igraph = 'https://igraph.r-universe.dev', CRAN = 'https://cloud.r-project.org' ) ) install.packages('igraph') ``` -------------------------------- ### Install igraph Development Dependencies Source: https://r.igraph.org/CONTRIBUTING.html Installs all dependencies required for igraph development, including core packages and roxygen2 add-ons. Ensure you are in the root of the rigraph Git repository. ```R # All Imports and Suggests dependencies pak::pak() # Then install packages like roxygen2 add-ons pak::pak(dependencies = "Config/Needs/build") ``` -------------------------------- ### Example: Degree Distribution in Random Graph Source: https://r.igraph.org/reference/degree.html Demonstrates obtaining the degree distribution for a random graph (GNP model). The output shows the relative frequency of vertices for each degree. ```R degree_distribution(g2) #> [1] 0.001 0.000 0.002 0.005 0.026 0.038 0.058 0.101 0.118 0.117 0.118 0.113 #> [13] 0.098 0.081 0.057 0.027 0.019 0.010 0.004 0.004 0.000 0.002 0.001 ``` -------------------------------- ### Set and Get igraph Options Source: https://r.igraph.org/reference/igraph_options.html Use `igraph_options()` to set multiple parameters at once or to retrieve all current option values. Use `igraph_opt()` to get the value of a single option. ```R igraph_options(...) igraph_opt(x, default = NULL) ``` -------------------------------- ### Create and Print a Ring Graph Source: https://r.igraph.org/reference/aaa-igraph-package.html Demonstrates how to create a ring graph using make_ring() and print its basic information. The output shows the graph type, number of vertices and edges, and graph attributes. ```R g <- make_ring(10) print(g) ```