Graphs can be represented in two forms in code:
- Adjacency List: Let adj be a list of vectors of size n then
adj[u] will be a list that contains v - Adjacency Matrix: Let
adjbe a matrix of size nxn and for each edge (u,v) in the matrix:adj[u][v] = 1
Adjacency List
vector<vector<int>> adj (n);
// for each edge u,v: v should be an element of adj[u] (and vice versa for undirected graphs)
for (int i = 0; i < edgeCount; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
}Adjacency Graph.
array<array<int,n>,n> adj;
// for each edge u,v: v should be an element of adj[u] (and vice versa for undirected graphs)
for (int i = 0; i < edgeCount; i++) {
int u, v;
cin >> u >> v;
adj[u][v] = 1;
}