1 Totalorderingexamples But arenottotalorderingssince - - PDF document

1
SMART_READER_LITE
LIVE PREVIEW

1 Totalorderingexamples But arenottotalorderingssince - - PDF document

PriorityQueue


slide-1
SLIDE 1

1

1

  • 2
  • !
  • "#

$ % &%

PriorityQueue

3

'( )'(

4

  • Hidden

Implementation

SortedSequence UnsortedSequence

5

  • '%!(

'%!(

  • *$$$+
  • %

6

  • %$

*# % * ,-!$≤ ≤ ≤ ≤

:k≤ ≤ ≤ ≤ k :ifk1≤ ≤ ≤ ≤ k2andk2≤ ≤ ≤ ≤ k1,thenk1 k2 :ifk1≤ ≤ ≤ ≤ k2andk2≤ ≤ ≤ ≤ k3,thenk1≤ ≤ ≤ ≤ k3

slide-2
SLIDE 2

2

7

Totalorderingexamples

  • ≤ isatotalordering
  • ≥ isalsoatotalordering
  • Alphabeticalorder:wedefinea≤ bif‘a’ is

before‘b’ inalphabeticalorder

  • Reversealphabeticalorder

8

But…

  • arenottotalorderingssince

theyarenotreflexive

  • isnotatotalorderingsincewecan’t

compareany2elementswith=. Givena,b,wedonotalwayshave !!

9

Moreexamplesofordering

Wecanordertheco/ordinatepairs p=(x1,y1)andq=(x2,y2)by 1.p≤ q ifx1≤ x2 2.p≤ qif y1 ≤ y2 3.p≤ qifx1≤ x2and y1 ≤ y2 Thelastoneisonlyapartial ordering!

10

  • #

./'(#

  • $

.'(#

  • .'%!(#%%

.'(#

  • '$&(

%0

  • .*'(#
  • %0

.)'(#- %0

11

EntryADT

  • An inapriorityqueueissimplyakey/

valuepair

  • Priorityqueuesstoreentriestoallowfor

efficientinsertionandremovalbasedon keys

  • Methods:

1 %'(# % 1 '(#

  • 12

"#

$ % # $+ 2$+"%$ $+ $ $+ # .3'!$( .3,'!$( .'!$( .4'!$( .4,'!$( .2$'(

slide-3
SLIDE 3

3

13

$#%& '(

5 !%! %!! '( 63'( )*+,

14

$#%&'(),+

  • 7'(!*'(!)'(!

% 5

  • )+,

15

$#%''(

855!$% '(!*'(! )'( %)*+

  • However,toimplementinsertItem(), wemustnowscanthroughthe

entiresequenceintheworstcase.ThusinsertItem() runsin

16

Anobservation… Withanunsortedsequence… removeMin()always takesO(n) Butwithasortedsequence… insertItem()takesatmost O(n)

17

##-'.

$ 5$# 1 . 5 '!( 1 . $%5 )'(

18

AlgorithmPriorityQueueSort(S,P): Input:AsequenceSstoringnelements,onwhicha totalorderrelationisdefined,andaPriority QueuePthatcompareskeyswiththesamerelation Output:TheSequenceSsortedbythetotal

  • rderrelation

while¬ ¬ ¬ ¬ S.isEmpty() do e← S.removeFirst() P.insertItem(e,e) while¬ ¬ ¬ ¬ P.isEmpty() do e← P.removeMin() S.insertLast(e)

slide-4
SLIDE 4

4

19

''

9 5 1 :! %,':( 1 ;!'( % $

20

5

''

21

SelectionSortExample

'(S P Input: (7,4,8,2,5,3,9) () Phase1 (a) (4,8,2,5,3,9) (7) (b) (8,2,5,3,9) (7,4) .. .. .. . . . (g) () (7,4,8,2,5,3,9) Phase2 (a) (2) (7,4,8,5,3,9) (b) (2,3) (7,4,8,5,9) (c) (2,3,4) (7,8,5,9) (d) (2,3,4,5) (7,8,9) (e) (2,3,4,5,7) (8,9) (f) (2,3,4,5,7,8) (9) (g) (2,3,4,5,7,8,9) ()

  • 5

22

''),+

  • 5.#
  • %'(

) %

  • :<;<=<

5.'2(

  • 23

''$

  • Insteadofusingan

externaldatastructure, wecanimplement selection/sortand insertion/sortin/place

  • Aportionoftheinput

sequenceitselfservesas thepriorityqueue

  • Forin/placeselect/sort

– keepfirstpartofthe sequenceordered, selectmin,putitatits place ...exampleontheboard...

24

$'

5 (

  • 5
slide-5
SLIDE 5

5

25

Insertion/SortExample

'(S (P Input: (7,4,8,2,5,3,9) () Phase1 (a) (4,8,2,5,3,9) (7) (b) (8,2,5,3,9) (4,7) (c) (2,5,3,9) (4,7,8) (d) (5,3,9) (2,4,7,8) (e) (3,9) (2,4,5,7,8) (f) (9) (2,3,4,5,7,8) (g) () (2,3,4,5,7,8,9) Phase2 (a) (2) (3,4,5,7,8,9) (b) (2,3) (4,5,7,8,9) .. .. .. . . . (g) (2,3,4,5,7,8,9) ()

  • 5

26

$'),+

  • .#
  • %

:<;<=<

) %'( .'2(

  • 27
  • In/placeInsertion/sort
  • noexternaldata

structure,

  • Aportionoftheinput

sequenceitselfservesas thepriorityqueue

  • Forin/placeinsertion/

sort

– Wekeepsortedthe initialportionofthe sequence – Wecanuseswaps insteadofmodifying thesequence ...exampleontheboard...