...

/

Solution: Likeability in a Small Town

Solution: Likeability in a Small Town

Follow step-by-step instructions to create a weighted graph in Python.

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:

Press + to interact
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)
...