38
アアアアアアアアアアアア 教教 教教 W4022 [email protected] SA 教教 教教 A4/A10 [email protected]

[PPT]Analysis of Algorithms · Web viewアルゴリズムの設計と解析 教授: 黄 潤和 (W4022) [email protected] SA: 広野 史明 (A4/A10) [email protected]

  • Upload
    buianh

  • View
    214

  • Download
    0

Embed Size (px)

Citation preview

Page 1: [PPT]Analysis of Algorithms · Web viewアルゴリズムの設計と解析 教授: 黄 潤和 (W4022) rhuang@hosei.ac.jp SA: 広野 史明 (A4/A10) fumiaki.hirono.5k@stu.hosei.ac.jp

アルゴリズムの設計と解析

教授: 黄 潤和  ( W4022 ) [email protected]

SA :  広野 史明 ( A4/A10 ) [email protected]

Page 2: [PPT]Analysis of Algorithms · Web viewアルゴリズムの設計と解析 教授: 黄 潤和 (W4022) rhuang@hosei.ac.jp SA: 広野 史明 (A4/A10) fumiaki.hirono.5k@stu.hosei.ac.jp

Goal

2

到達目標: The objectives of this course are to make students firmly laying  good foundation of data structures and algorithms, and one-step further comprehensively understanding algorithm analysis and having design and implementation skills in Python, Java, or other programming language.

Page 3: [PPT]Analysis of Algorithms · Web viewアルゴリズムの設計と解析 教授: 黄 潤和 (W4022) rhuang@hosei.ac.jp SA: 広野 史明 (A4/A10) fumiaki.hirono.5k@stu.hosei.ac.jp

3

Page 4: [PPT]Analysis of Algorithms · Web viewアルゴリズムの設計と解析 教授: 黄 潤和 (W4022) rhuang@hosei.ac.jp SA: 広野 史明 (A4/A10) fumiaki.hirono.5k@stu.hosei.ac.jp

Contents (L1 - Introduction)

What is an Algorithm?How to design?How to analyze?

4

Page 5: [PPT]Analysis of Algorithms · Web viewアルゴリズムの設計と解析 教授: 黄 潤和 (W4022) rhuang@hosei.ac.jp SA: 広野 史明 (A4/A10) fumiaki.hirono.5k@stu.hosei.ac.jp

Why study algorithms?Algorithms play the central role both in the science  practice From a practical standpoint

      - you have to know a standard set of important algorithms      - you should be able to design new algorithms

From theoretical standard       - the study of algorithms is the core of computer science                                             related to many other fields                                             useful in developing analytical skills

   5

Of computing 

Page 6: [PPT]Analysis of Algorithms · Web viewアルゴリズムの設計と解析 教授: 黄 潤和 (W4022) rhuang@hosei.ac.jp SA: 広野 史明 (A4/A10) fumiaki.hirono.5k@stu.hosei.ac.jp

Introduction  What is an Algorithm?An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate input in a finite amount of time.

A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 1 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 

合理的な

ambiguousあいまいな

The  algorithm  is  independent  from  any programming languages.

Page 7: [PPT]Analysis of Algorithms · Web viewアルゴリズムの設計と解析 教授: 黄 潤和 (W4022) rhuang@hosei.ac.jp SA: 広野 史明 (A4/A10) fumiaki.hirono.5k@stu.hosei.ac.jp

7

Page 8: [PPT]Analysis of Algorithms · Web viewアルゴリズムの設計と解析 教授: 黄 潤和 (W4022) rhuang@hosei.ac.jp SA: 広野 史明 (A4/A10) fumiaki.hirono.5k@stu.hosei.ac.jp

Two main approaches1.from typical problem types 2.from algorithm design techniques

8

Page 9: [PPT]Analysis of Algorithms · Web viewアルゴリズムの設計と解析 教授: 黄 潤和 (W4022) rhuang@hosei.ac.jp SA: 広野 史明 (A4/A10) fumiaki.hirono.5k@stu.hosei.ac.jp

1. from typical problem types     (a number of algorithms to a problem type)

9

Page 10: [PPT]Analysis of Algorithms · Web viewアルゴリズムの設計と解析 教授: 黄 潤和 (W4022) rhuang@hosei.ac.jp SA: 広野 史明 (A4/A10) fumiaki.hirono.5k@stu.hosei.ac.jp

10

Algorithm Features 

Page 11: [PPT]Analysis of Algorithms · Web viewアルゴリズムの設計と解析 教授: 黄 潤和 (W4022) rhuang@hosei.ac.jp SA: 広野 史明 (A4/A10) fumiaki.hirono.5k@stu.hosei.ac.jp

11

