Algorithms Lab
CSE Department, IIT Kharagpur
Autumn 2025
A4: Intersections of permutation lines 12-Aug-2025
Consider two parallel lines: 𝐿 B 𝑦 = 25 and 𝐿′ B 𝑦 = 125.
Suppose there are 𝑛 points uniformly placed on each of them such that:
1. The leftmost point on either line have 𝑥 = 25.
2. The 𝑖-th point from the left on each line has the same 𝑥-coordinate: 𝑥 = 25𝑖 ∀ 𝑖 ∈ [1, 𝑛].
3. The points on 𝐿 are labeled 1 through 𝑛 from left to right.
4. The points on 𝐿′ are also labeled by 1 to 𝑛, but in an arbitrary order.
For each label 𝜆, consider the pair of points (one on 𝐿 and one on 𝐿′) having that label. We thus
have 𝑛 such pairs and 𝑛 corresponding line segments, referred to as permutation lines. Determine
the number of intersections among these permutation lines.
Part A: Design and implement an O(𝑛 2 )-time algorithm. It should take 𝑛 as user input, place the
points uniformly on 𝐿 and 𝐿′, and print to the terminal all segment pairs that intersect, along with
the total number of intersections. 10 marks
As a visual output, it should also create an SVG file. Color each segment as follows: m5
• No intersections: green
• At most ⌈log2 𝑛⌉ intersections: blue
• More than ⌈log2 𝑛⌉ intersections: red
Part B: Design and implement an O(𝑛 log 𝑛)-time algorithm. Follow the same input/output
conventions as in Part A. This algorithm is analogous to counting inversions. 15 + 5 marks
Example 0.1 (Intersections of permutation lines)
1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10
3 2 1 4 6 8 7 5 9 10 7 2 6 9 5 3 8 1 10 4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
1 4 2 3 6 5 11 7 8 14 9 10 17 12 13 16 19 18 21 20 15 22 23 24 25
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
2 11 5 10 4 1 12 7 20 3 6 13 9 15 8 18 23 24 19 14 22 17 21 25 16
1
A4*: Minimum-ink Triangulation (Take home) 16-Aug-2025
Triangulation of a convex polygon 𝑃 with 𝑛 vertices is a partition of the polygon into triangles,
using non-crossing diagonals. Our goal is to triangulate 𝑃 by a pen, spending minimum ink, which
we refer to as minimum-ink triangulation.
≠ min ink ≠ min ink ≠ min ink min ink ≠ min ink
For a convex pentagon, there are just five triangulations, as shown above, the 4th one taking
minimum ink. As triangulation-count shoots up exponentially with 𝑛, minimum-ink triangulation
looks difficult. For example, for a decagon (𝑛 = 10), it is 1430, while for a 20-gon, it leaps to
656,412,042.
Solution
Any triangulation splits the polygon into 𝑛 − 2 triangles using 𝑛 − 3 non-crossing diagonals.
The number of triangulations being exponential in 𝑛, we use dynamic programming to find
the solution in polynomial time. We assume that the vertices are given in counterclockwise
order.
Definitions:
• 𝑃(𝑖, 𝑗) B subpolygon with vertices 𝑖, 𝑖 + 1, . . . , 𝑗.
• 𝜋(·) B perimeter.
• 𝜋(𝑖, 𝑘, 𝑗) B perimeter of △𝑖 𝑘 𝑗.
• 𝑓 (𝑖, 𝑗) B minimum ink needed to triangulate 𝑃(𝑖, 𝑗).
Observation 1: In any triangulation S:
• Every edge of 𝑃 appears in exactly one triangle 𝑇 ∈ S.
• Every diagonal d ∈ S is shared by exactly two triangles.
Hence, Õ Õ
𝜋(𝑇) = 𝜋(𝑃) + 2 · d.
𝑇 ∈S d∈S
Õ
As 𝜋(𝑃) is fixed, we revise our goal to: Minimize: 𝜋(𝑇).
𝑇 ∈S
Observation 2: For any subpolygon 𝑃(𝑖, 𝑗), where 𝑗 ≥ 𝑖 + 2 2
1
𝑛
(indices taken modulo 𝑛), there always exists a minimum-cost
·
triangulation that includes △𝑖𝑘 𝑗 for some vertex 𝑘 lying strictly 𝑖
··
···
between 𝑖 and 𝑗 in counterclockwise order. This triangle splits 𝑗
𝑃(𝑖, 𝑗) into two smaller subpolygons, 𝑃(𝑖, 𝑘) and 𝑃(𝑘, 𝑗), which Δ𝑖𝑘 𝑗
can then be recursively triangulated optimally— this leads natu-
rally to a dynamic programming formulation. 𝑃(𝑖, 𝑘) 𝑘 𝑃(𝑘, 𝑗)
Optimal substructure:
• 𝑗 − 𝑖 ≥ 2 ⇒ 𝑓 (𝑖, 𝑗) = min 𝑓 (𝑖, 𝑘) + 𝑓 (𝑘, 𝑗) + 𝜋(𝑖, 𝑘, 𝑗)
𝑖<𝑘<𝑗
• 𝑗 − 𝑖 < 2 ⇒ 𝑓 (𝑖, 𝑗) = 0 (no triangle).
2
Overlapping subproblems: The same subpolygon 𝑃(𝑖, 𝑗) may arise in multiple recursive
decompositions. Hence, memoization or bottom-up DP is used to avoid redundant recom-
putation.
Algorithm 1: Minimum-ink Triangulation 1 (24,16)
1 for 𝑖 ← 1 to 𝑛 do (12,20) 2
5 (44,24)
2 𝑓 (𝑖, 𝑖) ← 0
3 𝑓 (𝑖, (𝑖 + 1) mod 𝑛) ← 0
(20,36) 3 4 (36,36)
4 for gap ← 2 to 𝑛 − 1 do
for 𝑖 ← 1 to 𝑛 − gap do
ga
ga 2
ga 3
5
p
=
=
𝑗 ← 𝑖 + gap
4
6
7 𝑓 (𝑖, 𝑗) ← ∞ 𝑗 =1 0 0 51 111 170
8 for 𝑘 ← 𝑖 + 1 to 𝑗 − 1 do 2 0 0 63 134
9 cost ← 𝑓 (𝑖, 𝑘) + 𝑓 (𝑘, 𝑗) + 𝜋(𝑖, 𝑘, 𝑗)
3 0 0 57
10 if cost < 𝑓 (𝑖, 𝑗) then
𝑓 (𝑖, 𝑗) ← cost 4 0 0
U
11
nu
trace(𝑖, 𝑗) ← 𝑘
se
12 5 0
d
𝑖 =1 2 3 4 5
13 return 𝑓 (1, 𝑛) array of 𝑓 (𝑖, 𝑗)
Computational steps to fill up the DP table
gap 𝑖 𝑗 𝑘 𝜋(𝑖, 𝑘, 𝑗) 𝑓 (𝑖, 𝑗) trace(𝑖, 𝑗)
2 1 3 2 𝑓 (1, 2) + 𝑓 (2, 3) + 𝜋(1, 2, 3) = 0 + 0 + 112 = 112 51 2
2 4 3 𝑓 (2, 3) + 𝑓 (3, 4) + 𝜋(2, 3, 4) = 0 + 0 + 128 = 128 63 3
3 5 4 𝑓 (3, 4) + 𝑓 (4, 5) + 𝜋(3, 4, 5) = 0 + 0 + 96 = 96 57 4
3 1 4 2 𝑓 (1, 2) + 𝑓 (2, 4) + 𝜋(1, 2, 4) = 0 + 63 + 65 = 128
3 𝑓 (1, 3) + 𝑓 (3, 4) + 𝜋(1, 3, 4) = 51 + 0 + 60 = 111 111 3
2 5 3 𝑓 (2, 3) + 𝑓 (3, 5) + 𝜋(2, 3, 5) = 0 + 57 + 77 = 134 134 3
4 𝑓 (2, 4) + 𝑓 (4, 5) + 𝜋(2, 4, 5) = 63 + 0 + 76 = 139
4 1 5 2 𝑓 (1, 2) + 𝑓 (2, 5) + 𝜋(1, 2, 5) = 0 + 134 + 66 = 200
3 𝑓 (1, 3) + 𝑓 (3, 5) + 𝜋(1, 3, 5) = 51 + 57 + 69 = 177
4 𝑓 (1, 4) + 𝑓 (4, 5) + 𝜋(1, 4, 5) = 111 + 0 + 59 = 170 170 4
tracing back in linear time
to collect the diagonals (1, 4) and (1, 3), using trace(𝑖, 𝑗)
Time and space complexities: There are O(𝑛 2 ) distinct subproblems 𝑓 (𝑖, 𝑗) with 𝑖 < 𝑗. For
each such subproblem, we try all 𝑘 with 𝑖 < 𝑘 < 𝑗, giving at most 𝑛 choices. Hence, total
time complexity is O(𝑛 3 ). The space complexity is O(𝑛 2 ) to store the DP table.
3
Example 0.2 (Minimum-ink Triangulation of convex polygons)
The top-left point has coordinates (0, 0). The +𝑥-axis is directed rightward, the +𝑦-axis downward.
7 vertices: 7 1
370 30 6 2
440 60
490 110
260 180 3
90 130 5
160 60
260 30
4
8 1
8 vertices:
370 30
510 210
400 360
310 380
2
30 280 6
30 220
160 60
260 30 5
3
4
15 1
15 vertices:
14 2
370 30
440 60
490 110 3
510 210 13
450 320
400 360
310 380
4
200 380 12
130 365
70 320
30 280 11
30 220
90 130 10 5
160 60
260 30 9 6
8 7