1 / 68

Stacks and Queues Sections 3.6 and 3.7

Stacks and Queues Sections 3.6 and 3.7. Stack ADT. Collections: Elements of some proper type T Operations: void push(T t) void pop() T top() bool empty() unsigned int size() constructor and destructor. Uses of ADT Stack. Depth first search / backtracking

xander
Download Presentation

Stacks and Queues Sections 3.6 and 3.7

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Stacks and QueuesSections 3.6 and 3.7

  2. Stack ADT • Collections: • Elements of some proper type T • Operations: • void push(T t) • void pop() • T top() • bool empty() • unsigned int size() • constructor and destructor

  3. Uses of ADT Stack • Depth first search / backtracking • Evaluating postfix expressions • Converting infix to postfix • Function calls (runtime stack) • Recursion

  4. Stack Model—LIFO • Empty stack S • S.empty() is true • S.top() not defined • S.size() == 0 • S.push(“mosquito”) • S.empty() is false • S.top() == “mosquito” • S.size() == 1 • S.push(“fish”) • S.empty() is false • S.top() == “fish” • S.size() == 2 "fish" "mosquito" food chain stack

  5. Stack Model—LIFO • S.push(“raccoon”) • S.empty() is false • S.top() == “raccoon” • S.size() == 3 • S.pop() • S.empty() is false • S.top() == “fish” • S.size() == 2 "raccoon" "fish" "mosquito" food chain stack

  6. Queue ADT - FIFO • Collection • Elements of some proper type T • Operations • void push(T t) • void pop() • T front() • bool empty() • unsigned int size() • Constructors and destructors

  7. back front Queue Model—FIFO • Empty Q animal_parade_queue

  8. back back back back back front Queue Model—FIFO • Q.push( “ant” ) • Q.push( “bee” ) • Q.push( “cat” ) • Q.push( “dog” ) animal_parade_queue ANT BEE CAT DOG

  9. back back back back front Queue Model—FIFO • Q.pop(); • Q.pop(); • Q.push( “eel” ); • Q.pop(); • Q.pop(); animal_parade_queue BEE DOG CAT EEL ANT DOG EEL BEE CAT EEL DOG CAT DOG

  10. Uses of ADT Queue • Buffers • Breadth first search • Simulations • Producer-Consumer Problems

  11. Problem Discover a path from start to goal Solution Go deep If there is an unvisited neighbor, go there Backtrack Retreat along the path to find an unvisited neighbor Outcome If there is a path from start to goal, DFS finds one such path Depth First Search—Backtracking start 1 2 3 4 5 6 7 8 9 10 11 12 goal

  12. Depth First Search—Backtracking (2) • Stack 1 start 2 3 4 5 6 7 8 9 10 11 12 goal Push

  13. Depth First Search—Backtracking (3) • Stack 1 start 2 3 4 5 6 7 8 9 10 11 12 Push goal Push

  14. Depth First Search—Backtracking (4) • Stack 1 start 2 3 4 5 6 7 8 Push 9 10 11 12 Push goal Push

  15. Depth First Search—Backtracking (5) • Stack 1 start 2 3 4 Push 5 6 7 8 Push 9 10 11 12 Push goal Push

  16. Depth First Search—Backtracking (6) • Stack 1 start Push 2 3 4 Push 5 6 7 8 Push 9 10 11 12 Push goal Push

  17. Depth First Search—Backtracking (7) • Stack 1 start Pop 2 3 4 Push 5 6 7 8 Push 9 10 11 12 Push goal Push

  18. Depth First Search—Backtracking (8) • Stack 1 start 2 3 4 Pop 5 6 7 8 Push 9 10 11 12 Push goal Push

  19. Depth First Search—Backtracking (9) • Stack 1 start 2 3 4 5 6 7 8 Pop 9 10 11 12 Push goal Push

  20. Depth First Search—Backtracking (10) • Stack 1 start 2 3 4 5 6 7 8 9 10 11 12 Pop goal Push

  21. Depth First Search—Backtracking (11) • Stack 1 start 2 3 4 5 6 7 8 9 10 11 12 Push goal Push

  22. Depth First Search—Backtracking (12) • Stack 1 start 2 3 4 5 6 7 8 Push 9 10 11 12 Push goal Push

  23. Depth First Search—Backtracking (13) • Stack 1 start 2 3 4 Push 5 6 7 8 Push 9 10 11 12 Push goal Push

  24. DFS Implementation DFS() { stack<location> S; // mark the start location as visited S.push(start); while (S is not empty) { t = S.top(); if (t == goal) Success(S); if (// t has unvisited neighbors) { // choose an unvisited neighbor n // mark n visited; S.push(n); } else { BackTrack(S); } } Failure(S); }

  25. DFS Implementation (2) BackTrack(S) { while (!S.empty() && S.top() has no unvisited neighbors) { S.pop(); } } Success(S) { // print success while (!S.empty()) { output(S.top()); S.pop(); } } Failure(S) { // print failure while (!S.empty()) { S.pop(); } }

  26. Breadth First Search • Problem • Find a shortest path from start to goal start 1 2 3 4 5 6 7 8 9 10 11 12 goal

  27. Breadth First Search (2) • Queue push start 1 2 3 4 5 6 7 8 9 10 11 12 goal

  28. Breadth First Search (3) • Queue pop start 1 2 3 4 5 6 7 8 9 10 11 12 goal

  29. Breadth First Search (4) • Queue pop pop pop start 1 2 3 4 5 6 7 8 9 10 11 12 goal

  30. Breadth First Search (5) • Queue pop start 1 2 3 4 5 6 7 8 9 10 11 12 goal

  31. Breadth First Search (6) • Queue push push 1 start 2 3 4 5 6 7 8 9 10 11 12 goal

  32. Breadth First Search (7) • Queue Pop 1 start 2 3 4 5 6 7 8 9 10 11 12 goal

  33. Breadth First Search (8) • Queue Push Push 1 start 2 3 4 5 6 7 8 9 10 11 12 goal

  34. Breadth First Search (9) • Queue Pop 1 start 2 3 4 5 6 7 8 9 10 11 12 goal

  35. Breadth First Search (10) • Queue Pop 1 start 2 3 4 5 6 7 8 9 10 11 12 goal

  36. Breadth First Search (11) • Queue Pop 1 start 2 3 4 5 6 7 8 9 10 11 12 goal

  37. Breadth First Search (12) • Queue Push 1 start 2 3 4 5 6 7 8 9 10 11 12 goal

  38. Breadth First Search (13) • Queue Pop 1 start 2 3 4 5 6 7 8 9 10 11 12 goal

  39. Breadth First Search (14) • Queue Push 1 start 2 3 4 5 6 7 8 9 10 11 12 goal

  40. BFS Implementation BFS { queue<location> Q; // mark the start location as visited Q.push(start); while (Q is not empty) { t = Q.front(); for (// each unvisited neighbor n) { Q.push(n); if (n == goal) Success(S); } Q.pop(); } Failure(Q); }

  41. Evaluating Postfix Expressions • Postfix expressions: operands precede operator • Tokens: atomics of expressions, either operator or operand • Example: • z = 25 + x*(y – 5) • Tokens: z, =, 25, +, x, *, (, y, -, 5, )

  42. Evaluating Postfix Expressions (2) • Evaluation algorithm: • Use stack of tokens • Repeat • If operand, push onto stack • If operator • pop operands off the stack • evaluate operator on operands • push result onto stack • Until expression is read • Return top of stack

  43. Evaluating Postfix Expressions (3) • Most CPUs have hardware support for this algorithm • Translation from infix to postfix also uses a stack (software)

  44. Evaluating Postfix Expressions (4) • Original expression: 1 + (2 + 3) * 4 + 5 • Evaluate: 1 2 3 + 4 * + 5 +

  45. Evaluating Postfix Expressions (5) • Input: 1 2 3 + 4 * + 5 + • Push(1)

  46. Evaluating Postfix Expressions (6) • Input: 2 3 + 4 * + 5 + • Push(2)

  47. Evaluating Postfix Expressions (7) • Input: 3 + 4 * + 5 + • Push(3)

  48. Evaluating Postfix Expressions (8) • Input: + 4 * + 5 + • Pop() == 3 • Pop() == 2

  49. Evaluating Postfix Expressions (9) • Input: + 4 * + 5 + • Push(2 + 3)

  50. Evaluating Postfix Expressions (10) • Input: 4 * + 5 + • Push(4)

More Related