Difference between Algorithm and PseudocodeAn  algorithm  is  a  formal  definition  with  some  specific characteristics  that  describes  a  process,  which    could    be   executed    by    a    Turing-complete    computer   machine    to   perform  a  specific task. Generally, the word "algorithm" can be used to describe any high level task in computer science. 

Pseudocode    is    an    informal    and    (often    rudimentary)   human    readable  description  of  an  algorithm  leaving  many granular details of it. Writing a pseudocode has no restriction of  styles  and  its  only  objective  is  to  describe  the  high  level steps  of  algorithm  in  a  much  realistic  manner  in  natural language.

Page 12: [PPT]Analysis of Algorithms · Web viewアルゴリズムの設計と解析 教授: 黄 潤和 (W4022) rhuang@hosei.ac.jp SA: 広野 史明 (A4/A10) fumiaki.hirono.5k@stu.hosei.ac.jp

12

Sorting Searching (e.g. BFS, DFS, Red-black tree, 2-3-4 tree) Shortest paths in a graph (e.g. Dijkstra’s algorithm) Minimum spanning tree (e.g. Prim’s algorithm, Kruskal’s algorithm ) Traveling salesman problem (TSP tour approximation) Knapsack problem (Dynamic programming algorithm ) Chess Primality testing Towers of Hanoi Program termination

Some Well-known Computational Problems

Page 13: [PPT]Analysis of Algorithms · Web viewアルゴリズムの設計と解析 教授: 黄 潤和 (W4022) rhuang@hosei.ac.jp SA: 広野 史明 (A4/A10) fumiaki.hirono.5k@stu.hosei.ac.jp

13

Page 14: [PPT]Analysis of Algorithms · Web viewアルゴリズムの設計と解析 教授: 黄 潤和 (W4022) rhuang@hosei.ac.jp SA: 広野 史明 (A4/A10) fumiaki.hirono.5k@stu.hosei.ac.jp

14

e.g. Sorting problem      There are many different algorithms 

Page 15: [PPT]Analysis of Algorithms · Web viewアルゴリズムの設計と解析 教授: 黄 潤和 (W4022) rhuang@hosei.ac.jp SA: 広野 史明 (A4/A10) fumiaki.hirono.5k@stu.hosei.ac.jp

e.g. Greatest common factor

Problem:   Find gcd(m,n),                 the greatest common divisor of two nonnegative,                 not both zero integers m and n                e.g.:  gcd(60,24) = 12, gcd(60,0) = 60, …

(1) Euclid’s algorithm: it is based on repeated application of equality    

                           gcd(m,n) = gcd(n, m mod n)                           until the second number becomes 0,                            which makes the problem trivial.                           e.g.: gcd(60,24) = gcd(24,12) = gcd(12,0) = 

12

15

最大公約数

Page 16: [PPT]Analysis of Algorithms · Web viewアルゴリズムの設計と解析 教授: 黄 潤和 (W4022) rhuang@hosei.ac.jp SA: 広野 史明 (A4/A10) fumiaki.hirono.5k@stu.hosei.ac.jp

Other methods (to the same problem: Greatest common factor)(2) Brute force solution    Step 1  Assign the value of min{m,n} to t    Step 2  Divide m by t. If the remainder is 0, go to Step 3;

           otherwise, go to Step 4    Step 3  Divide n by t. If the remainder is 0, return t and stop;

           otherwise, go to Step 4    Step 4  Decrease t by 1 and go to Step 2(3) Finding the prime factors     Step 1  Find the prime factorization of m    Step 2  Find the prime factorization of n    Step 3  Find all the common prime factors    Step 4  Compute the product of all the  common prime 

factors           and return it as gcd(m,n)

16

Page 17: [PPT]Analysis of Algorithms · Web viewアルゴリズムの設計と解析 教授: 黄 潤和 (W4022) rhuang@hosei.ac.jp SA: 広野 史明 (A4/A10) fumiaki.hirono.5k@stu.hosei.ac.jp

2. from algorithm design techniques

17

Divide and conquer Decrease and conquer Transform and conquer Brute force Greedy approach Dynamic programming Backtracking and branch-and-bound Space and time tradeoffs

Some well-known algorithm design techniques

Page 18: [PPT]Analysis of Algorithms · Web viewアルゴリズムの設計と解析 教授: 黄 潤和 (W4022) rhuang@hosei.ac.jp SA: 広野 史明 (A4/A10) fumiaki.hirono.5k@stu.hosei.ac.jp

18

e. g. Divide and Conquer technique ( 分割統治 ) which is used in many different algorithms for solving different problems.

For example- Searching - Sorting- Matrix multiplication- ……

A design technique to solve different problems

