3 - Network Analysis Python
3 - Network Analysis Python
Cliques
& communities
Network Analysis in Python I
Cliques
● Social cliques: tightly-knit groups
● Network cliques: completely connected graphs
Network Analysis in Python I
Cliques
● Simplest complex clique: a triangle
Network Analysis in Python I
Triangle applications
● Friend recommendation systems
Network Analysis in Python I
Clique code
In [1]: G
Out[1]: <networkx.classes.graph.Graph at 0x10c99ecf8>
Let’s practice!
NETWORK ANALYSIS IN PYTHON I
Maximal cliques
Network Analysis in Python I
Maximal cliques
● Definition: a clique that, when extended by one node is no
longer a clique
Network Analysis in Python I
Maximal cliques
● Definition: a clique that, when extended by one node is no
longer a clique
Network Analysis in Python I
Maximal cliques
● Applications: community finding
Network Analysis in Python I
Communities
● Find cliques
● Find unions of cliques
Network Analysis in Python I
NetworkX API
● find_cliques finds all maximal cliques
Network Analysis in Python I
Maximal cliques
In [1]: import networkx as nx
In [3]: nx.find_cliques(G)
Out[3]: <generator object find_cliques at 0x1043f1f68>
In [4]: list(nx.find_cliques(G))
Out[4]: [[4, 0, 1, 2, 3], [4, 5], [6, 8, 9, 10, 7], [6, 5]]
NETWORK ANALYSIS IN PYTHON I
Let’s practice!
NETWORK ANALYSIS IN PYTHON I
Subgraphs
Network Analysis in Python I
Subgraphs
● Visualize portions of a large graph
● Paths
● Communities/cliques
● Degrees of separation from a node
Network Analysis in Python I
Subgraphs
In [1]: import networkx as nx
In [3]: G.nodes()
Out[3]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19]
In [5]: nodes
Out[5]: [2, 3, 4, 10]
In [6]: nodes.append(8)
Network Analysis in Python I
Subgraphs
In [7]: G_eight = G.subgraph(nodes)
In [8]: G_eight.edges()
Out[8]: [(8, 2), (8, 3), (8, 4), (8, 10), (2, 10)]
In [9]: G_eight
Out[9]: <networkx.classes.graph.Graph at 0x10cae39e8>
In [10]: G
Out[10]: <networkx.classes.graph.Graph at 0x10cad1f60>
Network Analysis in Python I
Subgraphs
In [11]: nx.draw(G_eight, with_labels=True)
NETWORK ANALYSIS IN PYTHON I
Let’s practice!