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 graphsocial_graph = nx.Graph()# Add nodes representing people in the communitysocial_graph.add_nodes_from([1, 2, 3, 4, 5, 6, 7])# Add weighted edges representing affinity/likeability between peoplesocial_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 networkscore = sum(data['weight'] for _, _, data in social_graph.edges(data=True))print("The total score is:" , score)
...