Data Structures and Algorithms: cram pack

Formula sheets, the key method steps for every topic and 6 practice problems, with full solutions at the back.

New problemsBack to the course

In the print window, choose “Save as PDF” to keep a copy on your device.

1Formulas and methods

Graph theory

Handshake lemma
Σ deg(v) = 2|E|
Complete graphs
|E(Kn)| = n(n − 1)/2, |E(Km,n)| = m·n
Regular graph
|E| = n·k/2
Tree and forest
|E| = n − 1, |E| = n − c
Leaves of a tree
L = 2 + Σ (deg(v) − 2) over deg(v) ≥ 3
Euler
circuit: all degrees even; path: exactly 2 odd degrees
Hamilton circuits in Kn
(n − 1)!/2
Dijkstra update
d(v) = min(d(v), d(u) + w(u, v))
Walks of length L
(Mᴸ)ij

Method

  1. Write down the degree of every vertex; the degrees add up to twice the number of edges.
  2. For Euler questions, check that the graph is connected and count the vertices with odd degree.
  3. For trees, use n − 1 edges and the handshake lemma.
  4. For Dijkstra, keep a table of tentative distances and settle the smallest unsettled vertex each round.
  5. For a minimum spanning tree, sort the edges by weight and skip every edge that closes a cycle (Kruskal), or grow one tree from a start vertex (Prim).

2Practice problems

1.Algorithms & complexityMedium

With coins of 1, 3 and 4 and an amount of 6, how many coins does the greedy "largest coin first" method use, compared to the optimum?

  1. AGreedy uses 4 coins; the optimum is 3
  2. BGreedy uses 3 coins; the optimum is 3
  3. CGreedy uses 2 coins; the optimum is 2
  4. DGreedy uses 3 coins; the optimum is 2
2.Algorithms & complexityHard

Which statement about P and NP is correct?

  1. AIt has been proven that P ≠ NP
  2. BNP problems cannot be verified quickly
  3. CNP means "not polynomial"
  4. DEvery problem in P is also in NP
3.Data structuresMedium

An adjacency matrix for a graph with V vertices uses how much memory?

  1. AO(log V)
  2. BO(E)
  3. CO(V²)
  4. DO(V + E)
4.Data structuresHard

What are the time and extra space costs of reversing a singly linked list in place?

  1. AO(1) time and O(1) extra space
  2. BO(n) time and O(1) extra space
  3. CO(n) time and O(n) extra space
  4. DO(n log n) time and O(1) extra space
5.Graph theoryMedium

A connected graph has 6 vertices and 14 edges. How many edges k must be removed to leave a spanning tree?

|V| = 6|E| = 14
k =
6.Graph theoryHard
915821413ABCDE

Prim’s algorithm builds a minimum spanning tree starting from vertex B. Click the edge it adds as edge number 2.

start: Bk = 2
  1. 1edge AB
  2. 2edge AC
  3. 3edge BC
  4. 4edge BD
  5. 5edge CE
  6. 6edge DE
  7. 7edge BE

3Solutions

1.Algorithms & complexity

Answer: D · Greedy uses 3 coins; the optimum is 2

Why this is the answer

Greedy takes 4 + 1 + 1 = 3 coins, but 3 + 3 uses only 2, so greedy is not optimal for this coin system; dynamic programming is.

Answer

D · Greedy uses 3 coins; the optimum is 2
2.Algorithms & complexity

Answer: D · Every problem in P is also in NP

Why this is the answer

P ⊆ NP: if you can solve a problem quickly you can verify a solution quickly. Whether P = NP is still open.

Answer

D · Every problem in P is also in NP
3.Data structures

Answer: C · O(V²)

Why this is the answer

An adjacency matrix stores V × V entries, efficient for dense graphs; adjacency lists use O(V + E), better for sparse graphs.

Answer

C · O(V²)
4.Data structures

Answer: B · O(n) time and O(1) extra space

Why this is the answer

Keeping three pointers (previous, current, next) lets you reverse each link in one pass without allocating new nodes.

Answer

B · O(n) time and O(1) extra space
5.Graph theory

Answer: k = 9

Euler, Hamilton and trees: a connected graph has an Euler circuit (every edge once, back to the start) when every vertex has even degree, and an Euler path that is not a circuit when exactly two vertices have odd degree; the path then runs from one odd vertex to the other. A Hamilton circuit visits every vertex once; Kn has (n − 1)!/2 of them. A tree on n vertices has n − 1 edges, and a connected graph with m edges keeps a spanning tree after removing m − n + 1 edges.

Edges of a spanning tree

6 − 1 = 5

Edges to remove

k = 14 − 5 = 9

Answer

k = 9
6.Graph theory

Answer: edge AC

Shortest paths and spanning trees: Dijkstra’s algorithm settles one vertex per round: the unsettled vertex with the smallest tentative distance. Its distance is then final, and every neighbour v gets d(v) = min(d(v), d(u) + w(u, v)). Kruskal’s algorithm builds a minimum spanning tree by taking the edges from light to heavy and skipping any edge that would close a cycle; Prim’s algorithm grows one tree from a start vertex by always adding the lightest edge that leaves it. A spanning tree of n vertices has n − 1 edges.

Prim’s algorithm

1. BC (5)
2. AC (1)
3. CE (2)
4. BD (8)

edge number 2

AC

Answer

edge AC

ExamProblems · examproblems.com/courses/data-structures-and-algorithms