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**:

1 |
import networkx as nx #importing networkx for use |

Next we need to create a graph,

1 |
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:

1 |
graph.add_node(''a'') |

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

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

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

1 |
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.

1 |
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.

1 |
nx.degree(graph) #To find the degree of graphnx.connected_components(graph) #Find all connected componentsnx.connected_component_subgraphs(graph) #get connected components as subgraphsgraph.degree(''a'') #Get degree of a nodenx.degree(graph) #Get degree of all nodesnx.closeness_centrality(graph) #get closeness centrality for all nodes in a dictnx.betweenness_centrality(graph) #get betweenness centralitynx.clustering(graph) #Find clustering coefficientsnx.shortest_path(graph,''a'',''d'') #find shortest path between 2 nodesnx.shortest_path_length(G,''a'', ''d'') #find length of shortest path between 2 nodesnx.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.

## Leave a Reply