Representation and Evaluation of Algorithms

(アルゴリズムの表現と評価)

Data Structures and Algorithms

2nd lecture, October 3, 2019

http://www.sw.it.aoyama.ac.jp/2019/DA/lecture2.html

Martin J. Dürst

AGU

© 2009-19 Martin J. Dürst Aoyama Gakuin University

Today's Schedule

 

Summary of Last Lecture

 

Solution to Homework 1, Question 1

[Sorry, removed!]

 

Last Week's Homework 3: Help Ms. Noda

Design an efficient (=fast) algorithm for Ms. Noda's problem.

Hint: Can you use an algorithm that you already know?

One idea/outline:

 

Last Week's Homework 2: Representation of Algorithms

Examine the algorithm representations on the additional handout, and think about each representation's advantages and disadvantages.

(no need to submit)

 

Methods for Representing Algorithms

 

Text

Principle: Describe the algorithm in a natural language

Advantages: Possible to write and understand for non-experts

Disadvantages:

 

Diagrams

Examples: Flowchart, ...

Advantages: Visual expression

Disadvantages:

(structured programming: replacing goto (jump to an arbitrary location in a program) with structured branches (if/switch/...) and loops (for/while/...) only)

 

What is Pseudocode

 

Evaluation of Pseudocode

Advantages:

Disadvantages:

 

Programing Language

Advantages:

Disadvantages:

 

The Programming Language Ruby

 

Last Week's Homework 4: Install Ruby

Install Ruby on your notebook computer (and/or on your computer at home)

Main installation methods (choose one):

How to check: Open a Cygwin Terminal or start Command Prompt with Ruby and execute ruby -v

Important: If you have problems with installing Ruby, come to my lab to fix it before the next lecture.

 

Ruby for Algorithm Representation

Important: In this course, you have to learn how to read Ruby programs as pseudocode to understand algorithms.
But you do not need to write Ruby programs.

 

Reading and Writing Ruby

 

First Ruby Example

Linear search and binary search: 2search.rb

How to execute:

 

Basics of Ruby Syntax

 

Overview of Algorithm Evaluation

Main evaluation criteria:

  1. Execution time (computational (time) complexity)
    (this is the main focus of this lecture, and of research on algorithms)
  2. Amout of memory used (space complexity)
    (in general, the difference between different algorithms or data structures for the same problem is minor)
  3. Ease of implementation
    (this is highly subjective, but in general, faster algorithms may be more difficult to implement)

Contextual information used for evaluation:

 

Comparing the Execution Time of Algorithms

Example: Comparing linear search and binary search

Possible questions:

Problem: These questions do not have a single, clear answer.

When we compare algorithms, we want a single, clear answer.

Our answer will be: Linear search is O(n), binary search is O(log n).

We will arrive at this answer, and understand it, in the next two weeks.

 

Comparing Execution Times: From Concrete to Abstract

Very concrete

Very abstract

 

Measuring Execution Time

⇒ We need a better method to compare algorithms, not implemenations or hardware

 

Counting Operation Steps

⇒ We need a more abstract way to compare algorithms

 

Homework 1: Example for Asymptotic Growth of Number of Steps

Use 2search.rb to fill in the following table. Set the COUNT variable to n. Divide the number of operations shown in the profiles by 2, because each operation is counted twice.

number of steps (counting additions and divisions)
n (number of data items)       1      8     64    512   4'096  32'768 262'144
linear search
binary search

 

Homework 2: Calculate Function Values

Fill in the following table
(use engineering notation (e.g. 1.5E+20) if the numbers get big;
round liberally, the magnitude of the number is more important than the exact value)

n      1     10    100   1'000  10'000 100'000
5n
n1.2
n2
n log2 n
1.01n

 

Homework 3: Compare Function Growth

Which function of each pair (left/right column) grows larger if n increases? And why?

left column right column column with larger growth reason
100n n2
1.1n n20
5 log2 n 10 log4 n
20n n!
100·2n 2.1n

 

Homework 4: Logarithms and Limits

Review logarithms (ln, log10, log2,...) and limits (limn→∞,...) based on high school books/notes and Web resources

 

Summary of Today's Lecture

 

This Week's Homework

(提出不要。ただし、記入した表などは来週授業に持参すること。)

  1. Ruby を使って探索アルゴリズムのステップ数を調べ、テーブルを完成
    (散布図を作成してもよい)
  2. n の増加の場合、複数の関数の値を計算しなさい
  3. 関数の増加の比較で、どちらの関数が「最終的に勝つ」か、そしてその理由を調べなさい
  4. 高校の教科書やウェブなどで対数 (ln, log10, log2 など) と極限 (limn→∞など) について調査・再確認

 

Glossary

representation
表現
evaluation
評価
pseudocode
疑似コード
non-expert
素人
ambiguity
曖昧さ
natural language
自然言語 (すなわち、人間が話す言語)
flowchart
流れ図
structured progamming
構造化プログラミング
obfuscation
ごまかし
object-oriented (programming language)
オブジェクト指向 ((プログラム) 言語)
scripting language
スクリプト言語
criterion (複数 criteria)
基準
execution time
実行時間
computational complexity
計算量
worst case
最悪の場合
asymptotic
漸近
round
四捨五入する、概数で表す
logarithm
対数
limit
極限