Skip to content
Learn Netverks

Lesson

Step 28/36 78% through track

graphs-representation

Graph representations

Last reviewed Jun 1, 2026 Content v20260601
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
beginner

This lesson

This lesson teaches Graph representations: data structure and algorithm concepts with complexity analysis and interview-ready C++ examples.

Networks, dependencies, and reachability are graph problems—BFS/DFS are universal explorers.

You will apply Graph representations in contexts like: Social graphs, dependency resolution, routing, and crawl pipelines.

Compile and run C++17 snippets in the playground (`int main`, `std::cout`); after each run, state time and space complexity before moving on.

When you can explain the previous lesson's ideas in your own words.

A graph has vertices (nodes) and edges. Represent with adjacency list (vector of neighbors—sparse friendly) or adjacency matrix (n×n—dense, O(1) edge lookup).

Directed vs undirected

  • Undirected — edge (u,v) both ways
  • Directed — edge u → v only
  • Weighted — cost on edges (distances, capacities)

Adjacency list

#include 
#include 

int main() {
    int n = 4;
    std::vector> adj(n);
    auto add = [&](int u, int v) {
        adj[u].push_back(v);
        adj[v].push_back(u);
    };
    add(0, 1); add(0, 2); add(1, 3);
    std::cout << "neighbors of 0: ";
    for (int v : adj[0]) std::cout << v << " ";
    std::cout << "\n";
    return 0;
}

Space

List: O(V+E). Matrix: O(V²). Social networks and road maps are usually sparse—lists win.

Important interview questions and answers

  1. Q: When matrix?
    A: Dense graphs, need fast edge existence check, small fixed V.
  2. Q: SciPy sparse link?
    A: CSR sparse matrices mirror adjacency lists—see SciPy sparse preview.

Self-check

  1. Compare adjacency list vs matrix space.
  2. How add undirected edge in list form?

Pitfall: Building adjacency matrix for sparse graphs wastes O(V²) memory.

Interview prep

Sparse graph?

Adjacency list O(V+E) space.

Matrix when?

Dense small V or fast edge lookup.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Adj list vs matrix?
  • Sparse graph?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump