Search⌘ K
AI Features

Solution: Likeability in a Small Town

Explore how to create a social network graph using NetworkX in Python by adding nodes and assigning weighted edges to represent likeability or affinity. Learn to visualize and interpret the connections in a small town social graph.

We'll cover the following...

To solve this exercise, you should create the graph using NetworkX, then add the nodes of the graph and finally assign the weight to each of the edges:

Python 3.8
import networkx as nx
# Create an empty graph
social_graph = nx.Graph()
# Add nodes representing people in the community
social_graph.add_nodes_from([1, 2, 3, 4, 5, 6, 7])
# Add weighted edges representing affinity/likeability between people
social_graph.add_weighted_edges_from([
(1, 2, 9),
(1, 3, 8),
(1, 4, 3),
(2, 3, 2),
(2, 5, 10),
(3, 6, 7),
(3, 7, 1),
(4, 5, 4),
(4, 6, 5),
(5, 7, 6),
(6, 7, 8)
])
# This score will evaluate your network
score = sum(data['weight'] for _, _, data in social_graph.edges(data=True))
print("The total score is:" , score)
...