• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Data Structure
 

Data Structure

on

  • 2,358 views

 

Statistics

Views

Total Views
2,358
Views on SlideShare
2,355
Embed Views
3

Actions

Likes
3
Downloads
113
Comments
0

2 Embeds 3

http://www.slideshare.net 2
http://www.brijj.com 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Data Structure Data Structure Presentation Transcript

    • Data Structures and Algorithms Lecture # 1 Book: Fundamentals of Data Structures in c++ Horwitz, Sahani, and Mehta
    • Software Design Quality
      • What is good design?
      • - Is it efficient code?
      • - compact implementation?
      • - Most maintainable?
      • . For most large, long life time software systems,
      • maintenance cost normally exceeds development cost by
      • factors ranging from 2 to 3.
    • Maintainable Design
      • Cost of system changes is minimal
      • Readily adaptable to modify existing functionality and enhance functionality
      • Design is understandable
      • Changes should be local in effect
      • Design should be modular
    • Abstraction
        • . Abstraction is a technique in which we construct a
        • model of an entity based upon its essential
        • Characteristics while ignoring the inessential details.
        • . The principle of abstraction also helps in handling the
        • inherent complexity of a system by allowing looking
        • at its important external characteristic, and hiding its
        • inner complexity at the same time.
        • . Hiding the internal details is called encapsulation.
        • . Engineers of all fields, including computer science,
        • have been practicing abstraction for mastering
        • complexity.
    • Types of data Abstraction
        • . Code and Data abstraction
        • What is Data ?
      • - What is code ?
    • Advantages of data Abstraction
      • Simplification of software development
      • Testing and Debugging
      • Reusability
      • Modification to representation of a data type
      • etc
      • void selectionSort(int a[],int size) { int I,j,min,temp; for(i=0; i<size-1; i++) { min=i; for(j=i; i<size; j++) { if(a[j]< a[min]) min=j; } temp=a[i]; a[i]=a[min]; a[min]=temp; } }
      • int minimum(int a[],int from,int to) void swap(int &x, int &y)
      • { {
      • int min=from; int temp=x;
      • for(int i=from;i<=to;i++) x=y;
      • if(a[i] < a[min]) min=i; y=temp;
      • return min;
      • } }
      • void selectionSort(int a[],int size)
      • {
      • int i,j,min;
      • for(i=0;i<size-1;i++){
      • min=minimum(a,I,size-1)
      • swap(a[i],a[min]);
      • }
      • }
      • void selectionSort(int a[], int size)
      • {
      • int I,j,min,temp;
      • for(i=0;i<size-1;i++)
      • {
      • min=i; void selectionSort(int a[],int size)
      • for(j=i;j<size; j++) {
      • { int i,j,min;
      • if(a[j]< a[min]) for(i=0;i<size-1;i++) {
      • min=j; min=minimum(a,i,size-1);
      • } swap(a[i],a[min]);
      • temp=a[i]; }
      • a[i]=a[min]; }
      • a[min]=temp;
      • }
      • }
    • Data Abstraction and Abstract Data Types(ADT)
      • A data type is a template for objects and a set of
      • operations that define the behavior of the objects (or
      • instances) of that type.
      • An Abstract data type (ADT) is a data type in which
      • the implementation details are hidden and the user is
      • concerned with the properties ( or behavior ) of that
      • type.
      • An ADT has two commponents:
      • - Interface – the behavior
      • - Implementation
      • Example:
      • -int,float
    • Abstract Data Type
      • The data structures used to
      • implement the data type can only
      • be accessed through the interface.
      • Any change in the implementation
      • does not change the user
      • programs as long as the interface
      • remains the same.
      • This is also known as data
      • encapsulation or data abstraction.
      implementation interface1 interfece2 interface3 interface4
    • Abstraction Vs. Implementation
      • X -> 01000001 01000010 01000011 00000000
      • X = ?
      • If x is CString
      • -then x -> “ABC”
      • If x is integer
      • - then x -> 1094861568
      • int main()
      • {
      • int i, *pi;
      • float f, *pf;
      • i = 1024;
      • pi = &i;
      • pf = (float *) pi ;
      • f = *pf;
      • cout << i << “ “ <<f<<“ ”;
      • f = i ;
      • cout << i << “ “ <<f<<“ ”;
      • return 0;
      • }
      • 1024 1.43493e-042
      • 1024 1024
      • press any key to continue
    • Abstraction Vs. Implementation Two dimensional array User’s view (abstraction) System’s view (implementation) 54 345 106 0 99 82 76 22 64 38 2 3 6 3 5 1 54 345 106 0 99 82 76 22 64 38 2 3 6 3 5 1
    • ADTs and C++ Classes
      • A class is used to define (and implement) an ADT
      • in C++.
      • A class consists of data and functions that operate
      • on that data.
      • A class in C++ has two parts – public and private
      • (let’s ignore the protected members for now).
      • The data and functions defined in the class are
      • called the members of the class.
    • ADTs and C++ Classes
      • Users of the class can only access and
      • manipulate the class state through the public
      • members of the class.
      • Private members can only be used by other
      • members of the class (let’s also ignore the friends for now).
      • Data encapsulation is achieved by declaring
      • all data members of a class to be private.
      • The interface of the class is provided through
      • the use of public member functions.
    • Data Structures
      • The primary objective of programming is to efficiently
      • process the input to generate the desired output.
      • We can achieve this objective in an efficient and neat
      • style if the input data is organized in a way to help us
      • meet our goal.
      • Data Structures is nothing but ways and means of
      • organizing data so that it can be processed easily and
      • efficiently.
      • Data structures dictate the manner in which the data
      • can be processed. In other words, the choice of an
      • algorithm depends upon the underlying data
      • organization. ( What is an Algorithm ? )
    • What is an Algorithm ?
      • An algorithm is a well defined list of steps for solving a particular problem
      • An algorithm manipulates the data in data structures in various ways, such as inserting a new element, searching for a particular item etc.
      • An algorithm must satisfy the following criteria
      • 1) Input 2) output 3) Definiteness ( each instruction should be clear and unambiguous) 4) Fitness (terminates after finite number of steps) 5) Effectiveness (each instruction must be feasible enough)
    • Data Structure Operations
      • Traversing
      • Searching
      • Inserting
      • Deleting
      • Sorting
      • Merging
      • Recursion
      • To perform operations on various data structures we use algorithms.
    • Types of Data Structures
      • Premitive/Scalar : data types that can be manipulated as a single quantity or can be represented alone
      • Structured/ Non-Premitive (Data type which is collection of other premitive or non-premitive data structures.
        • Can be further divided into
      • a) linear b) non-linear
      • - Linear can be further split into
      • a) physically linear b) logically linear
      • problem: Determine if a key is present in a collection of 10 integers
      • Organization 1: Data are stored in 10 disjoint ( as opposed to
      • composite ) variables: A0, A2, A3,……,A9
      • Algorithm
      • found=false;
      • if (key = = A0 ) found = true;
      • else if (key = = A1 ) found = true;
      • else if (key = = A2 ) found = true;
      • else if (key = = A3 ) found = true;
      • else if (key = = A4 ) found = true;
      • else if (key = = A5 ) found = true;
      • else if (key = = A6 ) found = true;
      • else if (key = = A7 ) found = true;
      • else if (key = = A8 ) found = true;
      • else if (key = = A9) found = true;
      • problem: Determine if a key is present in a collection of 10 integers
      • Organization 2: Data are stored in an array of 10 elements
      • Algorithm
      • found=false;
      • for (int i =0; i < 10; i ++)
      • if ( A[i] == key)
      • {
      • found = true;
      • break;
      • }
      • Average number of comparisons in both cases is the same so
      • both algorithms are equally efficient (or in efficient)
      • Organization 2 is better because it yields an algorithms which is
      • more maintainable. For example, if the collection contains 100 elements. In general, number of elements could be N.
      • problem: Determine if a key is present in a collection of 10 integers
      • Organization 3: Data are stored in an array A in ascending order
      • Algorithm
      • found=false;
      • 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;
      • }
      • Average number of comparisons ?
      • Order of “log(N)” as compared to N.
    • found=false; 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; } Key=29 73 55 50 48 47 32 29 28 22 15 12 10 7 5 3 2 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
    • Performance Comparison
      • NADRA database: ~80,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
      • Why?
    • 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.
      • Mechanism should be independent of
      • the compiler and underlying hardware.
    • Step Count
      • Program Step: A program step is a
      • meaningful program segment.
      • We can consider each statement as a
      • single step.
      • a = 2;
      • a = 2 * b + c + 3 * c / d – e;
    • Step Count
      • To count total number of steps, we must
      • determine:
      • 1. Step count for each statement –
      • steps/execution or s/e
      • 2. Frequency of each statement
      • 3. Total steps for each statement
      • 4. Finally sum these counts to get the total step
      • count
    • Example 1 – Summing of a list of numbers (Step count Table) 2n+3 Total 0 0 0 } 1 1 1 return temp; N n 1 temp+=list[i] ; n + 1 n + 1 1 for (i=0;i<n; i++) 0 0 0 int i ; 1 1 1 float temp=0; 0 0 0 { 0 0 0 Float sum (float list[],int n) Total Steps Frequency S/e Statement
    • Problems
      • Determining the exact step count of a program can be a
      • Very difficult task
      • - 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
      • - Frequency of execution
      • . How many steps are executed?
      • if (condition)
      • {
      • step1;step2step3;step4;step5;
      • }
      • else
      • step6;
      We need some asymptotic notation as a measure of growth
    • Big Oh (O)
      • Big Oh is defined as:
      • F(n)=O(g(n)) iff there exists positive constants c and n 0
      • such that f(n)<= cg(n) for all values of n > = n 0
      • No matter what the value of c 1 , c 2 ,c 3 there will be an n
      • beyond which the program with complexity c 3 n will be faster than the one with complexity c 1 n2+c 2 n
      • Example: 1) 3n +3 = O(n) as 3n +3 <=4n for all n >=3
      • 2) 10n 2 + 4n + 2 = O(n 2 ) as 10n 2 + 4n +2 <=11n 2
      • An estimate of how will the running time grow as a function
      • of the problem size.
      • There are infinitely many functions g for a given function f:
      • - N = O(N); N = O(N 2 ) N = O(N 3 )
      • - Choose the smallest function g.
      • Theorem:If f(n)=a m n m +…..a 1 n+a 0 , then f(n)=O(n m )
      • -Choose the largest term in the polynomial
    • ----------------------  n growth
    • Big Oh (O)
      • Summing of list of numbers O(n)
      • Matrix addition O(rows,cols)
      • Searching a key in an array O(n)
      • Binary Search O(n log n)
    • Big Oh (O)
      • 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)
      • void Selection_sort(int list[], int size)
      • {
      • int I,j;
      • for(i=0;i<size-1;i++)
      • { //O(size) or O(n)
      • j= search_min(list, i+1 , size-1)
      • //O(n).O(n)=O(n 2 )
      • swap (list[i], list[j])
      • //O(1).O(n)=O(n)
      • }
      • //O(n)+O(n 2 )+O(n)+O(n)= O(n 2 )
      • }
    • Big Oh (O)
      • O(1) - constant
      • O(log n) - logarithmic
      • O(n) - linear
      • O(n log n) - log linear
      • O(n 2 ) - quadratic
      • O(n 3 ) - cubic
      • O(2 n ) - exponential
    • Assignment# 1 Last Submission Day : 07-09-2009 (Monday)
      • Given the following code segment
      • int x=0;
      • for (int i= 1; i<=n; i++)
      • for (int j = 1; j<=I; j++)
      • for (int k = 1; k<=j; k++)
      • x++;
      • What will be the value of x in terms of n after the following code is executed ?
      • Give the time complexity of the above code in big Oh notation
      • Prove the following
      • i) 5n 2 – 6n = O(n 2 )
      • ii) 6*2 n + n 2 = O (2 n )