Data Structures-CSE207.

Slides:



Advertisements
Similar presentations
CHAPTER 2 ALGORITHM ANALYSIS 【 Definition 】 An algorithm is a finite set of instructions that, if followed, accomplishes a particular task. In addition,
Advertisements

Analysys & Complexity of Algorithms Big Oh Notation.
Chapter 1 – Basic Concepts
1 Data Structures Performance Analysis. 2 Fundamental Concepts Some fundamental concepts that you should know: –Dynamic memory allocation. –Recursion.
Introduction to Analysis of Algorithms
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.
© Love Ekenberg The Algorithm Concept, Big O Notation, and Program Verification Love Ekenberg.
Data Structures Performance Analysis.
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
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.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 1 Prepared by İnanç TAHRALI.
CHAPTER 2 ALGORITHM ANALYSIS 【 Definition 】 An algorithm is a finite set of instructions that, if followed, accomplishes a particular task. In addition,
Algorithm analysis and design Introduction to Algorithms week1
Instructor Neelima Gupta
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.
Prof. Hsin-Mu (Michael) Tsai ( 蔡欣穆 ) Department of Computer Science and Information Engineering National Taiwan University BASICS.
Program Performance & Asymptotic Notations CSE, POSTECH.
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.
1 R. Johnsonbaugh, Discrete Mathematics Chapter 4 Algorithms.
1 Recursion Algorithm Analysis Standard Algorithms Chapter 7.
Chapter 1 1. Overview: System life cycle Requirements Analysis Bottom-up Top-down Design Data objects (abstract data type) and operations (algorithm)
Unit III : Introduction To Data Structures and Analysis Of Algorithm 10/8/ Objective : 1.To understand primitive storage structures and types 2.To.
Data Structures in C Networking Laboratory Chap. 1-1 Copyright(c) 2000, Sungkyunkwan University Chapter #1: BASIC CONCEPTS Fundamentals of Data Structures.
Complexity Analysis Chapter 1.
CS Data Structures Chapter 1 Basic Concepts.
Introduction Algorithms and Conventions The design and analysis of algorithms is the core subject matter of Computer Science. Given a problem, we want.
1 COMP3040 Tutorial 1 Analysis of algorithms. 2 Outline Motivation Analysis of algorithms Examples Practice questions.
Complexity of Algorithms
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
CSC310 © Tom Briggs Shippensburg University Fundamentals of the Analysis of Algorithm Efficiency Chapter 2.
Algorithm Analysis Part of slides are borrowed from UST.
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.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
SPACE COMPLEXITY & TIME COMPLEXITY 1. ALGORITHMS COMPLEXITY 2.
Data Structures I (CPCS-204) Week # 2: Algorithm Analysis tools Dr. Omar Batarfi Dr. Yahya Dahab Dr. Imtiaz Khan.
2017, Fall Pusan National University Ki-Joune Li
Problem Solving & Computer Programming
Analysis of Non – Recursive Algorithms
Analysis of Non – Recursive Algorithms
Design and Analysis of Algorithms Chapter -2
Analysis of Algorithms
GC 211:Data Structures Week 2: Algorithm Analysis Tools
Introduction to Algorithms
GC 211:Data Structures Algorithm Analysis Tools
What is an Algorithm? Algorithm Specification.
Data Structures (1/2) 內容:Basic Concepts, Arrays and Structures, Stacks and Queues, Lists, Trees, Graphs, Sorting, Hashing, Heap Structures, and Search.
Lecture 2 Analysis of Algorithms
Data Structure 김용성 Data Structure in C.
Courtsey & Copyright: DESIGN AND ANALYSIS OF ALGORITHMS Courtsey & Copyright:
DATA STRUCTURES Introduction: Basic Concepts and Notations
Algorithm Analysis (not included in any exams!)
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.
GC 211:Data Structures Algorithm Analysis Tools
Algorithm Efficiency Chapter 10.
2018, Fall Pusan National University Ki-Joune Li
Analysys & Complexity of Algorithms
Advanced Analysis of Algorithms
Complexity Analysis Text is mainly from Chapter 2 by Drozdek.
Introduction to Algorithm and its Complexity Lecture 1: 18 slides
Performance analysis of algorithms
At the end of this session, learner will be able to:
Discrete Mathematics 7th edition, 2009
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
2019, Fall Pusan National University Ki-Joune Li
Presentation transcript:

Data Structures-CSE207

