CH1. BASIC CONCEPTS.

Slides:



Advertisements
Similar presentations
Lecture3: Algorithm Analysis Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Advertisements

Chapter 1 – Basic Concepts
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
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.
Data Structures Performance Analysis.
Chapter 1.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Lecture 9 Concepts of Programming Languages
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.
C++ fundamentals.
Program Performance & Asymptotic Notations CSE, POSTECH.
Chapter 1 basic Concepts
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
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.
1 R. Johnsonbaugh, Discrete Mathematics Chapter 4 Algorithms.
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)
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
CS 1704 Introduction to Data Structures and Software Engineering.
Unit III : Introduction To Data Structures and Analysis Of Algorithm 10/8/ Objective : 1.To understand primitive storage structures and types 2.To.
CS Data Structures Chapter 1 Basic Concepts.
Prepared By Ms.R.K.Dharme Head Computer Department.
Complexity of Algorithms
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Data Structures Using C++1 Chapter 1 Software Engineering Principles and C++ Classes.
Data Structure Introduction.
System Life Cycles Requirements Analysis Design Refinement and Coding Verification –Correction proofs –Testing –Error Removal.
Algorithm Analysis CS 400/600 – Data Structures. Algorithm Analysis2 Abstract Data Types Abstract Data Type (ADT): a definition for a data type solely.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
CS 261 – Data Structures Introduction to C Programming.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
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.
 FUNDAMENTALS OF ALGORITHMS.  FUNDAMENTALS OF DATA STRUCTURES.  TREES.  GRAPHS AND THEIR APPLICATIONS.  STORAGE MANAGEMENT.
Basic Concepts 2011, Fall Pusan National University Ki-Joune Li.
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
Prof. I. J. Chung Data Structure #1 Professor I. J. Chung.
SPACE COMPLEXITY & TIME COMPLEXITY 1. ALGORITHMS COMPLEXITY 2.
Introduction To Algorithm and Data Structures Course Teacher: Moona Kanwal -Algorithm Design -Algorithm Analysis -Data structures -Abstract Data Type.
2017, Fall Pusan National University Ki-Joune Li
Algorithm Analysis 1.
Data Structures 1st Week
Abstract Data Types and Encapsulation Concepts
Analysis of Algorithms
COP 3503 FALL 2012 Shayan Javed Lecture 15
DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING IN C++
Data Structure 김용성 Data Structure in C.
Review: Two Programming Paradigms
About the Presentations
Courtsey & Copyright: DESIGN AND ANALYSIS OF ALGORITHMS Courtsey & Copyright:
DATA STRUCTURES Introduction: Basic Concepts and Notations
Lecture 9 Concepts of Programming Languages
Algorithm design and Analysis
Abstract Data Types and Encapsulation Concepts
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.
Objective of This Course
Algorithm Efficiency Chapter 10.
2018, Fall Pusan National University Ki-Joune Li
Data Structures-CSE207.
Programming and Data Structure
Asst. Dr.Surasak Mungsing
Introduction to Data Structure
Revision of C++.
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
Lecture 9 Concepts of Programming Languages
2019, Fall Pusan National University Ki-Joune Li
Presentation transcript:

CH1. BASIC CONCEPTS

1.1 System Life Cycle Requirements Specification Gathering : Types of expression Analysis Elimination of ambiguities, Partition of requirements Specification Define what the system is to do Functional specification Performance specification 9/21/2018

1.1 System Life Cycle(Cont’) Design Description of how to achieve the specification Data objects → Abstract data types Operations → Algorithms Programming language independent Design approaches : Top-down / Bottom-up approach Coding Programming language dependent Verification Correctness proofs, Test, Error removal Operation and maintenance System installation, operation, System modification 9/21/2018

1.2 Object-Oriented Design Object-oriented design vs. structured programming design similarity : divide and conquer difference : decomposition method 9/21/2018