Page 19: [PPT]Analysis of Algorithms · Web viewアルゴリズムの設計と解析 教授: 黄 潤和 (W4022) rhuang@hosei.ac.jp SA: 広野 史明 (A4/A10) fumiaki.hirono.5k@stu.hosei.ac.jp

19

Which is better?Two main issues:(1) How to design algorithms?     (solve the problem)(2) How to analyze algorithms?     (evaluate/optimize the algorithms)

Page 20: [PPT]Analysis of Algorithms · Web viewアルゴリズムの設計と解析 教授: 黄 潤和 (W4022) rhuang@hosei.ac.jp SA: 広野 史明 (A4/A10) fumiaki.hirono.5k@stu.hosei.ac.jp

Algorithm design techniques

Brute forceDivide and conquerDecrease and conquerTransform and conquerSpace and time tradeoffsGreedy approachDynamic programmingIterative improvementBacktracking Branch and bound……

20

Page 21: [PPT]Analysis of Algorithms · Web viewアルゴリズムの設計と解析 教授: 黄 潤和 (W4022) rhuang@hosei.ac.jp SA: 広野 史明 (A4/A10) fumiaki.hirono.5k@stu.hosei.ac.jp

Analysis of algorithmsHow good is the algorithm? correctness time efficiency space efficiency

Does there exist a better algorithm? lower bounds optimality

21

Page 22: [PPT]Analysis of Algorithms · Web viewアルゴリズムの設計と解析 教授: 黄 潤和 (W4022) rhuang@hosei.ac.jp SA: 広野 史明 (A4/A10) fumiaki.hirono.5k@stu.hosei.ac.jp

1-22

For example: sorting Rearrange the items of a given list in ascending order. Input: A sequence of n numbers <a1,   a2, …, an> Output: A reordering <a´

1,   a´2, …, a´

n> of the input sequence such that a´

1≤ a´2 ≤ … ≤ a´

n.

Why sorting? Help searching Algorithms often use sorting as a key subroutine.Sorting key A specially chosen piece of information used to guide sorting. E.g., sort student records by student ID.

Page 23: [PPT]Analysis of Algorithms · Web viewアルゴリズムの設計と解析 教授: 黄 潤和 (W4022) rhuang@hosei.ac.jp SA: 広野 史明 (A4/A10) fumiaki.hirono.5k@stu.hosei.ac.jp

Analysis of Algorithms 23

An example:

Approach

Pseudo code 

Page 24: [PPT]Analysis of Algorithms · Web viewアルゴリズムの設計と解析 教授: 黄 潤和 (W4022) rhuang@hosei.ac.jp SA: 広野 史明 (A4/A10) fumiaki.hirono.5k@stu.hosei.ac.jp

Analysis of Algorithms 24

 the best-case running time is (n)

the best case occurs if the array is already sorted,  t j =1

Algorithm  

Page 25: [PPT]Analysis of Algorithms · Web viewアルゴリズムの設計と解析 教授: 黄 潤和 (W4022) rhuang@hosei.ac.jp SA: 広野 史明 (A4/A10) fumiaki.hirono.5k@stu.hosei.ac.jp

Analysis of Algorithms 25

A worst-case running time of (n2)

O(n2)bound on worst-case running time of insertion sort

Page 26: [PPT]Analysis of Algorithms · Web viewアルゴリズムの設計と解析 教授: 黄 潤和 (W4022) rhuang@hosei.ac.jp SA: 広野 史明 (A4/A10) fumiaki.hirono.5k@stu.hosei.ac.jp

Analysis of Algorithm 26

Running Time  Most algorithms transform input objects into output objects.The running time of an algorithm typically grows with the input size.Average case time is often difficult to determine.We focus on the worst case running time.

Easier to analyze Crucial to applications such 

as games, finance and robotics

0

20

40

60

80

100

120

Runn

ing

Tim

e1000 2000 3000 4000

Input Size

best caseaverage caseworst case

Wednesday, May 10, 2023

Page 27: [PPT]Analysis of Algorithms · Web viewアルゴリズムの設計と解析 教授: 黄 潤和 (W4022) rhuang@hosei.ac.jp SA: 広野 史明 (A4/A10) fumiaki.hirono.5k@stu.hosei.ac.jp

27

Experimental StudiesWrite a program implementing the algorithmRun the program with inputs of varying size and compositionUse a method in Java like System.currentTimeMillis() or 

to get an accurate measure of the actual running timePlot the results

0

1000

2000

3000

4000

5000

6000

7000

8000

9000

0 50 100Input Size

Tim

e (m

s)

Page 28: [PPT]Analysis of Algorithms · Web viewアルゴリズムの設計と解析 教授: 黄 潤和 (W4022) rhuang@hosei.ac.jp SA: 広野 史明 (A4/A10) fumiaki.hirono.5k@stu.hosei.ac.jp

28

