A beginners tutorial on Social Network Analysis – (Part 1)

Social Network Analysis refers to the methods used for analyzing social networks or interconnections among individuals. The individuals are taken as “nodes” and are connected to each other based on their interconnections, which may be of various types (friendship, co-authorship, kinship, sexual relations, financial exchange, common interest etc.) SNA uses various techniques from Graph Theory, Game Theory and several other to study, explain and predict the network.

Getting the tools:

NetworkX is a Python-based package for the creation, manipulation, and study of the structure, dynamics, and functions of complex networks. If you are on a linux distribution like Ubuntu chances are it will be in your package manager. Otherwise, you can download and install the binary or even compile it from source from here.

Head-first to NetworkX:

import networkx as nx #importing networkx for use

Next we need to create a graph,

graph=nx.Graph()

Let’s say we have 4 nodes named ‘a’,'b’,'c’ and ‘d’ and edges between (a,b) , (b,c) and (a,d).

to add a node we can just use:

graph.add_node('a')

We can also add all nodes from an iterable to the graph.

nodeList=['a','b','c','d']
graph.add_nodes_from(nodeList)

Nodes can also have attributes: attribute can be from a dictionary

nodeListAtrb=[('a',{'name':'alpha','color':'red'}),
('b',{'name':'beta','color':'blue'})]
graph.add_nodes_from(nodeListAtrb)
print graph.node['a']['color']
#to access an attribute of a node

You can have any number of attributes according to your choice.
Next we will add some edges. Adding an edge is exactly similar to adding a node.

edgeList=[('a','b',{'color':'blue'}),
('b','c',{'weight':8}),('c','d',{'connected':8})]
graph.add_edge('a', 'b')
graph.add_edges_from(edgeList)
print graph.edge['a']['b']['color']
#To access an edge's attribute

Next we will analyze this graph.

nx.degree(graph)
#To find the degree of graph
nx.connected_components(graph)
#Find all connected components
nx.connected_component_subgraphs(graph)
#get connected components as subgraphs
graph.degree('a') #Get degree of a node
nx.degree(graph) #Get degree of all nodes
nx.closeness_centrality(graph)
#get closeness centrality for all nodes in a dict
nx.betweenness_centrality(graph)
#get betweenness centrality
nx.clustering(graph) #Find clustering coefficients
nx.shortest_path(graph,'a','d')
#find shortest path between 2 nodes
nx.shortest_path_length(G,'a', 'd')
#find length of shortest path between 2 nodes
nx.neighbors(graph,'a') #find neighbors of a node

NetworkX provides many other methods to access different properties of the network and analyze them.

(In the next parts, we will discuss on how to get a visual representation of the network, collecting data for our network, and work with a real life network) In case of any issues or suggestions please add a comment to this post.

This entry was posted in Academics, Courses, Technology. Bookmark the permalink.

One Response to A beginners tutorial on Social Network Analysis – (Part 1)

  1. Pingback: A beginners tutorial on Social Network Analysis – (Part 1) » « Visual km 1.0

Leave a Reply

Your email address will not be published. Required fields are marked *

Connect with Facebook

* Copy This Password *

* Type Or Paste Password Here *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>