Transform & Conquer - University of Cape Town

Download Report

Transcript Transform & Conquer - University of Cape Town

Transform & Conquer
Replacing One Problem With Another
Saadiq Moolla
Introduction
• Two Stage Solution
− Transform into Another Problem
− Solve New Problem
Initial Problem
New
Representation
• Three Variations
− Instance Simplification
− Representation Change
− Problem Reduction
Solution
Instance Simplification
• Reducing the Problem to a Simpler One
• Techniques:
− Presorting
− Gaussian Elimination
− Search Trees
Presorting
• Example: Given a random list of
numbers, determine if there are any
duplicates.
• Brute Force: Compare Every Pair
• Transform and Conquer:
A
− Sort the List
− Compare A [i] with A [i + 1]
i
Presorting (Analysis)
• Old Idea, Many Different Ways
• Efficiency Dependant on Algorithm
• Compare Benefits vs Time Required
• Useful if Operation Repeated
Gaussian Elimination
a x+a x+…+a x=b
a x+a x+…+a x=b
11
12
1n
1
21
22
2n
2
…
a x+a x+…+a x=b
n1
n2
nn
1
a’ x + a’ x + … + a’ x = b’
a’ x + … + a’ x = b
11
12
1n
22
2n
1
2
…
a’ x = b
nn
1
Binary Search Trees
8
4
15
1
7
5
9
Binary Search Trees (Analysis)
• Time Efficiency
• Slow
• Methods to Balance
• Special Trees:
− AVL Trees
− B-trees
Heaps
9
• Type of Binary Tree
• Requirements:
− Essentially Complete
− Parental Dominance
5
4
7
2
1
Properties of Heaps
• Height is log n
2
• Root is largest element
• Node + Descendents = Heap
• Stored in Array:
− Children of A [i] are A [2i] and A [2i + 1]
Heap Insertion
9
5
7
9
4
2
1
8
5
4
8
2
1
7
Heaps (Analysis)
• Sorting
• Priority Queue
• O (n log n)
Representation Change
• Change One Problem Into Another
• Steps:
− Identify
− Transform
− Solve
• Mathematical Modeling
Problem Reduction
• Reduce to a Known Problem
• Use Known Algorithms:
− Dijkstra’s algorithm
− Horner’s Rule
− Karp – Rabin algorithm
− etc.
Horner’s Rule
• Used to Evaluate Polynomials
• 5x^2 – 3x + 8 » (5x + 3)x + 8
• 7x^3 + x^2 – 9x – 2 » ((7x + 1)x – 9)x – 2
• Linear
Horner’s Algorithm
algorithm Horner (x, P [])
// Evaluates a polynomial with coefficients P [] at x
for i ← n – 1 downto 0 do
v ← x * v + P [i]
return v
Fast Exponentiation
• Evaluate x^n
• 2^10 = 2^10102 = 2^8 * 2^2
Fast Exponentiation Algorithm
Algorithm FastExp (x, n)
// Returns x^n
term ← x
product ← 1
while n > 0 do
if n mod 2 = 1 then
product ← product * term
term ← term * term
n ← └ n/2 ┘
return product