Prof. Hsin-Mu (Michael) Tsai ( 蔡欣穆 ) Department of Computer Science and Information Engineering National Taiwan University BASICS.

Slides:



Advertisements
Similar presentations
MATH 224 – Discrete Mathematics
Advertisements

CHAPTER 2 ALGORITHM ANALYSIS 【 Definition 】 An algorithm is a finite set of instructions that, if followed, accomplishes a particular task. In addition,
Lecture3: Algorithm Analysis Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
CMPT 225 Sorting Algorithms Algorithm Analysis: Big O Notation.
Chapter 1 – Basic Concepts
Introduction to Analysis of Algorithms
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
Algorithms and Problem Solving-1 Algorithms and Problem Solving.
Complexity Analysis (Part I)
Algorithms and Problem Solving. Learn about problem solving skills Explore the algorithmic approach for problem solving Learn about algorithm development.
Complexity Analysis (Part I)
CHAPTER 11 Space and Time Complexity in Chapter 1 All the programs in this file are selected from Ellis Horowitz, Sartaj Sahni, and Susan Anderson-Freed.
1 Data Structures A program solves a problem. A program solves a problem. A solution consists of: A solution consists of:  a way to organize the data.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
C++ for Engineers and Scientists Third Edition
CHAPTER 1 BASIC CONCEPT All the programs in this file are selected from Ellis Horowitz, Sartaj Sahni, and Susan Anderson-Freed “Fundamentals of Data Structures.
What is Program  A Set of Instructions  Data Structures + Algorithms  Data Structure = A Container stores Data  Algoirthm = Logic + Control.
CHAPTER 2 ALGORITHM ANALYSIS 【 Definition 】 An algorithm is a finite set of instructions that, if followed, accomplishes a particular task. In addition,
Time Complexity Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Program Performance & Asymptotic Notations CSE, POSTECH.
CSC 201 Analysis and Design of Algorithms Lecture 03: Introduction to a CSC 201 Analysis and Design of Algorithms Lecture 03: Introduction to a lgorithms.
Week 2 CS 361: Advanced Data Structures and Algorithms
C. – C. Yao Data Structure. C. – C. Yao Chap 1 Basic Concepts.
For Wednesday Read Weiss chapter 3, sections 1-5. This should be largely review. If you’re struggling with the C++ aspects, you may refer to Savitch, chapter.
Discrete Structures Lecture 11: Algorithms Miss, Yanyan,Ji United International College Thanks to Professor Michael Hvidsten.
1 Recursion Algorithm Analysis Standard Algorithms Chapter 7.
Basic Concepts 2014, Fall Pusan National University Ki-Joune Li.
Chapter 1 1. Overview: System life cycle Requirements Analysis Bottom-up Top-down Design Data objects (abstract data type) and operations (algorithm)
Data Structures and Algorithms Lecture 5 and 6 Instructor: Quratulain Date: 15 th and 18 th September, 2009 Faculty of Computer Science, IBA.
Data Structures in C Networking Laboratory Chap. 1-1 Copyright(c) 2000, Sungkyunkwan University Chapter #1: BASIC CONCEPTS Fundamentals of Data Structures.
CS Data Structures Chapter 1 Basic Concepts.
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data Depends.
Complexity of Algorithms
Analysis of Algorithms CSCI Previous Evaluations of Programs Correctness – does the algorithm do what it is supposed to do? Generality – does it.
Pointers OVERVIEW.
Data Structure Introduction.
Algorithm Analysis CS 400/600 – Data Structures. Algorithm Analysis2 Abstract Data Types Abstract Data Type (ADT): a definition for a data type solely.
Algorithms and data structures: basic definitions An algorithm is a precise set of instructions for solving a particular task. A data structure is any.
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Software Learning Resource Service Platform BASIC CONCEPT CHAPTER 1 BASIC CONCEPT 1.
Program Performance 황승원 Fall 2010 CSE, POSTECH. Publishing Hwang’s Algorithm Hwang’s took only 0.1 sec for DATASET1 in her PC while Dijkstra’s took 0.2.
Chapter 15: Recursion. Recursive Definitions Recursion: solving a problem by reducing it to smaller versions of itself – Provides a powerful way to solve.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
Basic Concepts 2011, Fall Pusan National University Ki-Joune Li.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
1 7.Algorithm Efficiency These factors vary from one machine/compiler (platform) to another  Count the number of times instructions are executed So, measure.
SPACE COMPLEXITY & TIME COMPLEXITY 1. ALGORITHMS COMPLEXITY 2.
2017, Fall Pusan National University Ki-Joune Li
Algorithm Analysis 1.
Complexity Analysis (Part I)
Data Structures 1st Week
COP 3503 FALL 2012 Shayan Javed Lecture 15
Microprocessor Systems Design I
Introduction to Algorithms
Data Structure 김용성 Data Structure in C.
Algorithm An algorithm is a finite set of steps required to solve a problem. An algorithm must have following properties: Input: An algorithm must have.
Algorithm Efficiency Chapter 10.
2018, Fall Pusan National University Ki-Joune Li
Data Structures-CSE207.
Searching, Sorting, and Asymptotic Complexity
Asst. Dr.Surasak Mungsing
Algorithms and Problem Solving
Performance analysis of algorithms
CHAPTER 1 BASIC CONCEPT All the programs in this file are selected from Ellis Horowitz, Sartaj Sahni, and Susan Anderson-Freed “Fundamentals of Data Structures.
Data structures & Algorithm Strategies
Complexity Analysis (Part I)
Algorithms and data structures: basic definitions
Complexity Analysis (Part I)
2019, Fall Pusan National University Ki-Joune Li
Presentation transcript:

