Add 2026.07.15 session
This commit is contained in:
70
notes-and-examples/2026.07.15/graphs-intro.md
Normal file
70
notes-and-examples/2026.07.15/graphs-intro.md
Normal file
@@ -0,0 +1,70 @@
|
||||
## Fundamentals
|
||||
|
||||
- A *node* or *vertex* contains a data point
|
||||
- An *edge* is what connects one node to another
|
||||
|
||||
A graph is just a collection of vertices and edges.
|
||||
|
||||
> [!QUESTION] What is a real-life example of a simple graph with only vertices and edges?
|
||||
|
||||
Some additional properties we can put on a graph:
|
||||
|
||||
- A *weight* is a numerical value that can be assigned to an edge
|
||||
- We can also assign a *direction* to an edge, such that it only points from one node to another, not the other way around
|
||||
|
||||
We can, of course, combine both of these properties too.
|
||||
|
||||
> [!QUESTION] What's something we can model with weights in a graph? What about with directional edges?
|
||||
|
||||
Going forward, we'll use $V$ to represent the number of vertices in a graph, and $E$ to represent the number of edges.
|
||||
|
||||
## Representing a graph
|
||||
|
||||
The *adjacency list* stores a list of connected vertices for each node, and it can fit in a dictionary or hash map structure.
|
||||
|
||||
```
|
||||
1 -> 2, 4
|
||||
2 -> 1, 3
|
||||
3 -> 2
|
||||
4 -> 1
|
||||
```
|
||||
|
||||
> [!QUESTION] How would you draw out this graph?
|
||||
|
||||
> [!QUESTION] What would this mapping look like if we wanted to add weights? What about directional edges?
|
||||
|
||||
The *adjacency matrix* is a 2D array where each position `arr[x][y]` represents an edge, and `x` and `y` each represent a node.
|
||||
|
||||
```
|
||||
[
|
||||
[0, 1, 0, 1],
|
||||
[1, 0, 1, 0],
|
||||
[0, 1, 0, 0],
|
||||
[1, 0, 0, 0]
|
||||
]
|
||||
```
|
||||
|
||||
> [!QUESTION] How would you draw out this graph? What would it look like as an adjacency list?
|
||||
|
||||
> [!QUESTION] How do we add weights and/or directional edges to this graph?
|
||||
|
||||
## Categorizing graphs
|
||||
|
||||
We say a graph is *directed and acyclic*, or a *directed acyclic graph (DAG)*, if there are no cycles formed using the directional edges.
|
||||
|
||||
> [!QUESTION] What's something we can model with a DAG?
|
||||
|
||||
> [!QUESTION] What's another data structure we went over that also classifies as a DAG?
|
||||
|
||||
A graph is *dense* if $E$ is closer to $V^2$, and *sparse* if $E$ is closer to $V$.
|
||||
|
||||
> [!QUESTION] What's the implication for the graph if $E$ is closer to $V^2$, i.e. how is it different compared to a sparse graph?
|
||||
|
||||
> [!QUESTION] What's the maximum number of edges we can have in a graph with no self-loops, relative to the number of vertices $V$?
|
||||
|
||||
A graph can contain *self-loops* (a loop from a vertex to itself). A graph can also have *multiple edges* going from one vertex to another.
|
||||
|
||||
> [!QUESTION] How do we represent a self-loop using an adjacency matrix?
|
||||
|
||||
Finally, a graph is *connected* if every vertex is reachable from every other vertex.
|
||||
|
||||
17688
notes-and-examples/2026.07.15/graphs.excalidraw
Normal file
17688
notes-and-examples/2026.07.15/graphs.excalidraw
Normal file
File diff suppressed because it is too large
Load Diff
BIN
notes-and-examples/2026.07.15/graphs.png
Normal file
BIN
notes-and-examples/2026.07.15/graphs.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 509 KiB |
18
notes-and-examples/2026.07.15/overview.md
Normal file
18
notes-and-examples/2026.07.15/overview.md
Normal file
@@ -0,0 +1,18 @@
|
||||
## Outline
|
||||
|
||||
- Go over the homework
|
||||
- Go over the basics of graphs, see [Introduction to graphs](./graphs-intro.md)
|
||||
|
||||

|
||||
|
||||
To open the `.excalidraw` file:
|
||||
|
||||
- Launch https://excalidraw.com
|
||||
- Click the top left menu, then select "Open"
|
||||
- Select the `.excalidraw` file from the file picker
|
||||
|
||||
## Assignment
|
||||
|
||||
- Try to complete the red-black implementation from last week
|
||||
- Complete the exercise for the 2D array (see the picture)
|
||||
|
||||
Reference in New Issue
Block a user