Queue ADT Tiziana Ligorio 1 Todays Plan Announcements Queue ADT - - PowerPoint PPT Presentation

queue adt
SMART_READER_LITE
LIVE PREVIEW

Queue ADT Tiziana Ligorio 1 Todays Plan Announcements Queue ADT - - PowerPoint PPT Presentation

Queue ADT Tiziana Ligorio 1 Todays Plan Announcements Queue ADT Applications 2 Queue A data structure representing a waiting line Objects can be enqueued to the back of the line or dequeued from the front of the line 34


slide-1
SLIDE 1

Queue ADT

Tiziana Ligorio

1

slide-2
SLIDE 2

Today’s Plan

Announcements Queue ADT Applications

2

slide-3
SLIDE 3

Queue

A data structure representing a waiting line Objects can be enqueued to the back of the line

  • r dequeued from the front of the line

3

34

slide-4
SLIDE 4

Queue

A data structure representing a waiting line Objects can be enqueued to the back of the line

  • r dequeued from the front of the line

4

34

slide-5
SLIDE 5

Queue

A data structure representing a waiting line Objects can be enqueued to the back of the line

  • r dequeued from the front of the line

5

34 127

slide-6
SLIDE 6

Queue

A data structure representing a waiting line Objects can be enqueued to the back of the line

  • r dequeued from the front of the line

6

34 127

slide-7
SLIDE 7

Queue

A data structure representing a waiting line Objects can be enqueued to the back of the line

  • r dequeued from the front of the line

7

34 127 13

slide-8
SLIDE 8

Queue

A data structure representing a waiting line Objects can be enqueued to the back of the line

  • r dequeued from the front of the line

8

34 127 13

slide-9
SLIDE 9

Queue

A data structure representing a waiting line Objects can be enqueued to the back of the line

  • r dequeued from the front of the line

9

34 127 13

slide-10
SLIDE 10

Queue

A data structure representing a waiting line Objects can be enqueued to the back of the line

  • r dequeued from the front of the line

10

127 13

slide-11
SLIDE 11

Queue

A data structure representing a waiting line Objects can be enqueued to the back of the line

  • r dequeued from the front of the line

11

49 127 13

slide-12
SLIDE 12

Queue

A data structure representing a waiting line Objects can be enqueued to the back of the line

  • r dequeued from the front of the line

12

49 127 13

slide-13
SLIDE 13

Queue

A data structure representing a waiting line Objects can be enqueued to the back of the line

  • r dequeued from the front of the line

FIFO: First In First Out Only front of queue is accessible (front), no other

  • bjects in the queue are visible

13

slide-14
SLIDE 14

Queue Applications

Generating all substrings Recognizing Palindromes Any waiting queue


  • Print jobs

  • OS scheduling processes with equal priority

  • Messages between asynchronous processes


. . .

14

slide-15
SLIDE 15

Queue Applications

Generating all substrings Any waiting queue


  • Print jobs

  • OS scheduling processes with equal priority

  • Messages between asynchronous processes


. . .

15

slide-16
SLIDE 16

Generating all substrings

Generate all possible strings up to some fixed length n with repetition (same character included multiple times) We saw how to do something similar recursively (generate permutations of fixed size n no repetition) How might we do it with a queue? Example simplified to n = 2 and only letters A and B

16

slide-17
SLIDE 17

17

“ ” “A” “B” “AA” “AB” “BA” “BB”

Generate all substrings of size 2 from alphabet {‘A’, ‘B’}

slide-18
SLIDE 18

18

“ ” “A” “B” “AA” “AB” “BA” “BB”

“ “

Generate all substrings of size 2 from alphabet {‘A’, ‘B’}

slide-19
SLIDE 19

19

“ ” “A” “B” “AA” “AB” “BA” “BB”

“ “

Generate all substrings of size 2 from alphabet {‘A’, ‘B’}

slide-20
SLIDE 20

20

“ ” “A” “B” “AA” “AB” “BA” “BB”

“ “ “A“ “B“

Generate all substrings of size 2 from alphabet {‘A’, ‘B’}

{ “”}

slide-21
SLIDE 21

21

“ ” “A” “B” “AA” “AB” “BA” “BB”

“A“ “B“

Generate all substrings of size 2 from alphabet {‘A’, ‘B’}

{ “”}

