Unformatted text preview: ECE 2574 Introduction to Data Structures and Algorithms Lecture 13: Stacks cont.. Chris Wyatt Electrical and Computer Engineering and School of Biomedical Engineering and Sciences Virginia Tech Wednesday Schedule
Review Warmup Simulating recursion using stacks Standard library stack Stack implementations Homework #4  the EasyBMP library  a flood fill program for images Warmup #1
If I push A, push B, push C, pop, push D, pop, pop, what order are the items A,B,C,D deleted from the stack? CDBA 4.5 % CDB 95.5 % ABCD 0 % DCBA 0 % Warmup #2
Using the track analogy from Monday (inputs must be removed righttoleft), what ordering of push/pops is equivalent to reversing a sequence ABC to CBA reading leftright assuming an initially empty stack? push C, pop, push B, pop, push A, pop 0% push A, pop, push B, pop, push C, pop 9.2 % push C, push B, push A, pop, pop, pop 68 % pop, pop, pop, push A, push B, push C 22.8 % Warmup #3
Consider using a stack to build permutations as we discussed on Monday, is it possible to generate all possible permutations? True False 41 % 59 % Warmup Comments/Questions
is it easy to implement a pushback() method to allow for more permutation possiblities? We can convert any recursive function to a stack based function.
function fact(in n:integer): integer if(n == 0) return (1) else return ( n*fact(n1) ) endfunction We can convert any recursive function to a stack based function.
function fact(in n:integer): integer Stack s Integer result = 1 while( n != 0) s.push(n); n = n  1 endwhile while( !s.empty() ) result = result*s.pop() endwhile return(result) endfunction We can convert any recursive function to a stack based function.
Everywhere a recursive call is encountered use a push.
while( n != 0) s.push(n); n = n  1 endwhile Everywhere a recursive call returns, use a pop and operation.
while( !s.empty() ) result = result*s.pop() endwhile The standard library stack
#include <stack> stack<TYPE> s; // it is a template bool empty() const; void pop(); void push( const TYPE& val ); size_type size() const; TYPE& top(); In class exercise: stack based factorial
Use std::stack<int> to implement
function fact(in n:integer): integer Stack s Integer result = 1 while( n != 0) s.push(n); n = n  1 endwhile while( !s.empty() ) result = result*s.pop() endwhile return(result) endfunction Implementing the stack ADT
The stack is a list with specialized insert, retrieve, and remove operations. We can use either an array based implementation. or We can use a linked list implementation. Which would you use and why ? Linked List Implementation of the Stack
see stack.h and stack.cpp Array Implementation of the Stack
see stack2.h and stack2.cpp Array Implementation of the Stack
Disadvantages: It can only represent a stack of fixed size or Additional memory must be allocated with a copy. Advantages: pushes and pops (within the stack size limit) are somewhat faster. In class exercise: Battle of the stack implementations ......
1. Using the code given, implement both versions of pop and peek for stack2. 2. Using the two stack implementations, write two programs, test_stack and test_stack2, that perform the same sequence of pushes, pops, but using different implementations. Sequence: push 1000 integers, pop 1000 integers Test to see which is faster. Homework #4
The goal of Homework 4 is to gain experience with stacks by implementing a regiongrowing algorithm. The algorithm will be used to change the color in regions of an image. Homework #4
This project will use the EasyBMP library for image IO (easybmp.sourceforge.net). I have already setup a source directory with the CMakeLists.txt files needed to build and use the library (download HW4Starter.zip). The library has one class you will need, BMP, which can read files, give access to pixels, and write files. Example usage of EasyBMP
#include "EasyBMP.h" int main( int argc, char* argv ) { if( argc != 3 ){ cout << "Usage: ColorBMPtoGrayscale" << "<input_filename> <output_filename>" << endl << endl; return 1; } // declare and read the bitmap BMP Input; Input.ReadFromFile( argv[1] ); Example usage of EasyBMP
// convert each pixel to grayscale using RGB>YUV for( int j=0 ; j < Input.TellHeight() ; j++){ for( int i=0 ; i < Input.TellWidth() ; i++){ int Temp = (int) floor( 0.299*Input(i,j)>Red + 0.587*Input(i,j)>Green + 0.114*Input(i,j)>Blue ); ebmpBYTE TempBYTE = (ebmpBYTE) Temp; Input(i,j)>Red = TempBYTE; Input(i,j)>Green = TempBYTE; Input(i,j)>Blue = TempBYTE; } } Example usage of EasyBMP
// Create a grayscale color table if necessary if( Input.TellBitDepth() < 16 ){ CreateGrayscaleColorTable( Input ); } // write the output file Input.WriteToFile( argv[2] ); return 0; } Homework #4
Using the standard library stack implement a region growing algorithm as a class called RegionGrower with the following methods (in addition to constructors/destructor):  SetImage; set the input BMP image to use  SetSeed; set the seed pixel indices  SetReplacement; set the pixel color (RGB) to set the region to  Update; replace the color in a region defined by seed with new color  GetOutput; get the new BMP image You will need to modify the CMakeLists.txt file provided accordingly. Homework #4
For example the command line: Prompt>hw4 Test.bmp 0 0 128 128 128 Output.bmp would change the background color of the Test.bmp image to gray and write the result to Output.bmp. Homework #4
Find program invocations that will change the color of the ECE 2574 text to Hokie Maroon. Put these program invocations in a file called README in the source directory you turn in. In class exercise:
Start your homework by writing the main needed to parse the command line and read in the input file using EasyBMP. ...
View
Full Document
 Fall '08
 CHuang
 Algorithms, Data Structures, Recursion, const Type, School of Biomedical Engineering and Sciences Virginia Tech

Click to edit the document details