it data structures

Upload: romeofatima

Post on 08-Mar-2016

220 views

Category:

Documents


1 download

DESCRIPTION

IT data structures

TRANSCRIPT

  • FAP:UC-BCF 1

    Advanced Data Structures

    Chapter I:

    Algorithms

  • FAP:UC-BCF 2

    Algorithm

    Any method of solving a certain kind of problem.

    A precise method usable by a computer for the solution of a problem.

    A finite set of instructions which, if followed accomplishes a particular task.

  • FAP:UC-BCF 3

    Guiding Principles Input

    0 or more quantities which are externally supplied.

    Output At least 1 quantity is produced.

    Finiteness Must terminate after a finite number of steps; traceability.

    Definiteness Each instruction must be clear and unambiguous.

    Effectiveness Basic and feasible.

  • FAP:UC-BCF 4

    C.S. View

    Machines for executing algorithms involves the study of various fabrications and organizations for algorithms to be carried out.

    Languages for describing algorithms 2 phases: Language design Translation

  • FAP:UC-BCF 5

    Foundation of Algorithms

    Can a particular task be accomplished by a computing device?

    What is the minimum number of operations for any algorithm to perform a certain function?

  • FAP:UC-BCF 6

    Analysis of Algorithms

    Know the behavior of the algorithm. This includes the pattern and performance profile of an algorithm, that can be measured in terms of its execution time and the amount of space consumed.

  • FAP:UC-BCF 7

    How to create programs

    Requirements:Information given (input) and the results to be produced (output) must be explicitly specified.Understand the problem.

    Design:Write an algorithm that will solve the problem according to the requirement.

    Analysis:Trying to come up with another algorithm and compare it with the previous one.

  • FAP:UC-BCF 8

    How to create programs

    Refinement and Coding:A representation is chosen and a complete version of the program is made.

    Verification:Consist of 3 aspects:

    Program Proving Testing and Debugging

  • FAP:UC-BCF 9

    Example 1:Problem: Accept N inputs and get their sumRequirement:

    Input: Set of N integersOutput: Sum of N integers

    Design:Set a temporary variable to 0.For each value entered, add the value to the temporary variable.

  • FAP:UC-BCF 10

    Example 1:Refinement and Coding:

    tempsum = 0;for(i=1; i

  • FAP:UC-BCF 11

    Example 2:Problem: Sort a set of n>=1 integers in non-descending order.Requirement:

    Input: Set of N integers (N>=1)Output: Sorted set of N integers

    Design:Find the smallest and place it on the first position of the list.For the integers which remain to be unsorted, find the smallest and place it next in the sorted list.

  • FAP:UC-BCF 12

    Example 2:Refinement and Coding:Representation: 1 dimensional array

    #define n 4void sort(int A[]){

    int temp, i, k, smallest;for(i=1; i

  • FAP:UC-BCF 13

    How to analyze programs:

    Two Phases:1. Priori Estimates:

    Obtain a function which bounds the algorithms complexity time. The amount of time a single execution will take or the number of times a statement is executed.However, it is possible to know the exact amount of time to execute any command unless:a. the machine we are executing is known;b. machine instruction setc. time required by each machine instruction;d. translation of the compiler from source to machine lang.

  • FAP:UC-BCF 14

    How to analyze programs:

    2. Posteriori Estimates:Something to do with memory spaces consumed.

    Example:Use of arrays vs. linked-list

  • FAP:UC-BCF 15

    Frequency Count and Time Complexity (Big-Oh)Asymptotic notations mean N :

    if A(n) = Amnm + + A1n1 + A0 is a polynomial of degree m, then A(n) = O(nm).

    Common computing times.*N is the input parameterO(1) < O(log n) < O(n) < O(n log n) < O(n2) < O(n3) < O(2n)

  • FAP:UC-BCF 16

    Frequency Count and Time Complexity (Big-Oh)O(1)

    constant; most instructions are executed once or at most only a few times.

    O(log n)program slightly slower as N grows; normally in programs that solve a big problem by transforming it into a small problem, cutting the size by some constant factor.

    O(n)linear; proportional to the size of N

  • FAP:UC-BCF 17

    Frequency Count and Time Complexity (Big-Oh)O(n log n)

    occurs in algorithms that solve a problem by breaking it up into smaller subproblems, solve them independently and then combining the solution.

    O(n2)quadratic; can be seen in algorithms that process all pairs of data items.

    O(n3)cubic; algorithms that process triples of data items.

  • FAP:UC-BCF 18

    Frequency Count and Time Complexity (Big-Oh)O(2n)

    exponential; brute-force solution

    Given 2 algorithms performing the same task on N inputs:P1 P210n n2/2O(n) O(n2)

    Which is faster and efficient?

  • FAP:UC-BCF 19

    Frequency Count and Time Complexity (Big-Oh)

    N P1 P21 ___ ___5 ___ ___10 ___ ___15 ___ ___20 ___ ___30 ___ ___

    Substitute values in N and determine which among the two algorithms is more efficient and faster.

  • FAP:UC-BCF 20

    Frequency Count and Time Complexity (Big-Oh)

    N P1 P21 10 0.55 50 12.510 100 5015 150 112.520 200 20030 300 450

    P2 is more faster and efficient for N 200, P1 proves to be more faster.

  • FAP:UC-BCF 21

    General RulesRule 1: For Loops

    The running time for a for loop is at most the running time of the statements inside the for loop (including tests) times the number of iterations.

    Rule 2: Nested For LoopsAnalyze these inside out. The total running time of a statement inside a group of nested for is the running time of the statement multiplied by the product of the sizes of all the for loops.

    Rule 3: Consecutive StatementsThese just add, which means that the maximum is the one that counts.

  • FAP:UC-BCF 22

    General RulesRule 4: if/else Statement

    For the fragment:if(cond)

    S1;else S2;

    the running time of an if/else statement is never more than the running time of the test plus the larger of the running times of S1 and S2.

  • FAP:UC-BCF 23

    Exercise:Determine the frequency count and the corresponding

    Big-Oh Notation.Frequency Count

    1. K = 500; 1for(j=1; j

  • FAP:UC-BCF 24

    Exercise:2. {

    for(j=1; j

  • FAP:UC-BCF 25

    Exercise:3. for(k = 1; k

  • FAP:UC-BCF 26

    Exercise:6. ctr = 1;

    while ctr

  • FAP:UC-BCF 27

    Exercise:8. l =5; m = m + 123;

    do{

    k=k+1; y=y+1;for(l=2; l

  • FAP:UC-BCF 28

    Exercise:10. while(l>=n)

    {k = k + 1;x = x + 1;l = l 1;

    }

    11. while(l>n){

    k = k + 1;l = l 1;

    }

  • FAP:UC-BCF 29

    Exercise:10. do

    {h = h 1;

    }while(h != n);

    11. do{

    h = h 1;}while(h>=n);

  • FAP:UC-BCF 30

    Exercise:12. if(x>2)

    {x++;printf(%d, x);

    }else{

    for(l=0; l