queue adt
play

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


  1. Queue ADT Tiziana Ligorio � 1

  2. Today’s Plan Announcements Queue ADT Applications � 2

  3. 
 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 � 3

  4. 
 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 � 4

  5. 
 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 127 � 5

  6. 
 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 127 � 6

  7. 
 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 127 13 � 7

  8. 
 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 127 13 � 8

  9. 
 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 127 13 � 9

  10. 
 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 127 13 � 10

  11. 
 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 127 13 49 � 11

  12. 
 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 127 13 49 � 12

  13. 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 FIFO: First In First Out Only front of queue is accessible (front), no other objects in the queue are visible � 13

  14. Queue Applications Generating all substrings Recognizing Palindromes Any waiting queue 
 - Print jobs 
 - OS scheduling processes with equal priority 
 - Messages between asynchronous processes 
 . . . � 14

  15. Queue Applications Generating all substrings Any waiting queue 
 - Print jobs 
 - OS scheduling processes with equal priority 
 - Messages between asynchronous processes 
 . . . � 15

  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

  17. Generate all substrings of size 2 from alphabet {‘A’, ‘B’} “ ” “A” “B” “AB” “BA” “BB” “AA” � 17

  18. Generate all substrings of size 2 from alphabet {‘A’, ‘B’} “ ” “A” “B” “AB” “BA” “BB” “AA” “ “ � 18

  19. Generate all substrings of size 2 from alphabet {‘A’, ‘B’} “ ” “A” “B” “AB” “BA” “BB” “AA” “ “ � 19

  20. { “”} Generate all substrings of size 2 from alphabet {‘A’, ‘B’} “ ” “A” “B” “AB” “BA” “BB” “AA” “ “ “A“ “B“ � 20

  21. { “”} Generate all substrings of size 2 from alphabet {‘A’, ‘B’} “ ” “A” “B” “AB” “BA” “BB” “AA” “A“ “B“ � 21

  22. { “”, “A”} Generate all substrings of size 2 from alphabet {‘A’, ‘B’} “ ” “A” “B” “AB” “BA” “BB” “AA” “A“ “AA“ “AB“ “B“ � 22

  23. { “”, “A”} Generate all substrings of size 2 from alphabet {‘A’, ‘B’} “ ” “A” “B” “AB” “BA” “BB” “AA” “B“ “AA“ “AB“ � 23

  24. { “”, “A”} Generate all substrings of size 2 from alphabet {‘A’, ‘B’} “ ” “A” “B” “AB” “BA” “BB” “AA” “B“ “AA“ “AB“ � 24

  25. { “”, “A”, “B”} Generate all substrings of size 2 from alphabet {‘A’, ‘B’} “ ” “A” “B” “AB” “BA” “BB” “AA” “B“ “BA“ “BB“ “AA“ “AB“ � 25

  26. { “”, “A”, “B”} Generate all substrings of size 2 from alphabet {‘A’, ‘B’} “ ” “A” “B” “AB” “BA” “BB” “AA” “AA“ “AB“ “BA“ “BB“ � 26

  27. { “”, “A”, “B”} Generate all substrings of size 2 from alphabet {‘A’, ‘B’} “ ” “A” “B” “AB” “BA” “BB” “AA” “AA“ “AB“ “BA“ “BB“ � 27

  28. { “”, “A”, “B”, “AA”} Generate all substrings of size 2 from alphabet {‘A’, ‘B’} “ ” “A” “B” “AB” “BA” “BB” “AA” “AA“ “AB“ “BA“ “BB“ � 28

  29. { “”, “A”, “B”, “AA”} Generate all substrings of size 2 from alphabet {‘A’, ‘B’} “ ” “A” “B” “AB” “BA” “BB” “AA” “AB“ “BA“ “BB“ � 29

  30. { “”, “A”, “B”, “AA”, “AB”} Generate all substrings of size 2 from alphabet {‘A’, ‘B’} “ ” “A” “B” “AB” “BA” “BB” “AA” “AB“ “BA“ “BB“ � 30

  31. { “”, “A”, “B”, “AA”, “AB”, “BA”} Generate all substrings of size 2 from alphabet {‘A’, ‘B’} “ ” “A” “B” “AB” “BA” “BB” “AA” “BA“ “BB“ � 31

  32. { “”, “A”, “B”, “AA”, “AB”, “BA”, “BB” } Generate all substrings of size 2 from alphabet {‘A’, ‘B’} “ ” “A” “B” “AB” “BA” “BB” “AA” “BB“ � 32

  33. { “”, “A”, “B”, “AA”, “AB”, “BA”, “BB” } Generate all substrings of size 2 from alphabet {‘A’, ‘B’} “ ” “A” “B” “AB” “BA” “BB” “AA” � 33

  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

  35. Size of Substring 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; } � 35

  36. Analysis Finding all substrings (with repetition) of size up to n Assume alphabet (A, B, … , Z) of size 26 The empty string= 1= 26 0 ”” All strings of size 1 = 26 1 . . . A B C Z All strings of size 2 = 26 2 AA BA CA . . . ZA . . . AB BC ZB CB . . . . . . AZ BZ CZ ZZ . . . With repetition: I have 26 options for each of the All strings of size n = 26 n n characters � 36

  37. Lecture Activity Analyze the worst-case time complexity of this algorithm Size of Substring assuming alphabet of size 26 and up to strings of length n findAllSubstrings(int n) T(n) = ? { O(?) 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

  38. Will stop when all strings have been removed from queue 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

  39. Will stop when all strings have been removed from queue Removes 1 string from the queue findAllSubstrings(int n) Adds 26 strings to the queue { 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

  40. Will stop when all strings have been removed from queue Removes 1 string from the queue findAllSubstrings(int n) Adds 26 strings to the queue { 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; } Loop until queue is empty and dequeue only 1 each time. So the question becomes: How many strings are enqueued in total? � 40

  41. Will stop when all strings have been removed from queue Removes 1 string from the queue findAllSubstrings(int n) Adds 26 strings to the queue { 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; } T(n) = 26 0 + 26 1 + 26 2 + . . . 26 n � 41

Recommend


More recommend