Week 10 — Graph Theory
1. Graph
A graph is a mathematical structure consisting of:
- Vertices (or nodes)
- Edges connecting pairs of vertices
A graph is represented as
where:
- is the set of vertices.
- is the set of edges.
2. Undirected Graph
In an undirected graph, an edge has no direction.
An edge connecting vertices and is written as
Thus,
3. Directed Graph
In a directed graph, every edge has a direction.
An edge from to is written as
In general,
4. Adjacent Vertices
Two vertices are adjacent if there is an edge connecting them.
If
then and are adjacent.
5. Degree of a Vertex
The degree of a vertex is the number of edges incident on it.
It is denoted by
For a directed graph:
- In-degree = number of incoming edges
- Out-degree = number of outgoing edges
6. Path
A path is a sequence of vertices connected by edges.
For example,
represents a path from to .
7. Graph Representation
Graphs can be represented using different data structures.
Common representations include:
- Adjacency matrix
- Adjacency list
8. Adjacency Matrix
For a graph with vertices, an adjacency matrix is an matrix.
For an unweighted graph,
For an undirected graph,
so the adjacency matrix is symmetric.
9. Adjacency List
An adjacency list stores, for each vertex, the vertices directly connected to it.
For vertex ,
contains its neighboring vertices.
Adjacency lists are particularly useful for sparse graphs.
Breadth-First Search
10. Breadth-First Search
Breadth-First Search (BFS) explores a graph level by level.
Starting from a source vertex, BFS:
- Visits the source.
- Visits its unvisited neighbors.
- Visits the unvisited neighbors of those vertices.
- Continues until all reachable vertices are visited.
11. BFS Data Structure
BFS uses a queue.
The queue follows:
12. BFS Traversal
A basic BFS procedure is:
- Mark the source vertex as visited.
- Insert it into the queue.
- Remove a vertex from the front of the queue.
- Visit each unvisited neighbor.
- Mark each newly visited neighbor.
- Insert newly visited vertices into the queue.
- Repeat until the queue is empty.
13. BFS Levels
BFS naturally organizes vertices according to their distance from the source.
If the source is at level , then:
- Its neighbors are at level .
- Their unvisited neighbors are at level .
- And so on.
14. BFS and Shortest Paths
In an unweighted graph, BFS can find the shortest path in terms of the number of edges from a source vertex.
If denotes the distance from the source to ,
Depth-First Search
15. Depth-First Search
Depth-First Search (DFS) explores a graph by following a path as deeply as possible before backtracking.
16. DFS Data Structure
DFS can be implemented using:
- Recursion
- A stack
A stack follows:
17. DFS Traversal
A basic DFS procedure is:
- Visit a vertex.
- Mark it as visited.
- Select an unvisited neighbor.
- Continue recursively or using a stack.
- Backtrack when no unvisited neighbor remains.
18. BFS vs DFS
| Property | BFS | DFS |
|---|---|---|
| Main structure | Queue | Stack / recursion |
| Exploration | Level by level | Depth first |
| Shortest path in unweighted graph | Yes | Not generally |
| Traversal strategy | Breadth | Depth |
Applications of BFS and DFS
19. Connectivity
BFS or DFS can determine which vertices are reachable from a given source.
If every vertex in an undirected graph is reachable from every other vertex, the graph is connected.
20. Reachability
For vertices and , BFS or DFS can determine whether a path exists from to .
21. Connected Components
Repeated BFS or DFS can be used to identify connected components of an undirected graph.
22. Cycle Detection
Graph traversal can be used to detect cycles.
For directed graphs, DFS can help identify cycles using the traversal state of vertices.
Directed Acyclic Graphs
23. Directed Graph
A directed graph contains edges with an orientation:
24. Directed Acyclic Graph
A Directed Acyclic Graph (DAG) is a directed graph containing no directed cycle.
Therefore, there is no sequence
forming a directed cycle.
25. Applications of DAGs
DAGs can represent dependencies such as:
- Course prerequisites
- Task dependencies
- Build systems
- Workflow dependencies
Complexity of BFS and DFS
26. BFS Complexity
Using an adjacency-list representation, BFS has time complexity
where:
- is the number of vertices.
- is the number of edges.
27. DFS Complexity
Using an adjacency-list representation, DFS has time complexity
Each vertex and edge is processed a bounded number of times.
Topological Sorting
28. Topological Ordering
A topological ordering of a directed graph is an ordering of its vertices such that for every directed edge
vertex appears before vertex .
29. Topological Sorting
Topological sorting produces a topological ordering of a DAG.
A topological ordering exists if and only if the directed graph is acyclic.
30. Topological Sorting Using In-Degree
The in-degree of a vertex is the number of incoming edges.
A typical topological sorting procedure is:
- Find vertices with in-degree .
- Add them to the ordering.
- Remove their outgoing edges.
- Update the in-degrees of affected vertices.
- Add newly available zero-in-degree vertices.
- Continue until all vertices are processed.
31. Topological Sorting and Cycles
If vertices remain but no vertex has in-degree , the graph contains a cycle.
Therefore, topological sorting can also be used to detect cycles in directed graphs.
32. Topological Sorting Complexity
Using an adjacency-list representation,
Formula Reference
| Topic | Key Result |
|---|---|
| Graph | |
| Undirected edge | |
| Directed edge | |
| Vertex degree | |
| Adjacency matrix | if edge exists |
| BFS structure | Queue (FIFO) |
| DFS structure | Stack / recursion (LIFO) |
| BFS shortest path | Shortest path in unweighted graphs |
| BFS complexity | |
| DFS complexity | |
| DAG | Directed graph with no directed cycle |
| Topological ordering | appears before for every |
| Topological sorting complexity |