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

Quicksort always uses the first element as pivot. On which input does it show its worst-case behaviour?

  1. AAn array in random order
  2. BAn array of length 2
  3. CAn array whose elements are all different
  4. DAn array that is already sorted
2.Algorithms & complexityHard

The halting problem shows that:

  1. ALoops should be avoided
  2. BNo algorithm can decide for every program and input whether it halts
  3. CEvery program eventually halts
  4. DSome programs run faster on faster computers, so halting depends on hardware
3.Data structuresMedium

In a circular queue stored in an array of capacity n, how is the next position after index i computed?

  1. Ai + 1
  2. B2i + 1
  3. C(i + 1) mod n
  4. D(i − 1) mod n
4.Data structuresHard

Why are B-trees used in databases and file systems?

  1. ATheir high branching factor keeps the tree shallow, reducing slow disk reads
  2. BThey store data unsorted
  3. CThey use less memory than any array, so the entire index always fits in main memory
  4. DThey need no rebalancing
5.Graph theoryMedium

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

|V| = 15|E| = 21
k =
6.Graph theoryHard
1486118649ABCDEF

Dijkstra’s algorithm runs from A. Click the vertex that is settled (made permanent) in round 5; A itself is round 1.

start: Around 5
  1. 1A
  2. 2B
  3. 3C
  4. 4D
  5. 5E
  6. 6F

3Solutions

1.Algorithms & complexity

Answer: D · An array that is already sorted

Why this is the answer

On sorted input the pivot is always the minimum, so one side is empty and the other has n − 1 elements, giving O(n²).

Answer

D · An array that is already sorted
2.Algorithms & complexity

Answer: B · No algorithm can decide for every program and input whether it halts

Why this is the answer

Turing showed the halting problem is undecidable: assuming a decider exists leads to a contradiction with a program that does the opposite.

Answer

B · No algorithm can decide for every program and input whether it halts
3.Data structures

Answer: C · (i + 1) mod n

Why this is the answer

The modulo operation wraps index n − 1 back to 0, so the array is reused without shifting elements.

Answer

C · (i + 1) mod n
4.Data structures

Answer: A · Their high branching factor keeps the tree shallow, reducing slow disk reads

Why this is the answer

B-trees store many keys per node, so height is small and few block reads are needed per lookup; B+ trees also link leaves for range scans.

Answer

A · Their high branching factor keeps the tree shallow, reducing slow disk reads
5.Graph theory

Answer: k = 7

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

15 − 1 = 14

Edges to remove

k = 21 − 14 = 7

Answer

k = 7
6.Graph theory

Answer: D

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.

Dijkstra round by round

1. A (0): B: 14, C: 8
2. C (8): E: 16
3. B (14): D: 25
4. E (16): D: 22, F: 25
5. D (22)
6. F (25)

round 5

D

Answer

D

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