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

Which statement about a recursive function without a reachable base case is correct?

  1. AIt runs in O(1) time
  2. BIt returns 0 automatically
  3. CIt keeps calling itself until the call stack overflows
  4. DThe compiler always turns it into a loop that ends safely
2.Algorithms & complexityHard

Why is the average lookup in a hash table O(1) but the worst case O(n)?

  1. AArrays have O(n) access
  2. BHash functions are slow
  3. CKeys must be sorted first
  4. DMany keys may collide into the same bucket
3.Data structuresMedium

Why is traversing an array often much faster in practice than traversing a linked list of the same length?

  1. AArrays use fewer instructions to compare values
  2. BLinked lists always store elements on disk
  3. CArray traversal is O(1) in total, while linked list traversal is O(n)
  4. DArray elements are contiguous, so they make good use of the CPU cache
4.Data structuresHard

An object is stored as a key in a hash map, and then a field used by its hash function is changed. What is the likely result?

  1. ALooking up the object may fail because it sits in the bucket for its old hash
  2. BNothing changes, because a key's hash is computed only once when the program starts
  3. CThe map automatically moves the key to its new bucket
  4. DThe map throws an error at the moment the field changes
5.Graph theoryMedium

A tree has 3 vertices of degree 3, 2 vertices of degree 4, and all its other vertices are leaves (degree 1). How many leaves L does it have?

3 × deg 32 × deg 4
L =
6.Graph theoryHard
773134714155233ABCDEFG

Use Dijkstra’s algorithm to find the length d(F) of the shortest path from A to F in the weighted graph.

start: Aend: F
d(F) =

3Solutions

1.Algorithms & complexity

Answer: C · It keeps calling itself until the call stack overflows

Why this is the answer

Without a base case the recursion never stops; in practice memory for stack frames runs out and the program crashes.

Answer

C · It keeps calling itself until the call stack overflows
2.Algorithms & complexity

Answer: D · Many keys may collide into the same bucket

Why this is the answer

With a good hash function and load factor, buckets stay short on average; adversarial or poor hashing can put all keys in one bucket.

Answer

D · Many keys may collide into the same bucket
3.Data structures

Answer: D · Array elements are contiguous, so they make good use of the CPU cache

Why this is the answer

Contiguous memory gives good locality of reference, while linked nodes may be scattered and cause many cache misses.

Answer

D · Array elements are contiguous, so they make good use of the CPU cache
4.Data structures

Answer: A · Looking up the object may fail because it sits in the bucket for its old hash

Why this is the answer

The key's location was chosen from its old hash; with a new hash, lookups search the wrong bucket, so keys should be immutable.

Answer

A · Looking up the object may fail because it sits in the bucket for its old hash
5.Graph theory

Answer: L = 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.

Handshake lemma for a tree

L + 17 = 2(L + 5) − 2
L = 9

Answer

L = 9

Shortcut: L = 2 + Σ (d − 2) over the vertices with degree d ≥ 3.

6.Graph theory

Answer: d(F) = 26

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

Each line: the vertex settled, its final distance, and the neighbours whose distance improves.

A (0): B: 7, C: 7
B (7): D: 20, G: 12
C (7): E: 11, G: 9
G (9): D: 12
E (11): F: 26
D (12)
F (26)

Shortest path

A → C → E → F
d(F) = 26

Answer

d(F) = 26

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