All lessonsOpen Open Open Open Open
10. Graphs
Graph representation
0 of 5 activities0%
Reading 1
How to store edges
Adjacency matrix: V×V grid, O(1) edge query, O(V^2) space. Adjacency list: for each vertex, a list of neighbors — O(V+E) space, typical default.
Directed vs undirected: one-way vs two-way edges. Weighted edges store a cost.
vector<vector<int>> g(n);
g[u].push_back(v);
g[v].push_back(u); // undirectedCheck 2
Sparse graphs
For sparse graphs prefer
Fill in 3
Neighbors
Try it 4
Build undirected
Print degrees.
main.cpp
Loading editor…
Output will appear here.
Assignment 5
Degree of u
Read n m, then m edges (u v 0-based), then q. Print degree of q.
main.cpp
Loading editor…