Comparison of different algorithms

Page 29: [PPT]Analysis of Algorithms · Web viewアルゴリズムの設計と解析 教授: 黄 潤和 (W4022) rhuang@hosei.ac.jp SA: 広野 史明 (A4/A10) fumiaki.hirono.5k@stu.hosei.ac.jp

Analysis of Algorithm 29

Big-Oh Notation To simplify the running time estimation,

    for a function f(n), we ignore the constants and lower order terms.

Example: 10n3+4n2-4n+5  is O(n3).  

Wednesday, May 10, 2023

Page 30: [PPT]Analysis of Algorithms · Web viewアルゴリズムの設計と解析 教授: 黄 潤和 (W4022) rhuang@hosei.ac.jp SA: 広野 史明 (A4/A10) fumiaki.hirono.5k@stu.hosei.ac.jp

Analysis of Algorithm 30

Big-Oh Notation To simplify the running time estimation,

    for a function f(n), we ignore the constants and lower order terms.

Example: 10n3+4n2-4n+5  is O(n3).  

Wednesday, May 10, 2023

Page 31: [PPT]Analysis of Algorithms · Web viewアルゴリズムの設計と解析 教授: 黄 潤和 (W4022) rhuang@hosei.ac.jp SA: 広野 史明 (A4/A10) fumiaki.hirono.5k@stu.hosei.ac.jp

Function of Growth rate

31

Page 32: [PPT]Analysis of Algorithms · Web viewアルゴリズムの設計と解析 教授: 黄 潤和 (W4022) rhuang@hosei.ac.jp SA: 広野 史明 (A4/A10) fumiaki.hirono.5k@stu.hosei.ac.jp

32

Some approximate of several functions important for analysis of algorithms

Page 33: [PPT]Analysis of Algorithms · Web viewアルゴリズムの設計と解析 教授: 黄 潤和 (W4022) rhuang@hosei.ac.jp SA: 広野 史明 (A4/A10) fumiaki.hirono.5k@stu.hosei.ac.jp

33

Efficiency is very much depended on data structure

Apart from the linked list, there are other often used data structure.

 

Page 34: [PPT]Analysis of Algorithms · Web viewアルゴリズムの設計と解析 教授: 黄 潤和 (W4022) rhuang@hosei.ac.jp SA: 広野 史明 (A4/A10) fumiaki.hirono.5k@stu.hosei.ac.jp

About this course

34

Reference book 

Thomas H. Cor-men, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein, MIT Press, 2009Anany Levitin, Addison-Wesley, 2011 

Textbook 

Anany Levitin, Addison-Wesley, 2011 

Page 35: [PPT]Analysis of Algorithms · Web viewアルゴリズムの設計と解析 教授: 黄 潤和 (W4022) rhuang@hosei.ac.jp SA: 広野 史明 (A4/A10) fumiaki.hirono.5k@stu.hosei.ac.jp

About this course Teaching plan

    It is expected to have slight adjustments

Evaluation   - Mid-term: exercise problems (20%)   - Exams: final exam (80%)     

35

Page 36: [PPT]Analysis of Algorithms · Web viewアルゴリズムの設計と解析 教授: 黄 潤和 (W4022) rhuang@hosei.ac.jp SA: 広野 史明 (A4/A10) fumiaki.hirono.5k@stu.hosei.ac.jp

Exercise 1-1What is the output of Test1(200) ?

Test1 ( 200 )の出力結果は何ですか?     Test1 は次のアルゴリズムです。

     Algorithm Test1(n)         b  0         for i  1 to n do              if i mod 6 = 0 then b  b + 1              else if i mod 9 = 0 then b  b +10         return b

36

Page 37: [PPT]Analysis of Algorithms · Web viewアルゴリズムの設計と解析 教授: 黄 潤和 (W4022) rhuang@hosei.ac.jp SA: 広野 史明 (A4/A10) fumiaki.hirono.5k@stu.hosei.ac.jp

Exercise 1-2What are the output of Test2(100)?                         

              Test2 ( 100 )の出力結果は何ですか? Test2 は次のアルゴリズムです。

Algorithm Test2(n)     b  0

for i  1 to n do           for j  1 to i do 

      b  b +1    return b

37

Page 38: [PPT]Analysis of Algorithms · Web viewアルゴリズムの設計と解析 教授: 黄 潤和 (W4022) rhuang@hosei.ac.jp SA: 広野 史明 (A4/A10) fumiaki.hirono.5k@stu.hosei.ac.jp

Exercise 1-3What are the output of Test3(1000) ?                               

  Test3 ( 1000  )の出力結果は何ですか? Test3 は次のアルゴリズムです。

Algorithm Test3(n)i  1b  0while i < n do b  b + 1i  2i 

     return b   

38