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,


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:


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


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

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.

graph.add_edge('a', 'b')
print graph.edge['a']['b']['color']
#To access an edge's attribute

Next we will analyze this graph.

#To find the degree of graph
#Find all connected components
#get connected components as subgraphs
graph.degree('a') #Get degree of a node
nx.degree(graph) #Get degree of all nodes
#get closeness centrality for all nodes in a dict
#get betweenness centrality
nx.clustering(graph) #Find clustering coefficients
#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>