Prof. Hsin-Mu (Michael) Tsai ( 蔡欣穆 ) Department of Computer Science and Information Engineering National Taiwan University BASICS

WHAT IS AN ALGORITHM? A computable set of steps to achieve a desired result. All algorithm must satisfy the following criteria: Input Output Definiteness Finiteness Effectiveness DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL

EXAMPLE DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL

WHAT IS A DATA STRUCTURE? An organization of information, usually in memory, for better algorithm efficiency. Or, a way to store and organize data in order to facilitate access and modifications. DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL

ALGORITHM SPECIFICATION DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL

HOW DO WE DESCRIBE AN ALGORITHM? Human language (English, Chinese, …) Programming language A mix of the above DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL

EXAMPLE: SELECTION SORT DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL ㄅ 1 1 ㄆㄅ 2 1 ㄆㄅ 2

EXAMPLE: SELECTION SORT First attempt: for (i=0; i<n; ++i) { Examine list[i] to list[n-1] and suppose that the smallest integer is at list[min]; Interchange list[i] and list[min]; } Task 1 Task 2 DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL

void swap(int *x, int *y) { int temp = *x; *x=*y; *y=temp; } Or #define SWAP(x,y,t) ((t)=(x), (x)=(y), (y)=(t)) Task 2 DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL

min=i; for(j=i;j<n;++j) if (list[j]<list[min]) min=j; Task 1 Finally, see program 1.4 on p. 11 for a complete program of the selection sort we just develop. DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL

HOW DO WE PROVE THAT IT IS CORRECT? DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL

EXAMPLE: BINARY SEARCH DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL

EXAMPLE: searchnum=13; return middle; DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL

EXAMPLE: searchnum=13; left right middl e middle=(left+right)/2; left=middle+1; DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL

EXAMPLE: searchnum=5; return -1; DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL

