generated from PreTeXtBook/pretext-codespace
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6a1004c
commit d29f3c3
Showing
2 changed files
with
197 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,196 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<section xml:id="sec-trees-in-action"> | ||
<title>Trees in Action</title> | ||
<introduction> | ||
<p> | ||
Imagine your task is to create a railway between all the Chicago City College campus locations. The contract requests that you use minimal track material to save construction costs. For simplicity's sake, assume that there are no obstacles on each railway path between campus locations. Assume the railway paths do not need to take any turns. | ||
</p> | ||
</introduction> | ||
<subsection xml:id="subsec-Railway-Problem"> | ||
<title>Railway Problem</title> | ||
<p> | ||
Let's make a plan to solve our railway construction optimization problem. | ||
<ol> | ||
<li> | ||
<p> | ||
Find the latitude and longitude of each City College campus location | ||
</p> | ||
</li> | ||
<li> | ||
<p> | ||
Calculate the distances between the locations. | ||
</p> | ||
</li> | ||
<li> | ||
<p> | ||
Make a Graph of the City College campuses. Each location is a node. The weight of the edges represents the distance between locations. | ||
</p> | ||
</li> | ||
<li> | ||
<p> | ||
Find the minimal spanning tree of the City Colleges Graph. | ||
</p> | ||
</li> | ||
</ol> | ||
</p> | ||
</subsection> | ||
<subsection xml:id="subsec-location-distances"> | ||
<title>Location Distances</title> | ||
<table> | ||
<title>City College Locations</title> | ||
<tabular row-headers="no" left="medium" right="medium" top="medium" bottom="medium"> | ||
<row header="yes" bottom="medium"> | ||
<cell>Name</cell> | ||
<cell>(Latitude, Longitude)</cell> | ||
</row> | ||
<row> | ||
<cell>Harold Washington College</cell> | ||
<cell>(41.88609733324964, -87.62682604591349)</cell> | ||
</row> | ||
<row> | ||
<cell>Harry S Truman College</cell> | ||
<cell>(41.9646769664519, -87.65901943241516)</cell> | ||
</row> | ||
<row> | ||
<cell>Kennedy-King College</cell> | ||
<cell>(41.77847328856264, -87.6435785385309)</cell> | ||
</row> | ||
<row> | ||
<cell>Malcolm X College</cell> | ||
<cell>(41.87800548491064, -87.67453475017268)</cell> | ||
</row> | ||
<row> | ||
<cell>Olive-Harvey College</cell> | ||
<cell>(41.71006715754713, -87.5886722734757)</cell> | ||
</row> | ||
<row> | ||
<cell>Richard J. Daley College</cell> | ||
<cell>(41.75677704810169, -87.72315805813204)</cell> | ||
</row> | ||
<row> | ||
<cell>Wilbur Wright College</cell> | ||
<cell>(41.95836512405638, -87.78738482318016)</cell> | ||
</row> | ||
</tabular> | ||
</table> | ||
<p> | ||
Now, let's calculate the distances between campus locations. We will first create a <c>dictionary</c> to store the campus name, latitude, and longitude values. | ||
</p> | ||
<sage> | ||
<input> | ||
lat_long = { | ||
"HW": (41.88609733324964, -87.62682604591349), | ||
"HT": (41.9646769664519, -87.65901943241516), | ||
"KK": (41.77847328856264, -87.6435785385309), | ||
"MX": (41.87800548491064, -87.67453475017268), | ||
"OH": (41.71006715754713, -87.5886722734757), | ||
"RD": (41.75677704810169, -87.72315805813204), | ||
"WW": (41.95836512405638, -87.78738482318016) | ||
} | ||
lat_long | ||
</input> | ||
</sage> | ||
<p> | ||
Since the Earth is curved, we cannot use the hypotenuse. We will use the Haversine formula instead. Note that the Haversine formula still produces an approximation because the Earth is not a perfect sphere. Here is a function to compute the Haversine formula. | ||
</p> | ||
<sage> | ||
<input> | ||
def haversine(lat1, lon1, lat2, lon2): | ||
'''Reference: https://cs.nyu.edu/~visual/home/proj/tiger/gisfaq.html''' | ||
import math | ||
|
||
lat1, lon1, lat2, lon2 = map(math.radians, [lat1, lon1, lat2, lon2]) | ||
|
||
dlat = lat2 - lat1 | ||
dlon = lon2 - lon1 | ||
|
||
a = math.sin(dlat / 2)**2 + \ | ||
math.cos(lat1) * math.cos(lat2) * math.sin(dlon / 2)**2 | ||
|
||
c = 2 * math.asin(min(1.0, math.sqrt(a))) | ||
|
||
# Earth's approximate radius in kilometers | ||
R = 6367.0 | ||
|
||
distance = R * c | ||
|
||
return distance | ||
|
||
print("Ready to use `haversine()`") | ||
</input> | ||
</sage> | ||
<p> | ||
Now we can make an edge <c>list</c>. We will represent each campus as a node with the initials of the college name. The weight of the edge will represent the Haversine value between the locations. For example, express the route between Harold Washington College and Harry S Truman College as <c>("HW", "HT", Haversine)</c>. | ||
</p> | ||
<sage> | ||
<input> | ||
distances = [] | ||
colleges = list(lat_long.items()) | ||
for i in range(len(colleges)): | ||
college1, (lat1, lon1) = colleges[i] | ||
for j in range(i + 1, len(colleges)): | ||
college2, (lat2, lon2) = colleges[j] | ||
dist = haversine(lat1, lon1, lat2, lon2) | ||
distances.append((college1, college2, dist)) | ||
|
||
print("\nDistances between colleges (in kilometers):") | ||
for edge in distances: | ||
college1, college2, dist = edge | ||
print(f"{college1} - {college2}: {dist:.2f} km") | ||
</input> | ||
</sage> | ||
</subsection> | ||
<subsection xml:id="subsec-CCC-Graph"> | ||
<title>Graph</title> | ||
<p> | ||
Swap <m>(Latitude, Longitude)</m>coordinates for plotting with <m>(x,y)</m> coordinates. | ||
</p> | ||
<sage> | ||
<input> | ||
pos = {college: (lon, lat) for college, (lat, lon) in lat_long.items()} | ||
pos | ||
</input> | ||
</sage> | ||
<p> | ||
Create a <c>Graph</c> from the edge <c>list</c>: | ||
</p> | ||
<sage> | ||
<input> | ||
G = Graph(distances) | ||
G.show( | ||
pos=pos, # Positions are (longitude, latitude) | ||
edge_labels=True, | ||
vertex_size=500, | ||
figsize=20, | ||
title="City Colleges Distance Graph" | ||
) | ||
</input> | ||
</sage> | ||
</subsection> | ||
<subsection xml:id="subsec-Minimum-Spanning-Tree"> | ||
<title>Minimum Spanning Tree</title> | ||
<p> | ||
Let's find the minimal spanning tree edge <c>list</c> of the campus locations with the <c>min_spanning_tree(by_weight=True)</c> function. | ||
</p> | ||
<sage> | ||
<input> | ||
mst = G.min_spanning_tree(by_weight=True) | ||
mst | ||
</input> | ||
</sage> | ||
<p> | ||
Visualize the minimal spanning tree with the vertex positions mapped to the geographical coordinates of each campus location. | ||
</p> | ||
<sage> | ||
<input> | ||
Graph(mst).show( | ||
pos=pos, | ||
edge_labels=True, | ||
vertex_size=500, | ||
figsize=15, | ||
title="Minimum Spanning Tree of City Colleges" | ||
) | ||
</input> | ||
</sage> | ||
</subsection> | ||
</section> |