16
CS F364: DESIGN &A NALYSIS OF A LGORITHMS Lecture-01: Introduction Dr. Kamlesh Tiwari, Assistant Professor, Department of Computer Science and Information Systems, BITS Pilani, Rajasthan-333031 INDIA Jan 07, 2020 (Campus @ BITS-Pilani Jan-May 2017)

CS F364: Design & Analysis of Algorithms · Logistics: (CS F364) Design & Analysis of Algorithms T Th S (11-12PM) 5101@BITS-Pilani Jointly to be taught by Dr. Abhishek Mishra (IC)

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CS F364: Design & Analysis of Algorithms · Logistics: (CS F364) Design & Analysis of Algorithms T Th S (11-12PM) 5101@BITS-Pilani Jointly to be taught by Dr. Abhishek Mishra (IC)

CS F364: DESIGN & ANALYSIS OF ALGORITHMS

Lecture-01: Introduction

Dr. Kamlesh Tiwari,Assistant Professor,

Department of Computer Science and Information Systems,BITS Pilani, Rajasthan-333031 INDIA

Jan 07, 2020 (Campus @ BITS-Pilani Jan-May 2017)

Page 2: CS F364: Design & Analysis of Algorithms · Logistics: (CS F364) Design & Analysis of Algorithms T Th S (11-12PM) 5101@BITS-Pilani Jointly to be taught by Dr. Abhishek Mishra (IC)

Logistics: (CS F364) Design & Analysis of Algorithms

T Th S (11-12PM) 5101@BITS-PilaniJointly to be taught byDr. Abhishek Mishra (IC) and Dr. Kamlesh Tiwari.Grading

I Tutorial Quiz (32%) 4 of 8% each, Open BookI Mid Semester Exam (28%) Open BookI Comprehensive Exam (40%) Open Book

Learn algorithm design techniques like Divide and Conquer, Greedy,Dynamic Programming, Approximation Algorithms, and RandomizedAlgorithms. Explore topics like Computational Complexity etc.

Books:[1] T.H. Cormen, C.E. Leiserson, R.L. Rivest, C. Stein, Introduction to Algorithms, 3rd Edition, PHI, 2012.[2] S. Arora, B. Barak, Computational Complexity: A Modern Approach, Cambridge University Press, 2009[3] J.Kleinberg, E. Tardos, Algorithm Design, Pearson, 2013.

Design & Analysis of Algo. (CS F364) T Th S (11-12PM) 5101@BITS-Pilani Lecture-01 (Jan 07, 2020) 2 / 7

Page 3: CS F364: Design & Analysis of Algorithms · Logistics: (CS F364) Design & Analysis of Algorithms T Th S (11-12PM) 5101@BITS-Pilani Jointly to be taught by Dr. Abhishek Mishra (IC)

Introduction

Computational Problems

Algorithms: input, output, definiteness, finiteness, effectivenessPseudo code

Input sizeAnalysis

I Kind of resources1: time, space, number of gates ...I Cases: Best, Worst and Average

Correctness: initialize well, maintain invariance and terminateOrder of growth: O, o, θ, ω, Ω zooInsertion and Merge sort

1Complexity is a functionDesign & Analysis of Algo. (CS F364) T Th S (11-12PM) 5101@BITS-Pilani Lecture-01 (Jan 07, 2020) 3 / 7

Page 4: CS F364: Design & Analysis of Algorithms · Logistics: (CS F364) Design & Analysis of Algorithms T Th S (11-12PM) 5101@BITS-Pilani Jointly to be taught by Dr. Abhishek Mishra (IC)

Introduction

Computational ProblemsAlgorithms: input, output, definiteness, finiteness, effectivenessPseudo code

Input sizeAnalysis

I Kind of resources1: time, space, number of gates ...I Cases: Best, Worst and Average

Correctness: initialize well, maintain invariance and terminateOrder of growth: O, o, θ, ω, Ω zooInsertion and Merge sort

1Complexity is a functionDesign & Analysis of Algo. (CS F364) T Th S (11-12PM) 5101@BITS-Pilani Lecture-01 (Jan 07, 2020) 3 / 7

Page 5: CS F364: Design & Analysis of Algorithms · Logistics: (CS F364) Design & Analysis of Algorithms T Th S (11-12PM) 5101@BITS-Pilani Jointly to be taught by Dr. Abhishek Mishra (IC)

Introduction

Computational ProblemsAlgorithms: input, output, definiteness, finiteness, effectivenessPseudo code

Input size

AnalysisI Kind of resources1: time, space, number of gates ...I Cases: Best, Worst and Average

Correctness: initialize well, maintain invariance and terminateOrder of growth: O, o, θ, ω, Ω zooInsertion and Merge sort

