rm(list = ls())
Pertama-tama Anda harus mengaktifkan library(igraph)
library(igraph)
## Warning: package 'igraph' was built under R version 3.5.3
##
## Attaching package: 'igraph'
## The following objects are masked from 'package:stats':
##
## decompose, spectrum
## The following object is masked from 'package:base':
##
## union
g3 <- graph(c("Maya","Doni","Lina","Agus","Agus","Siti","Agus","Maya","Doni","Lina","Doni","Maya","Maya", "Danang","Danang", "Lina"),isolates=c("Amin"))
Tampilkan grafiknya
plot(g3,vertex.color="yellow",vertex.size=30)
plot(g3, edge.arrow.size=.5,
vertex.color="green", # warna isi vertex
vertex.size=30, # besar vertex
vertex.frame.color="red", # warna frame vertex
vertex.label.color="blue", # warna label vertex
vertex.label.cex=0.8, # besar huruf/labelvertex
vertex.label.dist=0, # jarak antara lingkaran vertex dengan label/huruf
edge.curved=0) # tingkat kelengkungan kurva
g5<-as.matrix(g3)
g5
## IGRAPH 2445973 DN-- 7 8 --
## + attr: name (v/c)
## + edges from 2445973 (vertex names):
## [1] Maya ->Doni Lina ->Agus Agus ->Siti Agus ->Maya
## [5] Doni ->Lina Doni ->Maya Maya ->Danang Danang->Lina
Tampilkan dalam bentuk Hubungan antar Matriks
g5[]
## 7 x 7 sparse Matrix of class "dgCMatrix"
## Maya Doni Lina Agus Siti Danang Amin
## Maya . 1 . . . 1 .
## Doni 1 . 1 . . . .
## Lina . . . 1 . . .
## Agus 1 . . . 1 . .
## Siti . . . . . . .
## Danang . . 1 . . . .
## Amin . . . . . . .
Degree Centrality, the number of edges attached to the node
Closeness Centrality, ease of reaching other nodes
Betweenness Centrality, who is connected to the most connected nodes
Eigenvector Centrality, role as an intermediary, connector
V(g5)
## + 7/7 vertices, named, from 2445973:
## [1] Maya Doni Lina Agus Siti Danang Amin
Ternyata ada 7 Vertex/ nama-nama aktor yang berperan
E(g5)
## + 8/8 edges from 2445973 (vertex names):
## [1] Maya ->Doni Lina ->Agus Agus ->Siti Agus ->Maya
## [5] Doni ->Lina Doni ->Maya Maya ->Danang Danang->Lina
Disini kita bisa melihat terdapat 8 edges/hubungan yang terjadi, pola hubungan bisa terlihat diatas, siapa memilih siapa dan seterusnya.
plot(g3, edge.arrow.size=.5,
vertex.color="green", # warna isi vertex
vertex.size=30, # besar vertex
vertex.frame.color="red", # warna frame vertex
vertex.label.color="blue", # warna label vertex
vertex.label.cex=0.8, # besar huruf/labelvertex
vertex.label.dist=0, # jarak antara lingkaran vertex dengan label/huruf
edge.curved=0) # tingkat kelengkungan kurva
Gunakan mode=‘in’ untuk mengetahui jumlah edges (tanda panah) yang mengarah ke dirinya
degree(g3, mode='in')
## Maya Doni Lina Agus Siti Danang Amin
## 2 1 2 1 1 1 0
Gunakan mode=‘out’ untuk mengetahui jumlah edges (tanda panah) pemilih memilih targetnya
degree(g3, mode='out')
## Maya Doni Lina Agus Siti Danang Amin
## 2 2 1 2 0 1 0
Gunakan mode=‘all’ untuk mengetahui total in dan out
degree(g3, mode='all')
## Maya Doni Lina Agus Siti Danang Amin
## 4 3 3 3 1 2 0
closeness(g3)
## Warning in closeness(g3): At centrality.c:2784 :closeness centrality is not
## well-defined for disconnected graphs
## Maya Doni Lina Agus Siti Danang
## 0.05555556 0.06250000 0.05555556 0.06250000 0.02380952 0.05000000
## Amin
## 0.02380952
betweenness(g3, directed = FALSE)
## Maya Doni Lina Agus Siti Danang Amin
## 3.00 0.50 2.00 4.25 0.00 0.25 0.00
Beberapa instruksi penting lainnya The density of a graph is the ratio of the number of edges and the number of possible edges.
edge_density(g3)
## [1] 0.1904762
The diameter of a graph is the length of the longest geodesic.
diameter(g3)
## [1] 4
distances calculates the length of all the shortest paths from or to the vertices in the network. Sshortest_paths calculates one shortest path (the path itself, and not just its length) from or to the given vertex.
distances(g3, mode = "all")
## Maya Doni Lina Agus Siti Danang Amin
## Maya 0 1 2 1 2 1 Inf
## Doni 1 0 1 2 3 2 Inf
## Lina 2 1 0 1 2 1 Inf
## Agus 1 2 1 0 1 2 Inf
## Siti 2 3 2 1 0 3 Inf
## Danang 1 2 1 2 3 0 Inf
## Amin Inf Inf Inf Inf Inf Inf 0
These functions find all, the largest or all the maximal cliques in an undirected graph. The size of the largest clique can also be calculated.
cliques(g3, min=3)
## Warning in cliques(g3, min = 3): At igraph_cliquer.c:56 :Edge directions
## are ignored for clique calculations
## list()
Articuation points or cut vertices are vertices whose removal increases the number of connected components in a graph.
articulation_points(g3)
## + 1/7 vertex, named, from 2445973:
## [1] Agus
Calculate the maximal (weakly or strongly) connected components of a graph
components(g3)
## $membership
## Maya Doni Lina Agus Siti Danang Amin
## 1 1 1 1 1 1 2
##
## $csize
## [1] 6 1
##
## $no
## [1] 2