int binsearch(int list[], int searchnum, int left, int right) { int middle; while(left<=right) { middle=(left+right)/2; switch(COMPARE(list[middle], searchnum)) { case -1: left=middle+1; break; case 0: return middle; case 1: right=middle-1; } return -1; } DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL

RECURSIVE ALGORITHMS What does “recursive” mean? A function which calls itself (direct recursion), or a function which calls other functions which call the calling function again (indirect recursion). Any function that we can write using assignment, if-else, and while statements can be written recursively. Why do we want to use recursive functions (or algorithms)? It allow us to express an otherwise complex process in very clear terms. Often the recursive function is easier to understand than its iterative counterpart. DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL

THE STORY OF RECURSIVE FUNCTION Function blah Function blah (clone 1) Function blah (clone 2) Function blah (clone 2) ㄅ is too much for me. I’ll partition it, do part 1, and clone two copies of myself to do the rest. Yo. Do the work ㄅ. I’m ㄅ. The work to be done. Hey man. Do the work ㄅ part 2. Hi. Do the work ㄅ part 3. ㄅ ㄅ DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL

EXAMPLE 1 DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL

EXAMPLE 2 int binsearch(int list[], int searchnum, int left, int right) { int middle; if (left<=right) { middle=(left+right)/2; switch (COMPARE(list[middle], searchnum)) { case -1: return binsearch(list, searchnum, middle+1, right); case 0: return middle; case 1: return binsearch(list, searchnum, left, middle-1); } 1. Termination condition 2. Recursive calls DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL

EXAMPLE 3 Output all permutations Input: {a,b,c} Output: (a,b,c),(a,c,b),(b,a,c),(b,c,a),(c,a,b),(c,b,a) How do we write this program recursively? Example: input={a,b,c,d}. Output = a followed by all permutations of {b,c,d} b followed by all permutations of {a,c,d} c followed by all permutations of {a,b,d} d followed by all permutations of {a,b,c} “all permutations of {…}”  recursive calls! Homework: read and understand program 1.9 DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL

DATA ABSTRACTION DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL

DATA TYPE What is a data type? A data type is a collection of objects and a set of operations that act on those objects. Data types in C char, int, float, long, double (unsigned, signed, …) Array Struct struct { int a; int b; char str[16]; int * iptr; } blah; int iarray[16]; DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL

DATA TYPE Operations +, -, *, /, %, == =, +=, -= ? : sizeof, - (negative) giligulu(int a, int b) DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL

DATA TYPE Representation of the objects of the data type Example: char char blah=‘A’; (‘A’: ASCII code is 65(dec), or 0x41 (hex)) Q: The maximum number which can be represented with a char variable? A: 255. Homework: How about char, int, long, float? byte of memory: DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL

DATA TYPE Q: Do we need to know about the representation of a data type? A: It depends. We can usually write algorithms which are more efficient if we make use of the knowledge about the representation. However!!! If the representation of the data type is changed, the program needs to be verified, revised, or completely re-written. 囧 Porting to a different platform (x86, ARM, embedded system, …) Changing the specification of a program or a library (ex. 16-bit int  32-bit long) DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL

ABSTRACT DATA TYPE An “abstract data type” (ADT) is a data type that is organized in such a way that the specification of the objects and the specification of the operations on the objects is separated from the representation of the objects and the implementation of the operations. Representation and Implementation Specification (Interface) User DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL

ABSTRACT DATA TYPE Specifications: Name of the function and the description of what the function does The type of the argument(s) The type of the result(s) (return value) Function categories: Creator/constructor Transformers Observer/reporter Homework: Read Example 1.5 on p. 20 DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL

PERFORMANCE ANALYSIS DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL

HOW DO YOU MAKE EVALUATIVE JUDGMENT ABOUT PROGRAM? DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL Does the program meet the original specifications of the task? 2.Does it work correctly? 3.Does the program contain documentation that shows how to use it and how it works? 4.Does the program effectively use functions to create logical units? 5.Is the program’s code readable?

HOW DO YOU MAKE EVALUATIVE JUDGMENT ABOUT PROGRAM? DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL Does the program efficiently use primary and secondary storage? Primary storage: memory? Secondary storage: Hard drive, flash disk, etc. 7.Is the program’s running time acceptable for the task? Example: Network intrusion detection system (1) 99.8% detection rate, 50 minutes to finish analysis of a minute of traffic (2) 85% detection rate, 20 seconds to finish analysis of a minute of traffic

HOW DO YOU MAKE EVALUATIVE JUDGMENT ABOUT PROGRAM? DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL Does the program efficiently use primary and secondary storage? 7.Is the program’s running time acceptable for the task?

SPACE & TIME COMPLEXITY DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL Space complexity of a program: The amount of memory that it needs to run to completion. Time complexity of a program: The amount of computer time that it needs to run to completion.

SPACE COMPLEXITY DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL

EXAMPLE DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL

EXAMPLE DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL

EXAMPLE DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL

DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL Is it good to use? (recursion) TypeNameNumber of bytes parameters: array pointerlist[]4 parameter: integern4 return address: (used internally) 4 TOTAL per recursive call12 Probably not for this problem.

TIME COMPLEXITY DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL

DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL Method 1: Count all instances of all operations in the program. AddSubtractLoadStore ADD(n)SUB(n)LDA(n)STA(n) Is it good to use? (method 1)

DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL Method 2: Separate the program into program steps whose execution time is independent of instance characteristics Count the number of steps Different program steps have different amounts of computing a=2; a=2*b+3*c/d-e+f/g/a/b/c; Count the number of steps needs to solve a particular instance

EXAMPLE 1 DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL float sum(float list[], int n) { float tempsum = 0; count++; //assignment int i; for (i=0;i<n;i++) { count++; // for loop tempsum+=list[i]; count++; //assignment } count++; //last iteration of for count++; return tempsum; }

EXAMPLE 1 DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL float sum(float list[], int n) { float tempsum = 0; int i; for (i=0;i<n;i++) { count+=2; } count+=3; return tempsum; } count = 2n+3 (steps)

EXAMPLE 2 DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL float rsum(float list[], int n) { count++; // if if (n) { count++; //return return rsum(list,n-1)+list[n-1]; } count++; //return return list[0]; } count = 2n+2 (steps) 2n+2 < 2n+3. Does this mean rsum is faster than sum ? No!

EXAMPLE 3 DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL void add(int a[][MAX_SIZE], int b[][MAX_SIZE], int c[][MAX_SIZE], int rows, int cols) { int i,j; for(i=0; i<rows; ++i) for(j=0;j<cols;++j) c[i][j]=a[i][j]+b[i][j]; }

EXAMPLE 3 DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL void add(int a[][MAX_SIZE], int b[][MAX_SIZE], int c[][MAX_SIZE], int rows, int cols) { int i,j; for(i=0; i<rows; ++i) { count++; for(j=0;j<cols;++j) { count++; c[i][j]=a[i][j]+b[i][j]; count++; } count++; } count++; } count=((2*cols)+2)*rows+1 =2cols*rows+2*rows+1

EXAMPLE 1 REVISITED (TABULAR METHOD) DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL Statements/eFrequencyTotal steps float sum(float list[], int n) { 000 float tempsum=0; 111 int i; 000 for (i=0;i<n;i++) 0n+1 tempsum+=list[i]; 1nn return tempsum; } 111 Please practice using the method for the other two examples.

SUMMARY DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL Instance characteristics? The number of inputs The number of outputs The magnitude of the inputs We then calculate the number of steps which is independent of the characteristics we selected.

DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL Sometimes, the time complexity does not depend on only these simple numbers. For example, binsearch. In that case, we are interested in the average case, the worst case, and the best case.

ASYMPTOTIC NOTATION DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL

MOTIVATION DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL Compare the time complexities of two programs that compute the same function. Predict the growth in run time as the instance characteristics change. “Steps” are not exact. “3n+3” faster than “3n+5” ? Usually “3n+3”, “7n+2”, or “2n+15” all runs for about the same time. Let’s go for a less accurate way to describe the run time…

EXAMPLE DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL Break even point No.

ASYMPTOTIC NOTATION – BIG OH DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL

EXAMPLE DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL

THE WORLD OF BIG OH DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL Faster Slower

BIG OH IS AN UPPER BOUND DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL

A USEFUL THEOREM DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL

ASYMPTOTIC NOTATION – OMEGA DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL

EXAMPLE DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL

DISCUSSION DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL Homework 1

ASYMPTOTIC NOTATION – THETA DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL Theta Platform GMC Terrain

EXAMPLE DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL

DISCUSSION DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL Homework 1

SO, WHAT NOW? DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL Asymptotic complexity can be determined quite easily Work on each statement first Then we add them up No need for the silly step counts anymore

EXAMPLE 1 DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL StatementAsymptotic complexity void add(int a[][MAX_SIZE]…) {0 int i,j;0 for(i=0;i<rows;i++) for(j-0;j<cols;j++) c[i][j]=a[i][j]+b[i][j]; }0 Total

EXAMPLE 2 DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL int binsearch(int list[], int searchnum, int left, int right) { int middle; while(left<=right) { middle=(left+right)/2; switch(COMPARE(list[middle], searchnum)) { case -1: left=middle+1; break; case 0: return middle; case 1: right=middle-1; } return -1; }

searchnum=13; left right middl e middle=(left+right)/2; left=middle+1; DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL Half the searching window every iteration.

MORE EXAMPLES DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL Please take a look at example 1.20 and 1.21 (p )

PRACTICAL COMPLEXITY DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL

兜基 ?? DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL

WHICH ONE IS BETTER? DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL

ON A 1 BILLION-STEPS-PER-SEC COMPUTER DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL n

下課 ! DATA STRUCTURE AND ALGORITHM I HSIN-MU TSAI, FALL 等等 ~~ Homework 1 is out today, the due date is two weeks from yesterday (5pm). Download the question sets from the course website your question to Or, come to our office hours. Have a nice weekend!