Course Detail BASIC CONCEPTS OF ALGORITHM (1.2.1,1.4,1.4.1,1.4.2,1.4.3 of Text3) POINTERS AND POINTER APPLICATIONS (9-3 to 9-7, 10.1 to 10.6 of Text1) DERIVED TYPES (12.1,12.2,12.4-12.7 of Text1) RECURSION (3.1-3.5 of Text2) STACKS AND QUEUES (3.1,3.2,3.4,3.5 of Text3) LINKED LIST (4.2-4.4,4.8 of Text3) TREES (5.1,5.2,5.3,5.7,10.2 of Text3) GRAPHS AND THEIR APPLICATIONS (6.1-6.4 of Text3) SEARCHING AND SORTING (7.1,7.3,7.4 of Text3) HASHING (8.2 of Text3)

Text Books Behrouz A. Forouzan, Richard F. Gilberg “A Structured Programming Approach Using C”,Thomson, 2nd Edition, 2003. Aaron M. Tenenbaum,Yedidyah Langsam,Moshe J. Augeustein,”Data Structures using C”, PEARSON Education , 2006. Ellis Horowitz, Sartaj Sahni , Anderson, “ Fundamentals of Data Structures in C”, Silicon Press, 2nd Edition, 2007.

REFERENCES Richard F. Gilberg, Behrouz A. Forouzan, “Data structures, A Pseudocode Approach with C”, Thomson, 2005. Robert Kruc & Bruce Lening, “ Data structures & Program Design in C”, Pearson Education, 2007. Mark Allen Weiss,” Data Structures and Algorithm Analysis in C”, Prentice Hall

DataStructure Definition A data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently.

Basic concepts of Algorithm An algorithm is a finite set of instructions that , if followed , accomplishes a particular task. Characteristics of an algorithm Input: These are 0 or more quantities that are externally supplied. Output: At least one quantity is produced Definiteness: Each instruction is clear and unambiguous. Finiteness: If we trace out instructions of an algorithm,then for all cases ,the algorithm terminates after a finite number of steps Effectiveness: Every instruction must be basic enough to be carried out, in principle by a person using a pencil and paper.i.e instructions must be feasible.

Sorting-Ascending Selection sort algorithm Find the minimum value in the list Swap it with the value in the first position Repeat the steps 1 and 2 for the remainder of the list (starting at the second position and advancing each time) Example: 64 25 12 22 11 11 25 12 22 64 11 12 25 22 64 11 12 22 25 64

Selection sort algorithm bit revised for(i=0;i<n;i++){ Examine list[i] to list[n-1] and suppose that the smallest integer is at list[min]; Interchange /swap list[i] with list[min]. }

Performance Analysis Space Complexity: The space complexity of a program is the amount of memory it needs to run to completion. Time Complexity: The time complexity of a program is the amount of computer time it needs to run to completion

Space complexity A fixed part that is independent of the characteristics of the inputs and outputs. This part typically includes the instruction space, space for simple varialbes and fixed-size component variables, space for constants, etc. A variable part that consists of the space needed by component variables whose size is dependent on the particular problem instance I being solved, the space needed by referenced variables, and the recursion stack space. The space requirement S(P)of any program P is written as S(P) = c +Sp(I) where c is a constant and Sp(I) is variable part.

Examples float abc(float a,float b, float c){ return a+b+c+b*c; } Sabc(I)=0 float sum(float list[],int n){ float tempsum=0; int i; for (i=0;i<n;i++) tempsum+=list[i]; return(tempsum); } in C Ssum(I)=0

float rsum(float list[],int n) { if(n) return rsum(list,n-1)+list[n-1]); return 0; } Srsum(I)=6*n

Time complexity The time, T(P), taken by a program P is the sum of the compile time and the run (or execution) time. The compile time does not depend on the instance characteristics. We focus on the run time of a program, denoted by Tp (instance characteristics).

Determining Tp requires detailed knowledge of Compiler translation procedure. Obtaining such a detailed estimate is rarely worth the effort. We count no.of operations in a program This gives us a m/c independent measure But we should know how to divide the program into distinct steps.

Definition: A program step is a syntactically or semantically meaningful program segment whose execution time is independent of instance characteristics. A=2 and A=2*a+b/c+d both are treated as steps one each though their actual time differs. What we need is they should be independent of instance characteristics

Computation of timecomplexity using global count method float sum(float list[],int n){ float tempsum=0; count++;//assignment int i; for (i=0;i<n;i++) {count++;//whole for loop as one // step tempsum+=list[i]; count++;//assignment } count++; //last execution of for count++; //for return return(tempsum); } // count=2n+3

float rsum(float list[],int n) { count++; //for if condition if(n){count++; //for return and rsum //invocation return rsum(list,n-1)+list[n-1]); } count++;// for return0 return 0; } //count=2n+2

Calculate step count for void add(int a[][maxsize],int b[][maxsize],int c[][maxsize],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];} }

