meljun_cortes_jedi slides-data structures-chapter01-basic concepts and notations

Upload: meljun-cortes-mbampa

Post on 06-Apr-2018

225 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/3/2019 MELJUN_CORTES_JEDI Slides-Data Structures-Chapter01-Basic Concepts and Notations

    1/23

    Data Structures Basic Concepts and Notations 1

    1 Basic Concepts andNotations

  • 8/3/2019 MELJUN_CORTES_JEDI Slides-Data Structures-Chapter01-Basic Concepts and Notations

    2/23

    Data Structures Basic Concepts and Notations 2

    Objectives

    At the end of the lesson, the student should be able to:

    Explain the process ofproblem solving

    Define data type, abstract data type and data structure

    Identify the properties of an algorithm

    Differentiate the two addressing methods - computedaddressing and link addressing

    Use the basic mathematical functions to analyze

    algorithms Measure complexity of algorithms by expressing the

    efficiency in terms of time complexity and big-O notation

  • 8/3/2019 MELJUN_CORTES_JEDI Slides-Data Structures-Chapter01-Basic Concepts and Notations

    3/23

    Data Structures Basic Concepts and Notations 3

    Problem Solving Process

    Programming a problem-solving process which could beviewed in terms of the following domains:

    Problem domain

    input or the raw data to process output or the processed data

    Machine domain

    storage medium - consists of serially arranged bits that are addressableas a unit

    processing unit - allow us to perform basic operations Solution domain - links the problem and machine domains

  • 8/3/2019 MELJUN_CORTES_JEDI Slides-Data Structures-Chapter01-Basic Concepts and Notations

    4/23

    Data Structures Basic Concepts and Notations 4

    Problem Solving Process

    Problem Domain

  • 8/3/2019 MELJUN_CORTES_JEDI Slides-Data Structures-Chapter01-Basic Concepts and Notations

    5/23

    Data Structures Basic Concepts and Notations 5

    Problem Solving Process

    Machine Domain

  • 8/3/2019 MELJUN_CORTES_JEDI Slides-Data Structures-Chapter01-Basic Concepts and Notations

    6/23

    Data Structures Basic Concepts and Notations 6

    Problem Solving Process

    Solution Domain

  • 8/3/2019 MELJUN_CORTES_JEDI Slides-Data Structures-Chapter01-Basic Concepts and Notations

    7/23

    Data Structures Basic Concepts and Notations 7

    Problem Solving Process

    Two related tasks at the solution domain

    Structuring of higher level data representations

    Synthesis of algorithms

    Data structures and algorithms are the building blocks ofcomputer programs

  • 8/3/2019 MELJUN_CORTES_JEDI Slides-Data Structures-Chapter01-Basic Concepts and Notations

    8/23

    Data Structures Basic Concepts and Notations 8

    Data Type, Abstract Data

    Type and Data Structure Data type - kind of data that variables can assume in a

    programming language and for which operations areautomatically provided

    Abstract Data Type (ADT) - mathematical model withdefined operations. In Java, an ADT can be expressed withan interface

  • 8/3/2019 MELJUN_CORTES_JEDI Slides-Data Structures-Chapter01-Basic Concepts and Notations

    9/23

    Data Structures Basic Concepts and Notations 9

    Data Type, Abstract Data

    Type and Data Structurepublic interface Stack{

    public int size(); /* returns the size of the stack */public boolean isEmpty(); /* checks if empty */public Object top() throws StackException;

    public Object pop() throws StackException;public void push(Object item) throws StackException;

    }

    Data structure the implementation of ADT in terms of the

    data types or other data structures.In Java, a data structurecan be expressed with a class

  • 8/3/2019 MELJUN_CORTES_JEDI Slides-Data Structures-Chapter01-Basic Concepts and Notations

    10/23

    Data Structures Basic Concepts and Notations 10

    Algorithm

    Finite set of instructions which, if followed, will accomplish atask

    Finiteness - an algorithm must terminate after a finite number ofsteps

    Definiteness - ensured if every step of an algorithm is preciselydefined

    Input - domain of the algorithm which could be zero or morequantities

    Output - set of one or more resulting quantities; also called the rangeof the algorithm

    Effectiveness - ensured if all the operations in the algorithm aresufficiently basic that they can, in principle, be done exactly and infinite time by a person using paper and pen

  • 8/3/2019 MELJUN_CORTES_JEDI Slides-Data Structures-Chapter01-Basic Concepts and Notations

    11/23

    Data Structures Basic Concepts and Notations 11

    Addressing Methods Computed Addressing Method - used to access the

    elements of a structure in pre-allocated space

    int x[10][20];

    a = x[4][3]; Link Addressing Method used to manipulate dynamic

    structures where the size and shape are not knownbeforehand or changes at runtime

    class Node{Object info;Node link;

    Node() { }

    Node (Object o, Node l){info = o;link = l;

    }

    }

  • 8/3/2019 MELJUN_CORTES_JEDI Slides-Data Structures-Chapter01-Basic Concepts and Notations

    12/23

    Data Structures Basic Concepts and Notations 12

    Addressing Methods

    Memory pool oravail list - source of the nodes from which linked

    structures are built

    class AvailList {Node head;

    AvailList(){

    head = null;}

    AvailList(Node n){head = n;

    }}

  • 8/3/2019 MELJUN_CORTES_JEDI Slides-Data Structures-Chapter01-Basic Concepts and Notations

    13/23

    Data Structures Basic Concepts and Notations 13

    Addressing Methods Two basic procedures that manipulate the avail list are getNode and

    retNode, which requests for a node and returns a node respectively.

    Node getNode(){Node a;

    if ( head == null) {return null; /* avail list is empty */

    } else {a = head.link; /* assign node to return to a */head = head.link.link;return a;

    }

    }

    void retNode(Node n){n.link = head.link; /* adds the new node at the

    start of the avail list */head.link = n;

    }

  • 8/3/2019 MELJUN_CORTES_JEDI Slides-Data Structures-Chapter01-Basic Concepts and Notations

    14/23

    Data Structures Basic Concepts and Notations 14

    Mathematical Functions

    Floor of x ( x ) - greatest integer less than or equal to x, xis any real number

    Ceiling of x ( x ) - smallest integer greater than or equal tox, where x is any real number

    Modulo - given any two real numbers x and y,

    x mod y = x if y = 0= x - y *x / y if y 0

  • 8/3/2019 MELJUN_CORTES_JEDI Slides-Data Structures-Chapter01-Basic Concepts and Notations

    15/23

    Data Structures Basic Concepts and Notations 15

    Mathematical Functions

    Identities

    x = x if and only if x is an integer

    x = x if and only if x is not an integer

    - x = x x + y

  • 8/3/2019 MELJUN_CORTES_JEDI Slides-Data Structures-Chapter01-Basic Concepts and Notations

    16/23

    Data Structures Basic Concepts and Notations 16

    Complexity of Algorithms

    Algorithm Efficiency

    Space utilization - amount of memory required to store the data

    Time efficiency - amount of time required to process the data

    Execution time - amount of time spent in executing instructions of agiven algorithm. Notation: T(n). Several factors that affect the executiontime include:

    input size

    Instruction type

    machine speed

    quality of source code of the algorithm implementation quality of the machine code generated from the source code by the

    compiler

  • 8/3/2019 MELJUN_CORTES_JEDI Slides-Data Structures-Chapter01-Basic Concepts and Notations

    17/23

    Data Structures Basic Concepts and Notations 17

    Complexity of Algorithms

    The Big-Oh Notation (or simply O-Notation)

    T(n) grows at a rate proportional to n and thus T(n) is said to haveorder of magnitude n denoted by the O-notation: T(n) = O(n)

    Formal definition:g(n) = O(f(n)) if there exists two constants c and n

    0such that

    | g(n) | = n0.

    Operations on the O-Notation:

    Rule for Sums

    Suppose that T1

    (n) = O( f(n) ) and T2

    (n) = O( g(n) ).

    Then, t(n) = T1(n) + T

    2(n) = O( max( f(n), g(n) ) ).

    Rule for Products

    Suppose that T1(n) = O( f(n) ) and T2(n) = O( g(n) ).Then, T(n) = T1(n) * T2(n) = O( f(n) * g(n) ).

  • 8/3/2019 MELJUN_CORTES_JEDI Slides-Data Structures-Chapter01-Basic Concepts and Notations

    18/23

    Data Structures Basic Concepts and Notations 18

    Complexity of AlgorithmsRule for Sums

    Suppose that T1(n) = O( f(n) ) and T

    2(n) = O( g(n) ).

    Then, t(n) = T1(n) + T

    2(n) = O( max( f(n), g(n) ) ).

    Proof: By definition of the O-notation,

    T1(n) = n1 andT2(n) = n2.

    Let n0 = max(n1, n2). ThenT1(n) + T2(n) = n0.

    = n0.= n0.

    Thus,

    T(n) = T1(n) + T2(n) = O( max( f(n), g(n) ) ).

    e.g. a. T(n) = 3n3 + 5n2 = O( n3 )

    b. T(n) = 2n + n4 + n log n = O( 2n )

  • 8/3/2019 MELJUN_CORTES_JEDI Slides-Data Structures-Chapter01-Basic Concepts and Notations

    19/23

    Data Structures Basic Concepts and Notations 19

    Complexity of AlgorithmsExamples

    Consider the algorithm below :

    for (i=1; i

  • 8/3/2019 MELJUN_CORTES_JEDI Slides-Data Structures-Chapter01-Basic Concepts and Notations

    20/23

    Data Structures Basic Concepts and Notations 20

    Complexity of AlgorithmsExamples

    LINEAR SEARCH ALGORITHM

    1 found = false;

    2 loc = 1;

    3 while ((loc

  • 8/3/2019 MELJUN_CORTES_JEDI Slides-Data Structures-Chapter01-Basic Concepts and Notations

    21/23

    Data Structures Basic Concepts and Notations 21

    Complexity of Algorithms

    The Big-Oh Notation

    Big-Oh Description Algorithm

    O(1) Constant

    Logarithmic Binary Search

    O(n) Linear Sequential Search

    Heapsort

    Quadratic Insertion Sort

    Cubic Floyds Algorithm

    Exponential

    O(log2n)

    O(n log2n)

    O(n2)

    O(n3)

    O( 2n )

    F(n) Running Time

    19.93 microseconds

    n 1.00 seconds

    19.93 seconds

    11.57 days

    317.10 centuries

    Eternity

    log2

    n

    n log2

    n

    n2

    n3

    2n

  • 8/3/2019 MELJUN_CORTES_JEDI Slides-Data Structures-Chapter01-Basic Concepts and Notations

    22/23

    Data Structures Basic Concepts and Notations 22

    Complexity of AlgorithmsGeneral rules on determining the running time of an algorithm

    FOR loops

    At most the running time of the statement inside the for loop timesthe number of iterations.

    NESTED FOR loops

    Analysis is done from the inner loop going outward. The totalrunning time of a statement inside a group of for loops is the runningtime of the statement multiplied by the product of the sizes of all thefor loops.

    CONSECUTIVE STATEMENTS

    The statement with the maximum running time.

    IF/ELSE

    Never more than the running time of the test plus the larger of therunning times of the conditional block of statements.

  • 8/3/2019 MELJUN_CORTES_JEDI Slides-Data Structures-Chapter01-Basic Concepts and Notations

    23/23

    Data Structures Basic Concepts and Notations 23

    Summary Programming as a problem solving process could be viewed in

    terms of 3 domains problem, machine and solution.

    Data structures provide a way for data representation. It is animplementation of ADT.

    An algorithm is a finite set of instructions which, if followed, willaccomplish a task. It has five important properties: finiteness,definiteness, input, output and effectiveness.

    Addressing methods define how the data items are accessed.Two general types are computed and link addressing.

    Algorithm efficiencyis measured in two criteria: space utilizationand time efficiency. The O-notation gives an approximatemeasure of the computing time of an algorithm for large numberof input