Algorithmic Problem Solving Course Overview
Algorithmic Problem Solving Course Overview
Problem Solving
6hp, vt2025
Fredrik Heintz
Dept of Computer and Information Science
Linköping University
Outline 2
Algorithms Programming
APS
Problem
Solving
Those that really understand
and take advantage of
software technology owns
the future!
Example: The 3n+1 problem 7
Example: The 3n+1 problem 8
Example: The 3n+1 problem 9
LAB1 4hp
▪ individually solving the 4 lab assignments
▪ optionally participating in problem solving sessions
UPPG1 2hp,
▪ individually solving the 13 weekly homework exercises, e.g.:
▪ Data structures
▪ Greedy Problems and Dynamic Programming
▪ Graph Algorithms
▪ Search
▪ Math-related Problems
▪ Computational Geometry.
Examination – Labs 12
Examination – Exercises 13
Examination – Course grade 14
The Schedule – VT 1 15
Simulation/Ad hoc
▪ Do what is stated in the problem
▪ Example: Simulate a robot
Greedy approaches
▪ Find the optimal solution by extending a partial solution by making locally
optimal decisions
▪ Example: Minimal spanning trees, coin change in certain currencies
Divide and conquer
▪ Take a large problem and split it up in smaller parts that are solved individually
▪ Example: Merge sort and Quick sort
Dynamic programming
▪ Find a recursive solution and compute it “backwards” or use memoization
▪ Example: Finding the shortest path in a graph and coin change in all currencies
Search
▪ Create a search space and use a search algorithm to find a solution
▪ Example: Exhaustive search (breadth or depth first search), binary search,
heuristic search (A*, best first, branch and bound)
Complete Search a.k.a. Brute Force 21
Quick-sort is a
randomized sorting x
algorithm based on the
divide-and-conquer
paradigm:
▪ Divide: pick a random
element x (called pivot) and x
partition S into
▪ L elements less than x
L E G
▪ E elements equal x
▪ G elements greater than x
▪ Recur: sort L and G
▪ Conquer: join L, E and G x
24
Quick-Sort Example 25
Pivot selection
7 2 9 43 7 6 1 → 1 2 3 4 6 7 8 9
7 2 9 4 → 2 4 7 9 3 8 6 1 → 1 3 8 6
3→3 4→4
Quick-Sort Example 26
7 2 9 4 3 7 6 1→ 1 2 3 4 6 7 8 9
2 4 3 1→ 2 4 7 9 3 8 6 1 → 1 3 8 6
3→3 4→4
Quick-Sort Example 27
7 2 9 43 7 6 1→→ 1 2 3 4 6 7 8 9
2 4 3 1 →→ 2 4 7 3 8 6 1 → 1 3 8 6
3→3 4→4
Quick-Sort Example 28
7 2 9 43 7 6 1→ 1 2 3 4 6 7 8 9
2 4 3 1 → 1 2 3 4 3 8 6 1 → 1 3 8 6
3→3 4→4
Quick-Sort Example 29
7 2 9 43 7 6 1→ 1 2 3 4 6 7 8 9
2 4 3 1 → 1 2 3 4 7 9 7 1 → 1 3 8 6
3→3 4→4
Quick-Sort Example 30
7 2 9 43 7 6 1→ 1 2 3 4 6 7 8 9
2 4 3 1 → 1 2 3 4 7 9 7 1 → 1 3 8 6
3→3 4→4
Quick-Sort Example 31
Join, join
7 2 9 4 3 7 6 1 →1 2 3 4 6 7 7 9
2 4 3 1 → 1 2 3 4 7 9 7 → 17 7 9
3→3 4→4
31
Greedy 32
A 4
4
3 (D,E) 1 (B,E) 4
8 (B,F) 4
6 (D,G) 2
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Kruskal’s MST Algorithm 35
A 4 3 (D,E) 1 (B,E) 4
8 4
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Kruskal’s MST Algorithm 36
A 4 3 (D,E) 1 (B,E) 4
8 4
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Kruskal’s MST Algorithm 37
A 4 3 (D,E) 1 (B,E) 4
8 4
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3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) 4
4
8
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Kruskal’s MST Algorithm 45
A 4 3 (D,E) 1 (B,E) 4
4
8
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Kruskal’s MST Algorithm 46
A 3 (D,E) 1 (B,E) 4
4
(D,G) 2 (B,F) 4
5
B D (E,G) 3 (B,H) 4
H 1
(C,D) 3 (A,H) 5
2
3
G E
(G,H)
(C,F)
(B,C)
3
3
4
(D,F)
(A,B)
(A,F)
6
8
10
}
not
considere
d
Done
Total Cost = dv = 21
Dynamic Programming 47
Given:
an integer bound W, and
a collection of n items, each with a positive, integer weight wi,
find a subset S of items that:
Motivation: You have a CPU with W free cycles and want to choose
the set of jobs (each taking wi time) that minimizes the number of
idle cycles.
Subset Sum 49
Notation:
Let S* be an optimal choice of items (e.g. a set {1,4,8}).
Let OPT(n, W) be the value of the optimal solution.
We design a dynamic programming algorithm to compute
OPT(n, W).
Subproblems:
To compute OPT(n, W): We need the optimal value for
subproblems consisting of the first j items for every knapsack size
0 <= w <= W.
Denote the optimal value of these subproblems by OPT(j, w).
Subset Sum 50
Subset Sum 51
Subset Sum 52
Subset Sum 53
Subset Sum 54
Subset Sum 55
SubsetSum(n, W):
Initialize M[0,r] = 0 for each r = 0,...,W
Initialize M[j,0] = 0 for each j = 1,...,n
8, 5, 4, 2
General DP Principles 58
Max 1D sum
Max 2D sum
Longest increasing subsequence (LIS)
▪ Longest decreasing subsequence (LDS)
0-1 Knapsack (subset sum)
Coin Change (general version)
Travelling Salesman Problem (TSP)
Data structures
▪ Standard library data structures
▪ Vector, stack, queue, heap, priority queue, sets, maps
▪ Other data structures
▪ Graph (adjacency list and adjacency matrix), Union/find, Segment tree,
Fenwick tree, Trie
Sorting
▪ Quick sort, Merge sort, Radix sort, Bucket sort
Strings
▪ String matching (Knuth Morris Pratt, Aho-Corasick), pattern matching,
trie, suffix trees, suffix arrays, recursive decent parsing
Important Data Structures and Algorithms 63
Dynamic programming
▪ Longest common subsequence, Longest increasing subsequence, 0/1
Knapsack, Coin Change, Matrix Chain Multiplication, Subset sum,
Partitioning
Graphs
▪ Traversal (pre-, in- and post-order), finding cycles, finding connected
components, finding articulation points, topological sort, flood fill, Euler
cycle/Euler path, SSSP - Single source shortest path (Dijkstra, Bellman-
Ford), APSP – All pairs shortest path (Floyd Warshall), transitive closure
(Floyd Warshall), MST – Minimum spanning tree (Prim, Kruskal (using
Union/find)), Maximal Bipartite Matching, Maximum flow, Maximum
flow minimal cost, Minimal cut
Search
▪ Exhaustive search (depth-first, breadth-first search, backtracking),
binary search (divide and conquer), greedy search (hill climbing),
heuristic search (A*, branch and bound), search trees
Important Data Structures and Algorithms 64
Mathematics
▪ Number theory (prime numbers, greatest common divisor (GCD),
modulus), big integers, combinatorics (count permutations), number
series (Fibonacci numbers, Catalan numbers, binomial coefficients),
probabilities, linear algebra (matrix inversion, linear equations systems),
finding roots to polynomial equations, diofantic equations, optimization
(simplex)
Computational geometry
▪ Representations of points, lines, line segments, polygons, finding
intersections, point localization, triangulation, Voronoi diagrams, area
and volume calculations, convex hull (Graham scan), sweep line
algorithms
Kattis ([Link] 69
How Kattis checks a program 70
Compilation
Compiles? Error
Incorrect Wrong
output? Answer
Accepted
UVA Online Judge 71
[Link]
Programming languages 72
Always create an example input (.in) and example output (.out) file with
verbatim copies of the example input and output from the problem
statement!
For most problems it is enough to diff your output with the example output:
./prog < [Link] | diff - [Link]
Create additional tests, such as:
▪ Extreme inputs, i.e. smallest and largest values (0, 1, “”, empty line, 2^31-1)
▪ Small inputs that you can compute by hand
▪ Potentially tricky cases such as when all inputs are equal, in the case of floating
points numbers when you have to round both up and down
▪ Very large cases, randomly generated to test that your program computes an
answer fast enough (even though you might not know the correct answer).
Use a correct but slow algorithm to compute answers.
Print intermediate information, such as values of relevant variables.
cout << “a=“ << a << “; b=“ << b << endl;
Remember to remove the debug output before submitting! (or use cerr)
Summary 75