Revenir
Revenir

Les graphes pour les réseaux sociaux

This is a quick tutorial aboutSocial Network Analysis usingNetworkx taking as examples the characters...

Sommaire

Le graphe social de Game of ThronesLes liens dans Game of Thrones - GrapheSocial Network AnalysisGame of Thrones in NetworkXCentrality MeasuresCliquesRecommendationsLiens utilesLes liens dans Games of ThronesLiens utiles
Représenter un réseau social par un grapheUn petit réseau d'amis - GrapheLecture du graphe "Un petit réseau d'amis"Le graphe d'un petit réseau d'amis - Questions
Une autre représentation - Graphe
Une autre représentation - Questions
Tableau des distances - Questions
Les caractéristiques d'un grapheD'autres caractéristiques des réseauxExcentricité d'un sommet du grapheCentre(s) du grapheRayon d’un grapheDiamètre d'un grapheD'autres caractéristiques des réseaux - QuestionsÀ quoi un graphe sert-il ?

Le graphe social de Game of Thrones

Les liens dans Game of Thrones - Graphe

This is a quick tutorial aboutSocial Network Analysis usingNetworkx taking as examples the characters of Game of Thrones. We got the data from thegithub merging all the 5 books and ignoring the “weight” attribute.

Social Network Analysis

WithNetwork Science we can approach many problems. Almost everything could be translated to a “Network” with Nodes and Edges. For example, the Google Maps is a network where the Nodes could be the “Places” and Edges can be the “Streets”. Of course the most famous network is theFacebookwhich is an “undirected” graph and theInstagramwhich is “directed” since we have the people that we follow and our followers. The nodes are the “users” and the “edges” are the connections between them. Notice that both “nodes” and “edges” can have attributes. For example, node attributes in Facebook can be the “Gender”, “Location”, “Age” etc and edge attribute can be “date of last conversation between two nodes”, ‘number of likes”, “date they connected” etc.
Notice that with Network Analysis we can apply recommendation systems but this is out of the scope of this tutorial.

Game of Thrones in NetworkX

We will use the NetworkX python library on “Game of Thrones” data. The first exercise is to load the data and to get the number of nodes of the network which is796 and the number of edges which is2823. Thus, we are dealing with 796 characters of Game of Thrones.
We will return some of the main Network properties such as “average shortest path length“, “diameter“, “density“, “average clustering” and “transitivity“. We commend out the answers of each property.
As we will see the “diameter” of our graph is 9, which is the longest of all the calculated shortest paths in a network. It is the shortest distance between the two most distant nodes in the network. In other words, once the shortest path length from every node to all other nodes is calculated, the diameter is the longest of all the calculated path lengths. The diameter is representative of the linear size of a network.
Also the “average shortest path length” is 3.41 which is calculated by finding the shortest path between all pairs of nodes, and taking the average over all paths of the length thereof. This shows us, on average, the number of steps it takes to get from one member of the network to another

Centrality Measures

We are going to represent some Centrality measures. We give the definition of the most common:
  • Degree centrality of a node in a network is the number of links (vertices) incident on the node.
  • Closeness centrality determines how “close” a node is to other nodes in a network by measuring the sum of the shortest distances (geodesic paths) between that node and all other nodes in the network.
  • Betweenness centrality determines the relative importance of a node by measuring the amount of traffic flowing through that node to other nodes in the network. This is done by measuring the fraction of paths connecting all pairs of nodes and containing the node of interest. Group Betweenness centrality measures the amount of traffic flowing through a group of nodes.