1.2 Object-Oriented Design(Cont’) 1.2.1 Algorithmic Decomposition vs. Object-Oriented Decomposition Algorithmic decomposition (Functional decomposition) software = process modules = steps of the process Object-oriented decomposition software = a set of objects that model entities and interact with each other provides reusability, flexible to change 9/21/2018

1.2 Object-Oriented Design(Cont’) 1.2.2 Fundamental Definitions and Concepts of Object-Oriented Programming Object is an entity, performs computations Object-oriented programming is a method of implementation objects are the fundamental building blocks each object is an instance of some type (class) classes are related to each other by inheritance relationships Object-oriented language : objects, class, inheritance 9/21/2018

1.3 Data Abstraction and Encapsulation Data encapsulation (Information hiding) concealing of the implementation of a data object from the outside world Data abstraction separation of the specification of a data object from its implementation C++ data types fundamental data types char, int, float, double modifiers: short, long, signed, unsigned derived data types pointer, reference grouping mechanisms array, struct, class user-defined data types 9/21/2018

1.3 Data Abstraction and Encapsulation (Cont’) Abstract Data Type(ADT) and Encapsulation objects (data) operations private public 9/21/2018

1.3 Data Abstraction and Encapsulation (Cont’) ADT NaturalNumber is   objects : An ordered subrange of integers starting at zero and             ending at the maximum integer (MAXINT) on the            computer.   functions :     for all x, y ∈ NaturalNumber, TRUE, FALSE ∈ Boolean     +, -, <, ==, = are the usual integer operations       Zero():NaturalNumber ::= 0       IsZero(x):Boolean ::= if (x == 0) IsZero = TRUE                                      else IsZero = FALSE       Add(x,y):NaturalNumber ::= if (x+y <= MAXINT) Add=                                     x+y                                               else Add = MAXINT       Equal(x,y):Boolean ::= if (x == y) Equal = TRUE                                       else Equal = FALSE end NaturalNumber ADT 1.1 : Abstract data type NaturalNumber 9/21/2018

1.3 Data Abstraction and Encapsulation (Cont’) Figure 1.1 : Search area for bugs 9/21/2018

1.3 Data Abstraction and Encapsulation (Cont’) Advantages of data abstraction and encapsulation simplification of software development decomposition of complex task into simpler tasks efficient testing and debugging amount of code to be searched enhance the reusability easy modifications to the representations of a data type implementation of a data type is not visible to the rest of the program change of a part of the shaded area 9/21/2018

1.3 Data Abstraction and Encapsulation (Cont’) rest of the program operation implementation of operations internal representation of data type Data type 9/21/2018

1.4 Basics of C++ 1.4.1 Program Organization in C++ Header file .h suffix store declarations system-defined (iostream.h) or user-defined #include preprocessor directive is used Source file .c suffix store C++ source code 9/21/2018

1.4 Basics of C++(Cont’) 1.4.2 Scope in C++ File scope to access a global variable locally use the scope operator :: to use in file2.c a global variable defined in file1.c use extern to declare a variable in file2.c both file1.c and file2.c define the same global variable for different entities use static to declare in both files Function scope Local scope Class scope 9/21/2018

1.4 Basics of C++(Cont’) 1.4.3 C++ Statements and Operators C++ operators : identical to C operators Exceptions new and delete for dynamic memory management >> and << for input and output operator overloading 9/21/2018

1.4 Basics of C++(Cont’) 1.4.4 Data Declarations in C++ same as C declarations except the reference type (1) Constant values (2) Variables (3) Constant variables (4) Enumeration types example : enum Boolean {FALSE, TRUE}; (5) Pointers hold memory address of objects example : int i=25; int *np; np=&i; (6) Reference types an alternate name for an object example : int i=5; int& j=i; i=7; printf("i=%d, j=%d", i, j); 9/21/2018

