Data Structures & AlgorithmsVery hardGATE CS · Codeforces Div 2
A chain of 6 matrices M1 to M6 has dimensions given by the sequence p = [20, 15, 18, 8, 2, 3, 20], where Mi has size p[i−1] × p[i] (1-indexed). Multiplying two matrices of sizes a×b and b×c costs a·b·c scalar multiplications. What is the minimum total number of scalar multiplications over all parenthesisations?
- A. 14720
- B. 3390
- C. 2348
- D. 9920
Correct answer: C. 2348
The interval DP gives 2348. Multiplying strictly left to right costs 9920; the greedy 'cheapest adjacent pair first' rule gives 3390, which is not optimal.
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- Strings a = "bccbc" and b = "bbacaa". The edit distance allows insert, delete and substitute, each costing 1 (the standard Levenshtein…
- Directed acyclic graph on vertices A, B, C, D, E, F, G (every edge goes from a smaller to a larger letter) with weighted edges A→C (9)…
- Undirected weighted graph on vertices A, B, C, D with edges A–B (3), A–C (2), A–D (1), B–C (2), B–D (2), C–D (1). How many distinct…
- Keys [2, 42, 28, 49, 8, 35] are inserted, in this order, into an empty AVL tree using the standard rebalancing (left, right, left–right…
- Distinct words ['a', 'cbb', 'c', 'b'] are inserted into an empty trie (one node per character, shared prefixes share nodes, and a root)…
- Directed weighted graph on vertices A, B, C, D, E with edges listed in this exact order: A→B (6); E→A (4); C→D (5); B→E (5); B→C (4); A→D…