### Motivations of Data Structures and Algorithms

### Hsuan-Tien Lin

### Dept. of CSIE, NTU

### March 3, 2020

### Introduction of Algorithms

Introduction of Algorithms

### What is Algorithm?

### descriptions to get something done correctly/efﬁciently by computer

譜 樂譜 暗器譜 食譜 程式譜

Introduction of Algorithms

### Five Criteria of Algorithm

### input, output, deﬁniteness, ﬁniteness, effectiveness

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

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;

## }

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

Introduction of Algorithms

### Efﬁciency of Algorithm

### knockout tournament for getMinIndex: not much faster overall,

### but possibly faster if done in parallel

### Expressing Algorithms with Pseudo Code

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

Expressing Algorithms with Pseudo Code

### Bad Pseudo Code: Too Detailed

### goal of pseudo code: communicate efﬁciently

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

## a = arr[i]

## b = arr[minpos]

## if a < b then ...

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 ...

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

Expressing Algorithms with Pseudo Code

### Good Pseudo Code of SelSort

### no “formal deﬁnition” and depends on the speaker/listener

## selSort

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

## // ﬁnd minIndex from arr[i .. len-1]

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

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

### Introduction of Data Structures

Introduction of Data Structures

### What is Data Structure?

### scheme of organizing data

### within computer

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

## 隨便

## 最高分 -〉最低分 學號

## 依尾數分十份

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

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

Introduction of Data Structures

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

### hidden “cost” of data structure: maintenance effort

### Why Data Structures and Algorithms?

Why Data Structures and Algorithms?

### Why Data Structures and Algorithms?

### use storage/computation resources properly =⇒ good program

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

Why Data Structures and Algorithms?

### Different Tradeoff on Different Platforms

### important to learn other CS subjects

## parallel

## transmission/computation

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

## reﬁnement & coding

## veriﬁcation: proof/test/debug

Why Data Structures and Algorithms?