1.4 Basics of C++(Cont’) 1.4.6 Input/Output in C++ #include <iostream.h> main() {   int n=50; float f=20.3;   cout<<"n:"<<n<<endl;   cout<<"f:"<<f<<endl; } #include <iostream.h> main() {   int a,b;   cin>>a>>b; } Program 1.1: Output in C++ Program 1.2 : Input in C++ Input 1 5  10 <Enter> Input 2 5 <Enter> 10 <Enter> Output n : 50 f : 20.3 9/21/2018

1.4 Basics of C++(Cont’) Advantages of I/O in C++ File I/O in C++ format-free : no formatting symbols I/O operators can be overloaded File I/O in C++ include the header file fstream.h 9/21/2018

1.4 Basics of C++(Cont’) 1.4.6 Input/Output in C++(Con’t) #include <iostream.h> #include <fstream.h> main() { ofstream outFile("my.out", ios::out);         if (!outFile) {           cerr<<"cannot open my.out"<<endl;//standard error device          return;         }         int n = 50; float f = 20.3;         outFile << "n: " << n << endl;         outFile << "f: " << f << endl; } Program 1.3 : File I/O in C++ 9/21/2018

1.4 Basics of C++(Cont’) 1.4.7 Functions in C++ regular functions member functions of specific C++ classes 9/21/2018

1.4 Basics of C++(Cont’) 1.4.8 Parameter Passing in C++ Pass by value : object is copied Pass by reference : address of object is copied Pass by constant reference retain the advantages of both methods const T& a Array types default is pass by reference a point to the first element of an array is passed 9/21/2018

1.4 Basics of C++(Cont’) 1.4.9 Function Name Overloading in C++ More than one function with the same name int max(int, int) ; int max(int, int, int) ; int max(int*, int) ; 9/21/2018

1.4 Basics of C++(Cont’) 1.4.10 Inline Functions inline int sum(int a, int b) {    return a+b ; } i=sum(x, 12) is replaced by i=x+12 9/21/2018

1.4 Basics of C++(Cont’) 1.4.11 Dynamic Memory Allocation in C++ Allocation/release to free store during runtime new operator creates an object of the desired type returns a pointer to data type that follows new if unable to create, the object, returns 0 delete operator an object created by new is deleted delete is applied to a pointer to the object example int *ip = new int; if (ip == 0) cerr << "Memory not allocated" << endl; ….. delete ip; 9/21/2018

1.5 Algorithm Specification 1.5.1 Introduction Algorithm a finite set of instructions, if followed, accomplishes a particular task Criteria input ≥ 0 output ≥ 1 definiteness finiteness effectiveness : feasible Program does not have to satisfy finiteness used interchangeably 9/21/2018

1.5 Algorithm Specification(Cont’) Description of an algorithm a natural language like English pseudo code programming language : C++ combination 9/21/2018

1.5 Algorithm Specification(Cont’) Example [Selection sort] a program that sorts a collection of n≥1 integers from those integers that are currently unsorted, find the smallest and place it next in the sorted list for (int i = 0; i < n; i++) { examine a[i] to a[n-1] and suppose the smallest integer is at a[j];    interchange a[i] and a[j]; } Program 1.5 : Selection sort algorithm 9/21/2018

1.5 Algorithm Specification(Cont’)       void sort(int *a, const int n)       // sort the n integers a[0] to a[n-1] into nondecreasing order       {              for (int i = 0; i < n; i++)              {                      int j = i;                      // find smallest integer in a[i] to a[n-1]                      for (int k = i + 1; k < n; k++)                         if (a[k] < a[j]) j = k;                      // interchange                      int temp = a[i]; a[i] = a[j]; a[j] = temp;              } } Program  1.6 : Selection sort 9/21/2018

1.5 Algorithm Specification(Cont’) 4 13 2 i=0 2 4 13 5 i=1 2 4 13 5 i=2 2 4 5 13 9/21/2018

