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

