---------------------------------------------------------------------------- COMP SCI 731 - Data Struc & Algorithms - Lecture 1 - Friday, June 23, 2000 ---------------------------------------------------------------------------- Today: [ ] Opening remarks [ ] Stacks and their use Introduction ------------ o Please tell me who you are. Write your name in English and Thai, then your nickname, and then some sentence or two to help me remember you. o I do this.... o Graduate-level course on data structures and algorithms. Data structures and algorithms intertwined: interesting algorithms require non-trivial data structures to support them, and data structures by themselves are useless -- only by supporting an algorithm does a data structure accomplish anything. o Homeworks of two types: programs and written problems. Some of the programs will be designed for you to discover something. You can write the programs in any language you wish, but I think most of you will choose to use C or C++. o I will select material from various books, particularly: - Corman, Leiserson, Rivest: Introduction to Algorithms - Kozen: The Design and Analysis of Algorithms - Weiss: Data Structures and Algorithm Analysis [in C++] When I follow closely from a book, I will give you copies of the material. You can also borrow any of these books from me; get it from my office. o Grading: 30% midterm, 40% final, 20% homework, 10% attendance o Please come visit me, talk to me. You must tell me what is going on. I do not know your background. It is very easy for me to accidentally go too fast, to make the class too hard, or to use English that is too difficult. If you are not understanding, I want to know. o Topics include: stacks and queues; disjoint sets; Fibonacci heaps; splay trees; search trees; graph algorithms; dynamic programming; greedy algorithms; hashing; string matching; ??? If you have topics requests, please tell me and I will try to find time. Stacks ------ Stacks are probably the most widely used data structure, insofar as almost every executing program employs one: the runtime stack that keeps the stack frames (activation records) of local variables. Stacks are used to record a sequence of *deferred obligations*. Something happens that generates an obligation to do something. You will get to it -- but in a little while, becase there are other obligations to take care of, too. With a stack, the most recently generated obligation is serviced first (LIFO: last-in, first-out). To compare, a queue is also used to generate deferred obligations, but those obligations are serviced in a FIFO order: first-in, first out. When you think about something like a stack do NOT think of it by thinking in terms of any specific implementation. Think of it in terms of an abstract data type (ADT). Remember that an ADT is collection of data and a collection of operations which manipulate that data. ADT Stack [of type T] Data: A number n>=0 and a sequence a_1, ..., a_n of elements, each element being of type T. Initially (when the stack is created) n=0. Operations: Boolean Empty() - return true if n==0, false otherwise void Push(T item) - increment n and then set a_n = item T Pop() - if n==0 then ERROR. Otherwise, item=a_n, n--, and return item Above we are giving the operational semantics. We are not meaning to imply anything about the implementation! There are many ways to implement a stack, and we will later talk about that. But sing a stack doesn't require you to know anything about how the stack is to be implemented. This is a general characteristic of using ADTs: understand the data and the operations, and then use the ADT. You should not think about how the ADT is implemented when you are using it. Respect the abstraction boundary. Example 1: Evaluating a Postfix ("reverse Polish noation - RPN") expression: Prefix: - * + 4 5 3 2 Infix: (4+5)*3-2 Postfix: 4 5 + 3 * 2 - <=== When you see an operand n, push it onto the stack. When you see a binary operator Op, pop the top stack entry into y, pop the next stack entry into x, apply the operator to get z=x Op y, and push z onto the stack. Generate an error if the stack is ever empty when you want to pop, or if the stack contains anything but one entry when you are done. That one entry is the answer. | | | | | | | | | | | | | | | | | | | | |5 | | | |3 | | | |2 | | | | | |4 | |4 | |9 | |9 | |27| |27| |25| |__| |__| |__| |__| |__| |__| |__| |__|