1.5 Algorithm Specification(Cont’) Theorem sort(a, n) correctly sorts a set of n≥1 integers Proof for any i, say i=q, after steps 6-11, a[q]≤a[r] for q<r≤n-1 when i>q, a[0]...a[q] is unchanged when i=n-1, a[0]≤a[1]≤...≤a[n-1] 9/21/2018

1.5 Algorithm Specification(Cont’) Example [Binary search] n≥1 distinct integers are already sorted in the array a[0]...a[n-1] if the integer x is present, return j such that x=a[j] otherwise return left, right : the left and right ends of the list to be searched(initially:0, n-1) middle = (left + right)/2 compare a[middle] with x (1) x<a[middle] : right = middle-1 (2) x=a[middle] : return middle (3) x>a[middle] : left = middle+1 two subtasks (1) determine if there are any integers left to check (2) compare x to a[middle] 9/21/2018

1.5 Algorithm Specification(Cont’) char compare(int x, int y) {         if (x > y) return '>';         else if (x < y) return '<';                 else return '=';         } // end of compare } Program 1.7 : Comparing two elements 9/21/2018

1.5 Algorithm Specification(Cont’)      int BinarySearch(int *a, const int x, const int n)      // Search the sorted array a[0], ..., a[n-1] for x      {              for (int left = 0, right = n-1; left <= right;) { // while more elements                     int middle = (left + right) / 2;                    switch (comare(x,a[middle])) {                           case '>' : left = middle + 1; break; // x > a[middle]                           case '<' : right = middle - 1; break; // x < a[middle]                           case '=' : return middle; // x == a[middle]                     } // end of switch              } // end of for              return -1; // not found      } // end of BinarySearch Program 1.9 : C++ function for binary search 9/21/2018

1.5 Algorithm Specification(Cont’) 1.5.2 Recursive Algorithms Example [Factorial] N! = N*(N-1)! int Fact (int N) {   if N=0 return (1);   else return (N*Fact(N-1)); } 9/21/2018

1.5 Algorithm Specification(Cont’) Example [Recursive binary search] int BinarySearch(int *a, const int x, const int left, const int right) // Search the sorted array a[left], ..., a[right] for x {         if(left <= right) {                 int middle = (left + right) / 2;                 switch(compare(x, a[middle])) {                         case '>' : return BinarySearch(a, x, middle+1, right);        // x > a[middle]                         case '<' : return BinarySearch(a, x, left, middle-1);        // x < a[middle]                         case '=' : return middle; // x == a[middle]                 } // end of switch         } // end of if         return -1; // not found } // end of BinarySearch Program 1.10 : Recursive implementation of binary search 9/21/2018

1.6 Performance Analysis and Measurement Program complexity space complexity : the amount of memory it needs time complexity : the amount of computer time it needs Performance evaluation phases performance analysis : a priori estimates performance measurement : a posteriori testing 9/21/2018

1.6 Performance Analysis and Measurement(Cont’) Space complexity fixed part : independent of the characteristics of the inputs and outputs variable part : depends on the instance characteristics S(P) = c+Sp(instance characteristics) S(P):space requirement of program P c : constant Example [Iterative sum] the problem instances are characterized by n n is passed by value : 1 word is allocated a is the address of a[0], 1 word is needed the space is independent of n: Sp(n)=0 9/21/2018

1.6 Performance Analysis and Measurement(Cont’) line  float sum(float *a, const int n)   1   {   2           float s = 0;   3           for(int i = 0; i < n; i++)   4                   s += a[i];   5           return s;   6   } Program 1.13 : Iterative function for sum 9/21/2018

1.6 Performance Analysis and Measurement(Cont’) Example [Recursive sum] the problem instances are characterized by n the depth of recursion depends on n : it is n+1 each call requires at least 4 words : n, a, returned value, return address the recursion stack space is 4(n+1) 9/21/2018

1.6 Performance Analysis and Measurement(Cont’) line float rsum(float *a, const int n)   1   {   2           if(n <= 0) return 0;   3           else return (rsum(a, n-1)+a[n-1]);   4   } Program 1.14 : Recursive function for sum 9/21/2018

