Graph Basics
Understand graph representation, terminology, and fundamental graph concepts for solving network problems.
Understand graph representation, terminology, and fundamental graph concepts for solving network problems. This hands-on tutorial focuses on practical implementation of graph basics concepts.
Graph Basics
Graphs model relationships between objects - social networks, maps, dependencies, and more.
Graph Terminology
- Vertex (Node): An individual record in the graph (A, B, C, D).
- Edge: The link between two vertices (A-B, A-C, etc.).
- Directed Graph: Edges have a direction (A $\rightarrow$ B).
- Undirected Graph: Edges have no direction (A $-$ B).
- Weighted Graph: Edges have numerical values (distance, cost).
Representation
Adjacency List
graph = {
'A': ['B', 'C'],
'B': ['A', 'D'],
'C': ['A', 'D'],
'D': ['B', 'C']
}
Space: O(V + E)
Check edge: O(degree)
Adjacency Matrix
# 0=A, 1=B, 2=C, 3=D
graph = [
[0, 1, 1, 0],
[1, 0, 0, 1],
[1, 0, 0, 1],
[0, 1, 1, 0]
]
Space: O(V²)
Check edge: O(1)
AI Mentor
Confused about "graphs vertices edges directed undirected adjacency list matrix"? Ask our AI mentor for a simplified explanation.
Quiz
Quiz
Question 1 of 1What's the space complexity of an adjacency list?
Key Takeaways
✅ Graphs model relationships and networks
✅ Adjacency list - space efficient for sparse graphs
✅ Adjacency matrix - fast edge lookup
Essential for social networks, maps, and dependencies! 🚀