What is a Graph?
A graph is a data structure consisting of vertices (nodes) and edges (connections between nodes). Graphs are fundamental for representing relationships and networks in computer science, from social networks to road maps to dependency systems.UCX DSA provides comprehensive graph implementations supporting both adjacency matrix and adjacency list representations, with support for directed/undirected and weighted/unweighted graphs.
Graph Components
Vertices
Nodes in the graph that represent entities. In UCX DSA, vertices are identified by string labels.
Edges
Connections between vertices. Edges can be directed (one-way) or undirected (two-way).
Weights
Optional numeric values assigned to edges representing cost, distance, or other metrics.
Neighbors
Vertices directly connected to a given vertex through an edge.
Graph Types
Directed vs Undirected
- Directed Graph
- Undirected Graph
In a directed graph, edges have a direction. An edge from vertex A to vertex B does not imply an edge from B to A.Use Cases:
- Web page links
- Social media followers (following relationship)
- Task dependencies
- One-way streets in a road network
Weighted vs Unweighted
- Weighted Graph
- Unweighted Graph
In a weighted graph, each edge has an associated numeric value (weight) representing cost, distance, capacity, or other metrics.Use Cases:
- Road networks with distances
- Flight routes with costs
- Network flow problems
- Shortest path algorithms
Graph Representations
UCX DSA supports two primary graph representations:Adjacency Matrix
A 2D matrix where cell (i, j) indicates if there’s an edge from vertex i to vertex j.Pros:
- O(1) edge lookup
- Good for dense graphs
- Simple to implement
- O(V²) space complexity
- Inefficient for sparse graphs
Adjacency List
Each vertex stores a list of its adjacent vertices.Pros:
- O(V + E) space complexity
- Efficient for sparse graphs
- Fast neighbor iteration
- O(degree) edge lookup
- More complex structure
Graph Terminology
| Term | Definition |
|---|---|
| Order | The number of vertices in the graph |
| Size | The number of edges in the graph |
| Degree | The number of edges connected to a vertex |
| Path | A sequence of vertices where each consecutive pair is connected by an edge |
| Cycle | A path that starts and ends at the same vertex |
| Connected | A graph where there’s a path between every pair of vertices |
| Adjacency | Two vertices are adjacent if they’re connected by an edge |
Common Graph Operations
All graph implementations in UCX DSA support these core operations:Choosing a Graph Type
Use this guide to choose the right graph implementation:When to use Adjacency Matrix
When to use Adjacency Matrix
Choose adjacency matrix when:
- The graph is dense (many edges)
- You need O(1) edge existence checks
- You have a fixed number of vertices
- Memory is not a primary concern
- You need to frequently check if edges exist
When to use Adjacency List
When to use Adjacency List
Choose adjacency list when:
- The graph is sparse (few edges)
- You need to iterate over neighbors frequently
- The number of vertices may change
- Memory efficiency is important
- You’re implementing graph traversal algorithms
When to use Weighted Graphs
When to use Weighted Graphs
Choose weighted graphs when:
- Edges have associated costs or distances
- You’re implementing shortest path algorithms (Dijkstra, Bellman-Ford)
- You need to optimize routes or flows
- You’re working with network capacity problems
When to use Directed Graphs
When to use Directed Graphs
Choose directed graphs when:
- Relationships are one-way
- You’re modeling dependencies or hierarchies
- You need to represent asymmetric relationships
- You’re implementing topological sorting
Graph Traversal
UCX DSA provides two fundamental graph traversal algorithms:Breadth-First Search (BFS)
Explores neighbors level by level. Useful for finding shortest paths in unweighted graphs.
Depth-First Search (DFS)
Explores as far as possible along each branch before backtracking. Useful for cycle detection and topological sorting.
Quick Start Example
Next Steps
Graph Factory
Learn how to create different types of graphs using the Graph factory class
Adjacency Matrix
Explore matrix-based graph implementations
Adjacency List
Explore list-based graph implementations
Graph Traversal
Learn BFS and DFS algorithms for graph traversal