w 5 Presentation

Embed Size (px)

Citation preview

  • 7/24/2019 w 5 Presentation

    1/32

    Computer Algorithms

    Submitted by:

    Rishi Jethwa

    Suvarna Angal

  • 7/24/2019 w 5 Presentation

    2/32

    Contents Red-Black Trees

    Basics

    roperties Rotations !nsertions

    "nion #ind Algorithms $inked $ist Representation "nion By Rank ath compression

  • 7/24/2019 w 5 Presentation

    3/32

    Red-Black Trees RB tress is a binary tree with one

    e%tra bit o& storage per node' its

    color( which can be either R)* orB$AC+,

    !ts data structure &or binary search

    tree with only dierence that thetrees are appro%imately balanced,

  • 7/24/2019 w 5 Presentation

    4/32

    Red-Black Trees A binary tree is a red-black tree i& it

    satis.es &ollowing rules &or red-black tree,

    )very node is either red or black, The root is always black, $ea& nodes are black, !& a node is red( then both its children are

    black, The number o& black nodes on every path are

    same,

  • 7/24/2019 w 5 Presentation

    5/32

    Red-Black Trees roperties o& red-black trees

    Suppose number o& black nodes are /0(

    then the minimum height can be /0 andma%imum height o& the tree can be at most/1, 2ence the ma%imum can be at most /less than twice o& its minimum height,

    3a%imum path length is 45log n6, $ookup &or searches are good( 45log n6, !nsertion and deletion are not an overhead

    e%actly( comple%ity is 45log n6,

  • 7/24/2019 w 5 Presentation

    6/32

    Red-Black Trees Rotation o& red-black trees,

    A structural change to the red-black trees, !nsertion and deletion modi&y the tree( the

    result may violate the properties o& red-black trees, To restore this propertiesrotations are done,

    7e can have either o& le&t rotation or rightrotation,

  • 7/24/2019 w 5 Presentation

    7/32

    Red-Black Trees

    b

    c

    d e

    a

    The above diagram depicts left and right rotations

    c

    eb

    a d

    Left rotation

    Right rotation

    Here in right diagram a < b < d < c < e

  • 7/24/2019 w 5 Presentation

    8/32

    Red-Black TreesInsertion in red black trees.

    11

    2 14

    151 7

    5

    4

    8

    riginal tree

    !"mber 4 added

  • 7/24/2019 w 5 Presentation

    9/32

    Red-Black Trees The idea to insertion is that we traverse

    the tree to see where it .ts( assume it

    .ts at end ( so the idea is to traverse upagain,

    Coloring rule while insertion, $ook at the &ather node( i& it is red and the

    uncle node is red too and i& the grand&athernode is black ( then make &ather and uncleas black and grand&ather as red,

  • 7/24/2019 w 5 Presentation

    10/32

    Red-Black TreesDiagram depicting rule for

    insertion mentioned in the

    previous slide.

  • 7/24/2019 w 5 Presentation

    11/32

    Red-Black TreesInsertion example

    11

    2 14

    151 7

    5 8

    4 #iolation of r"le$ after 4 added to the tree%

  • 7/24/2019 w 5 Presentation

    12/32

    Red-Black TreesInsertion example

    11

    2 14

    151 7

    5 8

    4 &ase 1

  • 7/24/2019 w 5 Presentation

    13/32

    Red-Black TreesInsertion example

    11

    7 14

    152 8

    4

    1 5

    &ase 2

  • 7/24/2019 w 5 Presentation

    14/32

    Red-Black TreesInsertion example

    2

    4

    1 5

    77

    11

    14

    15

    8

    &ase '

  • 7/24/2019 w 5 Presentation

    15/32

    "nion 4peration

    1 2

    43

    Union: Merge 2 sets

    and create a new set

    Initially each number is a set by itself.

    From n singleton sets gradually merge to form a set.

    After n1 union o!erations we get a single set of n

    numbers.

    Union o!eration is used for merging sets in "rus#al$s

    algorithm

  • 7/24/2019 w 5 Presentation

    16/32

    #ind operation )very set has a name

    Thus #ind5number6 returns name o&the set,

    er&ect application in +ruskal8salgorithm when there is a new

    edge added, *iscard the alreadyaccounted &or edge,

  • 7/24/2019 w 5 Presentation

    17/32

    $inked $ist Representation

    Represent each set using a linkedlist

    #irst ob9ect in each linked listserves as the set8s name

    )ach list maintains pointers head(

    to the representative( and tail( tothe last ob9ect in the list,

  • 7/24/2019 w 5 Presentation

    18/32

    $inked $ist Representation

    2 ' (

    5 7

    )%trapointers

    pointing tothe head

    7hen unitingthese sets(

    pointers &or nodes; and < will have

    to be madepointing to ,

  • 7/24/2019 w 5 Presentation

    19/32

    *rawbacks :

    By using this representation( #indwill take constant time 45/6,

    But "nion takes linear time as a&terunion all the pointers have to beredirected to the head,

  • 7/24/2019 w 5 Presentation

    20/32

    *evelop new datastructure

    2

    %

    3

    &

    ' Add the !ointer

    !ointing from

    new set$s head

    to the old old

    one.

    1

    4Union of 1 and 4

  • 7/24/2019 w 5 Presentation

    21/32

    Combination o& the sets

    2

    %

    3

    &

    '

    1

    4

    (hile combining) we can ha*e

    a !ointer from 1 to 2 or from 2to1.

    +ut we choose the one from 1

    to 2.

    ,his gi*es us a balanced

    structure. ,he highest ho!

    remains 2.

  • 7/24/2019 w 5 Presentation

    22/32

    "nion by Rank Algorithm

    The root o& the tree with &ewer nodes ismade to point to the root o& the tree

    with more nodes, #or each node( a rank is maintained that

    is an upper bound on the height o& thenode,

    !n union by rank( the root with smallerrank is made to point to the root withlarger rank during a "=!4= operation,

  • 7/24/2019 w 5 Presentation

    23/32

    $ongest path lengthunions:

    7hen we always take singleton

    sets and keep merging the sets weget a star structure,

    Time taken: n .nds and n-/ unions,

    This is best case,

  • 7/24/2019 w 5 Presentation

    24/32

    7orst case

    3erge sets o& e>ual path lengths,

    32

    1

    4

    &'

    %

    -

    ere) the !ath length becomes 3.

    For n1 unions and n finds:

    Union: /0n

    Finds: !ath lengths can get biggerso) /0n log n.

    ,otal sum: /0n log n.

  • 7/24/2019 w 5 Presentation

    25/32

    ath Compression

    #or union by rank(

    1

    Log n

    )est &ase

    *orst &ase

  • 7/24/2019 w 5 Presentation

    26/32

    Algorithm &or athCompression

    /stwalk: #ind the name o& the set ,Take a walk until we reach the root,

    ndwalk: Retrace the path and 9oinall the elements along the path tothe root using another pointer,

    This enables &uture .nds to takeshorter paths,

  • 7/24/2019 w 5 Presentation

    27/32

    ath compression

    /

    ? root / ?

    root

    Be&ore #ind: )achnode has pointer to

    its parent

    A&ter #ind: )ach

    node points directlyto the root

  • 7/24/2019 w 5 Presentation

    28/32

  • 7/24/2019 w 5 Presentation

    29/32

    Comparisons o& &unctions

    #5n6Dn

    $og n

    $ogn

    5n6

  • 7/24/2019 w 5 Presentation

    30/32

  • 7/24/2019 w 5 Presentation

    31/32

    Calculations

    A?56 D A5A5A5666

    7hen kD/( A/596 D 9H/

    #or k D( A596 D A/5A/5I, A/59666

    A596 D 9H/59H/6 -/

    "sing the above analysis: A?5/6 D 0