Introduction to graphs
easy 18 minA graph is a set of vertices joined by edges, directed or undirected, weighted or not.
Check yourself
One question at a time. Each answer explains why.
Question 1 of 10
What is the space complexity of an adjacency matrix for a graph with V vertices?
Which representation answers "is there an edge between u and v?" fastest?
In a directed graph, does the edge (A, B) imply the edge (B, A)?
What is the maximum number of edges in a simple undirected graph with V vertices?
Which representation is usually better for BFS and DFS?
What does the degree of a vertex mean in an undirected graph?
A graph has degrees 3, 2, 2, 1, 2. How many edges does it have?
Someone claims a graph has exactly three vertices of odd degree. What do you say?
A graph has 100,000 vertices and 300,000 edges. Roughly how many entries does each representation need?
You run DFS from vertex 0 on an undirected graph with no visited set. The graph contains the cycle 0-1-2-0. What happens?
Pick an answer to see why.
Not quite. A V × V grid has V² cells whether or not the edges exist.
Not quite. The matrix reads one cell. A list scans u's neighbours, O(deg(u)), unless you store neighbour sets; an edge list scans all E edges.
Not quite. A directed edge is an ordered pair. A two-way link needs both (A, B) and (B, A).
Not quite. Each of the V(V - 1)/2 unordered pairs can be joined at most once. V - 1 is the edge count of a tree.
Not quite. Traversals list every vertex's neighbours. With a list that totals O(V + E); with a matrix each listing scans a row, O(V²) in total.
Not quite. Degree counts incident edges. In a directed graph it splits into in-degree and out-degree.
Not quite. By the handshake lemma the degrees sum to 2E: 3 + 2 + 2 + 1 + 2 = 10, so E = 5.
Not quite. The degree sum is 2E, an even number. An odd count of odd degrees would make the sum odd.
Not quite. List: V + 2E = 100,000 + 600,000. Matrix: V² = 10^10 cells. This is why sparse graphs use lists.
Not quite. From 2 it can go back to 0, then 1, then 2 again with no end. A visited set stops it at vertices already entered.
Flashcards
Say your answer, then reveal it.
Card 1 of 11
What is a graph?
A pair G = (V, E): a set of vertices and a set of edges, each edge joining two vertices. Edges can be directed or undirected, weighted or unweighted.
Say your answer, then reveal it.
What is the degree of a vertex? In a digraph?
The number of edges touching it. In a directed graph it splits into in-degree (arrows arriving) and out-degree (arrows leaving).
Say your answer, then reveal it.
State the handshake lemma.
The sum of all degrees equals 2E, because every edge adds one to each of its two ends. In a digraph, in-degrees sum to E and out-degrees sum to E.
Say your answer, then reveal it.
Maximum edges in a simple graph with V vertices?
V(V - 1)/2 undirected, V(V - 1) directed. Both O(V²).
Say your answer, then reveal it.
What makes a graph a tree?
Connected and no cycles. Equivalently connected with exactly V - 1 edges. There is exactly one path between any two vertices.
Say your answer, then reveal it.
What is a DAG, and why does it matter?
A directed acyclic graph: arrows with no directed cycle. It models dependencies, and only a DAG has a topological order (a valid schedule).
Say your answer, then reveal it.
Space of an adjacency list versus an adjacency matrix?
List: O(V + E), V lists holding 2E entries in an undirected graph. Matrix: O(V²) cells regardless of how many edges exist.
Say your answer, then reveal it.
Edge check and neighbour listing costs in each representation?
Matrix: edge check O(1), neighbours O(V). List: edge check O(deg), neighbours O(deg). Neighbour sets make the list's edge check O(1) on average.
Say your answer, then reveal it.
Why do graph traversals need a visited set?
Graphs can have cycles and several paths to a vertex. Without memory DFS loops forever and BFS re-queues vertices. With it every vertex is processed once: O(V + E).
Say your answer, then reveal it.
How do you count connected components?
Loop over every vertex; at each unvisited one add 1 and run DFS or BFS to mark its whole component. Total O(V + E).
Say your answer, then reveal it.
What does VERN stand for?
Vertices (the points), Edges (the connections), Represent (list or matrix), Navigate (DFS or BFS).
Say your answer, then reveal it.
These cards come back in Today, just before you would forget them.