ds and algorithms oct 25th and 26th classes

Upload: ayaan-sharma

Post on 14-Apr-2018

223 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 DS and Algorithms Oct 25th and 26th Classes

    1/58

    Data Structures

    &

    Algorithms

    Prof. Ravi Prakash Gorthi

    Aug-Dec, 2010

  • 7/30/2019 DS and Algorithms Oct 25th and 26th Classes

    2/58

    Data Structures & Algorithms

    Topics

    Why Study DS & Algorithms?

    Abstract Data Types

    Arrays and Linked Lists

    Stacks & Queues

    Trees & Graphs

    Sorting and Searching

    Complexity of Algorithms

  • 7/30/2019 DS and Algorithms Oct 25th and 26th Classes

    3/58

    Heapsort

    Reference:

    www.cis.upenn.edu/~matuszek/cit594-2002/Slides/heapsort.ppt

  • 7/30/2019 DS and Algorithms Oct 25th and 26th Classes

    4/58

    A binary tree, which is, Balanced,

    Left-justified and

    Heapified

    is useful to achieve,

    Sorting and

    Priority Queues

  • 7/30/2019 DS and Algorithms Oct 25th and 26th Classes

    5/58

    Binary tree

    Every node has at most two sons!

    Binary

    tree

    Binary

    tree

    Not a binary

    tree

  • 7/30/2019 DS and Algorithms Oct 25th and 26th Classes

    6/58

    Balanced binary trees Depth of a Tree

    The depth of a node is its distance from the root

    The depth of a tree, n, is the depth of the deepest node

    A binary tree of depth n (n > = 2) is balanced iff allthe nodes at depths 0, 1, 2 through n-2 have twochildren

    Balanced Balanced Not balanced

    n-2n-1n

  • 7/30/2019 DS and Algorithms Oct 25th and 26th Classes

    7/58

    Balanced binary trees

    In other words, you can add a son-node to a

    node at level (n-1), iff, all nodes at the

    previous level, (n2), have two sons

    Balanced Balanced Not balanced

    n-2n-1n

  • 7/30/2019 DS and Algorithms Oct 25th and 26th Classes

    8/58

    A Complete Binary trees

    A balanced binary tree of depth n iscomplete, iff, all nodes at level (n-1) have two

    sons

    A Complete

    Binary Tree

    Not a Complete

    Binary Tree

    n-2n-1n

  • 7/30/2019 DS and Algorithms Oct 25th and 26th Classes

    9/58

    Left-justified balanced binary trees

    A balanced binary tree of depth, n, is left-justified if: It is a Complete Binary Tree, OR

    a node at level (n-1),

    Has a left-son iff all nodes to its left at level (n-1) havetwo sons

    Has a right-son iff it already has a left-son

    Left-justified Not left-justified

  • 7/30/2019 DS and Algorithms Oct 25th and 26th Classes

    10/58

  • 7/30/2019 DS and Algorithms Oct 25th and 26th Classes

    11/58

    Left-justified balanced binary trees

    Let us construct a few Left-justified, balancedbinary trees:

    0 0

    1

    0

    1

  • 7/30/2019 DS and Algorithms Oct 25th and 26th Classes

    12/58

    Left-justified balanced binary trees

    Let us construct a few Left-justified, balancedbinary trees:

    0 0

    1

    2

    0

    1 2

    0

    1

  • 7/30/2019 DS and Algorithms Oct 25th and 26th Classes

    13/58

    Left-justified balanced binary trees

    Let us construct a few Left-justified, balancedbinary trees:

    0 0

    1 2

    0

    1 2

    0

    1

    3

    2

    0

    1

    3

    2

    0

    1

    33

  • 7/30/2019 DS and Algorithms Oct 25th and 26th Classes

    14/58

    Left-justified balanced binary trees

    Let us construct a few Left-justified, balancedbinary trees:

    0 0

    1 2

    0

    1 2

    0

    1

    3

    2

    0

    1

    3 4

    2

    0

    1

    3 4

  • 7/30/2019 DS and Algorithms Oct 25th and 26th Classes

    15/58

    Left-justified balanced binary trees

    Let us construct a few Left-justified, balancedbinary trees:

    0 0

    1 2

    0

    1 2

    0

    1

    3

    2

    0

    1

    3 4

    2

    0

    1

    3 4 5

  • 7/30/2019 DS and Algorithms Oct 25th and 26th Classes

    16/58

    Left-justified balanced binary trees

    Let us construct a few Left-justified, balancedbinary trees:

    0 0

    1 2

    0

    1 2

    0

    1

    3

    2

    0

    1

    3 4

    2

    0

    1

    3 4 5

    2

    0

    1

    3 4 5 6

  • 7/30/2019 DS and Algorithms Oct 25th and 26th Classes

    17/58

    Left-justified balanced binary trees

    Incrementally construct a left-justifiedbalanced binary tree using the following

    sequence of integers:

    31, 76, 29, 64, 52, 48, 13, 85, 90

  • 7/30/2019 DS and Algorithms Oct 25th and 26th Classes

    18/58

    Left-justified balanced binary trees

    Incrementally construct a left-justifiedbalanced binary tree using the following

    sequence of integers:

    31, 76, 29, 64, 52, 48, 13, 85, 9031

  • 7/30/2019 DS and Algorithms Oct 25th and 26th Classes

    19/58

    Left-justified balanced binary trees

    Incrementally construct a left-justifiedbalanced binary tree using the following

    sequence of integers:

    31, 76, 29, 64, 52, 48, 13, 85, 9031

    76

  • 7/30/2019 DS and Algorithms Oct 25th and 26th Classes

    20/58

    Left-justified balanced binary trees

    Incrementally construct a left-justifiedbalanced binary tree using the following

    sequence of integers:

    31, 76, 29, 64, 52, 48, 13, 85, 90

    29

    31

    76

  • 7/30/2019 DS and Algorithms Oct 25th and 26th Classes

    21/58

    Left-justified balanced binary trees

    Incrementally construct a left-justifiedbalanced binary tree using the following

    sequence of integers:

    31, 76, 29, 64, 52, 48, 13, 85, 90

    29

    31

    76

    64

  • 7/30/2019 DS and Algorithms Oct 25th and 26th Classes

    22/58

    Left-justified balanced binary trees

    Incrementally construct a left-justifiedbalanced binary tree using the following

    sequence of integers:

    31, 76, 29, 64, 52, 48, 13, 85, 90

    29

    31

    76

    64 52

  • 7/30/2019 DS and Algorithms Oct 25th and 26th Classes

    23/58

    Left-justified balanced binary trees

    Incrementally construct a left-justifiedbalanced binary tree using the following

    sequence of integers:

    31, 76, 29, 64, 52, 48, 13, 85, 90

    29

    31

    76

    64 52 48

  • 7/30/2019 DS and Algorithms Oct 25th and 26th Classes

    24/58

    Left-justified balanced binary trees

    Incrementally construct a left-justifiedbalanced binary tree using the following

    sequence of integers:

    31, 76, 29, 64, 52, 48, 13, 85, 90

    29

    31

    76

    64 52 48 13

  • 7/30/2019 DS and Algorithms Oct 25th and 26th Classes

    25/58

    Left-justified balanced binary trees

    Incrementally construct a left-justifiedbalanced binary tree using the following

    sequence of integers:

    31, 76, 29, 64, 52, 48, 13, 85, 90

    29

    31

    76

    64 52 48 13

    85

  • 7/30/2019 DS and Algorithms Oct 25th and 26th Classes

    26/58

    Left-justified balanced binary trees

    Incrementally construct a left-justifiedbalanced binary tree using the following

    sequence of integers:

    31, 76, 29, 64, 52, 48, 13, 85, 90

    29

    31

    76

    64 52 48 13

    85 90

  • 7/30/2019 DS and Algorithms Oct 25th and 26th Classes

    27/58

    Left-justified balanced binary trees Here is a very interesting coincidence:

    29

    31

    76

    64 52 48 13

    85 90

    2

    0

    1

    3 4 5 6

    7 8

    0 1 2 3 4 5 6 7 8

    31 76 29 64 52 48 13 85 90

    Array Index

    Unsorted List

    Node Values

  • 7/30/2019 DS and Algorithms Oct 25th and 26th Classes

    28/58

    The heap property

    A node has the max heap property if the value inthe node is as large as or larger than the values in

    its children

    All leaf nodes automatically have the heap property

    A binary tree is a heap ifallnodes in it have the

    heap property

    12

    8 3

    Blue node has

    heap property

    12

    8 12

    Blue node has

    heap property

    12

    8 14

    Blue node does not

    have heap property

  • 7/30/2019 DS and Algorithms Oct 25th and 26th Classes

    29/58

    siftUp

    Given a node that does not have the heapproperty, you can give it the heap property by

    exchanging its value with the value of the largest

    child

    This is sometimes called sifting up

    14

    8 12

    Blue node hasheap property

    12

    8 14

    Blue node does nothave heap property

  • 7/30/2019 DS and Algorithms Oct 25th and 26th Classes

    30/58

    Constructing a heap I

    A tree consisting of a single node is automatically

    a heap

    We construct a heap by adding nodes one at a

    time:

    Add nodes such that the binary tree remainsbalanced and left-justified

    Examples:

    Add a new

    node here

    Add a new

    node here

  • 7/30/2019 DS and Algorithms Oct 25th and 26th Classes

    31/58

    Constructing a heap II

    Each time we add a node, we may destroy theheap property of its parent node;

    To fix this, we sift up;

    We repeat the sifting up process, moving up inthe tree, until either:

    We reach nodes whose values dont need to

    be swapped (because the parent is stilllarger than both children), or

    We reach the root

  • 7/30/2019 DS and Algorithms Oct 25th and 26th Classes

    32/58

    Constructing a heap III

    8 8

    10

    10

    8

    10

    8 5

    10

    8 5

    12

    10

    12 5

    8

    12

    10 5

    8

    1 2 3

    4

  • 7/30/2019 DS and Algorithms Oct 25th and 26th Classes

    33/58

    Other children are not affected

    The node containing 8 is not affected because its parent gets

    larger, not smaller

    The node containing 5 is not affected because its parent getslarger, not smaller

    12

    10 5

    8 14

    12

    14 5

    8 10

    14

    12 5

    8 10

  • 7/30/2019 DS and Algorithms Oct 25th and 26th Classes

    34/58

    A sample heap

    Heres a sample binary tree after it has beenheapified

    Notice that heapified does notmean sorted

    Heapifying does notchange the shape of the

    binary tree; this binary tree is balanced and left-

    justified because it started out that way

    19

    1418

    22

    321

    14

    119

    15

    25

    1722

  • 7/30/2019 DS and Algorithms Oct 25th and 26th Classes

    35/58

    Mapping into an array

    Notice:

    The left child of index i is at index 2*i+1

    The right child of index i is at index 2*i+2

    Example: the children of node 3 (19) are 7 (18) and 8 (14)

    19

    1418

    22

    321

    14

    119

    15

    25

    1722

    25 22 17 19 22 14 15 18 14 21 3 9 11

    0 1 2 3 4 5 6 7 8 9 10 11 12

  • 7/30/2019 DS and Algorithms Oct 25th and 26th Classes

    36/58

    Mapping into an array

    Notice:

    The left child of index i is at index 2*i+1

    The right child of index i is at index 2*i+2

    Example: the children of node 3 (19) are 7 (18) and 8 (14)

    19

    1418

    22

    321

    14

    119

    15

    25

    1722

    25 22 17 19 22 14 15 18 14 21 3 9 11

    0 1 2 3 4 5 6 7 8 9 10 11 12

  • 7/30/2019 DS and Algorithms Oct 25th and 26th Classes

    37/58

    Mapping into an array

    Notice:

    The left child of index i is at index 2*i+1

    The right child of index i is at index 2*i+2

    Example: the children of node 3 (19) are 7 (18) and 8 (14)

    19

    1418

    22

    321

    14

    119

    15

    25

    1722

    25 22 17 19 22 14 15 18 14 21 3 9 11

    0 1 2 3 4 5 6 7 8 9 10 11 12

  • 7/30/2019 DS and Algorithms Oct 25th and 26th Classes

    38/58

    Mapping into an array

    Notice:

    The left child of index i is at index 2*i+1

    The right child of index i is at index 2*i+2

    Example: the children of node 3 (19) are 7 (18) and 8 (14)

    19

    1418

    22

    321

    14

    119

    15

    25

    1722

    25 22 17 19 22 14 15 18 14 21 3 9 11

    0 1 2 3 4 5 6 7 8 9 10 11 12

  • 7/30/2019 DS and Algorithms Oct 25th and 26th Classes

    39/58

    Removing the root

    Notice that the largest number is now in the root

    Suppose we discardthe root:

    How can we fix the binary tree so it is once again

    balanced and left-justified?

    Solution: remove the rightmost leaf at the

    deepest level and use it for the new root

    19

    1418

    22

    321

    14

    119

    15

    1722

    11

    Th eHeap th d I

  • 7/30/2019 DS and Algorithms Oct 25th and 26th Classes

    40/58

    The reHeap method I

    Our tree is balanced and left-justified, but no longer a

    heap However, only the rootlacks the heap property

    We can siftUp() the root

    After doing this, one and only one of its children may have

    lost the heap property

    19

    1418

    22

    321

    14

    9

    15

    1722

    11

  • 7/30/2019 DS and Algorithms Oct 25th and 26th Classes

    41/58

    The reHeap method II

    Now the left child of the root (still the number 11) lacksthe heap property

    We can siftUp() this node

    After doing this, one and only one of its children may have

    lost the heap property

    19

    1418

    22

    321

    14

    9

    15

    1711

    22

  • 7/30/2019 DS and Algorithms Oct 25th and 26th Classes

    42/58

    The reHeap method III Now the right child of the left child of the root (still the

    number 11) lacks the heap property:

    We can siftUp() this node

    After doing this, one and only one of its children may have

    lost the heap property but it doesnt, because its a leaf

    19

    1418

    11

    321

    14

    9

    15

    1722

    22

  • 7/30/2019 DS and Algorithms Oct 25th and 26th Classes

    43/58

    The reHeap method III Now the right child of the left child of the root (still the

    number 11) lacks the heap property:

    We can siftUp() this node

    After doing this, one and only one of its children may have

    lost the heap property but it doesnt, because its a leaf

    19

    1418

    21

    311

    14

    9

    15

    1722

    22

  • 7/30/2019 DS and Algorithms Oct 25th and 26th Classes

    44/58

    Removing and replacing the root

    The root is the first element in the array

    The rightmost node at the deepest level is the lastelement in the array

    Swap them...

    ...And pretend that the last element in the array no longer

    existsthat is, the last index is 11 (9)

    25 22 17 19 22 14 15 18 14 21 3 9 11

    0 1 2 3 4 5 6 7 8 9 10 11 12

    11 22 17 19 22 14 15 18 14 21 3 9 25

    0 1 2 3 4 5 6 7 8 9 10 11 12

  • 7/30/2019 DS and Algorithms Oct 25th and 26th Classes

    45/58

    Heapifying the Tree Again . . .

    11 22 17 19 22 14 15 18 14 21 3 9 25

    0 1 2 3 4 5 6 7 8 9 10 11 12

    22 11 17 19 22 14 15 18 14 21 3 9 25

    0 1 2 3 4 5 6 7 8 9 10 11 12

  • 7/30/2019 DS and Algorithms Oct 25th and 26th Classes

    46/58

    Heapifying the Tree Again . . .

    11 22 17 19 22 14 15 18 14 21 3 9 25

    0 1 2 3 4 5 6 7 8 9 10 11 12

    22 11 17 19 22 14 15 18 14 21 3 9 25

    0 1 2 3 4 5 6 7 8 9 10 11 12

    22 22 17 19 11 14 15 18 14 21 3 9 25

    0 1 2 3 4 5 6 7 8 9 10 11 12

    f h

  • 7/30/2019 DS and Algorithms Oct 25th and 26th Classes

    47/58

    Heapifying the Tree Again . . .

    11 22 17 19 22 14 15 18 14 21 3 9 25

    0 1 2 3 4 5 6 7 8 9 10 11 12

    22 11 17 19 22 14 15 18 14 21 3 9 25

    0 1 2 3 4 5 6 7 8 9 10 11 12

    22 22 17 19 11 14 15 18 14 21 3 9 25

    0 1 2 3 4 5 6 7 8 9 10 11 12

    f h

  • 7/30/2019 DS and Algorithms Oct 25th and 26th Classes

    48/58

    Heapifying the Tree Again . . .

    11 22 17 19 22 14 15 18 14 21 3 9 25

    0 1 2 3 4 5 6 7 8 9 10 11 12

    22 11 17 19 22 14 15 18 14 21 3 9 25

    0 1 2 3 4 5 6 7 8 9 10 11 12

    22 22 17 19 11 14 15 18 14 21 3 9 25

    0 1 2 3 4 5 6 7 8 9 10 11 12

    22 22 17 19 21 14 15 18 14 11 3 9 25

    0 1 2 3 4 5 6 7 8 9 10 11 12

    h d

  • 7/30/2019 DS and Algorithms Oct 25th and 26th Classes

    49/58

    Reheap and repeat

    Reheap the root node (index 0, containing 11)...

    ...And again, remove and replace the root node

    Remember, though, that the last array index is changed

    Repeat until the last becomes first, and the array is sorted!

    22 22 17 19 21 14 15 18 14 11 3 9 25

    0 1 2 3 4 5 6 7 8 9 10 11 12

    11 22 17 19 22 14 15 18 14 21 3 9 25

    0 1 2 3 4 5 6 7 8 9 10 11 12

    9 22 17 19 21 14 15 18 14 11 3 22 25

    0 1 2 3 4 5 6 7 8 9 10 11 12

    R h d

  • 7/30/2019 DS and Algorithms Oct 25th and 26th Classes

    50/58

    Reheap and repeat

    Finally . . .

    3 9 11 14 14 15 17 18 19 21 22 22 25

    0 1 2 3 4 5 6 7 8 9 10 11 12

    H S Al i h

  • 7/30/2019 DS and Algorithms Oct 25th and 26th Classes

    51/58

    Heap Sort Algorithm

    Input: Unsorted array of n integers:

    Construct a balanced, left-justified, heapified binary tree from

    the given array, by taking one node-value at a time, starting

    from index 0 till the last index

    18 22 15 22 21 11 17 19 14 25 3 9 14

    0 1 2 3 4 5 6 7 8 9 10 11 12

    25 22 17 19 22 14 15 18 14 21 3 9 11

    0 1 2 3 4 5 6 7 8 9 10 11 12

    H S Al i h

  • 7/30/2019 DS and Algorithms Oct 25th and 26th Classes

    52/58

    Heap Sort Algorithm

    Interchange node values at 0 and 12

    25 22 17 19 22 14 15 18 14 21 3 9 11

    0 1 2 3 4 5 6 7 8 9 10 11 12

    11 22 17 19 22 14 15 18 14 21 3 9 25

    0 1 2 3 4 5 6 7 8 9 10 11 12

    Consider that the current tree has only (n-1) nodes and re-

    heapify it by sifting down the node value 11 to the extent

    required;

    22 22 17 19 21 14 15 18 14 11 3 9 25

    0 1 2 3 4 5 6 7 8 9 10 11 12

    H if i th T A i

  • 7/30/2019 DS and Algorithms Oct 25th and 26th Classes

    53/58

    Heapifying the Tree Again . . .

    11 22 17 19 22 14 15 18 14 21 3 9 25

    0 1 2 3 4 5 6 7 8 9 10 11 12

    22 11 17 19 22 14 15 18 14 21 3 9 25

    0 1 2 3 4 5 6 7 8 9 10 11 12

    H if i th T A i

  • 7/30/2019 DS and Algorithms Oct 25th and 26th Classes

    54/58

    Heapifying the Tree Again . . .

    11 22 17 19 22 14 15 18 14 21 3 9 25

    0 1 2 3 4 5 6 7 8 9 10 11 12

    22 11 17 19 22 14 15 18 14 21 3 9 25

    0 1 2 3 4 5 6 7 8 9 10 11 12

    22 22 17 19 11 14 15 18 14 21 3 9 25

    0 1 2 3 4 5 6 7 8 9 10 11 12

    H if i th T A i

  • 7/30/2019 DS and Algorithms Oct 25th and 26th Classes

    55/58

    Heapifying the Tree Again . . .

    11 22 17 19 22 14 15 18 14 21 3 9 25

    0 1 2 3 4 5 6 7 8 9 10 11 12

    22 11 17 19 22 14 15 18 14 21 3 9 25

    0 1 2 3 4 5 6 7 8 9 10 11 12

    22 22 17 19 11 14 15 18 14 21 3 9 25

    0 1 2 3 4 5 6 7 8 9 10 11 12

    H if i th T A i

  • 7/30/2019 DS and Algorithms Oct 25th and 26th Classes

    56/58

    Heapifying the Tree Again . . .

    11 22 17 19 22 14 15 18 14 21 3 9 25

    0 1 2 3 4 5 6 7 8 9 10 11 12

    22 11 17 19 22 14 15 18 14 21 3 9 25

    0 1 2 3 4 5 6 7 8 9 10 11 12

    22 22 17 19 11 14 15 18 14 21 3 9 25

    0 1 2 3 4 5 6 7 8 9 10 11 12

    22 22 17 19 21 14 15 18 14 11 3 9 25

    0 1 2 3 4 5 6 7 8 9 10 11 12

    H S t Al ith

  • 7/30/2019 DS and Algorithms Oct 25th and 26th Classes

    57/58

    Heap Sort Algorithm

    Interchange node values at 0 and 11

    Consider that the current tree has only (n-2) nodes and re-

    heapify it by sifting down the node value 9 to the extent

    required;

    22 21 17 19 11 14 15 18 14 9 3 22 25

    0 1 2 3 4 5 6 7 8 9 10 11 12

    22 22 17 19 21 14 15 18 14 11 3 9 25

    0 1 2 3 4 5 6 7 8 9 10 11 12

    9 22 17 19 21 14 15 18 14 11 3 22 250 1 2 3 4 5 6 7 8 9 10 11 12

    H S t Al ith

  • 7/30/2019 DS and Algorithms Oct 25th and 26th Classes

    58/58

    Heap Sort Algorithm

    Repeat the above step until only one node value isleft out . . .

    3 9 11 14 14 15 17 18 19 21 22 22 25

    0 1 2 3 4 5 6 7 8 9 10 11 12