col106 data structures and algorithms
play

COL106: Data Structures and Algorithms Ragesh Jaiswal, IIT Delhi - PowerPoint PPT Presentation

COL106: Data Structures and Algorithms Ragesh Jaiswal, IIT Delhi Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms Introduction How do Data Structures play a part in making computational tasks efficient? Ragesh Jaiswal, IIT


  1. COL106: Data Structures and Algorithms Ragesh Jaiswal, IIT Delhi Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

  2. Introduction How do Data Structures play a part in making computational tasks efficient? Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

  3. Introduction Digression: Binary Search → Recursive Functions Problem Multiplying two n -bit numbers: Given two n -bit numbers, A and B , Design an algorithm to output A · B . Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

  4. Introduction Digression: Binary Search → Recursive Functions Problem Multiplying two n -bit numbers: Given two n -bit numbers, A and B , Design an algorithm to output A · B . Solution 1: Use long multiplication. What is the running time of the algorithm that uses long multiplication? Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

  5. Introduction Digression: Binary Search → Recursive Functions Problem Multiplying two n -bit numbers: Given two n -bit numbers, A and B , Design an algorithm to output A · B . Solution 1: Use long multiplication. What is the running time of the algorithm that uses long multiplication? O ( n 2 ) Is there a faster algorithm? Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

  6. Introduction Digression: Binary Search → Recursive Functions Problem Multiplying two n -bit numbers: Given two n -bit numbers, A and B , Design an algorithm to output A · B . Solution 1: Algorithm using long multiplication with running time O ( n 2 ). Solution 2: (Assume n is a power of 2) Write A = A L · 2 n / 2 + A R and B = B L · 2 n / 2 + B R . So, A · B = ( A L · B L ) · 2 n + ( A L · B R + A R · B L ) · 2 n / 2 + ( A R · B R ) Main Idea: Compute ( A L · B L ), ( A R · B R ), and ( A R · B L ), and ( A L · B R ) and combine these values. Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

  7. Introduction Digression: Binary Search → Recursive Functions Problem Multiplying two n -bit numbers: Given two n -bit numbers, A and B , Design an algorithm to output A · B . Solution 1: Algorithm using long multiplication with running time O ( n 2 ). Solution 2: (Assume n is a power of 2) Write A = A L · 2 n / 2 + A R and B = B L · 2 n / 2 + B R . So, A · B = ( A L · B L ) · 2 n + ( A L · B R + A R · B L ) · 2 n / 2 + ( A R · B R ) Main Idea: Compute ( A L · B L ), ( A R · B R ), and ( A R · B L ), and ( A L · B R ) and combine these values. Algorithm DivideAndConquer( A , B ) - If ( | A | = | B | = 1) return( A · B ) - Split A into A L and A R - Split B into B L and B R - P ← DivideAndConquer( A L , B L ) - Q ← DivideAndConquer( A R , B R ) - R ← DivideAndConquer( A L , B R ) - S ← DivideAndConquer( A R , B L ) - return( Combine( P , Q , R , S ) ) What is the recurrence relation for the running time of the above algorithm? Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

  8. Introduction Digression: Binary Search → Recursive Functions Problem Multiplying two n -bit numbers: Given two n -bit numbers, A and B , Design an algorithm to output A · B . Algorithm DivideAndConquer( A , B ) - If ( | A | = | B | = 1) return( A · B ) - Split A into A L and A R - Split B into B L and B R - P ← DivideAndConquer( A L , B L ) - Q ← DivideAndConquer( A R , B R ) - R ← DivideAndConquer( A L , B R ) - S ← DivideAndConquer( A R , B L ) - return( Combine( P , Q , R , S ) ) What is the recurrence relation for the running time of the above algorithm? T ( n ) = 4 · T ( n / 2) + O ( n ) for n > 1 and T (1) = O (1). What is the solution to the above recurrence relation? Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

  9. Introduction Digression: Binary Search → Recursive Functions Problem Multiplying two n -bit numbers: Given two n -bit numbers, A and B , Design an algorithm to output A · B . Algorithm DivideAndConquer( A , B ) - If ( | A | = | B | = 1) return( A · B ) - Split A into A L and A R - Split B into B L and B R - P ← DivideAndConquer( A L , B L ) - Q ← DivideAndConquer( A R , B R ) - R ← DivideAndConquer( A L , B R ) - S ← DivideAndConquer( A R , B L ) - return( Combine( P , Q , R , S ) ) What is the recurrence relation for the running time of the above algorithm? T ( n ) = 4 · T ( n / 2) + O ( n ) for n > 1 and T (1) = O (1). What is the solution to the above recurrence relation? T ( n ) = O ( n 2 ). Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

  10. Introduction Digression: Binary Search → Recursive Functions Problem Multiplying two n -bit numbers: Given two n -bit numbers, A and B , Design an algorithm to output A · B . Solution 1: Algorithm using long multiplication with running time O ( n 2 ). Solution 2: Na¨ ıve Divide and Conquer with running time O ( n 2 ). Solution 3: Write A = A L · 2 n / 2 + A R and B = B L · 2 n / 2 + B R . So, A · B = ( A L · B L ) · 2 n + ( A L · B R + A R · B L ) · 2 n / 2 + ( A R · B R ) Main Idea: Compute ( A L · B L ), ( A R · B R ), and ( A L + B L ) · ( A R + B R ) − ( A L · B L ) − ( A R · B R ). Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

  11. Introduction Digression: Binary Search → Recursive Functions Problem Multiplying two n -bit numbers: Given two n -bit numbers, A and B , Design an algorithm to output A · B . Algorithm Karatsuba( A , B ) - If ( | A | = | B | = 1) return( A · B ) - Split A into A L and A R - Split B into B L and B R - P ← Karatsuba( A L , B L ) - Q ← Karatsuba( A R , B R ) - R ← Karatsuba( A L + A R , B L + B R ) - return( Combine( P , Q , R ) ) What is the recurrence relation for the running time of the above algorithm? Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

  12. Introduction Digression: Binary Search → Recursive Functions Problem Multiplying two n -bit numbers: Given two n -bit numbers, A and B , Design an algorithm to output A · B . Algorithm Karatsuba( A , B ) - If ( | A | = | B | = 1) return( A · B ) - Split A into A L and A R - Split B into B L and B R - P ← Karatsuba( A L , B L ) - Q ← Karatsuba( A R , B R ) - R ← Karatsuba( A L + A R , B L + B R ) - return( Combine( P , Q , R ) ) Recurrence relation: T ( n ) ≤ 3 · T ( n / 2) + cn ; T (1) ≤ c . What is the solution of this recurrence relation? Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

  13. Introduction Digression: Binary Search → Recursive Functions Problem Multiplying two n -bit numbers: Given two n -bit numbers, A and B , Design an algorithm to output A · B . Algorithm Karatsuba( A , B ) - If ( | A | = | B | = 1) return( A · B ) - Split A into A L and A R - Split B into B L and B R - P ← Karatsuba( A L , B L ) - Q ← Karatsuba( A R , B R ) - R ← Karatsuba( A L + A R , B L + B R ) - return( Combine( P , Q , R ) ) Recurrence relation: T ( n ) ≤ 3 · T ( n / 2) + cn ; T (1) ≤ c . What is the solution of this recurrence relation? T ( n ) ≤ O ( n log 2 3 ) Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

  14. End Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

Recommend


More recommend