Graph and Heap

Embed Size (px)

Citation preview

  • 8/12/2019 Graph and Heap

    1/58

    DSAGraph, Binary Search Tree

    and Heap Sort

  • 8/12/2019 Graph and Heap

    2/58

    What is a Graph?

    A graph G = (V,E) is composed of:

    V: set of vertices

    E: set ofedgesconnecting the verticesin V

    An edgee = (u,v) is a pair of vertices Example:

    a b

    c

    de

    V= {a,b,c,d,e}

    E= {(a,b),(a,c),(a,d),

    (b,e),(c,d),(c,e),

    (d,e)}

  • 8/12/2019 Graph and Heap

    3/58

    Definitions and Representation

    a) An undirected graph and (b) a directed graph.

  • 8/12/2019 Graph and Heap

    4/58

    Definitions and Representation

    An undirected graph is connectedif every pair

    of vertices is connected by a path.

    A graph that has weights associated with each

    edge is called a weighted graph.

  • 8/12/2019 Graph and Heap

    5/58

    5

    Heaps

    A heapis a completebinary treethat is either:

    empty, or

    consists of a root and two subtrees, such that both subtrees are heaps, and

    the root contains a search key that is the searchkey of each of its children.

  • 8/12/2019 Graph and Heap

    6/58

    6

    Array-Based Representation of a

    Heap

    Search

    Key

    Item

    10 Tom

    7 Pam9 Sue

    5 Mary

    6 Mike

    8 Sam

    1 Ann

    4 Joe

    2 Bob

    3 Jane

    Mary Mike Sam Ann

    Tom

    Pam Sue

    Joe Bob Jane3

    2

    15

    4

    7

    6

    9

    8

    10

  • 8/12/2019 Graph and Heap

    7/58

    7

    Array-Based Representation of a

    Heap

    Note that, for any node, the search key of its left childisnot necessarily or the search key of its right child.

    The only constraint is that anyparentnode must have asearch key that is the search key of both of itschildren.

    Note that this is sufficient to ensure that the item withthe greatestsearch key in the heap is stored at the root.

  • 8/12/2019 Graph and Heap

    8/58

    8

    The ADT Priority Queue

    A priority queue is an ADT in which items areordered by a priority value. The item with thehighest priority is always the next to be removedfrom the queue. (Highest Priority In, First Out:HPIFO)

    Supported operations include: Createan empty priority queue

    Destroya priority queue

    Determine whether a priority queue is empty Inserta new item into a priority queue

    Retrieve, and then delete from the priority queue theitem with the highest priorityvalue

  • 8/12/2019 Graph and Heap

    9/58

    9

    PriorityQ: Retrieve & Delete

    Consider the operation, Retrieve, and then delete from the priorityqueue the item with the highest priorityvalue.

    In a heap where search keys represent the priority of the items, theitem with the highest priority is stored at theroot.

    Consequently, retrieving the item with the highest priority value istrivial.

    However, if the root of a heap is deleted we will be left with two

    separate heaps.

    We need a way to transform the remaining nodes back into a singleheap.

  • 8/12/2019 Graph and Heap

    10/58

    10

    PriorityQ: Public Member Function Definition

    // retrieve, and then delete from the PriorityQ the item

    // with the highest priority valuebool PriorityQ::pqRetrieve( PQItemType &priorityItem )

    { if( pqIsEmpty( ) ) return false;

    // set priorityItem to the highest priority item in the PriorityQ,// which is stored in the root of a heap

    priorityItem = items[ 0 ];

    // move item from the last node in the heap to the root,// and delete the last node

    items[ 0 ] = items[size ];

    // transform the resulting semiheapback into a heapheapRebuild( 0 );

    return true;}

  • 8/12/2019 Graph and Heap

    11/58

    11

    Semiheap

    A semiheapis a completebinary tree in whichthe roots left and right subtrees are both heaps.

    10

    5 10 40 30

    15 50 35 20

    55 60

    45

    25

    55 45

    25

  • 8/12/2019 Graph and Heap

    12/58

    12

    Rebuilding a Heap:Basic Idea

    Problem: Transform a semiheapwith given root into a heap.

    Let key( n )represent the search key value of noden.

    1) If the rootof the semiheapis not a leaf, and

    key( root) < key( child of rootwith larger search key value )

    then swap the item in the root with the child containing the largersearch key value.

    2) If any items were swapped in step 1, then repeat step 1 with the

    subtree rooted at the node whose item was swapped with theroot. If no items were swapped, then we are done: the resultingtree is a heap.

  • 8/12/2019 Graph and Heap

    13/58

    13

    Retrieve & Delete:Example

    Retrieve the item with the

    highest priorityvalue (= 60)

    from the root. Move the item from the last

    node in the heap (= 25) to

    the root, and delete the last

    node.

    5 10 40 30

    15 50 35 20

    55 45

    60

    25

    move 25 to here

  • 8/12/2019 Graph and Heap

    14/58

    14

    Rebuilding a Heap: Example

    (Contd.)

    The resulting data structure

    is a semiheap, a complete

    binary tree in which the

    roots left and right subtrees

    are both heaps.

    To transform this semiheap

    into a heap, start by

    swapping the item in theroot with its child containing

    the larger search key value.

    15 50 35 20

    5 10 40 30

    55 45

    25

    swap 25 with theitem in this node

  • 8/12/2019 Graph and Heap

    15/58

    15

    Rebuilding a Heap: Example

    (Contd.)

    Note that the subtree

    rooted at the node

    containing 25 is a semiheap.

    As before, swap the item in

    the root of this semiheap

    with its child containing the

    larger search key value.15 50 35 20

    5 10 40 30

    25 45

    55

    swap 25 with the

    item in this node

  • 8/12/2019 Graph and Heap

    16/58

    16

    Rebuilding a Heap: Example

    (Contd.)

    Note that the subtree

    rooted at the node

    containing 25 is a semiheap.

    As before, swap the item in

    the root of this semiheap

    with its child containing the

    larger search key value.15 25 35 20

    5 10 40 30

    50 45

    55

    swap 25 with the

    item in this node

  • 8/12/2019 Graph and Heap

    17/58

    17

    Rebuilding a Heap: Example

    (Contd.)

    Note that the subtree

    rooted at the node

    containing 25 is a semiheap

    with two empty subtrees.

    Since the root of this

    semiheapis also a leaf, we

    are done.

    The resulting tree rooted atthe node containing 55 is a

    heap.

    15 40 35 20

    5 10 25 30

    50 45

    55

  • 8/12/2019 Graph and Heap

    18/58

    18

    PriorityQ: Private Member Function Definition

    void PriorityQ::heapRebuild( int root )

    {

    int child = 2 * root + 1; // set child to roots left child, if anyif( child < size ) // if roots left child exists . . .

    {

    int rightChild = child + 1;

    if( rightChild < size && getKey( items[ rightChild ] ) > getKey(items[ child ] ) )

    child = rightChild; // child has the larger search key

    if( getKey( items[ root ] ) < getKey( items[ child ] ) )

    {swap( items[ root ], items[ child ] );

    heapRebuild( child );

    }

    }

    }

  • 8/12/2019 Graph and Heap

    19/58

    19

    PriorityQ Insert: Basic IdeaProblem: Insert a new item into a priority queue, where the

    priority queue is implemented as a heap.Let key( n )represent the search key value of noden.

    1) Store the new item in a new node at the end of the heap.

    2) If the node containing the new item has a parent, and key(node containing new item ) > key( nodesparent ) then swapthe new item with the item in its parent node.

    3) If the new item was swapped with its parent in step 2, thenrepeat step 2 with the new item in the parent node. If noitems were swapped, then we are done: the resulting tree is aheap containing the new item.

  • 8/12/2019 Graph and Heap

    20/58

    20

    PriorityQ Insert: Example

    Suppose that we wish to

    insert an item with search

    key = 47.

    First, we store the new item

    in a new node at the end of

    the heap.15 40 35 20

    5 10 25 30

    50 45

    55

    47

  • 8/12/2019 Graph and Heap

    21/58

    21

    PriorityQ Insert: Example (Contd.)

    Since the search key of the

    new item (= 47) > the search

    key of its parent (= 35), swap

    the new item with its parent.

    15 40 35 20

    5 10 25 30

    50 45

    55

    47

  • 8/12/2019 Graph and Heap

    22/58

    22

    PriorityQ Insert: Example (Contd.)

    Since the search key of the

    new item (= 47) > the search

    key of its parent (= 45), swap

    the new item with its parent.

    15 40 47 20

    5 10 25 30

    50 45

    55

    35

  • 8/12/2019 Graph and Heap

    23/58

    23

    PriorityQ Insert: Example (Contd.)

    Since the search key of the

    new item (= 47) thesearch key of its parent (=

    55), we are done.

    The resulting tree is a heap

    containing the new item.15 40 45 20

    5 10 25 30

    50 47

    55

    35

  • 8/12/2019 Graph and Heap

    24/58

    24

    PriorityQ: Public Member Function Definition

    // insert newItem into a PriorityQ

    bool PriorityQ::pqInsert( const PQItemType &newItem )

    {

    if( size > MaxItems ) return false;

    items[ size ] = newItem; // store newItem at the end of a heap

    int newPos = size, parent = (newPos1) / 2;

    while( parent >= 0 &&getKey( items[ newPos ] ) > getKey( items[ parent ] ) )

    {

    swap( items[ newPos ], items[ parent ] );

    newPos = parent;

    parent = (newPos1) / 2;

    }

    size++; return true;}

  • 8/12/2019 Graph and Heap

    25/58

    25

    Heap-Based PriorityQ: Efficiency In the best case, no swaps are needed after an item is inserted at

    the end of the heap. In this case, insertionrequires constant time,which is O( 1 ).

    In the worst case, an item inserted at the end of a heap will be

    swapped until it reaches the root, requiring O( height of tree )

    swaps. Since heaps are complete binary trees, and hence, balanced,

    the height of a heap with n nodes is log2(n + 1) . Therefore, inthis case,insertionis O( log n ).

    In the average case, the inserted item will travel halfway to the

    root, which makes insertionin this case also O( log n ).

    The retrieve & delete operation spends most of its timerebuilding a heap. A similar analysis shows that this is O( log

    n ) in the best, average, and worst cases.

    The averageand worstcase performance of these operations is the

    same as for a balanced, binary tree.

  • 8/12/2019 Graph and Heap

    26/58

    26

    Heapsort: Basic Idea

    Problem: Arrange an array of items into sorted order.

    1) Transform the array of items into a heap.

    2) Invoke the retrieve & delete operation repeatedly, toextract the largest item remaining in the heap, until theheap is empty. Store each item retrieved from the heapinto the array from back to front.

    Note: We will refer to the version of heapRebuild used by

    Heapsort as rebuildHeap,to distinguish it from theversion implemented for the class PriorityQ.

  • 8/12/2019 Graph and Heap

    27/58

    27

    Transform an Array Into a Heap: Example

    The items in the array, above,

    can be considered to bestored in the complete binary

    tree shown at right.

    Note that leaves 2, 4, 9 & 10

    are heaps; nodes 5 & 7 are

    roots of semiheaps.

    rebuildHeapis invoked on the

    parentof the last node in the

    array (= 9).

    7 2 4 10

    9

    3 5

    6

    543210

    427536

    76

    910

    rebuildHeap

  • 8/12/2019 Graph and Heap

    28/58

    28

    Transform an Array Into a Heap: Example

    Note that nodes 2, 4, 7, 9 &

    10 are roots of heaps; nodes3 & 5 are roots of semiheaps.

    rebuildHeapis invoked on the

    node in the arraypreceding

    node 9.

    9 2 4 10

    7

    3 5

    6

    543210

    429536

    76

    710

    rebuildHeap

  • 8/12/2019 Graph and Heap

    29/58

    29

    Transform an Array Into a Heap: Example

    Note that nodes 2, 4, 5, 7, 9

    & 10 are roots of heaps;node 3 is the root of a

    semiheap.

    rebuildHeapis invoked on the

    node in the arraypreceding

    node 10.9 2 4 5

    7

    3 10

    6

    543210

    4291036

    76

    75

    rebuildHeap

  • 8/12/2019 Graph and Heap

    30/58

    30

    Transform an Array Into a Heap: Example

    Note that nodes 2, 4, 5, 7 &

    10 are roots of heaps; node3 is the root of a semiheap.

    rebuildHeapis invoked

    recursively on node 3 to

    complete the transformation

    of the semiheaprooted at 9into a heap.

    3 2 4 5

    7

    9 10

    6

    543210

    4231096

    76

    75

    rebuildHeap

  • 8/12/2019 Graph and Heap

    31/58

    31

    Transform an Array Into a Heap: Example

    Note that nodes 2, 3, 4, 5, 7,

    9 & 10 are roots of heaps;node 6 is the root of a

    semiheap.

    The recursive call to

    rebuildHeap returns to node

    9. rebuildHeapis invoked on the

    node in the arraypreceding

    node 9.

    7 2 4 5

    3

    9 10

    6

    543210

    4271096

    76

    35

    rebuildHeap

  • 8/12/2019 Graph and Heap

    32/58

    32

    Transform an Array Into a Heap: Example

    Note that node 10 is now the

    root of a heap.

    The transformation of the array

    into a heapis complete.

    7 2 4 5

    3

    9 6

    10

    543210

    4276910

    76

    35

  • 8/12/2019 Graph and Heap

    33/58

    33

    Transform an Array Into a Heap (Contd.)

    Transforming an array into a heap begins by invoking

    rebuildHeapon theparent of the last nodein the array.

    Recall that in an array-based representation of a complete

    binary tree, theparentof any node at array position, i, is

    (i1) / 2

    Since the last node in the array is at position n 1, it follows

    that transforming an array into a heap begins with the node at

    position

    (n2) / 2 = n / 2 1

    and continues with each preceding node in the array.

  • 8/12/2019 Graph and Heap

    34/58

    34

    Transform an Array Into a Heap: C++

    for( int root = n/21; root >= 0; root)

    {

    rebuildHeap( a, root, n );

    }

  • 8/12/2019 Graph and Heap

    35/58

    35

    Transform a Heap Into a Sorted Array:

    Basic Idea

    Problem: Transform array a[ ] from a heap of nitems into a

    sequence of nitems in sorted order.

    Let lastrepresent the position of the last node in the heap. Initially,

    the heap is in a[ 0 .. last], where last= n1.

    1) Move the largest item in the heap to the beginning of an (initially

    empty) sorted region of a[ ] by swapping a[0] with a[ last ].

    2) Decrement last. a[0] now represents the root of a semiheap in

    a[ 0 .. last], and the sorted region is in a[ last+ 1 .. n1 ].

    3) Invoke rebuildHeap on the semiheap rooted at a[0] to transformthe semiheap into a heap.

    4) Repeat steps 1 - 3 until last= -1. When done, the items in array

    a[ ] will be arranged in sorted order.

  • 8/12/2019 Graph and Heap

    36/58

    36

    Heapsort: C++

    void heapsort( ItemType a[ ], int n )

    {

    for( int root = n/21; root >= 0; root)

    rebuildHeap( a, root, n );

    for( int last = n 1; last > 0; )

    {

    swap( a[0], a[ last ] ); last;

    rebuildHeap( a, 0, last );

    }

    }

  • 8/12/2019 Graph and Heap

    37/58

    37

    Transform a Heap Into a Sorted Array: Example

    We start with the heap thatwe formed from an unsorted

    array.

    The heap is in a[0..7] and the

    sorted region is empty.

    We move the largest item in

    the heap to the beginning of

    the sorted region by

    swapping a[0] with a[7].

    7 2 4 5

    3

    9 6

    10

    4276910

    543210 76

    35a[ ]:

    Heap

  • 8/12/2019 Graph and Heap

    38/58

    38

    Transform a Heap Into a Sorted Array: Example

    a[0..6] now represents asemiheap.

    a[7] is the sorted region.

    Invoke rebuildHeapon the

    semiheap rooted at a[0].

    7 2 4 5

    9 6

    3

    427693

    543210 76

    105a[ ]:

    Semiheap Sorted

    rebuildHeap

  • 8/12/2019 Graph and Heap

    39/58

    39

    Transform a Heap Into a Sorted Array: Example

    rebuildHeapis invokedrecursively on a[1] to

    complete the transformation

    of the semiheap rooted at

    a[0] into a heap.

    7 2 4 5

    3 6

    9

    427639

    543210 76

    105a[ ]:

    Becoming a Heap Sorted

    rebuildHeap

  • 8/12/2019 Graph and Heap

    40/58

    40

    Transform a Heap Into a Sorted Array: Example

    a[0] is now the root of a heapin a[0..6].

    We move the largest item in

    the heap to the beginning of

    the sorted region by

    swapping a[0] with a[6].

    3 2 4 5

    7 6

    9

    423679

    543210 76

    105a[ ]:

    Heap Sorted

  • 8/12/2019 Graph and Heap

    41/58

    41

    Transform a Heap Into a Sorted Array: Example

    a[0..5] now represents asemiheap.

    a[6..7] is the sorted region.

    Invoke rebuildHeapon the

    semiheap rooted at a[0].

    3 2 4

    7 6

    5

    423675

    543210 76

    109a[ ]:

    Semiheap Sorted

    rebuildHeap

  • 8/12/2019 Graph and Heap

    42/58

    42

    Transform a Heap Into a Sorted Array: Example

    Since a[1] is the root of aheap, a recursive call to

    rebuildHeapdoes nothing.

    a[0] is now the root of a heap

    in a[0..5].

    We move the largest item in

    the heap to the beginning of

    the sorted region by

    swapping a[0] with a[5].

    3 2 4

    5 6

    7

    423657

    543210 76

    109a[ ]:

    Heap Sorted

    rebuildHeap

  • 8/12/2019 Graph and Heap

    43/58

    43

    Transform a Heap Into a Sorted Array: Example

    a[0..4] now represents asemiheap.

    a[5..7] is the sorted region.

    Invoke rebuildHeapon the

    semiheap rooted at a[0].

    3 2

    5 6

    4

    723654

    543210 76

    109a[ ]:

    Semiheap Sorted

    rebuildHeap

  • 8/12/2019 Graph and Heap

    44/58

    44

    Transform a Heap Into a Sorted Array: Example

    a[0] is now the root of a heapin a[0..4].

    We move the largest item in

    the heap to the beginning of

    the sorted region by

    swapping a[0] with a[4].

    3 2

    5 4

    6

    723456

    543210 76

    109a[ ]:

    Heap Sorted

  • 8/12/2019 Graph and Heap

    45/58

  • 8/12/2019 Graph and Heap

    46/58

    46

    Transform a Heap Into a Sorted Array: Example

    rebuildHeapis invokedrecursively on a[1] to

    complete the transformation

    of the semiheap rooted at

    a[0] into a heap.

    3

    2 4

    5

    Becoming a Heap

    763425

    543210 76

    109a[ ]:

    Sorted

    rebuildHeap

  • 8/12/2019 Graph and Heap

    47/58

    47

    Transform a Heap Into a Sorted Array: Example

    a[0] is now the root of a heapin a[0..3].

    We move the largest item in

    the heap to the beginning of

    the sorted region by

    swapping a[0] with a[3].

    2

    3 4

    5

    762435

    543210 76

    109a[ ]:

    Heap Sorted

  • 8/12/2019 Graph and Heap

    48/58

    48

    Transform a Heap Into a Sorted Array: Example

    a[0..2] now represents asemiheap.

    a[3..7] is the sorted region.

    Invoke rebuildHeapon the

    semiheap rooted at a[0].3 4

    2

    765432

    543210 76

    109a[ ]:

    Semiheap Sorted

    rebuildHeap

  • 8/12/2019 Graph and Heap

    49/58

    49

    Transform a Heap Into a Sorted Array: Example

    a[0] is now the root of a heapin a[0..2].

    We move the largest item in

    the heap to the beginning of

    the sorted region by

    swapping a[0] with a[2].

    3 2

    4

    765234

    543210 76

    109a[ ]:

    Heap Sorted

  • 8/12/2019 Graph and Heap

    50/58

    50

    Transform a Heap Into a Sorted Array: Example

    a[0..1] now represents asemiheap.

    a[2..7] is the sorted region.

    Invoke rebuildHeapon the

    semiheap rooted at a[0].3

    2

    765432

    543210 76

    109a[ ]:

    Semiheap Sorted

    rebuildHeap

  • 8/12/2019 Graph and Heap

    51/58

    51

    Transform a Heap Into a Sorted Array: Example

    a[0] is now the root of a heapin a[0..1].

    We move the largest item in

    the heap to the beginning of

    the sorted region by

    swapping a[0] with a[1].

    2

    3

    765423

    543210 76

    109a[ ]:

    Heap Sorted

  • 8/12/2019 Graph and Heap

    52/58

    52

    Transform a Heap Into a Sorted Array: Example

    a[1..7] is the sorted region. Since a[0] is a heap, a

    recursive call to rebuildHeap

    does nothing.

    We move the only item in

    the heap to the beginning ofthe sorted region.

    2

    765432

    543210 76

    109a[ ]:

    Heap Sorted

  • 8/12/2019 Graph and Heap

    53/58

    53

    Transform a Heap Into a Sorted Array: Example

    Since the sorted regioncontains all the items in the

    array, we are done.

    765432

    543210 76

    109a[ ]:

    Sorted

  • 8/12/2019 Graph and Heap

    54/58

    54

    Growth Rates for Selected Sorting Algorithms

    Bestcase Averagecase (

    ) WorstcaseSelection sort n2

    n2

    n2

    Bubble sort n n2 n2

    Insertion sort n n2

    n2

    Mergesort n * log2n n * log2n n * log2n

    Heapsort n * log2n n * log2n n * log2n

    Treesort n * log2n n * log2n n2

    Quicksort n * log2n n * log2n n2

    Radix sort n n n

  • 8/12/2019 Graph and Heap

    55/58

    Tree Vs Graph

  • 8/12/2019 Graph and Heap

    56/58

    Binary Tree Search Algorithm

    S h i BST C d

  • 8/12/2019 Graph and Heap

    57/58

    Search in a BST: C code

    Ptnode search(ptnode root,int key)

    {

    /* return a pointer to the node that

    contains key. If there is no suchnode, return NULL */

    if (!root) return NULL;

    if (key == root->key) return root;

    if (key < root->key)

    return search(root->left,key);

    return search(root->right,key);

    }

  • 8/12/2019 Graph and Heap

    58/58

    Thank You