1Complexity is a functionDesign & Analysis of Algo. (CS F364) T Th S (11-12PM) 5101@BITS-Pilani Lecture-01 (Jan 07, 2020) 3 / 7

Page 6: CS F364: Design & Analysis of Algorithms · Logistics: (CS F364) Design & Analysis of Algorithms T Th S (11-12PM) 5101@BITS-Pilani Jointly to be taught by Dr. Abhishek Mishra (IC)

Introduction

Computational ProblemsAlgorithms: input, output, definiteness, finiteness, effectivenessPseudo code

Input sizeAnalysis

I Kind of resources1: time, space, number of gates ...I Cases: Best, Worst and Average

Correctness: initialize well, maintain invariance and terminateOrder of growth: O, o, θ, ω, Ω zooInsertion and Merge sort

1Complexity is a functionDesign & Analysis of Algo. (CS F364) T Th S (11-12PM) 5101@BITS-Pilani Lecture-01 (Jan 07, 2020) 3 / 7

Page 7: CS F364: Design & Analysis of Algorithms · Logistics: (CS F364) Design & Analysis of Algorithms T Th S (11-12PM) 5101@BITS-Pilani Jointly to be taught by Dr. Abhishek Mishra (IC)

Introduction

Computational ProblemsAlgorithms: input, output, definiteness, finiteness, effectivenessPseudo code

Input sizeAnalysis

I Kind of resources1: time, space, number of gates ...I Cases: Best, Worst and Average

Correctness: initialize well, maintain invariance and terminate

Order of growth: O, o, θ, ω, Ω zooInsertion and Merge sort

1Complexity is a functionDesign & Analysis of Algo. (CS F364) T Th S (11-12PM) 5101@BITS-Pilani Lecture-01 (Jan 07, 2020) 3 / 7

Page 8: CS F364: Design & Analysis of Algorithms · Logistics: (CS F364) Design & Analysis of Algorithms T Th S (11-12PM) 5101@BITS-Pilani Jointly to be taught by Dr. Abhishek Mishra (IC)

Introduction

Computational ProblemsAlgorithms: input, output, definiteness, finiteness, effectivenessPseudo code

Input sizeAnalysis

I Kind of resources1: time, space, number of gates ...I Cases: Best, Worst and Average

Correctness: initialize well, maintain invariance and terminateOrder of growth: O, o, θ, ω, Ω zooInsertion and Merge sort

1Complexity is a functionDesign & Analysis of Algo. (CS F364) T Th S (11-12PM) 5101@BITS-Pilani Lecture-01 (Jan 07, 2020) 3 / 7

Page 9: CS F364: Design & Analysis of Algorithms · Logistics: (CS F364) Design & Analysis of Algorithms T Th S (11-12PM) 5101@BITS-Pilani Jointly to be taught by Dr. Abhishek Mishra (IC)

Insertion Sort

Incremental algorithm paradigm:

Algorithm 1: INSERTION-SORT (A)1 for j = 2 to A.length do2 key = A[j]3 i = j − 14 while i > 0 and A[i] > key do5 A[i + 1] = A[i]6 i = i − 1

7 A[i + 1] = key

Design & Analysis of Algo. (CS F364) T Th S (11-12PM) 5101@BITS-Pilani Lecture-01 (Jan 07, 2020) 4 / 7

Page 10: CS F364: Design & Analysis of Algorithms · Logistics: (CS F364) Design & Analysis of Algorithms T Th S (11-12PM) 5101@BITS-Pilani Jointly to be taught by Dr. Abhishek Mishra (IC)

Analyse Insertion Sort

INSERTION-SORT (A) cost times1 for j = 2 to A.length c1 n2 key = A[j] c2 n − 13 i = j − 1 c3 n − 14 while i > 0 and A[i] > key c4

∑nj=2 tj

5 A[i + 1] = A[i] c5∑n

j=2(tj − 1)

6 i = i − 1 c6∑n

j=2(tj − 1)

7 A[i + 1] = key c7 n − 1

Best case T (n) = O(n)

Worst case T (n) = O(n2)

Average ?

Design & Analysis of Algo. (CS F364) T Th S (11-12PM) 5101@BITS-Pilani Lecture-01 (Jan 07, 2020) 5 / 7

Page 11: CS F364: Design & Analysis of Algorithms · Logistics: (CS F364) Design & Analysis of Algorithms T Th S (11-12PM) 5101@BITS-Pilani Jointly to be taught by Dr. Abhishek Mishra (IC)

Analyse Insertion Sort

INSERTION-SORT (A) cost times1 for j = 2 to A.length c1 n2 key = A[j] c2 n − 13 i = j − 1 c3 n − 14 while i > 0 and A[i] > key c4