1.6 Performance Analysis and Measurement(Cont’) Time complexity T(P) = c + Tp(n) T(P) : time taken by program P c : compile time Tp : run time n : instance characteristics Program step a meaningful segment of a program execution time is independent of the instance characteristics the number of steps of a program statement : depends on the nature of the statement determine the number of steps of a program introduce a new variable, count, into the program build a table to list steps by each statement 9/21/2018

1.6 Performance Analysis and Measurement(Cont’) Example of count [Iterative sum] line  float sum(float *a, const int n)   1   {   2           float s = 0;   3           for(int i = 0; i < n; i++)   4                   s += a[i];   5           return s;   6   } line  float rsum(float *a, const int n)   1   {   2           if(n <= 0) return 0;   3           else return (rsum(a, n-1)+a[n-1]);   4   } Program 1.13 : Iterative function for sum Program 1.14 : Recursive function for sum 9/21/2018

1.6 Performance Analysis and Measurement(Cont’) Example [Iterative sum] Example [Recursive sum] line s/e frequency total steps Line s/e* frequency total steps 1 1 n=0 n>0 n=0 n>0 2 1 1 1 1 1 1 3 1 n+1 n+1 2(a) 1 1 1 1 1 4 1 n n 2(b) 1 1 1 5 1 1 1 3 1+trsum(n-1) 1 1+trsum(n-1) 6 1 4 1 1 Total number of steps 2n+3 Total number of steps 2 2+trsum(n-1) Table 1.1 Step table for Program 1.13 Table 1.2 Step table for Program 1.14 (*s/e : step per execution) 9/21/2018

1.6 Performance Analysis and Measurement(Cont’) Solution of recurrence relation trsum(n) = 2 + trsum(n-1) = 2 + 2 + trsum(n-2)              = 2 * 2 + trsum(n-2)                     ……              = 2n + trsum(0)              = 2n + 2 9/21/2018

1.6 Performance Analysis and Measurement(Cont’) Three kinds of step counts best-case worst-case average 9/21/2018

1.6 Performance Analysis and Measurement(Cont’) Asymptotic notation (O, Ω, Θ) determining the exact step count is very difficult the notion of a step is inexact Definition [Big "oh"] f(n)=O(g(n)) iff there exist positive constants c and n0 such that f(n)≤cg(n) for all n, n≥n0 3n+3= O(n) as 3n+3≤4n for all n≥3 3n+3=O(n2) as 3n+3≤3n2 for n≥2 g(n) is an upper bound 9/21/2018

1.6 Performance Analysis and Measurement(Cont’) Definition [Omega] f(n)=Ω(g(n)) iff there exist positive constants c and n0 such that f(n)≥cg(n) for all n, n≥n0 g(n) is a lower bound ex) 3n+2=Ω(n)             10n2+4n+2=Ω(n2) Definition [Theta] f(n)=Θ(g(n)) iff there exist positive constants c1, c2 and n0 such that c1g(n)≤f(n)≤c2g(n) for all n, n≥n0 g(n) is both an upper and lower bound ex) 3n+2=Θ(n)              10n2+4n+2=Θ(n2)                6×2n+n2=Θ(2n) 9/21/2018

1.6 Performance Analysis and Measurement(Cont’) Asymptotic complexity(in terms of O,Ω,Θ) easily determined without determining the exact step count line (s) s/e frequency   total steps 1 - (0) 2 1 1 (1) 3 1 n+1 (n) 4 1 n (n) 5 1 1 (1) 6 - (0) Tsum(n)=Θ(max {gi(n)}) = Θ(n), 1≤i≤6 Table1.4:Asymptotic Complexity of sum (Program 1.13) 9/21/2018

1.6 Performance Analysis and Measurement(Cont’) Practical complexities for large n, only programs of small complexity are feasible Figure 1.3 : plot of function values 9/21/2018