BDA Experiment 8
BDA Experiment 8
• Syntax:
library(package, help, logical.return = FALSE. ........................................... )
• make_full_graph() function
This function is used to create a full graph.
Syntax: make_full_graph(n, loops = FALSE, directed =
FALSE)
• make_ring() function
A ring is a one-dimensional lattice and it can create lattices of arbitrary dimensions,
periodic or non-periodic ones.
Syntax: make_ring(n, directed = FALSE, circular = TRUE, mutual
= FALSE)
• make_star() function
This Function creates a star graph, where every single vertex is connected to the
center vertex and nobody else.
Syntax:
make_star(n, center = 1, mode = c("in", "out", "mutual", "undirected"))
• sample_gnp() function
This is a simple model where every possible edge is created with the same constant
probability.
Syntax:
• sample_gnp(n, p, loops = FALSE, directed = FALSE)
• plot() function
This function is used to draw the given graph in the active graphics window.
Syntax:
plot(defined_graph_name)
Full Graph
Syntax:
make_full_graph ()
Parameters:
• Number of vertices.
• directed = TRUE/FALSE Whether to create a directed graph or not.
• loops = TRUE/FALSE Whether to add self-loops to the graph or not.
Example:
Output:
Ring Graph
The Ring graph is a one-dimensional lattice and is a special case of make_lattice function.
Syntax:
make_ring ()
Parameters:
• Number of vertices.
• directed = TRUE/FALSE Whether to create a directed graph or not.
• mutual =TRUE/FALSE Whether directed edges are mutual or not. It is ignored in
undirected graph.
• circular =TRUE/FALSE Whether to create circular ring.
Example:
library(igraph)
Ring_Graph <- make_ring(12, directed = FALSE, mutual = FALSE, circular = TRUE)
plot(Ring_Graph)
Output:
Star Graph
A star graph is where every single vertex is connected to the center vertex and nobody else.
Syntax: make_star()
Parameters:
• Number of vertices
• center = Id of the center vertex
• mode = It defines direction of the edges in/out/mutual/undirected.
• in – The edges point to the center.
• out – The edges point from the center.
• mutual – A directed star graph is created with mutual edges.
• undirected – The edges are undirected.
Example:
library(igraph)
Star_Graph <- make_star(10, center = 1)
plot(Star_Graph)
Output:
Example:
library(igraph)
gnp_Graph <- sample_gnp(20, 0.3, directed = FALSE, loops = FALSE)
plot(gnp_Graph)
Similarly, you can try different graphs by changing their arguments as done below.
Output :
Analyzing graphs
Connectedness of graph
One of the basic measures of the vertices in a graph is how many connections they have with
other vertices. This measure can either be the number of connections to the total possible
connections also called density.
Now let us find the degree of each node/vertex in a random graph.
Syntax:
degree(graph)
The degree function is used to find out the number of vertices does each vertex is connected
to.
Example:
library(igraph)
gnp_Graph <- sample_gnp(7, 0.4, directed = FALSE, loops = FALSE)
plot(gnp_Graph) degree(gnp_Graph)
Output:
Betweenness of graph
In social networks, betweenness is defined as bridges between and among groups of
network members. One way to calculate the betweenness is to calculate the betweenness of
each vertex. In general, the higher the betweenness score associated with a vertex, the more
control over the network.
Syntax: betweenness(graph)
betweenness() function is defined by the number of shortest paths going through a vertex or
an edge.
Example:
library(igraph)
gnp_Graph <- sample_gnp(7, 0.4, directed = FALSE, loops = FALSE)
plot(gnp_Graph) betweenness(gnp_Graph)
Output:
Network Density
The Network’s density is defined as the number of connections to the total number of
possible connections. A complete graph has density = 1 while other networks can have
a decimal value.
Syntax:
edge_density(graph)
It is the ratio of the number of edges to the total number of possible edges.
Example:
library(igraph)
sample_graph <- sample_gnp(10, 0.3, directed = FALSE) plot(sample_graph)
sample_density <- edge_density(sample_graph, loops = FALSE)
sample_density
Output:
Identifying cliques in a network
A clique can be defined as a group of vertices where all possible links are present.
Syntax:
cliques(graph, min=NULL,max=NULL)
This function finds all the largest or maximal cliques in an undirected graph.
Example:
library(igraph)
sample_graph <- sample_gnp(20, 0.3, directed = FALSE, loops = FALSE)
library(igraph)
sample_graph <- sample_gnp(30, 0.07, directed = FALSE, loops =
)
This function allows us to do a random walk starting from
start_node till Number_of_stepsand in case if stuck or cannot move
forward either returns or gives an error.
Example:
library(igraph)
sample_graph <- sample_gnp(30, 0.07, directed = FALSE, loops =
FALSE)plot(sample_graph)
random_walk(sample_graph, 8, 10, stuck = "return")
Output:
Visualizing a Network
setwd(dir) set working directory function allows you to set your desired
directory for working
with.
Conclusion:
We have successfully implemented Social Network Analysis using R