slide-22
SLIDE 22

22

“ ” “A” “B” “AA” “AB” “BA” “BB”

“A“ “B“ “AA“ “AB“

Generate all substrings of size 2 from alphabet {‘A’, ‘B’}

{ “”, “A”}

slide-23
SLIDE 23

23

“ ” “A” “B” “AA” “AB” “BA” “BB”

“B“ “AA“ “AB“

Generate all substrings of size 2 from alphabet {‘A’, ‘B’}

{ “”, “A”}

slide-24
SLIDE 24

24

“ ” “A” “B” “AA” “AB” “BA” “BB”

“B“ “AA“ “AB“

Generate all substrings of size 2 from alphabet {‘A’, ‘B’}

{ “”, “A”}

slide-25
SLIDE 25

25

“ ” “A” “B” “AA” “AB” “BA” “BB”

“B“ “AA“ “AB“ “BA“ “BB“

Generate all substrings of size 2 from alphabet {‘A’, ‘B’}

{ “”, “A”, “B”}

slide-26
SLIDE 26

26

“ ” “A” “B” “AA” “AB” “BA” “BB”

“AA“ “AB“ “BA“ “BB“

Generate all substrings of size 2 from alphabet {‘A’, ‘B’}

{ “”, “A”, “B”}

slide-27
SLIDE 27

27

“ ” “A” “B” “AA” “AB” “BA” “BB”

“AA“ “AB“ “BA“ “BB“

Generate all substrings of size 2 from alphabet {‘A’, ‘B’}

{ “”, “A”, “B”}

slide-28
SLIDE 28

28

“ ” “A” “B” “AA” “AB” “BA” “BB”

“AA“ “AB“ “BA“ “BB“

Generate all substrings of size 2 from alphabet {‘A’, ‘B’}

{ “”, “A”, “B”, “AA”}

slide-29
SLIDE 29

29

“ ” “A” “B” “AA” “AB” “BA” “BB”

“AB“ “BA“ “BB“

Generate all substrings of size 2 from alphabet {‘A’, ‘B’}

{ “”, “A”, “B”, “AA”}

slide-30
SLIDE 30

30

“ ” “A” “B” “AA” “AB” “BA” “BB”

“AB“ “BA“ “BB“

Generate all substrings of size 2 from alphabet {‘A’, ‘B’}

{ “”, “A”, “B”, “AA”, “AB”}

slide-31
SLIDE 31

31

“ ” “A” “B” “AA” “AB” “BA” “BB”

“BA“ “BB“

Generate all substrings of size 2 from alphabet {‘A’, ‘B’}

{ “”, “A”, “B”, “AA”, “AB”, “BA”}

slide-32
SLIDE 32

32

“ ” “A” “B” “AA” “AB” “BA” “BB”

“BB“

Generate all substrings of size 2 from alphabet {‘A’, ‘B’}

{ “”, “A”, “B”, “AA”, “AB”, “BA”, “BB” }

slide-33
SLIDE 33

33

“ ” “A” “B” “AA” “AB” “BA” “BB”

Generate all substrings of size 2 from alphabet {‘A’, ‘B’}

{ “”, “A”, “B”, “AA”, “AB”, “BA”, “BB” }

slide-34
SLIDE 34

Breadth-First Search

Applications
 Find shortest path in graph
 GPS navigation systems
 Crawlers in search engines
 . . . Generally good when looking for the “shortest” or “best” way to do something => lists things in increasing order of “size” stopping at the “shortest” solution

34

slide-35
SLIDE 35

35

