Representing Graphs
We typically represent graphs as a set of vertices (V) and a set of edges (E).
Problem: This is quite slow for certain tasks e.g. finding what vertices a given vertex has an edge to
Solution: We have other ways of representing graphs
Adjacency Matrix
We have an matrix where iff there is an edge .
Otherwise, .
Time Complexities
Checking all edges for a given node:
Checking all edges:
Checking a node is connected to another node:
Space:
Adjacency List
Linked List for each node in the graph, which contains the nodes connected to the original node.
![Note]- Time/Space Complexities
Space is
Graph Search (unweighted)
BFS
Theorem: we have that consists of nodes at a distance from .
Let's use an inductive argument.
Base case i=0: Node is at a distance of from
Assume true for i=k: For each node we have that is at a distance from .
Induction for i=k+1: Let’s consider , which consists of unexplored nodes that are neighbours to some .
For each , has an edge to some and is at a distance from . Since is a distance 1 from , we now know there is a path with distance from to . We know there is no smaller path from to because otherwise would have been explored previously. Hence, is at a distance from .
BFS Time Complexity
Dijkstra’s
Dijkstra Proof
Note that is the length of a single step. is the length of the path so far (defined for all explored points so far)