∑nj=2 tj

5 A[i + 1] = A[i] c5∑n

j=2(tj − 1)

6 i = i − 1 c6∑n

j=2(tj − 1)

7 A[i + 1] = key c7 n − 1

Best case T (n) = O(n)

Worst case T (n) = O(n2)

Average ?

Design & Analysis of Algo. (CS F364) T Th S (11-12PM) 5101@BITS-Pilani Lecture-01 (Jan 07, 2020) 5 / 7

Page 12: CS F364: Design & Analysis of Algorithms · Logistics: (CS F364) Design & Analysis of Algorithms T Th S (11-12PM) 5101@BITS-Pilani Jointly to be taught by Dr. Abhishek Mishra (IC)

Merge sortDivide and conquer paradigm: Divide, Conquer and Combine

T (n) =

Θ(1) if n ≤ caT (n/b) + D(n) + C(n) otherwise

MERGE-SORT (A,p,r)1 if p < r2 q = b(p + r)/2c3 MERGE-SORT (A,p,q)4 MERGE-SORT (A,q+1,r)5 MERGE (A,p,q,r)

Average case T (n) = O(n log n). Best and Worst?

Design & Analysis of Algo. (CS F364) T Th S (11-12PM) 5101@BITS-Pilani Lecture-01 (Jan 07, 2020) 6 / 7

Page 13: CS F364: Design & Analysis of Algorithms · Logistics: (CS F364) Design & Analysis of Algorithms T Th S (11-12PM) 5101@BITS-Pilani Jointly to be taught by Dr. Abhishek Mishra (IC)

Merge sortDivide and conquer paradigm: Divide, Conquer and Combine

T (n) =

Θ(1) if n ≤ caT (n/b) + D(n) + C(n) otherwise

MERGE-SORT (A,p,r)1 if p < r2 q = b(p + r)/2c3 MERGE-SORT (A,p,q)4 MERGE-SORT (A,q+1,r)5 MERGE (A,p,q,r)

Average case T (n) = O(n log n). Best and Worst?

Design & Analysis of Algo. (CS F364) T Th S (11-12PM) 5101@BITS-Pilani Lecture-01 (Jan 07, 2020) 6 / 7

Page 14: CS F364: Design & Analysis of Algorithms · Logistics: (CS F364) Design & Analysis of Algorithms T Th S (11-12PM) 5101@BITS-Pilani Jointly to be taught by Dr. Abhishek Mishra (IC)

Merge sortDivide and conquer paradigm: Divide, Conquer and Combine

T (n) =

Θ(1) if n ≤ caT (n/b) + D(n) + C(n) otherwise

MERGE-SORT (A,p,r)1 if p < r2 q = b(p + r)/2c3 MERGE-SORT (A,p,q)4 MERGE-SORT (A,q+1,r)5 MERGE (A,p,q,r)

Average case T (n) = O(n log n). Best and Worst?

Design & Analysis of Algo. (CS F364) T Th S (11-12PM) 5101@BITS-Pilani Lecture-01 (Jan 07, 2020) 6 / 7

Page 15: CS F364: Design & Analysis of Algorithms · Logistics: (CS F364) Design & Analysis of Algorithms T Th S (11-12PM) 5101@BITS-Pilani Jointly to be taught by Dr. Abhishek Mishra (IC)

Merge sortDivide and conquer paradigm: Divide, Conquer and Combine

T (n) =

Θ(1) if n ≤ caT (n/b) + D(n) + C(n) otherwise

MERGE-SORT (A,p,r)1 if p < r2 q = b(p + r)/2c3 MERGE-SORT (A,p,q)4 MERGE-SORT (A,q+1,r)5 MERGE (A,p,q,r)

Average case T (n) = O(n log n). Best and Worst?

Design & Analysis of Algo. (CS F364) T Th S (11-12PM) 5101@BITS-Pilani Lecture-01 (Jan 07, 2020) 6 / 7

Page 16: CS F364: Design & Analysis of Algorithms · Logistics: (CS F364) Design & Analysis of Algorithms T Th S (11-12PM) 5101@BITS-Pilani Jointly to be taught by Dr. Abhishek Mishra (IC)

Thank You!

Thank you very much for your attention! (Reference2)

Queries ?

2[1] Book - Introduction to Algorithm, By THOMAS H. CORMEN, CHARLES E. LEISERSON, RONALD L. RIVEST,

CLIFFORD STEIN

Design & Analysis of Algo. (CS F364) T Th S (11-12PM) 5101@BITS-Pilani Lecture-01 (Jan 07, 2020) 7 / 7