All lessons

10. Graphs

Graph representation

0 of 5 activities0%

Reading 1

How to store edges

Open

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); // undirected

Check 2

Sparse graphs

Open

For sparse graphs prefer

Fill in 3

Neighbors

Open

The list of vertices connected to u is u’s

Try it 4

Build undirected

Open

Print degrees.

main.cpp
Loading editor…
Output will appear here.

Assignment 5

Degree of u

Open

Read n m, then m edges (u v 0-based), then q. Print degree of q.

main.cpp
Loading editor…