Introduction to Graph Structures
What are Graphs, and why do they matter?
Overview of the Course:
In this section, We will cover the following topics:
- Breadth First Search
- Depth First Search
- All Pairs, Shortest Path
- Single Source, Shortest Path
- Disjoint Set
But first! What is a graph in the world of computer science?
Overview of Graphs:
A graph is a fundamental data structure that consists of two key components:
- Vertices (Nodes): Represent entities or objects in the graph.
- Edges (Arcs): Represent relationships or connections between the vertices.
A graph is often defined as:
Where:
- is the set of vertices (nodes).
- is the set of edges (connections between nodes).
Types of Graphs:
- Directed Graph (Digraph): Edges have a direction, represented as an ordered pair .
- Undirected Graph: Edges do not have a direction, represented as an unordered pair .
Additional Notes:
- The set of vertices, , is finite.
- Edges, , can be thought of as a binary relation, where each edge connects two vertices.
Important Graph Definitions:
- Cycle: A path that starts and ends at the same vertex, with no other repeated vertices.
- Acyclic: A graph that contains no cycles.
- Incident: The relationship between an edge and the two nodes it connects.
- Adjacent: Two nodes are adjacent if there is an edge connecting them.
- Node Degree: The number of edges incident to a particular node.
- Regular Graph: A graph where every node has the same degree.
- Sparse Graph: A graph with relatively few edges compared to the number of vertices.
Edge Properties:
- In a disconnected graph, the minimum number of edges is 0 (no connections between vertices).
- In a connected graph, the minimum number of edges is approximately (where is the number of vertices), and the maximum number of edges is approximately for an undirected graph (dense graphs).
Path Definition:
A path of length from vertex to vertex is a sequence of vertices:
Where:
-
-
-
for all (i.e., each pair of consecutive vertices is connected by an edge).
-
A simple path means all vertices on the path are distinct.
If there exists a path from to , then is said to be reachable from .
Connected Components:
A connected component is a subset of a graph where there is a path between every pair of nodes in the component.
For example:
- Graph with and is connected because every node is reachable from every other node.
- Graph with and has two connected components: one containing nodes and , and the other containing nodes and . Nodes and are not connected.
Directed Graphs:
- Directed graphs may have cycles, where you can traverse from one vertex and return to it by following directed edges.
- Self-loops: A directed graph can have edges where both ends are the same vertex. This is not relevant for our example in maze graphs.
- A strongly connected subgraph is a subgraph where any vertex can reach any other vertex within the subgraph. For example, in the cycle , all nodes are mutually reachable.
Graph Representation:
There are several ways to represent graphs in memory. Below are the two most commonly used representations:
- Adjacency Matrix
- Adjacency List
- Custom Method (e.g., for maze graphs)
Adjacency List:
- Adjacency Lists are a preferred representation, especially for sparse graphs (graphs with few edges compared to the number of vertices).
Properties of Adjacency Lists:
- For each node, an array or list contains its adjacent nodes (nodes connected to it by an edge).
- Space Complexity: , where is the number of edges.
- Time Complexity: To traverse all adjacent nodes, the time complexity is (where is the number of vertices).
Example:
For the graph with and , the adjacency list representation would be:
- A:
- B:
- C:
- D:
Adjacency Matrix:
- An Adjacency Matrix is a 2D array where the cell at row , column contains 1 if there is an edge between vertex and vertex , and 0 if there is no edge.
Example:
For the same graph :
a | b | c | d | |
---|---|---|---|---|
a | 0 | 1 | 1 | 1 |
b | 1 | 0 | 1 | 1 |
c | 1 | 1 | 0 | 1 |
d | 1 | 1 | 1 | 0 |
Properties:
- Space Complexity: , where is the number of vertices (because every pair of vertices is represented by an entry in the matrix).
- Time Complexity: for checking if an edge exists between two vertices.
Use Case for Adjacency Matrix:
- Efficient for dense graphs: If a graph is dense (many edges), the adjacency matrix is efficient as it allows time complexity for checking if two vertices are connected.
- Memory Use: Can become inefficient if the graph is sparse, as it requires memory.