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.

Slides:



Advertisements
Similar presentations
Write a program step by step. Step 1: Problem definition. Given the coordinate of two points in 2-D space, compute and print their straight distance.
Advertisements

CHAPTER 2 ALGORITHM ANALYSIS 【 Definition 】 An algorithm is a finite set of instructions that, if followed, accomplishes a particular task. In addition,
Programming and Data Structure
Chapter 1 – Basic Concepts
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 3: Functions.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
Multiple-Subscripted Array
1 Matrix Addition, C = A + B Add corresponding elements of each matrix to form elements of result matrix. Given elements of A as a i,j and elements of.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
The Complexity of Algorithms: Selected Exercises Goal: Introduce computational complexity analysis.
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.
CS Data Structures Chapter 2 Arrays and Structures.
Macro & Function. Function consumes more time When a function is called, the copy of the arguments are passed to the parameters in the function. After.
Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Programming Arrays. Question Write a program that reads 3 numbers from the user and print them in ascending order. How many variables do we need to store.
CHAPTER 2 ALGORITHM ANALYSIS 【 Definition 】 An algorithm is a finite set of instructions that, if followed, accomplishes a particular task. In addition,
1 Chapter 2 Program Performance – Part 2. 2 Step Counts Instead of accounting for the time spent on chosen operations, the step-count method accounts.
Chapter 1 Algorithm Analysis
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Prof. Hsin-Mu (Michael) Tsai ( 蔡欣穆 ) Department of Computer Science and Information Engineering National Taiwan University BASICS.
Program Performance & Asymptotic Notations CSE, POSTECH.
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.
CSCI 130 Scope of Variables Chapter 6. What is scope Parts of program which can access a variable –accessibility –visibility How long variable takes up.
SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY Lecture 12 CS2110 – Fall 2009.
Chapter 1 1. Overview: System life cycle Requirements Analysis Bottom-up Top-down Design Data objects (abstract data type) and operations (algorithm)
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
LANGUAGE TRANSLATORS: WEEK 24 TRANSLATION TO ‘INTERMEDIATE’ CODE (overview) Labs this week: Tutorial Exercises on Code Generation.
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.
A.Abhari CPS1251 Multidimensional Arrays Multidimensional array is the array with two or more dimensions. For example: char box [3] [3] defines a two-dimensional.
Lecture 4. RAM Model, Space and Time Complexity
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data Depends.
5-1 Chapter 5 - Languages and the Machine Department of Information Technology, Radford University ITEC 352 Computer Organization Principles of Computer.
5-1 Chapter 5 - Languages and the Machine Principles of Computer Architecture by M. Murdocca and V. Heuring © 1999 M. Murdocca and V. Heuring Principles.
1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data.
Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of the problem.
SNU IDB Lab. Ch4. Performance Measurement © copyright 2006 SNU IDB Lab.
***** SWTJC STEM ***** Chapter 7 cg 50 Arrays as Parameters Regular variables are passed to a method by value; i.e., the variable value is copied to a.
Foundations of Algorithms, Fourth Edition
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Functions Functions, locals, parameters, and separate compilation.
Methods.
Software Learning Resource Service Platform BASIC CONCEPT CHAPTER 1 BASIC CONCEPT 1.
Arrays and Matrices. One-Dimensional Arrays An array is an indexed data structure All variables stored in an array are of the same data type An element.
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.
1 Chapter 2 Program Performance. 2 Concepts Memory and time complexity of a program Measuring the time complexity using the operation count and step count.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Arrays float Scores[9]; ? index: element // one dimensional array 2.
1 7.Algorithm Efficiency These factors vary from one machine/compiler (platform) to another  Count the number of times instructions are executed So, measure.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
SPACE COMPLEXITY & TIME COMPLEXITY 1. ALGORITHMS COMPLEXITY 2.
User-Written Functions
Introduction to the C Language
Data Structures (1/2) 內容:Basic Concepts, Arrays and Structures, Stacks and Queues, Lists, Trees, Graphs, Sorting, Hashing, Heap Structures, and Search.
Data Structure 김용성 Data Structure in C.
CS 213: Data Structures and Algorithms
Data Structures Unit-1 Engineered for Tomorrow CSE, MVJCE.
Introduction to the C Language
Introduction to the C Language
Lecture 10 Arrays.
Data Structures-CSE207.
CS100: Discrete structures
Searching, Sorting, and Asymptotic Complexity
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.
Matrix Addition, C = A + B Add corresponding elements of each matrix to form elements of result matrix. Given elements of A as ai,j and elements of B as.
Data structures & Algorithm Strategies
Presentation transcript:

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 “Fundamentals of Data Structures in C”, Computer Science Press, 1992.

CHAPTER 12 Space Complexity S(P)=C+S P (I) n Fixed Space Requirements (C) Independent of the characteristics of the inputs and outputs –instruction space –space for simple variables, fixed-size structured variable, constants n Variable Space Requirements (S P (I)) depend on the instance characteristic I –number, size, values of inputs and outputs associated with I –recursive stack space, formal parameters, local variables, return address

