Algorithms and Time Complexity

Slides:



Advertisements
Similar presentations
Complexity Theory  Complexity theory is a problem can be solved? Given: ◦ Limited resources: processor time and memory space.
Advertisements

Algorithm Complexity Analysis: Big-O Notation (Chapter 10.4)
Analysys & Complexity of Algorithms Big Oh Notation.
Chapter 1 – Basic Concepts
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
1 CSE1301 Computer Programming Lecture 31: List Processing (Search)
Data Structure Algorithm Analysis TA: Abbas Sarraf
Abstract Data Types (ADTs) Data Structures The Java Collections API
Algorithm Analysis (Big O)
C. – C. Yao Data Structure. C. – C. Yao Chap 1 Basic Concepts.
Data Structures and Algorithms Lecture # 1 Book: Fundamentals of Data Structures in c++ Horwitz, Sahani, and Mehta.
CSE1301 Computer Programming: Lecture 26 List Processing (Search)
Big Oh Algorithms are compared to each other by expressing their efficiency in big-oh notation Big O notation is used in Computer Science to describe the.
1 Lecture 5 Generic Types and Big O. 2 Generic Data Types A generic data type is a type for which the operations are defined but the types of the items.
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Chapter 2 Computational Complexity. Computational Complexity Compares growth of two functions Independent of constant multipliers and lower-order effects.
Algorithm Analysis (Big O)
1 Algorithm Analysis. 2 Question Suppose you have two programs that will sort a list of student records and allow you to search for student information.
DS.A.1 Algorithm Analysis Chapter 2 Overview Definitions of Big-Oh and Other Notations Common Functions and Growth Rates Simple Model of Computation Worst.
1 Chapter 2 Algorithm Analysis All sections. 2 Complexity Analysis Measures efficiency (time and memory) of algorithms and programs –Can be used for the.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
SORTING Sorting is storage of data in some order, it can be in ascending or descending order. The term Sorting comes along-with the term Searching. There.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
Section 1.7 Comparing Algorithms: Big-O Analysis.
GC 211:Data Structures Week 2: Algorithm Analysis Tools Slides are borrowed from Mr. Mohammad Alqahtani.
2017, Fall Pusan National University Ki-Joune Li
Algorithm Analysis 1.
Complexity Analysis (Part I)
Chapter 2 Algorithm Analysis
Mathematical Foundation
Analysis of Algorithms
Analysis of Algorithms
Algorithm Analysis.
GC 211:Data Structures Week 2: Algorithm Analysis Tools
COMP108 Algorithmic Foundations Algorithm efficiency
Introduction to complexity
Unit 2 Introduction to Complexity Analysis
Asymptotic Notations Algorithms perform f(n) basic operations to accomplish task Identify that function Identify size of problem (n) Count number of operations.
Data structure – is the scheme of organizing related information.
Introduction to Algorithms
GC 211:Data Structures Algorithm Analysis Tools
COMP 53 – Week Seven Big O Sorting.
Analysis of Algorithms
Building Java Programs
Chapter 12: Analysis of Algorithms
GC 211:Data Structures Algorithm Analysis Tools
What is CS 253 about? Contrary to the wide spread belief that the #1 job of computers is to perform calculations (which is why the are called “computers”),
Asymptotic Notations Algorithms perform f(n) basic operations to accomplish task Identify that function Identify size of problem (n) Count number of operations.
2018, Fall Pusan National University Ki-Joune Li
CSC 205 Java Programming II
Analysys & Complexity of Algorithms
Programming and Data Structure
Analysis of Algorithms
DS.A.1 Algorithm Analysis Chapter 2 Overview
Programming and Data Structure
Analyzing an Algorithm Computing the Order of Magnitude Big O Notation
Searching, Sorting, and Asymptotic Complexity
Asymptotic Notations Algorithms perform f(n) basic operations to accomplish task Identify that function Identify size of problem (n) Count number of operations.
Revision of C++.
Analysis of Algorithms
Sum this up for me Let’s write a method to calculate the sum from 1 to some n public static int sum1(int n) { int sum = 0; for (int i = 1; i
Analysis of Algorithms
Complexity Analysis (Part II)
Algorithm/Running Time Analysis
Complexity Analysis (Part I)
Analysis of Algorithms
Algorithms and data structures: basic definitions
Complexity Analysis (Part I)
2019, Fall Pusan National University Ki-Joune Li
Data Structures & Programming
Presentation transcript:

Algorithms and Time Complexity

Find Maximum Element int maximum(int B[],int n) { int max = B[0]; for(int j=0;j<n;j++) if(B[j] > max) max = [j]; } return max;

Linear Search int search(int B[],int size, int value) { for(int j=0;j<size;j++) if(B[j] = = value) return 1; } return 0;

Binary Search Algorithm found = false; low = 0; high = N – 1; while (( ! found) && ( low <= high)) { mid = (low + high)/2; if (A[mid] = = key) found = true; else if (A[mid] > key) high = mid – 1; else low = mid + 1; }

found = false; low = 0; high = N – 1; while (( ! found) && ( low <= high)) { mid = (low + high)/2; if (A[mid] = = key) found = true; else if (A[mid] > key) high = mid – 1; else low = mid + 1; } 15 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 22 28 29 32 47 48 50 55 73 8 7 11 7 11 9 15 10 29 key = 29 Average number of comparisons ? Order of “log(N)” as compared to N.

Performance Comparison NADRA database: ~80,000,000 records Computer which can perform 10,000 comparisons per second Linear search: ~2.22 hours Binary search: ~0.005 seconds Roughly 1.6 million times less

Performance Analysis Does the program efficiently use primary and secondary storage? Is the program’s running time acceptable for the task? Space Complexity: The space complexity of a program is the measure of the amount of memory that it needs to run to completion. Time Complexity: The time complexity of a program is the measure of the amount of computer time it needs to run to completion.

Performance Estimation How to determine which algorithm is better? We need some mechanism to predict the performance without actually executing the program.

Example 1 – Summing of a list of numbers #include <iostream> using namespace std; int main() { int size=10; int a[size]; for (int n =0; n < size;n++) cin >> a[n]; } int sum = 0; for (int i=0; i<size; i++) sum = sum + a[i]; cout << sum << endl; return 0; O(n))

Example 2 – Matrix addition #include <iostream> using namespace std; const int row =2; const int col=3; void sum (int a[][col], int b[][col],int c[][col], int row, int col); int main() { int a[row][col]; int b[row][col]; int c[row][col]; for (int i =0; i < row;i++) { for(int j=0; j<col;j++) { cout<<"Enter values for first matrix"<<endl; cin >> a[i][j]; cout<<"Enter values for second matrix"<<endl; cin >> b[i][j]; } sum (a,b,c,row,col); { for(int j=0; j<col;j++) cout<< c[i][j]; cout<<" "; cout<<endl; return 0; void sum (int a[][col], int b[][col],int c[][col], int row, int col) c[i][j]=a[i][j]+b[i][j]; Example 2 – Matrix addition O(n2))

for (int i = 1; i <= 4; i++) { int sum = 0; if (i for (int i = 1; i <= 4; i++) { int sum = 0; if (i != 4) { for (j = 1; j <= 4; j++) cin >> num; cout << num << " "; sum = sum + num; } cout << "sum = " << sum << endl; O(n2))

int main() { int i,j,k; for(k=1;k<=32;k++) { for(i=k;i<=30;i++) cout<<"*"; for(j=2;j<=k;j++) cout<<" "; cout<<endl; } O(n2))

int main() { for(int i=0; i<1;i++) { cout<<i+1<<endl; for(int j=0;j<2;j++) { cout<<j+1; } cout<<"\n"; for(int k=0; k<3; k++) { cout<<k+1; for(int l=0; l<4; l++) { cout<<l+1; for(int m=4; m>0; m--) { cout<<m; for(int o=2; o>0; o--) { cout<<o; } cout<<"\n"; for(int p=0; p<1; p++) { cout<<p+1; return 0;

Big Oh (O) Determining the exact step count of a program can be a very difficult task Because of the inexactness of the definition of a step, exact step count is not very useful for comparative purposes. e.g., which one is better 45n+3 or 100n+10 We use some asymptotic notations as measure of growth.

Big Oh (O) An estimate of how will the running time grow as a function of the problem size.

Big Oh (O) O(n) int search_min(int list[], int from, int to) { } int i; int min= from; for (i=from; i<=to; i++) If (list[i] < list[min]) min = i; return min; } O(n)

void swap(int &a, int &b) { int temp=a; a = b; b = temp; } O(1)

Big Oh (O) O(1) - constant O(log n) - logarithmic O(n) - linear O(n log n) - log linear O(n2) - quadratic O(n3) - cubic O(2n) - exponential

Big Oh (O) Summing of list of numbers Matrix addition Searching a key in an array Binary Search O(n) O(rows.cols) O(log n)