Lecture-11-CS210-2012 (1).pptx

Embed Size (px)

Citation preview

  • 8/11/2019 Lecture-11-CS210-2012 (1).pptx

    1/22

    Data Structures and Algorithms

    (CS210/ESO207/ESO211)

    Lecture 11 Data Structure queue:

    Mathematical modeling, implementation using array

    Two interesting problems (to be solved in the next class)

    Proving correctness of 2-majority element.

    1

  • 8/11/2019 Lecture-11-CS210-2012 (1).pptx

    2/22

    Queue: a new data structure

    Data Structure Queue:

    Mathematical Modeling of Queue

    Implementation of Queue using arrays

    2

  • 8/11/2019 Lecture-11-CS210-2012 (1).pptx

    3/22

    Stack

    A special kind of list where all operations (insertion, deletion, query) take

    place at one end only, called the top.

    Behavior of Stack: Last in First out(LIFO)

    3

    1

    1

    top

  • 8/11/2019 Lecture-11-CS210-2012 (1).pptx

    4/22

    4

    Queue: a new data structure

    A special kind of list based on First in First Out (FIFO) strategy.

    21

    front rear

  • 8/11/2019 Lecture-11-CS210-2012 (1).pptx

    5/22

    Operations on a Queue

    Query Operations

    IsEmpty(Q): determine if Qis an empty queue.

    Front(Q): returns the element at the front position of the queue.

    Example:If Q is 1, 2, ,,thenFront(Q) returns ?? .

    Update Operations

    CreateEmptyQueue(Q): Create an empty queue.

    Enqueue(x,Q): insert xat the endof the queue Q.

    Example: If Q is 1, 2,, , then after Enqueue(x,Q), queue Qbecomes

    ??

    Dequeue(Q): Delete element from the frontof the queue Q.

    Example:If Q is 1, 2,, , then after Dequeue(Q), queue Qbecomes

    ??

    5

    1,2,, ,

    2,,

    1

  • 8/11/2019 Lecture-11-CS210-2012 (1).pptx

    6/22

    6

    How to access ith element from the front ?

    To access ith element, we mustperform dequeue(hence

    delete) the first i-1elements from the queue.

    11

    An Important point you must remember for

    every data structure

    You can define any newoperation only in

    terms of the primitive operations of the data

    structures defined during its modeling.

  • 8/11/2019 Lecture-11-CS210-2012 (1).pptx

    7/22

    Implementation of Queue usingarray

    Assumption:At any moment of time, the number of elements in queue is n.

    Keep an array of Qsize n, and two variables front andrear.

    front: the position of the firstelement of the queue in the array.

    rear: the position of the lastelement of the queue in the array.

    Enqueue(x,Q)

    { rearrear+1;

    Q[rear]x

    }Dequeue(Q)

    { frontfront+1}

    7

    b d x y

    front rear

    u

    rearfront

    There is a serious

    problem!

  • 8/11/2019 Lecture-11-CS210-2012 (1).pptx

    8/22

    Implementation of Queue usingarray

    8

    t

    rear

    u h k

    front

    v

    How to perform

    Enqueue(t,Q) ?How to perform

    Enqueue(x,Q) ?

    x

  • 8/11/2019 Lecture-11-CS210-2012 (1).pptx

    9/22

    Implementation of Queue usingarray

    Enqueue(x,Q)

    { rearrear+1 ;

    Q[rear]x

    }

    Dequeue(Q)

    { front front+1 }

    IsEmpty(Q)

    { ?? }

    9

    rear+1 mod n

    (front+1) mod n

    Do it as an exercise

  • 8/11/2019 Lecture-11-CS210-2012 (1).pptx

    10/22

    Two interesting problems

    Applications of queue/stack

    10

  • 8/11/2019 Lecture-11-CS210-2012 (1).pptx

    11/22

    8 queen problem

    Place 8 queens on a chess board so that no two of them attack each other.

    11

    Q

    Q

  • 8/11/2019 Lecture-11-CS210-2012 (1).pptx

    12/22

    Shortest route in a grid

    From a cell in the grid, we can move to any of its neighboring cell in one step.

    From top left corner, find shortest route to each green cell avoiding obstacles.

    12

  • 8/11/2019 Lecture-11-CS210-2012 (1).pptx

    13/22

    Proving correctness of algorithm

    for 2-majority element

    13

  • 8/11/2019 Lecture-11-CS210-2012 (1).pptx

    14/22

    Algorithm for 2-majority elementRevisiting from Lecture 4.

    Key ideaon which the algorithmis based:

    The majority element of an array remains preserved whenever we

    eliminate/cancel a pair of distinct elements from the array.

    14

    x

    y

    w

    v

    Majority

    element

    other

    elements

    >n/2< n/2

  • 8/11/2019 Lecture-11-CS210-2012 (1).pptx

    15/22

    Algorithm for 2-majority elementRevisiting from Lecture 4.

    Algo-2-majority(A)

    { count0;

    for(i=0to n-1)

    { if (count=0){ xA[i];

    count1;

    }

    else if(x A[i]) countcount- 1;

    else countcount+ 1;

    }Count the occurrences of xin A, and ifit is more than n/2, then

    print(xis 2-majority element) elseprint(there is no majority element in A)

    }

    15

  • 8/11/2019 Lecture-11-CS210-2012 (1).pptx

    16/22

    Proving the correctnessof

    Algo-2-majority(A)

    Question: What is to be proved ?

    Answer:For every possible instance of A, the output of algorithm is correct.

    Observation:If Adoes not have any majority element, the output of the

    algorithm is correct. (It follows from the last statement of the algorithm).

    Inference: To prove correctness of the algorithm, it suffices if we can prove

    the following:

    If Ahas a majority element, say , then at the end of the Forloop, x=

    16

    To establish this, we certainly need to analyze the Forloop carefully.

    So revisit the previous slide containing the algorithm for some time

  • 8/11/2019 Lecture-11-CS210-2012 (1).pptx

    17/22

    Proving the correctnessof

    Algo-2-majority(A)

    How to capture progress/status of the algorithm at the end of ith iteration ?

    At the end of ith iteration, we have processed {A[0],A[1],,A[i-1]} .

    Question:Can we state:

    At the end of ith iteration, xis a majority element of {A[0],A[1],,A[i-1]} ?

    Answer: No (convince yourself).

    How to proceed then ?

    Hint:Focus on how the algorithm is implementing the key idea (slide 14).

    What does xand countrefer to at the end of ith iteration ? Ponder over this

    question

    17

  • 8/11/2019 Lecture-11-CS210-2012 (1).pptx

    18/22

    Proving the correctnessof

    Algo-2-majority(A)

    Insight into the For loop : (along with the key idea underlying the algo)

    During ith iteration, we compare A[i-1] with x, and cancel both if they are

    different, and increment countotherwise.

    So if count=0, then all elements upto A[i-1] would have been eliminated

    through distinct-elements pair formations. If count >0, then {x ,counttimes,x} elements would have still survived at the end of the ith iteration.

    Notation:Let denote the multiset {x,count times,x, A[i],,A[n-1]}, where

    countis the value of the variable countat the end of ith iteration of FORloop.

    The insight into For Loop suggests that the following proposition has to holdat the end of ith iteration.

    18

    P(i): is 2-majority element of nonemptymultiset .

  • 8/11/2019 Lecture-11-CS210-2012 (1).pptx

    19/22

    Proving the correctnessof

    Algo-2-majority(A)

    Question: What is P(n) ?

    Answer:is 2-majority element of the nonempty multiset which is

    {x,count times,x, A[n],,A[n-1]}.

    What does it mean ?

    is 2-majority element of {x,count times,x} and count>0. Hence =x.

    But that it what we wanted to prove. Cool

    So all we need to establish is that P(i) holds true for every i.

    19

  • 8/11/2019 Lecture-11-CS210-2012 (1).pptx

    20/22

    Proving the correctnessof

    Algo-2-majority(A)

    Proof of P(i): (by mathematical induction on i)

    Base case: (i=0).

    Note the end of 0thiteration is the same as the beginning of 1st iteration.

    Since count=0at this stage, so 0= {x,0times,x, A[0],,A[n-1]} is the same

    as {A[0],,A[n-1]}, which is A. Since is majority element of A. So

    P(0)holds true.

    Induction hypothesis : P(i) holds true. That is,

    is 2-majority element of nonempty multiset .

    Induction step: We need to prove that P(i+1)holds at the end of i+1th

    iteration. (Use your skills to prove it before studying the solution on the next slide.)

    20

  • 8/11/2019 Lecture-11-CS210-2012 (1).pptx

    21/22

    Proving the correctnessof

    Algo-2-majority(A)

    In the beginning of i+1th iteration, using induction hypothesis,is 2-majority element of nonempty multiset = {x,count times,x, A[i],,A[n-1]}.

    During the execution of i+1th iteration, there are the following two cases:

    count=0or A[i]=x

    In this situation, countgets incremented during the iteration. As a result,

    observe that +1 is the same as . So using Induction hypothesis, is a

    2-majority element of +1 . So P(i+1) holds.

    A[i]x

    In this case, we are cancelling an occurrence of xwith A[i] in . This givesset +1. Using the key idea (slide 14), the majority element is preserved in

    this process. Secondly existence of 2-majority element in implies that this

    cancellation does not eliminate all elements from the multiset. So +1is

    nonempty and is its 2-majority element. Hence P(i+1) holds true in this

    case as well.21

  • 8/11/2019 Lecture-11-CS210-2012 (1).pptx

    22/22

    Homework

    1. Study and fully internalize the proof of correctness given in this lecture.

    2. Do you realize that the proof is just formalizing the intuition you have

    about the correctness of the algorithm using a better insight into the For

    loop.

    3. If you feel the proof is an overkill using unnecessary formalism, try todesign a better/simpler proof.

    4. Design the proof of correctness for the algorithm for k-majority element

    given in Assignment 2. In particular, what propositionholds at the end of

    ith iteration. Then prove this proposition using principle of mathematical

    induction.

    5. Along similar lines, design a proof of correctness for other iterative

    algorithms designed in the course. For example, local minima in array,

    local minima in a grid, binary search.

    22