Practical Journal Sna With Writeups
Practical Journal Sna With Writeups
CERTIFICATE
This is to certify that
As laid down in the regulation of University of Mumbai for the purpose of M.Sc. Computer Science
______________________ ___________________________
Write a program to compute the following for a given a network: (i) number of
1 edges, (ii) number of nodes; (iii) degree of node; (iv) node with lowest degree;
(v)the adjacency list; (vi) matrix of the graph.
Perform following tasks: (i) View data collection forms and/or import one-
2 mode/two-mode datasets; (ii) Basic Networks matrices transformations
Compute the following node level measures: (i) Density; (ii) Degree;
3 (iii) Reciprocity; (iv) Transitivity; (v) Centralization; (vi) Clustering.
For a given network find the following: (i) Length of the shortest path from a
4 given node to another node; (ii) the density of the graph; (iii) Draw egocentric
network of node G with chosen configuration parameters.
9 Identify ties within the network using two-mode core periphery analysis.
Page | 1
PRACTICAL NO.1
AIM: Write a program to compute the following for a given a network: (i) number of edges,
(ii) number of nodes; (iii) degree of node; (iv) node with lowest degree; (v) the adjacency
list; (vi) matrix of the graph.
CODE;
>library(igraph)
> g<-graph. formula(1-2,1-3,2-3,2-4,3-5,4-5,4-6,4-7,5-6,6-7)
>V(g)
[1] "1" "2" "3" "4" "5" "6" "7"
>E(g)
[1] 2 -- 1
[2] 3 -- 1
[3] 3 -- 2
[4] 4 -- 2
[5] 5 -- 3
[6] 5 -- 4
[7] 6 -- 4
[8] 7 -- 4
[9] 6 -- 5
[10] 7 – 6
>plot(g)
Directed graph:
>dg<-graph.formula(1-+2,1-+3,2++3)
>V(dg)
Page | 2
[1] "1" "2" "3"
>E(dg)
[1] 1 -> 2
[2] 1 -> 3
[3] 2 -> 3
[4] 3 -> 2
>plot(dg)
>dgn<-graph.formula(Sam-+Mary,Sam-+Tom,Mary++Tom)
>V(dgn)
[1] "Sam" "Mary" "Tom"
>E(dgn)
[1] Sam -> Mary
Page | 3
>plot(dgn)
Number of Edges/Dyad/Ties:
>ecount(g)
[1] 10
Number of Node/Vertices:
>vcount(g)
[1] 7
>degree(dg,mode="in")
123
022
In-degree:
>degree(dg,mode="out")
123
211
>V(dg)$name[degree(dg)==min(degree(dg))]
[1] "1"
>V(dg)$name[degree(dg)==max(degree(dg))]
[1] "2" "3"
Page | 4
Find neighbors /adjacency list:
>neighbors(g,5)
[1] 3 4 6
>neighbors(g,2)
[1] 1 3 4
>get.adjlist(dg)
$`1` [1] 2 3
$`2`
[1] 1 3 3
$`3`
[1] 1 2 2
Adjacency Matrix:
>get.adjacency(g)
7 x 7 sparse Matrix of class "dgCMatrix" 1 2 3 4 5 6 7
1.11....
21.11...
311..1..
4 .1 . . 1 1 1
5 . . 1 1 .1 .
6...11.1
7 . . . 1 .1 .
Page | 5
Page | 6
PRACTICAL NO.2
AIM: Perform following tasks: (i) View data collection forms and/or import one-mode/two-mode datasets;
(ii) Basic Networks matrices transformations.
>head(nodes)
Reading data from a text file(firstly creat the txt file in notepad)
Page | 7
> read.table("mydata.txt")
>m=as.matrix(net)
>get.adjacency(m)
Page | 8
>plot(net)
Page | 9
Page | 10
PRACTICAL NO.3
AIM: Compute the following node level measures: (i) Density; (ii) Degree; (iii) Reciprocity; (iv)
Transitivity; (v) Centralization; (vi) Clustering.
(NOTE: library(igraph))
> vcount(g)
[1] 7
>ecount(g)
[1] 10
>ecount(g)/(vcount(g)*(vcount(g)-1))
[1] 0.2380952
>plot(g)
Degree
>setwd("C:/Users/hashp/Desktop/SNA PRCTL")
>nodes <- read.csv("Dataset1-Media-Example-NODES.csv", header=T, as.is=T)
>links <- read.csv("Dataset1-Media-Example-EDGES.csv", header=T, as.is=T)
>net <- graph.data.frame(d=links, vertices=nodes, directed=T)
>degree(net)
s01 s02 s03 s04 s05 s06 s07 s08 s09 s10 s11 s12 s13 s14 s15 s16 s17 10 7 13 9 5 8 5 6 5 5 3 6 4 4 6 3 5
Reciprocity:
Note:- Is found only for directed graph
>dg <- graph.formula(1-+2, 1-+3, 2++3)
>plot(dg)
Page | 11
>reciprocity(dg)
[1] 0.5
Formula as per text book
>dyad.census(dg)
$mut
[1] 1
$asym
[1] 2
$null
[1] 0
>2*dyad.census(dg)$mut/ecount(dg)
[1] 0.5
Transitivity
>kite <- graph.famous("Krackhardt_Kite")
>atri <- adjacent.triangles(kite)
>plot(kite, vertex.label=atri)
Page | 12
>transitivity(kite, type="local")
[1] 0.6666667 0.6666667 1.0000000 0.5333333 1.0000000 0.5000000 0.5000000
[8] 0.3333333 0.0000000 NaN
Betweeness Centrality
>betweenness(net, directed=T, weights=NA)
s01 s02 s03 s04 s05 s06 s07
26.857143 6.238095 126.511905 92.642857 13.000000 20.333333 1.750000
s08 s09 s10 s11 s12 s13 s14
21.000000 1.000000 15.000000 0.000000 33.500000 20.000000 4.000000
s15 s16 s17
5.666667 0.000000 58.500000
>edge.betweenness(net, directed=T, weights=NA)
[1] 6.619048 6.619048 11.785714 8.333333 6.500000 11.166667 21.333333
[8] 4.250000 4.250000 16.000000 64.476190 9.500000 3.261905 3.261905
[15] 15.000000 1.000000 15.000000 17.000000 16.750000 2.000000 1.250000
[22] 8.000000 12.500000 4.000000 26.000000 18.000000 14.500000 17.000000
[29] 7.500000 4.500000 2.738095 23.000000 11.000000 31.000000 9.011905
[36] 18.000000 28.500000 0.000000 3.000000 6.500000 17.000000 8.666667
[43] 74.500000 11.750000 34.000000 4.500000 6.333333 8.809524 5.333333
[50] 3.000000 28.000000 10.000000
>centralization.betweenness(net, directed=T, normalized=T)
$res
[1] 26.857143 6.238095 126.511905 92.642857 13.000000 20.333333
[7] 1.750000 21.000000 1.000000 15.000000 0.000000 33.500000
[13] 20.000000 4.000000 5.666667 0.000000 58.500000
$centralization
[1] 0.4439329
$theoretical_max
[1] 3840
Eigenvector centrality
>centralization.evcent(net, directed=T, normalized=T)
$vector
[1] 0.7694528 0.5623895 1.0000000 0.8569443 0.3049992 0.9285033 0.1025656
[8] 0.3362816 0.4696841 0.6510633 0.6361813 0.6479337 0.2674341 0.2289017
[15] 0.3277070 0.2831928 0.7125008
$value
[1] 3.278697
$options
$options$bmat
[1] "I"
$options$n [1] 17
$options$which
[1] "LR"
$options$nev
Page | 14
[1] 1
$options$tol
[1] 0
$options$ncv
[1] 0
$options$ldv
[1] 0
$options$ishift
[1] 1
$options$maxiter [1] 3000
$options$nb
[1] 1
$options$mode
[1] 1
$options$start
[1] 1
$options$sigma
[1] 0
$options$sigmai
[1] 0
$options$info
[1] 0
$options$iter
[1] 7
$options$nconv
[1] 1
$options$numop [1] 31
$options$numopb
[1] 0
$options$numreo [1] 18
$centralization [1] 0.4946416
$theoretical_max [1] 16
Clustering
# let's generate two networks and merge them into one graph.
>g2 <- barabasi.game(50, p=2, directed=F)
>g1 <- watts.strogatz.game(1, size=100, nei=5, p=0.05)
>g <- graph.union(g1,g2)
#Let's remove multi-edges and loops
>g <- simplify(g)
# 1st we calculate the edge betweenness,
>ebc <- edge.betweenness.community(g, directed=F)
>mods <- sapply(0:ecount(g), function(i){
+g2 <- delete.edges(g, ebc$removed.edges[seq(length=i)])
+cl <- clusters(g2)$membership
+modularity(g,cl)
+})
# we can now plot all modularities
>plot(mods, pch=20)
Page | 15
# Now, let's color the nodes according to their membership
>g2<-delete.edges(g, ebc$removed.edges[seq(length=which.max(mods)-1)])
>V(g)$color=clusters(g2)$membership # Let's choose a layout for the graph
>g$layout <- layout.fruchterman.reingold
# plot it
>plot(g, vertex.label=NA)
Page | 16
# fastgreedy.community agorithm
>fc <- fastgreedy.community(g)
>com<-community.to.membership(g, fc$merges, steps= which.max(fc$modularity)-1)
>V(g)$color <- com$membership+1
>g$layout <- layout.fruchterman.reingold
>plot(g, vertex.label=NA)
Page | 17
Page | 18
Page | 19
PRACTICAL NO.4
AIM: For a given network find the following: (i) Length of the shortest path from a given node to another
node; (ii) the density of the graph; (iii) Draw egocentric network of node G with chosen configuration
parameters.(library igraph)
Code:
>matt <- as.matrix(read.table(text=
+ "node R S T U
+R 7 5 00
+S 7 0 02
+T 0 6 0 0
+ U 4 0 1 0", header=T))
> nms <- matt[,1 ]
> matt <- matt[, -1]
> colnames(matt) <- rownames(matt) <- nms
> matt[is.na(matt)] <- 0
> g <- graph.adjacency(matt, weighted=TRUE)
> plot(g)
> print(s.paths)
RSTU R0554
S5032
T5301
U4210
> shortest.paths(g, v="R", to="S")
S
R5
Page | 20
> plot(g, edge.label=E(g)$weight)
Code:
>ng<-graph.formula(Andy++Garth,Garth-+Bill,Bill-+Elena,Elena++Frank,Carol-Andy,Carol-
+Elena,Carol++Dan,Carol++Bill,Dan++Andy,Dan++Bill)
>plot(ng)
A network as a matrix
>get.adjacency(ng)
7 x 7 sparse Matrix of class "dgCMatrix" Andy Garth Bill Elena Frank Carol Dan
Andy . 1 1
Garth 1 . 1 . . . .
Bill . . . 1 . 1 1
Elena . . . . 1 . .
Frank . . . 1 . . .
Carol 1 . 1 1 1
Dan 1 . 1 . . 1 .
A network as an edge list
> E(ng)
$Frank
[1] 8
$Carol [1] 6 16
$Dan
[1] 2 7 13
Page | 24
Page | 25
PRACTICAL NO.6
AIM: Write a program to exhibit structural equivalence, automatic equivalence, and
(NOTE: Instead of setting your current working directory you can give absolute path of Dataset file.)
Page | 26
Plot a metric MDS of vertex positions in two dimensions
>plot(cmdscale(as.dist(g.se)))
Blockmodeling
Page | 27
> b<-blockmodel(links2,eq,h=10)
> plot(b)
Page | 28
Page | 29
PRACTICAL NO.7
AIM: Create sociograms for the persons-by-persons network and the committee-bycommittee network for a
given relevant problem. Create one-mode network and two-node network for the same.
>library(Dominance)
>data(data_Network_1)
## set 1 for action you want to show
>bytes= "00111111111000000000"
>Sociogram(data_Network_1,bytes)
Page | 30
> print(data_Network_1)
Beschreibung item.number dominance.order age sex action.from.
1 1 P:fe rd 1 1 1 NA 2 4
2 2 P:fe rd2 2 2 NA 1 9
3 3 Pterd3 3 NA NA 1 4
4 4 P:ferd4 4 5 NA 1 12
5 P:ferd5 5 10 NA 1 5
6 6 P:ferd6 6 3 NA 1 9
7 T P:ferd7 7 6 NA 1 5
R H P4errlH H NA NA 1 9
test.3.kind.o[.acttion name . of . act ion act ion . number c lass if icat ion
1 leading 1 1
2 following 2 2
3 B roach 3 1
4 bite 4 1
threat to bite 5 1
kick 6 1
weighting
1 1
2 -1
1
1
6 1
Page | 31
Page | 32
PRACTICAL NO.8
AIM: Perform SVD analysis of a network.
+ 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1), 9, 4)
> print(a)
[,1] [,2] [,3] [,4]
[1,] 1 1 0 0
[2,] 1 1 0 0
[3,] 1 1 0 0
[4,] 1 0 1 0
[5,] 1 0 1 0
[6,] 1 0 1 0
[7,] 1 0 0 1
[8,] 1 0 0 1
[9,] 1 0 0 1
> svd(a)
$d
[1] 3.464102e+00 1.732051e+00 1.732051e+00 9.687693e-17
$u
[,1] [,2] [,3] [,4]
[1,] -0.3333333 0.4687136 0.05029703 3.375152e-01
[2,] -0.3333333 0.4687136 0.05029703 -8.126230e-01
[3,] -0.3333333 0.4687136 0.05029703 4.751078e-01
[4,] -0.3333333 -0.2779153 0.38076936 1.160461e-16
[5,] -0.3333333 -0.2779153 0.38076936 1.160461e-16
[6,] -0.3333333 -0.2779153 0.38076936 1.160461e-16
[7,] -0.3333333 -0.1907983 -0.43106639 -7.755807e-17
[8,] -0.3333333 -0.1907983 -0.43106639 -7.755807e-17
[9,] -0.3333333 -0.1907983 -0.43106639 -7.755807e-17
$v
[,1] [,2] [,3] [,4]
[1,] -0.8660254 -2.464364e-17 0.00000000 0.5
[2,] -0.2886751 8.118358e-01 0.08711702 -0.5
[3,] -0.2886751 -4.813634e-01 0.65951188 -0.5
[4,] -0.2886751 -3.304723e-01 -0.74662890 -0.5
Page | 33
Page | 34
PRACTICAL NO.9
AIM: Identify ties within the network using two-mode core periphery analysis.
>library(igraph)
> links <- read.csv("C:/Users/Asim/Desktop/Paper 2 Pract/Dataset1-Media-Example- EDGES.csv",
header=T, as.is=T)
> net <- graph.data.frame(d=links, vertices=nodes, directed=T)
> netm <- get.adjacency(net, attr="weight", sparse=F)
> colnames(netm) <- V(net)$media
> rownames(netm) <- V(net)$media
> palf <- colorRampPalette(c("white", "black"))
> heatmap(netm[,17:1], Rowv = NA, Colv = NA, col = palf(100),
+ scale="none", margins=c(10,10) )
Page | 35