We will return also the famousPageRank although it is most common in “directed” graphs.
Based on this centrality measures, we will define the5 more important characters in Game of Thrones.
[('Jon-Snow', 0.19211961968354493), ('Tyrion-Lannister', 0.16219109611159815), ('Daenerys-Targaryen', 0.11841801916269228), ('Theon-Greyjoy', 0.11128331813470259), ('Stannis-Baratheon', 0.11013955266679568)]
[('Tyrion-Lannister', 0.15345911949685534), ('Jon-Snow', 0.14339622641509434), ('Jaime-Lannister', 0.1270440251572327), ('Cersei-Lannister', 0.1220125786163522), ('Stannis-Baratheon', 0.11194968553459118)]
[('Jon-Snow', 0.01899956924856684), ('Tyrion-Lannister', 0.018341232619311032), ('Jaime-Lannister', 0.015437447356269757), ('Stannis-Baratheon', 0.013648810781186758), ('Arya-Stark', 0.013432050115231265)]
[('Tyrion-Lannister', 0.4763331336129419), ('Robert-Baratheon', 0.4592720970537262), ('Eddard-Stark', 0.455848623853211), ('Cersei-Lannister', 0.45454545454545453), ('Jaime-Lannister', 0.4519613416714042)]
As we can see, for different Centrality measures, we get different results, for instance, “Jon-Snow” has the highest ”Betweenness” and “Tyrion-Lannister” the highest “Closeness” centrality.

Cliques

We will represent how we can get all theCliquesusing NetworkX and we will show the largest one.
['Tyrion-Lannister', 'Cersei-Lannister', 'Joffrey-Baratheon', 'Sansa-Stark', 'Jaime-Lannister', 'Robert-Baratheon', 'Eddard-Stark', 'Petyr-Baelish', 'Renly-Baratheon', 'Varys', 'Stannis-Baratheon', 'Tywin-Lannister', 'Catelyn-Stark', 'Robb-Stark']

Recommendations

You noticed that Facebook suggests you friends. There are many algorithms, but one of these is based on the “Open Triangles” which is a concept insocial network theory. Triadic closure is the property among three nodes A, B, and C, such that if a strong tie exists between A-B and A-C, there is a weak or strong tie between B-C. This property is too extreme to hold true across very large, complex networks, but it is a useful simplification of reality that can be used to understand and predict networks.
Let’s try to make the top ten suggestions based on the “Open Triangles”
[('Catelyn-Stark', 'Tommen-Baratheon'), ('Eddard-Stark', 'Brienne-of-Tarth'), ('Petyr-Baelish', 'Brienne-of-Tarth'), ('Rodrik-Cassel', 'Stannis-Baratheon'), ('Arya-Stark', 'Brienne-of-Tarth'), ('Arya-Stark', 'Stannis-Baratheon'), ('Bran-Stark', 'Jaime-Lannister'), ('Bran-Stark', 'Stannis-Baratheon')]
So for example we suggest connecting‘Catelyn-Stark’ with‘Tommen-Baratheon’ etc!

Liens utiles

https://en.wikipedia.org/wiki/Social_network_analysis

https://en.wikipedia.org/wiki/Social_network_analysis

https://networkx.github.io/documentation/stable/

https://networkx.github.io/documentation/stable/

https://github.com/mathbeveridge/asoiaf

https://github.com/mathbeveridge/asoiaf

https://en.wikipedia.org/wiki/Network_science

https://en.wikipedia.org/wiki/Network_science

https://en.wikipedia.org/wiki/PageRank

https://en.wikipedia.org/wiki/PageRank

https://en.wikipedia.org/wiki/Clique_(graph_theory)

https://en.wikipedia.org/wiki/Clique_(graph_theory)

https://en.wikipedia.org/wiki/Triadic_closure

https://en.wikipedia.org/wiki/Triadic_closure

https://en.wikipedia.org/wiki/Social_network

https://en.wikipedia.org/wiki/Social_network

Les liens dans Games of Thrones

