Compare commits
9 Commits
1d412caaad
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 3e546c2bfb | |||
| cb0c3d651e | |||
| 5048f19a4d | |||
| ed0293c838 | |||
| 580c9fab33 | |||
| 5920408271 | |||
| 8d50900eaa | |||
| 4e7375c732 | |||
| 8a2ee0a557 |
4
assignments/python-packages/python-packages.md
Normal file
4
assignments/python-packages/python-packages.md
Normal file
@@ -0,0 +1,4 @@
|
||||
## Overview
|
||||
|
||||
Create a program which creates a topological ordering of Python packages.
|
||||
See this [starter template](https://gitea.bchen.dev/brendan/python-packages-starter)
|
||||
34
notes-and-examples/2026.08.20/exercise2.py
Normal file
34
notes-and-examples/2026.08.20/exercise2.py
Normal file
@@ -0,0 +1,34 @@
|
||||
# See the overview from 2026.07.31.
|
||||
# Note: I added Anaheim to the sample graph here. The solution should work
|
||||
# appropriately with either graph.
|
||||
|
||||
sample_graph = {
|
||||
'Santa Ana': {
|
||||
'Los Angeles': 5,
|
||||
'Anaheim': 3,
|
||||
'Palm Springs': 50
|
||||
},
|
||||
'Anaheim': {
|
||||
'Los Angeles': 2,
|
||||
'Santa Ana': 3
|
||||
},
|
||||
'Los Angeles': {
|
||||
'Anaheim': 2,
|
||||
'San Francisco': 25,
|
||||
'Santa Ana': 5
|
||||
},
|
||||
'Palm Springs': {
|
||||
'San Francisco': 30,
|
||||
'Santa Ana': 50
|
||||
},
|
||||
'San Francisco': {
|
||||
'Los Angeles': 25,
|
||||
'Palm Springs': 30
|
||||
}
|
||||
}
|
||||
|
||||
def possible_paths(start: str, end: str):
|
||||
# TODO
|
||||
pass
|
||||
|
||||
print(possible_paths('Santa Ana', 'San Francisco'))
|
||||
49
notes-and-examples/2026.08.20/overview.md
Normal file
49
notes-and-examples/2026.08.20/overview.md
Normal file
@@ -0,0 +1,49 @@
|
||||
## Outline
|
||||
|
||||
We're working on exercise 2 from [2026.07.31](../2026.07.31/overview.md).
|
||||
The exercise and starter file is included again below for convenience.
|
||||
|
||||
In a *weighted* road network, return all possible paths that a car can take to
|
||||
reach point A to point B. For each path, also sum up the total weight that
|
||||
taking that path requires.
|
||||
|
||||
Your algorithm should take the starting and ending points, and return a list
|
||||
of tuples. Each tuple should contain the traversal from point A to point B,
|
||||
followed by the total weight.
|
||||
|
||||
In this fictitious graph, calling `possible_paths("Santa Ana", "San Francisco")`
|
||||
should yield the return below.
|
||||
|
||||
```python
|
||||
graph = {
|
||||
'Santa Ana': {
|
||||
'Los Angeles': 5,
|
||||
'Palm Springs': 50
|
||||
},
|
||||
'Los Angeles': {
|
||||
'San Francisco': 25,
|
||||
'Santa Ana': 5
|
||||
},
|
||||
'Palm Springs': {
|
||||
'San Francisco': 30,
|
||||
'Santa Ana': 50
|
||||
},
|
||||
'San Francisco': {
|
||||
'Los Angeles': 25,
|
||||
'Palm Springs': 30
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
```
|
||||
[
|
||||
(
|
||||
["Santa Ana", "Los Angeles", "San Francisco"],
|
||||
30
|
||||
),
|
||||
(
|
||||
["Santa Ana", "Palm Springs", "San Francisco"],
|
||||
80
|
||||
)
|
||||
]
|
||||
```
|
||||
BIN
notes-and-examples/2026.08.27/classes.png
Normal file
BIN
notes-and-examples/2026.08.27/classes.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 28 KiB |
5
notes-and-examples/2026.08.27/overview.md
Normal file
5
notes-and-examples/2026.08.27/overview.md
Normal file
@@ -0,0 +1,5 @@
|
||||
## Overview
|
||||
|
||||
We'll go over exercise 2 from [last week](../2026.08.20/overview.md), as well as
|
||||
a new topic: [topological sort](./topological_sort.md). Try to complete the
|
||||
exercise from the document.
|
||||
32
notes-and-examples/2026.08.27/topological_sort.md
Normal file
32
notes-and-examples/2026.08.27/topological_sort.md
Normal file
@@ -0,0 +1,32 @@
|
||||
## Class ordering
|
||||
|
||||
These are some classes for Chapman University, for
|
||||
the [computer science program](https://catalog.chapman.edu/preview_program.php?catoid=33&poid=6408&print).
|
||||
|
||||

|
||||
|
||||
This is a *directed acyclic graph*, or DAG.
|
||||
|
||||
- Directed: edges point from one node to another, not necessarily the other way around
|
||||
- Acyclic: there are no *cycles* in the graph
|
||||
|
||||
We say that a class is a *prerequisite* or a *dependency* of another class
|
||||
if we need to take it before that other class. So, we need to take CPSC 230
|
||||
before CPSC 231, for example.
|
||||
|
||||
Could you come up with something like this for your school?
|
||||
|
||||
## The algorithm
|
||||
|
||||
If you were to come up with a list of classes, such that the list of classes
|
||||
is in the order which you need to take them, what would that look like?
|
||||
|
||||
This order is what we're trying to achieve with the topological sort
|
||||
algorithm. Note that there can be multiple correct solutions.
|
||||
|
||||
By the way, [here is a link to the whiteboard](https://excalidraw.com/#json=69478-DnuxlMCcyaOkazP,I_-nOWaO9m0wJkAFfqt9JA).
|
||||
|
||||
## The exercise
|
||||
|
||||
Let's write the algorithm. First, test it against the Chapman classes, then
|
||||
put in your classes and see how it fares against that.
|
||||
21
notes-and-examples/2026.08.27/topological_sort.py
Normal file
21
notes-and-examples/2026.08.27/topological_sort.py
Normal file
@@ -0,0 +1,21 @@
|
||||
from collections import deque
|
||||
# double-ended queue, this may be helpful
|
||||
# see the docs: https://docs.python.org/3/library/collections.html#collections.deque
|
||||
|
||||
classes = {
|
||||
'CPSC 230': {'CPSC 231'},
|
||||
'CPSC 231': {'CPSC 350', 'CPSC 330'},
|
||||
'CPSC 350': {'CPSC 380', 'CPSC 408', 'CPSC 406'},
|
||||
'CPSC 330': {'CPSC 351'},
|
||||
'CPSC 351': set(),
|
||||
'ENGR 101': set(),
|
||||
'CPSC 380': set(),
|
||||
'CPSC 406': set(),
|
||||
'CPSC 408': set()
|
||||
}
|
||||
|
||||
def topological_sort(adjacency_list: dict[str, set[str]]):
|
||||
# return the sorted ordering ['CPSC 230', 'ENGR 101', 'CPSC 231', ...]
|
||||
return []
|
||||
|
||||
print(topological_sort(classes))
|
||||
47
notes-and-examples/2026.08.27/topological_sort_solution.py
Normal file
47
notes-and-examples/2026.08.27/topological_sort_solution.py
Normal file
@@ -0,0 +1,47 @@
|
||||
from collections import deque
|
||||
|
||||
# This is a solution which uses DFS to trace the paths; there
|
||||
# are other solutions as well
|
||||
|
||||
classes = {
|
||||
'CPSC 230': {'CPSC 231'},
|
||||
'CPSC 231': {'CPSC 350', 'CPSC 330'},
|
||||
'CPSC 350': {'CPSC 380', 'CPSC 408', 'CPSC 406'},
|
||||
'CPSC 330': {'CPSC 351'},
|
||||
'CPSC 351': set(),
|
||||
'ENGR 101': set(),
|
||||
'CPSC 380': set(),
|
||||
'CPSC 406': set(),
|
||||
'CPSC 408': set()
|
||||
}
|
||||
|
||||
|
||||
def topological_sort(adjacency_list: dict[str, set[str]]):
|
||||
result = deque()
|
||||
visited = set()
|
||||
|
||||
def dfs(current: str, visited_in_traversal=None):
|
||||
if visited_in_traversal is None:
|
||||
visited_in_traversal = set()
|
||||
if current in visited:
|
||||
return
|
||||
if current in visited_in_traversal:
|
||||
raise ValueError("Graph has a cycle")
|
||||
|
||||
visited_in_traversal.add(current)
|
||||
|
||||
for neighbor in adjacency_list[current]:
|
||||
dfs(neighbor, visited_in_traversal)
|
||||
|
||||
# to trace the reverse path, use .append
|
||||
result.appendleft(current)
|
||||
visited.add(current)
|
||||
|
||||
for node in adjacency_list.keys():
|
||||
if node not in visited:
|
||||
dfs(node)
|
||||
|
||||
return result
|
||||
|
||||
|
||||
print(topological_sort(classes))
|
||||
7
notes-and-examples/2026.09.04/overview.md
Normal file
7
notes-and-examples/2026.09.04/overview.md
Normal file
@@ -0,0 +1,7 @@
|
||||
## Overview
|
||||
|
||||
We will go over the exercise from last week and start on [Python Packages](https://gitea.bchen.dev/brendan/python-packages-starter),
|
||||
a new assignment. For reference, this assignment is also in the Assignments section.
|
||||
|
||||
Whiteboard for optimizing last week's assignment: https://excalidraw.com/#json=rd-1CkdxA2AMpQsiVOQkY,t7-KayQW3DntCDkXVktEJQ
|
||||
|
||||
Reference in New Issue
Block a user