870 likes | 1.11k Views
Stacks and Queues. Andy Wang Data Structures, Algorithms, and Generic Programming. Abstract Data Type. A collection of data A set of operations on the data or subsets of the data A set of axioms , or rules of behavior governing the interaction of operators
E N D
Stacks and Queues Andy Wang Data Structures, Algorithms, and Generic Programming
Abstract Data Type • A collection of data • A set of operations on the data or subsets of the data • A set of axioms, or rules of behavior governing the interaction of operators • Examples: stack, queue, list, vector, deque, priority queue, table (map), associative array, set, graph, digraph
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
Stack ADT (2) • Axioms (for any stack S) • S.size, S.empty(), and S.push(t) are always defined • S.pop() and S.top() are defined iff S.empty() is false • S.empty(), S.size(), S.top() do not change S • S.empty() is true iff S.size() == 0 • S.push(t) followed by S.pop() leaves S unchanged
Stack ADT (3) • Axioms (for any stack S) • After S.push(t), S.top() returns t • S.push(t) increases S.size() by 1 • S.pop() decreases S.size() by 1
Stack Model—LIFO • Empty stack S • S.empty() is true • S.top() not defined • S.size() == 0 food chain stack
Stack Model—LIFO • S.push(“mosquito”) • S.empty() is false • S.top() == “mosquito” • S.size() == 1 food chain stack
Stack Model—LIFO • S.push(“fish”) • S.empty() is false • S.top() == “fish” • S.size() == 2 food chain stack
Stack Model—LIFO • S.push(“raccoon”) • S.empty() is false • S.top() == “raccoon” • S.size() == 3 food chain stack
Stack Model—LIFO • S.pop() • S.empty() is false • S.top() == “fish” • S.size() == 2 food chain stack
Derivable Behaviors (Theorems) • If (S.size() == n) is followed by k push operations, then S.size() == n + k • If (S.size() == n) is followed by k pop operations, then S.size == n – k (k <= n) • The last element of S pushed onto S is the top of S • S.pop() removes the last element of S pushed onto S
Uses of ADT Stack • Depth first search / backtracking • Evaluating postfix expressions • Converting infix to postfix • Function calls (runtime stack) • Recursion
Queue ADT • Collection • Elements of some proper type T • Operations • void push(T t) • void pop() • T front() • bool empty() • unsigned int size() • Constructors and destructors
Queue ADT • Axioms (for any Queue Q) • Q.size(), Q.empty(), Q.push(t) are always defined • Q.pop() and Q.front() are defined iff Q.empty() is false • Q.empty(), Q.size(), Q.front() do not change Q • Q.empty() is true iff Q.size() == 0 • Suppose Q.size() == n, and the next element pushed onto Q is t; then, after n elements have been popped from Q, t = Q.front()
Queue ADT • Axioms (for any Queue Q) • Q.push(t) increases Q.size() by 1 • Q.pop() decreases Q.size() by 1 • If t = Q.front() then Q.pop() removes t from Q
Queue Model—FIFO • Empty Q animal parade queue
front back Queue Model—FIFO • Q.Push(“ant”) animal parade queue
front back Queue Model—FIFO • Q.Push(“bee”) animal parade queue
front back Queue Model—FIFO • Q.Push(“cat”) animal parade queue
front back Queue Model—FIFO • Q.Push(“dog”) animal parade queue
front back Queue Model—FIFO • Q.Pop() animal parade queue
front back Queue Model—FIFO • Q.Pop() animal parade queue
front back Queue Model—FIFO • Q.Push(“eel”) • Q.Pop() • Q.Pop() animal parade queue
Derivable Behaviors (Theorems) • If (Q.size() == n) is followed by k push operations, then Q.size() == n + k • If (Q.size() == n) is followed by k pop operations, then Q.size() == n – k (k <= n) • The first element pushed onto Q is the the front of Q • Q.pop() removes the front element of Q
Uses of ADT Queue • Buffers • Breadth first search • Simulations
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 1 start 2 3 4 5 6 7 8 9 10 11 12 goal
Depth First Search—Backtracking (2) • Stack 1 start 2 3 4 5 6 7 8 9 10 11 12 goal Push
Depth First Search—Backtracking (3) • Stack 1 start 2 3 4 5 6 7 8 9 10 11 12 Push goal Push
Depth First Search—Backtracking (4) • Stack 1 start 2 3 4 5 6 7 8 Push 9 10 11 12 Push goal Push
Depth First Search—Backtracking (5) • Stack 1 start 2 3 4 Push 5 6 7 8 Push 9 10 11 12 Push goal Push
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
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
Depth First Search—Backtracking (8) • Stack 1 start 2 3 4 Pop 5 6 7 8 Push 9 10 11 12 Push goal Push
Depth First Search—Backtracking (9) • Stack 1 start 2 3 4 5 6 7 8 Pop 9 10 11 12 Push goal Push
Depth First Search—Backtracking (10) • Stack 1 start 2 3 4 5 6 7 8 9 10 11 12 Pop goal Push
Depth First Search—Backtracking (11) • Stack 1 start 2 3 4 5 6 7 8 9 10 11 12 Push goal Push
Depth First Search—Backtracking (12) • Stack 1 start 2 3 4 5 6 7 8 Push 9 10 11 12 Push goal Push
Depth First Search—Backtracking (13) • Stack 1 start 2 3 4 Push 5 6 7 8 Push 9 10 11 12 Push goal Push
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 // mark n visited; S.push(n); } else { BackTrack(S); } } Failure(S); }
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(); } }
Breadth First Search • Problem • Find a shortest path from start to goal 1 start 2 3 4 5 6 7 8 9 10 11 12 goal
Breadth First Search (2) • Queue Push 1 start 2 3 4 5 6 7 8 9 10 11 12 goal
Breadth First Search (3) • Queue Pop 1 start 2 3 4 5 6 7 8 9 10 11 12 goal
Breadth First Search (4) • Queue Push Push Push 1 start 2 3 4 5 6 7 8 9 10 11 12 goal
Breadth First Search (5) • Queue Pop 1 start 2 3 4 5 6 7 8 9 10 11 12 goal
Breadth First Search (6) • Queue Push Push 1 start 2 3 4 5 6 7 8 9 10 11 12 goal
Breadth First Search (7) • Queue Pop 1 start 2 3 4 5 6 7 8 9 10 11 12 goal
Breadth First Search (8) • Queue Push Push 1 start 2 3 4 5 6 7 8 9 10 11 12 goal
Breadth First Search (9) • Queue Pop 1 start 2 3 4 5 6 7 8 9 10 11 12 goal
Breadth First Search (10) • Queue Pop 1 start 2 3 4 5 6 7 8 9 10 11 12 goal