From d29f3c35cdfb49c05dfa4bbe903935cde05be9e8 Mon Sep 17 00:00:00 2001 From: Samuel-Lubliner Date: Wed, 9 Oct 2024 00:31:27 +0000 Subject: [PATCH] add trees in action --- source/trees/ch-trees.ptx | 1 + source/trees/sec-trees-in-action.ptx | 196 +++++++++++++++++++++++++++ 2 files changed, 197 insertions(+) create mode 100644 source/trees/sec-trees-in-action.ptx diff --git a/source/trees/ch-trees.ptx b/source/trees/ch-trees.ptx index 738c089..080da2d 100644 --- a/source/trees/ch-trees.ptx +++ b/source/trees/ch-trees.ptx @@ -12,6 +12,7 @@ + \ No newline at end of file diff --git a/source/trees/sec-trees-in-action.ptx b/source/trees/sec-trees-in-action.ptx new file mode 100644 index 0000000..031a82b --- /dev/null +++ b/source/trees/sec-trees-in-action.ptx @@ -0,0 +1,196 @@ + +
+ Trees in Action + +

+ 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. +

+
+ + Railway Problem +

+ Let's make a plan to solve our railway construction optimization problem. +

    +
  1. +

    + Find the latitude and longitude of each City College campus location +

    +
  2. +
  3. +

    + Calculate the distances between the locations. +

    +
  4. +
  5. +

    + Make a Graph of the City College campuses. Each location is a node. The weight of the edges represents the distance between locations. +

    +
  6. +
  7. +

    + Find the minimal spanning tree of the City Colleges Graph. +

    +
  8. +
+

+
+ + Location Distances + + City College Locations + + + Name + (Latitude, Longitude) + + + Harold Washington College + (41.88609733324964, -87.62682604591349) + + + Harry S Truman College + (41.9646769664519, -87.65901943241516) + + + Kennedy-King College + (41.77847328856264, -87.6435785385309) + + + Malcolm X College + (41.87800548491064, -87.67453475017268) + + + Olive-Harvey College + (41.71006715754713, -87.5886722734757) + + + Richard J. Daley College + (41.75677704810169, -87.72315805813204) + + + Wilbur Wright College + (41.95836512405638, -87.78738482318016) + + +
+

+ Now, let's calculate the distances between campus locations. We will first create a dictionary to store the campus name, latitude, and longitude values. +

+ + + 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 + + +

+ 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. +

+ + + 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()`") + + +

+ Now we can make an edge list. 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 ("HW", "HT", Haversine). +

+ + + 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") + + +
+ + Graph +

+ Swap (Latitude, Longitude)coordinates for plotting with (x,y) coordinates. +

+ + + pos = {college: (lon, lat) for college, (lat, lon) in lat_long.items()} + pos + + +

+ Create a Graph from the edge list: +

+ + + G = Graph(distances) + G.show( + pos=pos, # Positions are (longitude, latitude) + edge_labels=True, + vertex_size=500, + figsize=20, + title="City Colleges Distance Graph" + ) + + +
+ + Minimum Spanning Tree +

+ Let's find the minimal spanning tree edge list of the campus locations with the min_spanning_tree(by_weight=True) function. +

+ + + mst = G.min_spanning_tree(by_weight=True) + mst + + +

+ Visualize the minimal spanning tree with the vertex positions mapped to the geographical coordinates of each campus location. +

+ + + Graph(mst).show( + pos=pos, + edge_labels=True, + vertex_size=500, + figsize=15, + title="Minimum Spanning Tree of City Colleges" + ) + + +
+
\ No newline at end of file