Finding stepcount using tabular method We first find steps/execution or s/e . Next we find number of times that each statement is executed.This is called frequency. s/e*frequency gives us step count for each statement Summing these for every statement gives total step count for whole function.

Statement s/e frequency Total steps float sum(float list[],int n) { float tempsum=0; int i; for (i=0;i<n;i++) tempsum+=list[i]; return(tempsum); } 1 1 n+1 n 1 n+1 n Total 2n+3

Statement s/e frequency Total steps float rsum(float list[],int n) { if(n) return rsum(list,n-1)+list[n-1]); return 0; } 1 n+1 n 1 n+1 n 1 Total 2n+2

Statement s/e frequency Total steps void add(int a[][maxsize],int b[][maxsize],int c[][maxsize],int rows,int cols) { int i,j; for(i=0;i<r;i++) for(j=0;j<c;j++) c[i][j]=a[i][j]+b[i][j]; } 1 r+1 r(c +1) r.c r+1 rc+r rc Total 2rc+2r+1

Asymptotic Notation Determining step counts help us to compare the time complexities of two programs and to predict the growth in run time as the instance characteristics change. But determining exact step counts could be very difficult. Since the notion of a step count is itself inexact, it may be worth the effort to compute the exact step counts. 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

Examples of Asymptotic Notation 3n + 2 = O(n) 3n + 2 ≤ 4n for all n ≥ 3 100n + 6 = O(n) 100n + 6 ≤ 101n for all n ≥ 10 10n2 + 4n + 2 = O(n2) 10n2 + 4n + 2 ≤ 11n2 for all n ≥ 5 Note: O(2)=O(1)=constant computing time

Asymptotic Notation (Cont.) Theorem 1.2: If f(n) = amnm + … + a1n + a0, then f(n) = O(nm). Proof: for n ≥ 1 So, f(n) = O(nm)

Asymptotic Notation f(n)=O(g(n)) only states that g(n) is an upperbound on the value of f(n) for all values of n>=n0. n=O(n2),n=O(n3)…. Inorder for this statement to be informative g(n) must be as small function of n one can come up with for which f(n)=O(g(n)).So we say 3n+2=O(n) rather than O(n2). Lastly f(n)=O(g(n)) does not imply O(g(n))=f(n). Here = should be read as “is”

Asymptotic Notation (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. (n0>0) Example: 3n + 2 = Ω(n) as 3n+2>=3n n>=1 100n + 6 = Ω(n) as 100n+6>=100n ,n>=1 10n2 + 4n + 2 =Ω(n2) as 10n2 + 4n + 2 >=n2 ,n>=1. Like O(n) here g(n) is only a lower bound on f(n)i.e g(n) should be as large a function of n as possible for which the statement f(n)= Ω(g(n)) is true. Hence we never say 3n+2= Ω(1) though it is true.

Asymptotic Notation (Cont.) Definition: 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. 3n+2= Θ(n) as 3n+2<=4n,n>=2 and 3n+2>=3n n>=2 f(n)= Θ(g(n)) iff g(n) is both upper and lower bound of f(n). Note: In all these we neglect coefficients i.e we do not say 3n+2=O(3n) though it is correct

Asymptotic Notation (Cont.) Theorem 1.3: If f(n) = amnm + … + a1n + a0 and am > 0, then f(n) = Ω(nm). Theorem 1.4: If f(n) = amnm + … + a1n + a0 and am > 0, then f(n) = Θ(nm).

Use of Asymptotic notations The asymptotic complexity can be determined quite easily without determining the exact step count. We determine asymptotic complexity of each or a group of statements in a program and then add these to get total asymptotic complexity

Statement Asymptotic complexity void add(int a[][maxsize],int b[][maxsize],int c[][maxsize],int rows,int cols) { int i,j; for(i=0;i<r;i++) for(j=0;j<c;j++) c[i][j]=a[i][j]+b[i][j]; } Θ(r) Θ(r.c) Total Θ(r.c)

Practical Complexities If a program P has complexities Θ(n) and program Q has complexities Θ(n2), then, in general, we can assume program P is faster than Q for a sufficient large n. However, caution needs to be used on the assertion of “sufficiently large”.

Function Values log n n n log n n2 n3 2n 1 1 1 2 1 2 2 4 8 4 2 4 8 16 1 1 1 2 1 2 2 4 8 4 2 4 8 16 64 16 3 8 24 64 512 256 4 16 64 256 4096 65536 5 32 160 1024 32768 4294967296

S.T.The following statements are correct/incorrect 5n2-6n= Θ(n2). 2n2+nlogn= Θ(n2) 10n2+9=O(n) N2logn= Θ(n2).