CHAPTER 13 *Program 1.9: Simple arithmetic function (p.19) float abc(float a, float b, float c) { return a + b + b * c + (a + b - c) / (a + b) ; } *Program 1.10: Iterative function for summing a list of numbers (p.20) float sum(float list[ ], int n) { float tempsum = 0; int i; for (i = 0; i<n; i++) tempsum += list [i]; return tempsum; } S abc (I) = 0 S sum (I) = 0 Recall: pass the address of the first element of the array & pass by value

CHAPTER 14 *Program 1.11: Recursive function for summing a list of numbers (p.20) float rsum(float list[ ], int n) { if (n) return rsum(list, n-1) + list[n-1]; return 0; } *Figure 1.1: Space needed for one recursive call of Program 1.11 (p.21) S sum (I)=S sum (n)=6n Assumptions:

CHAPTER 15 Time Complexity n Compile time (C) independent of instance characteristics n run (execution) time T P n Definition A program step is a syntactically or semantically meaningful program segment whose execution time is independent of the instance characteristics. n Example –abc = a + b + b * c + (a + b - c) / (a + b) –abc = a + b + c Regard as the same unit machine independent T(P)=C+T P (I) T P (n)=c a ADD(n)+c s SUB(n)+c l LDA(n)+c st STA(n)

CHAPTER 16 Methods to compute the step count n Introduce variable count into programs n Tabular method –Determine the total number of steps contributed by each statement step per execution  frequency –add up the contribution of all statements

CHAPTER 17 *Program 1.12: Program 1.10 with count statements (p.23) float sum(float list[ ], int n) { float tempsum = 0; count++; /* for assignment */ int i; for (i = 0; i < n; i++) { count++; /*for the for loop */ tempsum += list[i]; count++; /* for assignment */ } count++; /* last execution of for */ return tempsum; count++; /* for return */ } 2n + 3 steps Iterative summing of a list of numbers

CHAPTER 18 *Program 1.13: Simplified version of Program 1.12 (p.23) float sum(float list[ ], int n) { float tempsum = 0; int i; for (i = 0; i < n; i++) count += 2; count += 3; return 0; } 2n + 3 steps

CHAPTER 19 *Program 1.14: Program 1.11 with count statements added (p.24) float rsum(float list[ ], int n) { count++; /*for if conditional */ if (n) { count++; /* for return and rsum invocation */ return rsum(list, n-1) + list[n-1]; } count++; return list[0]; } 2n+2 Recursive summing of a list of numbers

CHAPTER 110 *Program 1.15: Matrix addition (p.25) 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]; } Matrix addition

CHAPTER 111 *Program 1.16: Matrix addition with count statements (p.25) void add(int a[ ][MAX_SIZE], int b[ ][MAX_SIZE], int c[ ][MAX_SIZE], int row, int cols ) { int i, j; for (i = 0; i < rows; i++){ count++; /* for i for loop */ for (j = 0; j < cols; j++) { count++; /* for j for loop */ c[i][j] = a[i][j] + b[i][j]; count++; /* for assignment statement */ } count++; /* last time of j for loop */ } count++; /* last time of i for loop */ } 2rows * cols + 2 rows + 1

CHAPTER 112 *Program 1.17: Simplification of Program 1.16 (p.26) 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++) count += 2; count += 2; } count++; } 2rows  cols + 2rows +1 Suggestion: Interchange the loops when rows >> cols

CHAPTER 113 *Figure 1.2: Step count table for Program 1.10 (p.26) Tabular Method steps/execution Iterative function to sum a list of numbers

CHAPTER 114 *Figure 1.3: Step count table for recursive summing function (p.27) Recursive Function to sum of a list of numbers

CHAPTER 115 *Figure 1.4: Step count table for matrix addition (p.27) Matrix Addition

CHAPTER 116 *Program 1.18: Printing out a matrix (p.28) void print_matrix(int matrix[ ][MAX_SIZE], int rows, int cols) { int i, j; for (i = 0; i < row; i++) { for (j = 0; j < cols; j++) printf(“%d”, matrix[i][j]); printf( “\n”); } } Exercise 1

CHAPTER 117 *Program 1.19:Matrix multiplication function(p.28) void mult(int a[ ][MAX_SIZE], int b[ ][MAX_SIZE], int c[ ][MAX_SIZE]) { int i, j, k; for (i = 0; i < MAX_SIZE; i++) for (j = 0; j< MAX_SIZE; j++) { c[i][j] = 0; for (k = 0; k < MAX_SIZE; k++) c[i][j] += a[i][k] * b[k][j]; } } Exercise 2

CHAPTER 118 *Program 1.20:Matrix product function(p.29) void prod(int a[ ][MAX_SIZE], int b[ ][MAX_SIZE], int c[ ][MAX_SIZE], int rowsa, int colsb, int colsa) { int i, j, k; for (i = 0; i < rowsa; i++) for (j = 0; j< colsb; j++) { c[i][j] = 0; for (k = 0; k< colsa; k++) c[i][j] += a[i][k] * b[k][j]; } } Exercise 3

CHAPTER 119 *Program 1.21:Matrix transposition function (p.29) void transpose(int a[ ][MAX_SIZE]) { int i, j, temp; for (i = 0; i < MAX_SIZE-1; i++) for (j = i+1; j < MAX_SIZE; j++) SWAP (a[i][j], a[j][i], temp); } Exercise 4