Ego graph Using Networkx in Python
Ego network is a special type of network consisting of one central node and all other nodes directly connected to it. The central node is known as ego, while the other surrounding nodes directly connected to it are known as alters. Ego networks are mostly used in analyzing social connections, links, and relationships. The network shown below is an illustration of Ego Network. The central node (ego) is shown as circular surrounded by the neighboring nodes (alters).
Ego network is widely used in Social network analysis. The basic hypothesis about ego networks is that strong ties are homophilous. Homophilous is the tendency of individuals who are socially connected in some way to display certain traits or similarities to each other. In simple words, like-minded people are connected strongly in some manner. The ego network helps us to identify these hidden connections.
The following functions are served by Ego Networks:
- Propagation of information efficiently.
- Sensemaking from links, For example – Social links, relationships.
- Access to resources, efficient connection path generation.
- Community detection, identification of the formation of groups.
- Analysis of the ties among individuals for social support.
The easiest way to implement an ego network on any graph database is by using the Networkx library. It provides many predefined functions for the analysis and visualization of networks.
Networkx: Networkx is a Python package for the creation, analysis, and studies the nature of complex networks. It is one of the most popular python libraries used for network analysis.
Installation of the package:
pip install networkx
The Networkx library provides the method ego_graph() to generate an ego network from any graph. The method takes two mandatory parameters and four additional optional parameters.
Syntax: ego_graph(G, n, radius=1, center=True, undirected=False, distance=None)
- G (graph) – A NetworkX Graph (The parent network in whose ego network is to be created)
- N (node) – A single node (Ego/Central node of the ego network)
- Radius (number, optional) – Include all neighbors of distance <= radius from n.
- Center (bool, optional) – If False, do not include center node (ego) in graph
- Undirected (bool, optional) – If True use both in- and out-neighbors of directed graphs.
- Distance (key, optional) – Use specified edge data key as distance. For example, setting distance=’weight’ will use the edge weight to measure the distance from the node n.
Creating a sample network
We are taking a sample network with few nodes interconnected to each other. In this example, the nodes are – (A, B, C, D, E, F, G, H), out of these nodes one node is taken as the central node (ego), in our case we have taken A as the ego. The following code creates and shows our sample network.
Node A is defined as ego and hence shown in red.
Creating ego network
The below code creates and shows the ego network considering node A as the ego.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course