Homework Assignment 2#
Risk Graph#
(Notebook 2 of 2)#
✅ Put your name here.
#✅ Put your GitHub username here.
#Assignment Overview#
In this assignment, we’ll explore graphs using the board game Risk as our practical example. Risk is a strategy game played on a world map where territories are connected to their neighbors. This makes it a perfect candidate for graph representation and analysis.
Learning Objectives#
Implement a graph representation of a real-world game board
practice using networkx
do some research
visualize graph data using NetworkX
The assignment is split into two notebooks:
Notebook 1 of 2 is worth 14 + 33 + 3 = 51 points
Notebook 2 of 2 is worth 1+ 42 + 1 = 44 points for a total of 95 points. Point values for each part are included in the section headers and question prompts.
This assignment is due at 11:59 pm on Friday, March 21st. It should be uploaded into the “Homework Assignments” submission folder for Homework #2 on D2L. Submission instructions can be found at the end of the notebook. You must also fill out a survey regarding this assignment. The link to this survey can also be found at the end of the notebook.
🛑 STOP#
Pause to add this file and commit your changes to your Git repository! (1 points)
import networkx as nx
import matplotlib.pyplot as plt
import matplotlib as mpl
import seaborn as sns
import pandas as pd
import numpy as np
Part 3: Creating the Risk Game Graph (42 points)#
✅ Question 3.1 (5 points): Create a dictionary that defines the territories in Risk, organized by continents. Each territory should include its neighboring territories. Use the image above for completing the code below
# Define the Risk board territories and their connections
risk_territories = {
'North America': {
'Alaska': ['Northwest Territory', 'Alberta', 'Kamchatka'],
'Northwest Territory': ['Alaska', 'Alberta', 'Greenland'],
'Greenland': ['Northwest Territory', 'Ontario', 'Quebec', 'Iceland'],
'Alberta': ['Alaska', 'Northwest Territory', 'Ontario', 'Western United States'],
'Ontario': ['Alberta', 'Northwest Territory', 'Greenland', 'Quebec', 'Western United States', 'Eastern United States'],
'Quebec': ['Ontario', 'Greenland', 'Eastern United States'],
'Western United States': ['Alberta', 'Ontario', 'Eastern United States', 'Central America'],
'Eastern United States': ['Western United States', 'Ontario', 'Quebec', 'Central America'],
'Central America': ['Western United States', 'Eastern United States', 'Venezuela']
},
# Complete the remaining continents
'South America': {
# Add territories and their connections
},
'Europe': {
# Add territories and their connections
},
'Africa': {
# Add territories and their connections
},
'Asia': {
# Add territories and their connections
},
'Australia': {
# Add territories and their connections
}
}
Graph Creation#
✅ Question 3.2 (7 points): Create a NetworkX graph object from the territory definitions. The territories are the nodes of the graph. Implement a function that builds the graph and adds appropriate attributes for continents. Hint: Look up the documentation of add_node
, what are the possible inputs? Don’t forget to connect each territory to its neighbors. Make sure to add plenty of comments in your code.
def create_risk_graph(territories):
"""
Create a NetworkX graph from the Risk territory definitions.
Parameters:
territories (dict): Dictionary containing territory and connection information
Returns:
G (nx.Graph): NetworkX graph representing the Risk board
"""
# Your code here
pass
# Create the graph
G = create_risk_graph(risk_territories)
Graph Visualization#
✅ Question 3.3 (4 points): Create a visualization of the Risk board graph. You can use AI to help you with this. The function below should:
make a plot of the risk graph.
color nodes by continent
allow the user to choose a type of layout. Hint: search networkx documentation for the possible layout. Min 5 layouts should be available.
print the name of each territory on the respective node
make a legend for the graph
have very descriptive comments
increase the size of each node from the default value
increase the font size of label from the default value
def visualize_risk_graph(G):
"""
Create a visualization of the Risk board graph with continents colored differently.
Parameters:
G (nx.Graph): NetworkX graph of the Risk board
"""
# Your code here
pass
# Visualize the graph
visualize_risk_graph(G)
✅ Question 3.4 (26 points): Answer the following questions based on the Risk board graph visualization. Each answer should have a minimum of 1-2 sentences and snippet of the code that supports your answer. You must explain what each function in the code snippet does.
All answers should be in the same markdown cell.
The code snippet should be properly displayed using code markdown.
Which layout type provides the best visualization of the continent-based clustering in the Risk board graph?
Explain the rationale behind your choice of layout.
Do a little research and explain what the Kamada-Kawai layout does? Make sure to cite your sources.
Which part of the code is used to assign the color of each node based on its continent?
How did you make the legend? Explain any non-obvious part of the code.
What colormap did you choose for the continents and how did you import the colormap?
How did you set the node size and font size in the visualization?
How did you print the labels for the nodes?
✎ Do This - Erase the contents of this cell and replace it with your answer to the above question! (double-click on this text to edit this cell, and hit shift+enter to save the text)
🛑 STOP#
Pause to commit your changes to your Git repository! (1 points)
Take a moment to save your notebook, commit the changes to your Git repository using the commit message “Committing Part 3”, no need to push the changes to GitHub yet, but you can if you want.
Assignment wrap-up#
Please fill out the form that appears when you run the code below. You must completely fill this out in order to receive credit for the assignment!
from IPython.display import HTML
HTML(
"""
<iframe
src="https://forms.office.com/r/mB0YjLYvAA"
width="800px"
height="600px"
frameborder="0"
marginheight="0"
marginwidth="0">
Loading...
</iframe>
"""
)
Congratulations, you’re done!#
If you like, you can upload this file to D2L for a record. Nevertheless, we will grade the copy on GitHub.
© Copyright 2025, Department of Computational Mathematics, Science and Engineering at Michigan State University