Le site https://predictivehacks.com/social-network-analysis-of-game-of-thrones/ propose une représentation graphique des liens entre les personnages de la série Games of Thrones. Le schéma achève de nous convaincre de la complexité des liens interpersonnels dans le contexte de la série. 
Le schéma permet toutefois de se faire une idée de la proximité entre certains personnages et de la distance relationnelle qui sépare les protagonistes. 
Si l'on souhaitait diffuser une information dans tout le royaume en la communiquant à quelques personnes, on pourrait utiliser ce graphe. Par exemple en identifiant les personnages les mieux reliés aux autres, les plus centraux. 
Cette question peut se poser pour des réseaux sociaux tels qu'Instagram, Snapchat, Facebook ou autre. 

Liens utiles

https://predictivehacks.com/social-network-analysis-of-game-of-thrones

https://predictivehacks.com/social-network-analysis-of-game-of-thrones

Représenter un réseau social par un graphe

Un petit réseau d'amis - Graphe

Lecture du graphe "Un petit réseau d'amis"

Le graphe "Un petit réseau d'amis" représente les liens entre neuf personnes, chacune représentée par un ovale. 
Les traits entre les personnes représentent les liens d'amitié dans un réseau social. 
Gabriel et Margot sont amis, Gabriel et Luc sont amis, mais Luc et Margot ne sont pas amis. 
Gabriel et Louna sont plus éloignés en amitié de réseau que le sont Margot et Luc. En effet, Gabriel et Louna sont reliés par quatre amitiés, par exemple par l'intermédiaire de Luc, Alexandre et Hugo. Il faut parcourir quatre traits pour relier Alexandre à Hugo. Margot et Luc ne sont reliés que par deux amitiés, si l'on passe par Gabriel. 
On définit la distance entre deux personnes comme la longueur de la plus petite chaîne qui les relie. Sur le schéma, elle se compte en nombre de traits.

Le graphe d'un petit réseau d'amis - Questions

1. Quelle est la distance entre Louna et Margot ? 
2. Quelle est la distance entre Leila et Margot ? 

Une autre représentation - Graphe

Une autre représentation - Questions

1. Le graphe "Une autre représentation" est-il équivalent au graphe "Un petit réseau d'amis" ?
2. S'agit-il du même réseau ou existe-t-il des différences entre les liens d'amitié ?

Tableau des distances - Questions

Ce tableau recense toutes les distances entre les membres du réseau. Il manque toutefois les distances entre Margot et Leila et entre Margot et Louna.  Quelles sont-elles ?

Les caractéristiques d'un graphe

D'autres caractéristiques des réseaux

Excentricité d'un sommet du graphe

L’excentricité d’un sommet est la distance maximale entre ce sommet et les autres sommets du graphe. Ainsi, l'excentricité de Luc est de 3 (distance avec Inès ou Louna). L'excentricité de Gabriel est de 4 (distance avec Louna).
Voici la liste des excentricités de tous les membres du réseau.
  • Alexandre : 3
  • Emma : 3
  • Gabriel : 4
  • Hugo : 4
  • Inès : 4
  • Leila : 4
  • Louna : 4
  • Luc : 3
  • Margot : 4

Centre(s) du graphe

Le centre d’un graphe est le sommet dont l’excentricité est la plus petite. Il peut y avoir plusieurs centres dans un graphe.

Rayon d’un graphe

Le rayon d’un graphe, c’est l’excentricité du centre du graphe (ou de l’un des centres s’il y en a plusieurs).
Le rayon du graphe est ici égal à 3.

Diamètre d'un graphe

Le diamètre d’un graphe est la distance maximale entre deux sommets de ce graphe.

D'autres caractéristiques des réseaux - Questions

1. Dans le graphe "Un petit réseau d'amis", il y a trois centres. Identifiez-les. 
2. Déterminez la valeur du diamètre de ce graphe. 

À quoi un graphe sert-il ?

Les caractéristiques d'un graphe représentant un réseau social servent à en connaître l'étendue, la structure. Les centres du graphe peuvent par exemple être des points d'entrée privilégiés d'une information que l'on souhaite faire circuler.