Motivations of Data Structures and Algorithms

26  Download (0)

Full text

(1)

Motivations of Data Structures and Algorithms

Hsuan-Tien Lin

Dept. of CSIE, NTU

March 3, 2020

(2)

Introduction of Algorithms

(3)

Introduction of Algorithms

What is Algorithm?

descriptions to get something done correctly/efficiently by computer

譜 樂譜 暗器譜 食譜 程式譜

(4)

Introduction of Algorithms

Five Criteria of Algorithm

input, output, definiteness, finiteness, effectiveness

H.-T. Lin (NTU CSIE) Motivations of DSA 3/24

(5)

Introduction of Algorithms

getMinIndex with Sequential Search Algorithm

returns index to minimum element within array

int getMinIndex(int* arr, int len){

int minpos = 0; int i;

for(i=1; i<len; i++){

if (arr[i] < arr[minpos]) minpos = i;

}

return minpos;

}

(6)

Introduction of Algorithms

Correctness Proof of Algorithm

e.g. loop invariance by mathematical induction

—discrete math helps!

H.-T. Lin (NTU CSIE) Motivations of DSA 5/24

"algorithm" returns m such that arr[m] <= arr[j] for all j

claim2: at (end of) loop of i = k

arr[minpos] <= arr[j] for j = 0, 1, .., k i=1: trivial

i=k ==> i=k+1

(7)

Introduction of Algorithms

Efficiency of Algorithm

knockout tournament for getMinIndex: not much faster overall,

but possibly faster if done in parallel

(8)

Expressing Algorithms with Pseudo Code

(9)

Expressing Algorithms with Pseudo Code

Pseudo Code for getMinIndex

pseudo code: “spoken language” of programming

口語

getMinIndex

(integer array arr, integer len) minpos <- 0

for i <- 1 to len do

if arr[i] < arr[minpos] then minpos <- i

return minpos

(10)

Expressing Algorithms with Pseudo Code

Bad Pseudo Code: Too Detailed

goal of pseudo code: communicate efficiently

H.-T. Lin (NTU CSIE) Motivations of DSA 9/24

a = arr[i]

b = arr[minpos]

if a < b then ...

(11)

Expressing Algorithms with Pseudo Code

Bad Pseudo Code: Too Mysterious

goal of pseudo code: communicate correctly

minpos, i a = 0

for b = 1 to len-1

if arr[b] < arr[a] then ...

(12)

Expressing Algorithms with Pseudo Code

Bad Pseudo Code: Too Abstract

goal of pseudo code: communicate effectively

H.-T. Lin (NTU CSIE) Motivations of DSA 11/24

run a loop that updates minpos in

every iteration

(13)

Expressing Algorithms with Pseudo Code

Good Pseudo Code of SelSort

no “formal definition” and depends on the speaker/listener

selSort

(integer array arr, integer len) for i <- 0 to len-1 do

// find minIndex from arr[i .. len-1]

min <- getMinIndex(arr[i .. len-1]) // put arr[min] at arr[i]

swap(arr[min], arr[i])

(14)

Introduction of Data Structures

(15)

Introduction of Data Structures

What is Data Structure?

scheme of organizing data

within computer

(16)

Introduction of Data Structures

How to Organize 200 Exam Sheets?

different use cases

= ⇒ different organization scheme (data structure)

H.-T. Lin (NTU CSIE) Motivations of DSA 15/24

隨便

最高分 -〉最低分 學號

依尾數分十份

(17)

Introduction of Data Structures

Good Algorithm Needs Proper Data Structure

if having data structure such that getMinIndex faster,

= ⇒ SelSort also faster (we will see)

algorithm :: data structure ∼ recipe :: kitchen structure

(18)

Introduction of Data Structures

Good Data Structure Needs Proper Accessing Algorithms: get, insert

rule of thumb for speed: often-get ⇔ “nearby”

H.-T. Lin (NTU CSIE) Motivations of DSA 17/24

(19)

Introduction of Data Structures

Good Data Structure Needs Proper Maintenance Algorithms: construct, update, remove

hidden “cost” of data structure: maintenance effort

(20)

Why Data Structures and Algorithms?

(21)

Why Data Structures and Algorithms?

Why Data Structures and Algorithms?

use storage/computation resources properly =⇒ good program

(22)

Why Data Structures and Algorithms?

Proper Use: Tradeoff of Different Factors

understand tradeoff =⇒ good program

H.-T. Lin (NTU CSIE) Motivations of DSA 21/24

(23)

Why Data Structures and Algorithms?

Different Tradeoff on Different Platforms

important to learn other CS subjects

parallel

transmission/computation

(24)

Why Data Structures and Algorithms?

Programming �= Coding

programming :: building house ∼ coding :: construction work

H.-T. Lin (NTU CSIE) Motivations of DSA 23/24

requirement analysis

design

refinement & coding

verification: proof/test/debug

(25)

Why Data Structures and Algorithms?

C Programming versus DSA

moving from coding to designing

C & DSA req. simple simple analysis simple simple design simple more coding *** **

proof none some test simple **

debug *** **

(26)

Figure

Updating...

References

Related subjects :