findAllSubstrings(int n) { put empty string on the queue while(queue is not empty){ let current_string = dequeue and add to result if(size of current_string < n){ for(each character ch)//every character in alphabet append ch to current_string and enqueue it } } return result; } Size of Substring

slide-36
SLIDE 36

Finding all substrings (with repetition) of size up to n Assume alphabet (A, B, … , Z) of size 26 The empty string= 1= 260 All strings of size 1 = 261 All strings of size 2 = 262 . . . All strings of size n = 26n

Analysis

36

With repetition: I have 26

  • ptions for each of the

n characters A B C

. . .

Z

AA BA CA . . .

ZA

AB BC

. . .

AZ

. . . . . .

ZB ZZ

CB

BZ CZ

””

slide-37
SLIDE 37

Lecture Activity

findAllSubstrings(int n) { put empty string on the queue while(queue is not empty){ let current_string = dequeue and add to result if(size of current_string < n){ for(each character ch)//every character in alphabet append ch to current_string and enqueue it } } return result; }

37

Analyze the worst-case time complexity of this algorithm assuming alphabet of size 26 and up to strings of length n T(n) = ? O(?) Size of Substring

slide-38
SLIDE 38

findAllSubstrings(int n) { put empty string on the queue while(queue is not empty){ let current_string = dequeue and add to result if(size of current_string < n){ for(each character ch)//every character in alphabet append ch to current_string and enqueue it } } return result; }

38

Will stop when all strings have been removed from queue

slide-39
SLIDE 39

findAllSubstrings(int n) { put empty string on the queue while(queue is not empty){ let current_string = dequeue and add to result if(size of current_string < n){ for(each character ch)//every character in alphabet append ch to current_string and enqueue it } } return result; }

39

Adds 26 strings to the queue Removes 1 string from the queue Will stop when all strings have been removed from queue

slide-40
SLIDE 40

findAllSubstrings(int n) { put empty string on the queue while(queue is not empty){ let current_string = dequeue and add to result if(size of current_string < n){ for(each character ch)//every character in alphabet append ch to current_string and enqueue it } } return result; }

40

Adds 26 strings to the queue Removes 1 string from the queue Will stop when all strings have been removed from queue

Loop until queue is empty and dequeue only 1 each time. So the question becomes: How many strings are enqueued in total?

slide-41
SLIDE 41

findAllSubstrings(int n) { put empty string on the queue while(queue is not empty){ let current_string = dequeue and add to result if(size of current_string < n){ for(each character ch)//every character in alphabet append ch to current_string and enqueue it } } return result; }

41

Adds 26 strings to the queue Removes 1 string from the queue Will stop when all strings have been removed from queue

T(n) = 260 + 261 + 262 + . . . 26n

slide-42
SLIDE 42

findAllSubstrings(int n) { put empty string on the queue while(queue is not empty){ let current_string = dequeue and add to result if(size of current_string < n){ for(each character ch)//every character in alphabet append ch to current_string and enqueue it } } return result; }

42

Adds 26 strings to the queue Removes 1 string from the queue Will stop when all strings have been removed from queue

T(n) = 260 + 261 + 262 + . . . 26n

slide-43
SLIDE 43

findAllSubstrings(int n) { put empty string on the queue while(queue is not empty){ let current_string = dequeue and add to result if(size of current_string < n){ for(each character ch)//every character in alphabet append ch to current_string and enqueue it } } return result; }

43

Adds 26 strings to the queue Removes 1 string from the queue Will stop when all strings have been removed from queue

O( 26n)

slide-44
SLIDE 44

44

“ ” “A” “B” “AA” “AB” “BA” “BB”

Let n = 3, alphabet still {‘A’,’B’}

“AAA” “AAB” “ABA” “ABB” “BAA” “BAB” “BBA” “BBB

“ “

20

slide-45
SLIDE 45

45

“ ” “A” “B” “AA” “AB” “BA” “BB” “AAA” “AAB” “ABA” “ABB” “BAA” “BAB” “BBA” “BBB

“A“ “B“

Let n = 3, alphabet still {‘A’,’B’}

21

slide-46
SLIDE 46

46

“ ” “A” “B” “AA” “AB” “BA” “BB” “AAA” “AAB” “ABA” “ABB” “BAA” “BAB” “BBA” “BBB

“AA“ “AB“ “BA“ “BB“

Let n = 3, alphabet still {‘A’,’B’}

22

slide-47
SLIDE 47

47

“ ” “A” “B” “AA” “AB” “BA” “BB” “AAA” “AAB” “ABA” “ABB” “BAA” “BAB” “BBA” “BBB

“AB“ “BA“ “BB“

Let n = 3, alphabet still {‘A’,’B’}

“AAA“ “AAB“ “AA“

slide-48
SLIDE 48

48

“ ” “A” “B” “AA” “AB” “BA” “BB” “AAA” “AAB” “ABA” “ABB” “BAA” “BAB” “BBA” “BBB

“BA“ “BB“

Let n = 3, alphabet still {‘A’,’B’}

“AAA“ “AAB“ “ABA“ “ABB“ “AB“

slide-49
SLIDE 49

49

“ ” “A” “B” “AA” “AB” “BA” “BB” “AAA” “AAB” “ABA” “ABB” “BAA” “BAB” “BBA” “BBB

“BA“ “BB“

Let n = 3, alphabet still {‘A’,’B’}

“AAA“ “AAB“ “ABA“ “ABB“ “BAA“ “BAB“

slide-50
SLIDE 50

50

“ ” “A” “B” “AA” “AB” “BA” “BB” “AAA” “AAB” “ABA” “ABB” “BAA” “BAB” “BBA” “BBB

“BB“

Let n = 3, alphabet still {‘A’,’B’}

“AAA“ “AAB“ “ABA“ “ABB“ “BAA“ “BAB“

slide-51
SLIDE 51

51

“ ” “A” “B” “AA” “AB” “BA” “BB” “AAA” “AAB” “ABA” “ABB” “BAA” “BAB” “BBA” “BBB

“AAA“ “AAB“ “ABA“ “ABB“ “BAA“ “BAB“ “BBA“ “BBB“

Let n = 3, alphabet still {‘A’,’B’}

“BB“

slide-52
SLIDE 52

52

“ ” “A” “B” “AA” “AB” “BA” “BB” “AAA” “AAB” “ABA” “ABB” “BAA” “BAB” “BBA” “BBB

“AAA“ “AAB“ “ABA“ “ABB“ “BAA“ “BAB“ “BBA“ “BBB“

Let n = 3, alphabet still {‘A’,’B’}

23

slide-53
SLIDE 53

Memory Usage

With alphabet {’A’, ’B’, …, ’Z’}, at some point we end up with 26n strings in memory Size of string on my machine = 24 bytes Running this algorithm for n = 7 (≈ 193GB) is the maximum that can be handled by a standard personal computer For n = 8 ≈ 5TB

53

Massive space requirement

slide-54
SLIDE 54

What if we use a stack?

54

O( 26n)

findAllSubstrings(int n) { push empty string on the stack while(stack is not empty){ let current_string = pop and add to result if(size of current_string < n){ for(each character ch)//every character in alphabet append ch to current_string and push it } } return result; }

slide-55
SLIDE 55

55

“ ” “A” “B” “AA” “AB” “BA” “BB”

“ “

slide-56
SLIDE 56

56

“ ” “A” “B” “AA” “AB” “BA” “BB”

“ “ { “” }

slide-57
SLIDE 57

57

“ ” “A” “B” “AA” “AB” “BA” “BB”

“ “ “A“ “B“ { “” }

slide-58
SLIDE 58

58

“ ” “A” “B” “AA” “AB” “BA” “BB”

“A“ “B“ { “” }

slide-59
SLIDE 59

59

“ ” “A” “B” “AA” “AB” “BA” “BB”

“A“ “B“ { “” }

slide-60
SLIDE 60

60

“ ” “A” “B” “AA” “AB” “BA” “BB”

“A“ “B“ “BA“ “BB“ { “”,“B”}

slide-61
SLIDE 61

61

“ ” “A” “B” “AA” “AB” “BA” “BB”

“A“ “BA“ “BB“ { “”,“B”}

slide-62
SLIDE 62

62

“ ” “A” “B” “AA” “AB” “BA” “BB”

“A“ “BA“ “BB“ { “”,“B”,”BB”}

slide-63
SLIDE 63

63

“ ” “A” “B” “AA” “AB” “BA” “BB”

“A“ “BA“ { “”,“B”,”BB”,”BA”}

slide-64
SLIDE 64

64

“ ” “A” “B” “AA” “AB” “BA” “BB”

“A“ { “”,“B”,”BB”,”BA”,”A”}

slide-65
SLIDE 65

65

“ ” “A” “B” “AA” “AB” “BA” “BB”

“A“ “AA“ “AB“ { “”,“B”,”BB”,”BA”,”A”}

slide-66
SLIDE 66

66

“ ” “A” “B” “AA” “AB” “BA” “BB”

“AA“ “AB“ { “”,“B”,”BB”,”BA”,”A”}

slide-67
SLIDE 67

67

“ ” “A” “B” “AA” “AB” “BA” “BB”

“AA“ “AB“ { “”,“B”,”BB”,”BA”,”A”,”AB”}

slide-68
SLIDE 68

68

“ ” “A” “B” “AA” “AB” “BA” “BB”

“AA“ { “”,“B”,”BB”,”BA”,”A”,”AB”,”AA”}

slide-69
SLIDE 69

69

“ ” “A” “B” “AA” “AB” “BA” “BB”

What’s the difference?

{ “”,“B”,”BB”,”BA”,”A”,”AB”,”AA”}

slide-70
SLIDE 70

Depth-First Search

Applications
 Detecting cycles in graphs
 Path finding
 Finding strongly connected components in graph
 . . . Same worst-case runtime analysis
 More space efficient than previous approach
 Does not explore options in increasing order of size

70

slide-71
SLIDE 71

Comparison

Breadth-First Search
 (using a queue) Time O( 26n) Space O( 26n) Good for exploring options in increasing order of size when expecting to find “shallow” or “short” solution Memory inefficient when must keep each “level” in memory

71

Depth-First Search
 (using a stack) Time O( 26n) Space O( n) Explores each option individually to max size - does NOT list options by increasing size More memory efficient

slide-72
SLIDE 72

Other ADTs

72

slide-73
SLIDE 73

Deque

Double ended queue (deque) Can add and remove to/from front and back

73

34

slide-74
SLIDE 74

Deque

Double ended queue (deque) Can add and remove to/from front and back

74

34

slide-75
SLIDE 75

Deque

Double ended queue (deque) Can add and remove to/from front and back

75

34 127

slide-76
SLIDE 76

Deque

Double ended queue (deque) Can add and remove to/from front and back

76

34 127

slide-77
SLIDE 77

Deque

Double ended queue (deque) Can add and remove to/from front and back

77

34 127 49

slide-78
SLIDE 78

Deque

Double ended queue (deque) Can add and remove to/from front and back

78

34 127 49

slide-79
SLIDE 79

Deque

Double ended queue (deque) Can add and remove to/from front and back

79

34 127 49

slide-80
SLIDE 80

Deque

Double ended queue (deque) Can add and remove to/from front and back

80

34 127

slide-81
SLIDE 81

Deque

Double ended queue (deque) Can add and remove to/from front and back

81

34 127

slide-82
SLIDE 82

Deque

In STL :


  • does not use contiguous memory

  • more complex to implement (keep track of memory

blocks)


  • grows more efficiently than vector


 


82

slide-83
SLIDE 83

Deque

In STL :


  • does not use contiguous memory

  • more complex to implement (keep track of memory

blocks)


  • grows more efficiently than vector

In STL stack and queue are adapters of deck
 


83

slide-84
SLIDE 84

Deque

In STL :


  • does not use contiguous memory

  • more complex to implement (keep track of memory

blocks)


  • grows more efficiently than vector

In STL stack and queue are adapters of deque STL standardized the use of “push” and “pop”, adapting with “push_back”, “push_front” etc. for all containers


84

slide-85
SLIDE 85

Priority Queue

A queue of items “sorted” by priority

85

A Low Priority High Priority

slide-86
SLIDE 86

Priority Queue

A queue of items “sorted” by priority

86

A Low Priority High Priority

slide-87
SLIDE 87

Priority Queue

A queue of items “sorted” by priority

87

A Low Priority High Priority D

slide-88
SLIDE 88

Priority Queue

A queue of items “sorted” by priority

88

A Low Priority High Priority D

slide-89
SLIDE 89

Priority Queue

A queue of items “sorted” by priority

89

A Low Priority High Priority D X

slide-90
SLIDE 90

Priority Queue

A queue of items “sorted” by priority

90

X Low Priority High Priority A D

slide-91
SLIDE 91

Priority Queue

A queue of items “sorted” by priority

91

X Low Priority High Priority A D

slide-92
SLIDE 92

Priority Queue

A queue of items “sorted” by priority

92

Low Priority High Priority A D

If value indicates priority, it amounts to a sorted list that accesses/removes 
 the “highest” items first

slide-93
SLIDE 93

Priority Queue

Orders elements by priority => removing an element will return the element with highest priority value Elements with same priority kept in queue order (in some implementations)

93

slide-94
SLIDE 94

Priority Queue

Spoiler Alert!!!! Often implemented with a Heap Will tell you what it is in soon… but it is another example of ADT vs data structure

94