Week 6. Data structures
|
|
- Kristian Lamb
- 5 years ago
- Views:
Transcription
1 n Week 6 Data structures 6 7 n 8
2 General remarks We start considering Part III Data Structures from CLRS. As a first example we consider two special of buffers, namely stacks and queues. Reading from CLRS for week 5 The introduction to Part III on using on a computer. Chapter 10, Section 10.1.
3 Sets The most fundamental mathematical notion is that of a set: We have the possibility to determine the elements of a set. And we can form, either by some set-defining property, or by using already given (e.g., unions, intersections, differences). Now to bring the eternal and infinite world of mathematics to a computer, we need to take care of construction of objects destruction of objects naming (basically of functions) order issues ( are unordered, but in computation there is always order). For this, CLRS uses the (generic) ADT of dynamic. ADT: abstract data type values (like ) and how to operate with them.
4 Elements of a dynamic A dynamic set might contain pointers (or iterators) to objects, or the objects themselves (in Java this can be only integers and other primitive types, in C++ this is possible for every type of object). Whatever the objects in a set are, access to them (especially for changing them) is only possible via a pointer (or iterator). For insertion into a dynamic set, we must be given the object itself, and typically we obtain the pointer (iterator, handle) to the copy of that object in the dynamic set back. For deletion from a dynamic set, we typically have the pointer of an object (already) in the dynamic set, and we want to delete that (very specific) object. For searching, we typically have only given some key information, and we want to search for some element in the dynamic set, which fits this (key) information.
5 Keys Besides objects (which become elements once they are in the set) and pointers, CLRS uses the notion of a key to identify an object: If the object is for example a record of personal attributes, then the name or some form of ID can be used as a key. Often they keys are used for sorting. For example for that database of personal attributes, we might sort it according to alphabetical sorting of names.
6 : elementship and search With S we can ask whether x S is true. This is the most basic set operation, and the equivalent for dynamic is the operation SEARCH(S,k) for key k and dynamic set S, returning either a pointer (iterator) to an object in S with key k, or NIL if there is no such object. We require the ability to extract the key from an object in S, and to compare keys for equality. 1 Storing S via an array (or a list), SEARCH can be performed in O( S ) time (that is, linear time) by simple sequential search. 2 To do faster than this, typically in time O(log( S )) (logarithmic time), under various circumstances, is a major aim of data structures for dynamic.
7 : modifying operations With the following operations we can build and change dynamic : INSERT(S, x) inserts an object into dynamic set S, where x is either a pointer or the object itself. DELETE(S, x) deletes an object from dynamic set S, where here x is a pointer (iterator) into S. Note that the x in DELETE is of a different nature than the x in INSERT: It is a pointer (iterator!) into the (dynamic) set (and thus these two x are of different type). The most important application of INSERT is for creating a dynamic set: To create a set S of size n, call INSERT(S, ) n-times. We always have INSERT, while we might not have DELETE.
8 : using order Often it is assumed that a linear order is given on the keys: So besides k == k? we now can ask k k?. In practice using strict orders < is more common, however this creates some (necessary) technical complications, which in this module we won t be much concerned about (we discuss issues when the need arises). (These complications have to do with the notion of equality, since < includes not equal. Considering Java, recall that there (lacking operator overloading and lacking the ability to distinguish between object and pointer ) you have two operations for checking equality : = = for object-identity and.equals() for object-equality, and now we needed appropriate object-equality (consistent with the algorithms).)
9 : four further operations Given a linear order on the objects (to be put into the set), we have the following additional operations: MINIMUM(S) returns a pointer (iterator) to the element with the smallest key MAXIMUM(S) returns a pointer (iterator) to the element with the largest key SUCCESSOR(S, x), where x is a pointer (iterator) into S, returns the next element in S w.r.t. the order on the keys PREDECESSOR(S, x), where x is a pointer (iterator) into S, returns the previous element in S. Operations for computing successors and predecessors can fail (there is no successor resp. predecessor iff we are already at the end resp. beginning), and in such we return NIL.
10 Using sorting algorithms: static case can be realised using sorting, where we have to assume a linear order on the keys. If the set S with n elements is to be built only once, at the beginning, from a sequence of elements, then storing the elements in an array and sorting them, using for example MERGE-SORT with time complexity O(n logn), is a good option: SEARCH then takes time O(log n) (using binary search) while each of MINIMUM, MAXIMUM, SUCCESSOR, PREDECESSOR takes constant time. However we are concerned here with the dynamic case, where insertions and deletions are used in unpredictable ways. If we not assume that building the set is done (once and for all) at the beginning, but we want to have insertion and deletion, then the case is much less favourable.
11 Using sorting algorithms: dynamic case Now INSERTION and DELETION take time O(log(n)+n) = O(n), searching first for the right insertion/deletion place, and then shifting the other elements appropriately, while the five other (non-modifying) operations still take logarithmic resp. constant time. For practical applications, the linear complexity of insertion and deletion is not acceptable. And we also see that most of the intelligence of sophisticated searching algorithms is blown out of the window, and only some form of INSERTION-SORT (in combination with binary search) survived. It could be said that data structures for dynamic try to introduce some of the intelligent methods into the dynamic framework, making insertion and deletion more efficient (necessarily at the cost of making the other operations (somewhat) less efficient).
12 of dynamic Often not all of the operations for dynamic are needed, opening up the possibilities of specialised and more efficient s. Three important are as follows: Buffer Only INSERTION, SHOW-SELECTED-ELEMENT (like MINIMUM and MAXIMUM, but not necessarily related to some order) and DELETE-SELECTED-ELEMENT; special are stacks and queues. Priority queue Only INSERTION, MINIMUM resp. MAXIMUM (for min- resp. max-priority queues) and DELETE-MIN resp. DELETE-MAX (special queues, where the selected element is given by a linear order on the keys). Dictionary Only INSERTION, DELETION and SEARCH (i.e., no order-requirements).
13 of dynamic (cont.) In other words: Buffers (stacks, queues, and priority queues) have the notion of a selected element, which they can show and delete (but general deletion is not possible); searching in the general sense is not possible. Dictionaries can perform arbitrary deletions and searches, but they have no order on the elements (and thus also no special elements ). Insertion is always possible (given that there is enough space).
14 Last in first out A stack is a buffer where the selected element is the element last entered. Thus a stack is characterised by the slogan last in first out LIFO. One could also say first in last out (FILO). So if you enter numbers 1,2,3 into a stack (in that order), then you get back 3,2,1 (in that order).
15 Stack operations: PUSH, POP, TOP For dynamic in general we talk about INSERT, but for stacks this is called PUSH. And the DELETE operation (which here deletes a selected element) is called POP. The operation for returning the selected element is called TOP. In the book the operation POP combines the above POP (just deleting the selected element) and the above TOP (just returning the selected element), which is the old style for a stack, which has been shown to have severe disadvantages: Amongst others, separation of concerns requires that we have two different operations POP and TOP.
16 Examples If we have a stack, and perform (in this order) then TOP yields 3. PUSH(1), PUSH(2), PUSH(3), 1 After POP then TOP yields 2. 2 And after another POP then TOP yields 1. 3 A final POP yields an empty stack. Note that we have precisely as many PUSH as POP instructions (that holds for every buffer). What happens if on an empty stack we use POP or TOP?!?
17 The EMPTY operation In order to check whether the stack is empty, the fourth stack-operation is EMPTY, returning a boolean which is true if and only if the stack is empty. It depends on the concrete stack- (i.e., on the interface) what happens in case of an error. There are two principle error possibilities: stack overflow and stack underflow. Overflow means that a push-operation exceeds the stack-capacity. Underflow means the use of a top- or pop-operation on an empty stack.
18 n via an array Using an array we can easily implement the ADT Stack: 1 The class (Java or, say, C++) contains as data member ( instance variable ) a fixed-size array. 2 We fill that array from the left. 3 An index-variable points to the currently open slot in the array. 4 Popping an element from the stack just means decrementing that index. We consider an via the Java-class Stack.
19 Class Stack class Stack { private final int [] stack ; private final int N; // max number elements private int n; // number of elements public Stack( final int N ) { // Standard exceptions raised if N < 0 // or N is too big for available memory. N = N ; stack = new int [N]; n = 0; } Remark: Note that n is the current number of elements as well as the index of the next open array-slot.
20 Class Stack (cont.) public boolean empty() { return n==0; } public int top() { // Standard exception raised if n == 0. assert (stack!=null && stack. length==n) ; assert (n >= 0 && n <= N) ; return stack [n 1]; } Remark: assert is here used for conditions which are enforced by the internal logic of the class (and thus a violation would show that there is a bug in the class definition). The case n == 0 is absolutely possible according to the logic of the class Stack: Either the caller has to ensure that this never happens, or the exception has to be handled.
21 Class Stack (cont.) public boolean push( final int x) { assert (n >= 0 && n <= N) ; if (n == N) return false ; assert (stack!=null && stack. length==n) ; stack [n++] = x; return true ; } public boolean pop() { assert (n >= 0) ; assert (n <= N) ; if (n == 0) return false ; n; return true ; } Remarks: push and pop don t throw (exceptions). Both operations can fail (if we have already N elements, or there is no element left), in which case they return false.
22 Class Stack (cont.) // additional functionality : public int size () { return n; } public int max size () { return N; } public boolean equals(stack S) { if (n!= S.n) return false ; for ( int i = 0; i < n; ++i ) if (stack [ i ]!= S. stack [ i ]) return false ; return true ; } Remark: Recall that for Stack-variables a, b the comparison a == b checks whether these pointers are equal if we wish to check for equal content we need to use a.equals(b).
23 Class Stack (cont.) } public String tostring () { String out = [ + n +, + N + ]\n ; for ( int i = 0; i < n 1; ++i ) out += stack [ i ] + ; if (n > 0) out += stack [n 1]; return out ; } Remark: Here we take care to avoid a trailing space (after the last stack element).
24 First in first out A queue is a buffer where the selected element is the element first entered. Thus a queue is characterised by the slogan first in first out FIFO. One could also say last in last out ( LILO ), but that is apparently not used. So if you enter numbers 1,2,3 into a queue (in that order), then you get back 1,2,3 (in that order).
25 Queue operations We will use very similar notions as for stacks: The only difference is that instead of TOP we use FRONT. Besides that we have PUSH, POP, EMPTY. The book uses ENQUEUE instead of PUSH, and DEQUEUE instead of POP. However our choice is more popular with programming languages.
26 Examples If we have a queue, and perform (in this order) PUSH(1), PUSH(2), PUSH(3), then FRONT yields 1. 1 After POP then FRONT yields 2. 2 And after another POP then FRONT yields 3. 3 A final POP yields an empty queue. Note that we have precisely as many PUSH as POP instructions (that holds for every buffer).
27 n via an array Using an array we can (relatively) easily implement the ADT Queue: 1 The class (Java or, say, C++) contains as data member (instance variable) a fixed-size array. 2 We have furthermore index-variables for the left ( head ) and the right ( tail ) end. 3 PUSH moves forward the right end, POP the left end. 4 We do not need to re-allocate the elements, when we reach the right boundary of the array while actually not the whole array is filled, since we can wrap around the part of the array used for the queue. 5 For a real, one needs to get the details right, but the basic idea is very simple. We consider an via the Java-class Queue.
28 Class Queue class Queue { private final int [] queue ; private final int N; // maximal number private int n; // number of elements private int a, b; // first and one past the last index of current elements public Queue( final int N ) { // Standard exceptions raised if N < 0 or N is too big for available memory. N = N ; queue = new int [N]; n = a = b = 0; }
29 Class Queue (cont.) public boolean empty() { return n==0; } public int front () { // Queue underflow if n==0 (not detected) assert (queue!=null && queue. length==n) ; assert (a >= 0 && a < N) ; return queue [a ]; }
30 Class Queue (cont.) public boolean push( final int x) { assert (n >= 0 && n <= N) ; if (n == N) return false ; assert (queue!=null && queue. length==n) ; assert (b >= 0 && b < N) ; queue [b] = x; if (b == N 1) b = 0; else ++b; ++n; return true ; }
31 Class Queue (cont.) public boolean pop() { assert (n >= 0 && n <= N) ; if (n == 0) return false ; assert (a >= 0 && a < N) ; if (a == N 1) a = 0; else ++a; n; return true ; }
32 Class Queue (cont.) // additional functionality : public int size () { return n; } public int max size () { return N; } public boolean equals(queue S) { if (n!= S.n) return false ; for ( int i=a, j=s.a, c=0; c<n; i=(i==n 1)?0: i+1, j=(j==n 1)?0: j+1, ++c) if (queue [ i ]!= S. queue [ j ]) return false ; return true ; } Note that c is used here as a counter.
33 Class Queue (cont.) } public String tostring () { String out = [ + n +, + N + ]\n ; for ( int i = a, c = 0; c < n; i = ( i==n 1)? 0: i+1, ++c) out += queue [ i ] + ; return out ; }
34 Pushing and popping From [Sedgewick, Exercise 4.6]: A letter means push and an asterisk means pop in the following sequence. Give the sequence of values returned by the pop operations when this sequence of operations is performed on an initially empty stack: Solution: E A S * Y * Q U E * * * S T * * * I O * N * * * S Y E U Q T S A O N I E
35 Pushing and popping From [Sedgewick, Exercise 4.6]: A letter means push and an asterisk means pop in the following sequence. Give the sequence of values returned by the pop operations when this sequence of operations is performed on an initially empty stack: Solution: E A S * Y * Q U E * * * S T * * * I O * N * * * S Y E U Q T S A O N I E
36 Checking balancedness Develop the idea for a program that reads in a sequence of characters, and determines whether its parentheses, braces, and curly braces are balanced. For example is balanced, while is not. Solution: ( [ { ( [ ] ) ( [ ] ) } ] ) ( ( [ { } { } [ ] ) ) ) When you read an opening bracket-symbol, push it onto the stack, when you read a closing bracket-symbol, check whether it s the top-symbol, and pop it.
37 Checking balancedness Develop the idea for a program that reads in a sequence of characters, and determines whether its parentheses, braces, and curly braces are balanced. For example is balanced, while is not. Solution: ( [ { ( [ ] ) ( [ ] ) } ] ) ( ( [ { } { } [ ] ) ) ) When you read an opening bracket-symbol, push it onto the stack, when you read a closing bracket-symbol, check whether it s the top-symbol, and pop it.
38 Pushing and popping again A letter means enqueue and an asterisk means dequeue in the sequence E A S * Y * Q U E * * * S T * * * I O * N * * * Give the sequence of values returned by the dequeue operations when this sequence of operations is performed on an initially empty queue. Solution: E A S Y Q U E S T I O N
39 Pushing and popping again A letter means enqueue and an asterisk means dequeue in the sequence E A S * Y * Q U E * * * S T * * * I O * N * * * Give the sequence of values returned by the dequeue operations when this sequence of operations is performed on an initially empty queue. Solution: E A S Y Q U E S T I O N
40 A queue via two stacks Can we implement a queue using two stacks in an efficient way?! Solution: the basic idea is = + 1 Use two stacks, IN and OUT. 2 The queue is empty iff both stacks are empty. 3 push(x) : IN.push(x). 4 front(): if OUT is not-empty, perform OUT.top(), otherwise pop everything from IN, push it to OUT, and then perform OUT.top(). 5 pop(): The same as with front, only replacing OUT.top() with OUT.pop().
41 A queue via two stacks Can we implement a queue using two stacks in an efficient way?! Solution: the basic idea is = + 1 Use two stacks, IN and OUT. 2 The queue is empty iff both stacks are empty. 3 push(x) : IN.push(x). 4 front(): if OUT is not-empty, perform OUT.top(), otherwise pop everything from IN, push it to OUT, and then perform OUT.top(). 5 pop(): The same as with front, only replacing OUT.top() with OUT.pop().
1 P age DS & OOPS / UNIT II
UNIT II Stacks: Definition operations - applications of stack. Queues: Definition - operations Priority queues - De que Applications of queue. Linked List: Singly Linked List, Doubly Linked List, Circular
More informationCS 270 Algorithms. Oliver Kullmann. Binary search. Lists. Background: Pointers. Trees. Implementing rooted trees. Tutorial
Week 7 General remarks Arrays, lists, pointers and 1 2 3 We conclude elementary data structures by discussing and implementing arrays, lists, and trees. Background information on pointers is provided (for
More informationLIFO : Last In First Out
Introduction Stack is an ordered list in which all insertions and deletions are made at one end, called the top. Stack is a data structure that is particularly useful in applications involving reversing.
More information3. Fundamental Data Structures
3. Fundamental Data Structures CH08-320201: Algorithms and Data Structures 233 Data Structures Definition (recall): A data structure is a way to store and organize data in order to facilitate access and
More informationCSC148H Week 3. Sadia Sharmin. May 24, /20
CSC148H Week 3 Sadia Sharmin May 24, 2017 1/20 Client vs. Developer I For the first couple of weeks, we have played the role of class designer I However, you are also often in the opposite role: when a
More informationLists, Stacks, and Queues. (Lists, Stacks, and Queues ) Data Structures and Programming Spring / 50
Lists, Stacks, and Queues (Lists, Stacks, and Queues ) Data Structures and Programming Spring 2016 1 / 50 Abstract Data Types (ADT) Data type a set of objects + a set of operations Example: integer set
More informationSummer Final Exam Review Session August 5, 2009
15-111 Summer 2 2009 Final Exam Review Session August 5, 2009 Exam Notes The exam is from 10:30 to 1:30 PM in Wean Hall 5419A. The exam will be primarily conceptual. The major emphasis is on understanding
More informationWeek 8. BinaryTrees. 1 Binary trees. 2 The notion of binary search tree. 3 Tree traversal. 4 Queries in binary search trees. 5 Insertion.
Week 8 Binarys 1 2 of 3 4 of 5 6 7 General remarks We consider, another important data structure. We learn how to use them, by making efficient queries. And we learn how to build them. Reading from CLRS
More informationWeek 8. BinaryTrees. 1 Binary trees. 2 The notion of binary search tree. 3 Tree traversal. 4 Queries in binary search trees. 5 Insertion.
Week 8 Binarys 1 2 3 4 5 6 7 General remarks We consider, another important data structure. We learn how to use them, by making efficient queries. And we learn how to build them. Reading from CLRS for
More informationComputer Science 9608 (Notes) Chapter: 4.1 Computational thinking and problem-solving
In Computer Science, an abstract data type (ADT) is a mathematical model for a certain data type or structures. This doesn t mean that the ADTs cannot be programmed. But that we must first understand them
More informationBasic Data Structures (Version 7) Name:
Prerequisite Concepts for Analysis of Algorithms Basic Data Structures (Version 7) Name: Email: Concept: mathematics notation 1. log 2 n is: Code: 21481 (A) o(log 10 n) (B) ω(log 10 n) (C) Θ(log 10 n)
More informationCSC148 Week 2. Larry Zhang
CSC148 Week 2 Larry Zhang 1 Admin Discussion board is up (link on the course website). 2 Outline for this week Abstract Data Type Stack Queue 3 Abstract Data Type (ADT) 4 What is an abstract data type
More informationStacks and queues (chapters 6.6, 15.1, 15.5)
Stacks and queues (chapters 6.6, 15.1, 15.5) So far... Complexity analysis For recursive and iterative programs Sorting algorithms Insertion, selection, quick, merge, (intro, dual-pivot quick, natural
More informationCSCA48 Summer 2018 Week 3: Priority Queue, Linked Lists. Marzieh Ahmadzadeh University of Toronto Scarborough
CSCA48 Summer 2018 Week 3: Priority Queue, Linked Lists Marzieh Ahmadzadeh University of Toronto Scarborough Administrative Detail All practicals are up on course website. Term test # 1 and #2 schedule
More information811312A Data Structures and Algorithms, , Exercise 1 Solutions
811312A Data Structures and Algorithms, 2018-2019, Exercise 1 Solutions Topics of this exercise are stacks, queues, and s. Cormen 3 rd edition, chapter 10. Task 1.1 Assume that L is a containing 1000 items.
More informationCMSC Introduction to Algorithms Spring 2012 Lecture 7
CMSC 351 - Introduction to Algorithms Spring 2012 Lecture 7 Instructor: MohammadTaghi Hajiaghayi Scribe: Rajesh Chitnis 1 Introduction In this lecture we give an introduction to Data Structures like arrays,
More informationDATA STRUCTURE UNIT I
DATA STRUCTURE UNIT I 1. What is Data Structure? A data structure is a mathematical or logical way of organizing data in the memory that consider not only the items stored but also the relationship to
More informationdata structures and algorithms lecture 6
data structures and algorithms 2017 09 21 lecture 6 overview lower bound counting sort radix sort stacks queues material question we have seen for example insertion sort: worst-case in O(n 2 ) merge sort:
More information/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17
01.433/33 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/2/1.1 Introduction In this lecture we ll talk about a useful abstraction, priority queues, which are
More informationCS301 - Data Structures Glossary By
CS301 - Data Structures Glossary By Abstract Data Type : A set of data values and associated operations that are precisely specified independent of any particular implementation. Also known as ADT Algorithm
More informationINFO1x05 Tutorial 6. Exercise 1: Heaps and Priority Queues
INFO1x05 Tutorial 6 Heaps and Priority Queues Exercise 1: 1. How long would it take to remove the log n smallest elements from a heap that contains n entries, using the operation? 2. Suppose you label
More informationCS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University
CS 11 Introduction to Computing II Wayne Snyder Department Boston University Today Object-Oriented Programming Concluded Stacks, Queues, and Priority Queues as Abstract Data Types Reference types: Basic
More informationDepartment of Computer Science and Technology
UNIT : Stack & Queue Short Questions 1 1 1 1 1 1 1 1 20) 2 What is the difference between Data and Information? Define Data, Information, and Data Structure. List the primitive data structure. List the
More informationCH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES
CH4.2-4.3. ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN JAVA, GOODRICH, TAMASSIA AND GOLDWASSER
More informationThe smallest element is the first one removed. (You could also define a largest-first-out priority queue)
Priority Queues Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-first-out The smallest element is the first one removed (You could also define a largest-first-out
More informationUnit 8: Analysis of Algorithms 1: Searching
P Computer Science Unit 8: nalysis of lgorithms 1: Searching Topics: I. Sigma and Big-O notation II. Linear Search III. Binary Search Materials: I. Rawlins 1.6 II. Rawlins 2.1 III. Rawlins 2.3 IV. Sigma
More information16. Dynamic Data Structures
Data Structures 6. Dynamic Data Structures A data structure is a particular way of organizing data in a computer so that it can be used efficiently Linked lists, Abstract data types stack, queue, Sorted
More informationcsci 210: Data Structures Stacks and Queues
csci 210: Data Structures Stacks and Queues 1 Summary Topics Stacks and Queues as abstract data types ( ADT) Implementations arrays linked lists Analysis and comparison Applications: searching with stacks
More informationStandard ADTs. Lecture 19 CS2110 Summer 2009
Standard ADTs Lecture 19 CS2110 Summer 2009 Past Java Collections Framework How to use a few interfaces and implementations of abstract data types: Collection List Set Iterator Comparable Comparator 2
More information.:: UNIT 4 ::. STACK AND QUEUE
.:: UNIT 4 ::. STACK AND QUEUE 4.1 A stack is a data structure that supports: Push(x) Insert x to the top element in stack Pop Remove the top item from stack A stack is collection of data item arrange
More informationCSCA48 Winter 2018 Week 3: Priority Queue, Linked Lists. Marzieh Ahmadzadeh, Nick Cheng University of Toronto Scarborough
CSCA48 Winter 2018 Week 3: Priority Queue, Linked Lists Marzieh Ahmadzadeh, Nick Cheng University of Toronto Scarborough Administrative Detail Term test # 1 and #2 schedule is now on course website We
More informationData Structures Question Bank Multiple Choice
Section 1. Fundamentals: Complexity, Algorthm Analysis 1. An algorithm solves A single problem or function Multiple problems or functions Has a single programming language implementation 2. A solution
More informationIII Data Structures. Dynamic sets
III Data Structures Elementary Data Structures Hash Tables Binary Search Trees Red-Black Trees Dynamic sets Sets are fundamental to computer science Algorithms may require several different types of operations
More informationDiscussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:
Discussion 2C Notes (Week 3, January 21) TA: Brian Choi (schoi@cs.ucla.edu) Section Webpage: http://www.cs.ucla.edu/~schoi/cs32 Abstraction In Homework 1, you were asked to build a class called Bag. Let
More informationLecture 3: Stacks & Queues
Lecture 3: Stacks & Queues Prakash Gautam https://prakashgautam.com.np/dipit02/ info@prakashgautam.com.np 22 March, 2018 Objectives Definition: Stacks & Queues Operations of Stack & Queues Implementation
More informationStacks. stacks of dishes or trays in a cafeteria. Last In First Out discipline (LIFO)
Outline stacks stack ADT method signatures array stack implementation linked stack implementation stack applications infix, prefix, and postfix expressions 1 Stacks stacks of dishes or trays in a cafeteria
More informationAlgorithms and Data Structures
Algorithms and Data Structures PD Dr. rer. nat. habil. Ralf Peter Mundani Computation in Engineering / BGU Scientific Computing in Computer Science / INF Summer Term 2018 Part 2: Data Structures PD Dr.
More informationStacks, Queues and Hierarchical Collections
Programming III Stacks, Queues and Hierarchical Collections 2501ICT Nathan Contents Linked Data Structures Revisited Stacks Queues Trees Binary Trees Generic Trees Implementations 2 Copyright 2002- by
More information1.00 Lecture 26. Data Structures: Introduction Stacks. Reading for next time: Big Java: Data Structures
1.00 Lecture 26 Data Structures: Introduction Stacks Reading for next time: Big Java: 19.1-19.3 Data Structures Set of primitives used in algorithms, simulations, operating systems, applications to: Store
More information3. Priority Queues. ADT Stack : LIFO. ADT Queue : FIFO. ADT Priority Queue : pick the element with the lowest (or highest) priority.
3. Priority Queues 3. Priority Queues ADT Stack : LIFO. ADT Queue : FIFO. ADT Priority Queue : pick the element with the lowest (or highest) priority. Malek Mouhoub, CS340 Winter 2007 1 3. Priority Queues
More informationFun facts about recursion
Outline examples of recursion principles of recursion review: recursive linked list methods binary search more examples of recursion problem solving using recursion 1 Fun facts about recursion every loop
More informationLinked Lists, Stacks, and Queues
Department of Computer Science and Engineering Chinese University of Hong Kong In a nutshell, a data structure describes how data are stored in memory, in order to facilitate certain operations. In all
More informationData Structures and Algorithms for Engineers
04-630 Data Structures and Algorithms for Engineers David Vernon Carnegie Mellon University Africa vernon@cmu.edu www.vernon.eu Data Structures and Algorithms for Engineers 1 Carnegie Mellon University
More informationWeek 10. Sorting. 1 Binary heaps. 2 Heapification. 3 Building a heap 4 HEAP-SORT. 5 Priority queues 6 QUICK-SORT. 7 Analysing QUICK-SORT.
Week 10 1 Binary s 2 3 4 5 6 Sorting Binary s 7 8 General remarks Binary s We return to sorting, considering and. Reading from CLRS for week 7 1 Chapter 6, Sections 6.1-6.5. 2 Chapter 7, Sections 7.1,
More informationMidterm Exam (REGULAR SECTION)
Data Structures (CS 102), Professor Yap Fall 2014 Midterm Exam (REGULAR SECTION) October 28, 2014 Midterm Exam Instructions MY NAME:... MY NYU ID:... MY EMAIL:... Please read carefully: 0. Do all questions.
More informationCSE373: Data Structures & Algorithms Lecture 9: Priority Queues. Aaron Bauer Winter 2014
CSE373: Data Structures & Algorithms Lecture 9: Priority Queues Aaron Bauer Winter 2014 Midterm On Wednesday, in class Closed book Closed note Closed electronic devices Closed classmate Covers everything
More informationMotivation for Queues
CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 178] Motivation for Queues Some examples of first-in, first-out (FIFO) behavior: æ waiting in line to check out at a store æ cars on
More informationAssociate Professor Dr. Raed Ibraheem Hamed
Associate Professor Dr. Raed Ibraheem Hamed University of Human Development, College of Science and Technology Computer Science Department 2015 2016 1 What this Lecture is about: Stack Structure Stack
More informationAbstract vs concrete data structures HEAPS AND PRIORITY QUEUES. Abstract vs concrete data structures. Concrete Data Types. Concrete data structures
10/1/17 Abstract vs concrete data structures 2 Abstract data structures are interfaces they specify only interface (method names and specs) not implementation (method bodies, fields, ) HEAPS AND PRIORITY
More informationDesign and Analysis of Algorithms
CSE 1, Winter 201 Design and Analysis of Algorithms Lecture 7: Bellman-Ford, SPs in DAGs, PQs Class URL: http://vlsicad.ucsd.edu/courses/cse1-w1/ Lec. Added after class Figure.: Single-Edge Extensions
More informationQueues. Lesson 4. CS 32: Data Structures Dept. of Computer Science
Queues Lesson 4 Outline What is a queue? Straight queue Circular Queue Sequential Implementation Linked Implementation Application: Topological sort Deques Final Notes Outline What is a queue? Straight
More informationStacks and Queues. CSE Data Structures April 12, 2002
Stacks and Queues CSE 373 - Data Structures April 12, 2002 Readings and References Reading Section 3.3 and 3.4, Data Structures and Algorithm Analysis in C, Weiss Other References 12-Apr-02 CSE 373 - Data
More informationCpt S 122 Data Structures. Course Review Midterm Exam # 1
Cpt S 122 Data Structures Course Review Midterm Exam # 1 Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Midterm Exam 1 When: Friday (09/28) 12:10-1pm Where:
More informationLecture 9 Stacks & Queues
Lecture 9 Stacks & Queues 15-122: Principles of Imperative Computation (Fall 2018) Frank Pfenning, André Platzer, Rob Simmons In this lecture we introduce queues and stacks as data structures, e.g., for
More informationWeek 10. Sorting. 1 Binary heaps. 2 Heapification. 3 Building a heap 4 HEAP-SORT. 5 Priority queues 6 QUICK-SORT. 7 Analysing QUICK-SORT.
Week 10 1 2 3 4 5 6 Sorting 7 8 General remarks We return to sorting, considering and. Reading from CLRS for week 7 1 Chapter 6, Sections 6.1-6.5. 2 Chapter 7, Sections 7.1, 7.2. Discover the properties
More informationKeeping Order:! Stacks, Queues, & Deques. Travis W. Peters Dartmouth College - CS 10
Keeping Order:! Stacks, Queues, & Deques 1 Stacks 2 Stacks A stack is a last in, first out (LIFO) data structure Primary Operations: push() add item to top pop() return the top item and remove it peek()
More informationLinked List. April 2, 2007 Programming and Data Structure 1
Linked List April 2, 2007 Programming and Data Structure 1 Introduction head A linked list is a data structure which can change during execution. Successive elements are connected by pointers. Last element
More informationDay 6. COMP1006/1406 Summer M. Jason Hinek Carleton University
Day 6 COMP1006/1406 Summer 2016 M. Jason Hinek Carleton University today s agenda assignments Assignment 3 is due on Monday a quick look back abstract classes and interfaces casting objects abstract data
More informationWeek 4 Stacks & Queues
CPSC 319 Week 4 Stacks & Queues Xiaoyang Liu xiaoyali@ucalgary.ca Stacks and Queues Fundamental data types. Value: collection of objects. Operations: insert, remove, iterate, test if empty. Intent is clear
More informationMULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR
STUDENT IDENTIFICATION NO MULTIMEDIA COLLEGE JALAN GURNEY KIRI 54100 KUALA LUMPUR FIFTH SEMESTER FINAL EXAMINATION, 2014/2015 SESSION PSD2023 ALGORITHM & DATA STRUCTURE DSEW-E-F-2/13 25 MAY 2015 9.00 AM
More informationRevision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted,
[1] Big-O Analysis AVERAGE(n) 1. sum 0 2. i 0. while i < n 4. number input_number(). sum sum + number 6. i i + 1 7. mean sum / n 8. return mean Revision Statement no. of times executed 1 1 2 1 n+1 4 n
More informationOperations on Heap Tree The major operations required to be performed on a heap tree are Insertion, Deletion, and Merging.
Priority Queue, Heap and Heap Sort In this time, we will study Priority queue, heap and heap sort. Heap is a data structure, which permits one to insert elements into a set and also to find the largest
More informationCSE 373 SEPTEMBER 29 STACKS AND QUEUES
CSE 373 SEPTEMBER 29 STACKS AND QUEUES DESIGN DECISIONS Shopping list? DESIGN DECISIONS Shopping list? What sorts of behavior do shoppers exhibit? What constraints are there on a shopper? What improvements
More informationLecture 15 Binary Search Trees
Lecture 15 Binary Search Trees 15-122: Principles of Imperative Computation (Fall 2017) Frank Pfenning, André Platzer, Rob Simmons, Iliano Cervesato In this lecture, we will continue considering ways to
More informationDATA STRUCTURES AND ALGORITHMS
LECTURE 8 Babeş - Bolyai University Computer Science and Mathematics Faculty 2017-2018 In Lecture 7... ADT Queue ADT Matrix ADT List ADT Stack Today ADT Queue 1 ADT Queue 2 3 4 Note ADT Queue We will not
More informationLecture 3 Linear Data Structures: Arrays, Array Lists, Stacks, Queues and Linked Lists
Lecture 3 Linear Data Structures: Arrays, Array Lists, Stacks, Queues and Linked Lists Chapters 3.1-3.3, 5.1-5.2, 6.1-1 - Core Collection Interfaces - 2 - The Java Collections Framework Interface Abstract
More informationDATA STRUCUTRES. A data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently.
DATA STRUCUTRES A data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently. An algorithm, which is a finite sequence of instructions, each of which
More informationCS 206 Introduction to Computer Science II
CS 206 Introduction to Computer Science II 07 / 26 / 2016 Instructor: Michael Eckmann Today s Topics Comments/Questions? Stacks and Queues Applications of both Priority Queues Michael Eckmann - Skidmore
More informationPRIORITY QUEUES AND HEAPS
PRIORITY QUEUES AND HEAPS Lecture 17 CS2110 Spring 201 Readings and Homework 2 Read Chapter 2 A Heap Implementation to learn about heaps Exercise: Salespeople often make matrices that show all the great
More informationThe Stack ADT. Stacks. The Stack ADT. The Stack ADT. Set of objects in which the location an item is inserted and deleted is prespecified.
The Stack ADT Stacks Set of objects in which the location an item is inserted and deleted is prespecified Stacks! Insert in order! Delete most recent item inserted! LIFO - last in, first out Stacks 2 The
More informationCS 221 Review. Mason Vail
CS 221 Review Mason Vail Inheritance (1) Every class - except the Object class - directly inherits from one parent class. Object is the only class with no parent. If a class does not declare a parent using
More informationCS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University
CS 112 Introduction to Computing II Wayne Snyder Department Boston University Today Introduction to Linked Lists Stacks and Queues using Linked Lists Next Time Iterative Algorithms on Linked Lists Reading:
More informationAlgorithms and Data Structures
Algorithms and Data Structures CMPSC 465 LECTURE 10 Abstract Data Types Queues Adam Smith 1 Data Structures So far in this class: designing algorithms Inputs and outputs were specified, we wanted to design
More informationAnnouncements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop
Announcements Lab Friday, 1-2:30 and 3-4:30 in 26-152 Boot your laptop and start Forte, if you brought your laptop Create an empty file called Lecture4 and create an empty main() method in a class: 1.00
More informationLinear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013
Linear Structure Linear Structures Chapter 4 CPTR 318 Every non-empty linear structure has A unique element called first A unique element called last Every element except last has a unique successor Every
More informationProgramming, Data Structures and Algorithms Prof. Hema Murthy Department of Computer Science and Engineering Indian Institute of Technology, Madras
Programming, Data Structures and Algorithms Prof. Hema Murthy Department of Computer Science and Engineering Indian Institute of Technology, Madras Module 06 Lecture - 46 Stacks: Last in first out Operations:
More informationStacks, Queues and Hierarchical Collections. 2501ICT Logan
Stacks, Queues and Hierarchical Collections 2501ICT Logan Contents Linked Data Structures Revisited Stacks Queues Trees Binary Trees Generic Trees Implementations 2 Queues and Stacks Queues and Stacks
More informationCSE 142 Su 04 Computer Programming 1 - Java. Objects
Objects Objects have state and behavior. State is maintained in instance variables which live as long as the object does. Behavior is implemented in methods, which can be called by other objects to request
More information12 Abstract Data Types
12 Abstract Data Types 12.1 Foundations of Computer Science Cengage Learning Objectives After studying this chapter, the student should be able to: Define the concept of an abstract data type (ADT). Define
More informationinfix expressions (review)
Outline infix, prefix, and postfix expressions queues queue interface queue applications queue implementation: array queue queue implementation: linked queue application of queues and stacks: data structure
More informationData Structures and Algorithms
Data Structures and Algorithms About the course (objectives, outline, recommended reading) Problem solving Notions of Algorithmics (growth of functions, efficiency, programming model, example analysis)
More informationAlgorithms and Data Structures
Algorithms and Data Structures Dr. Malek Mouhoub Department of Computer Science University of Regina Fall 2002 Malek Mouhoub, CS3620 Fall 2002 1 6. Priority Queues 6. Priority Queues ffl ADT Stack : LIFO.
More informationData Structures and Algorithms. Chapter 5. Dynamic Data Structures and Abstract Data Types
1 Data Structures and Algorithms Chapter 5 and Abstract Data Types Werner Nutt 2 Acknowledgments The course follows the book Introduction to Algorithms, by Cormen, Leiserson, Rivest and Stein, MIT Press
More information8. Fundamental Data Structures
172 8. Fundamental Data Structures Abstract data types stack, queue, implementation variants for linked lists, [Ottman/Widmayer, Kap. 1.5.1-1.5.2, Cormen et al, Kap. 10.1.-10.2] Abstract Data Types 173
More informationCSE 332: Data Structures & Parallelism Lecture 7: Dictionaries; Binary Search Trees. Ruth Anderson Winter 2019
CSE 332: Data Structures & Parallelism Lecture 7: Dictionaries; Binary Search Trees Ruth Anderson Winter 2019 Today Dictionaries Trees 1/23/2019 2 Where we are Studying the absolutely essential ADTs of
More informationADVANCED DATA STRUCTURES USING C++ ( MT-CSE-110 )
ADVANCED DATA STRUCTURES USING C++ ( MT-CSE-110 ) Unit - 2 By: Gurpreet Singh Dean Academics & H.O.D. (C.S.E. / I.T.) Yamuna Institute of Engineering & Technology, Gadholi What is a Stack? A stack is a
More informationData structure and algorithm in Python
Data structure and algorithm in Python Linked Lists Xiaoping Zhang School of Mathematics and Statistics, Wuhan University Table of contents 1. Singly Linked Lists 2. Circularly Linked Lists 3. Doubly Linked
More informationCMSC 132: Object-Oriented Programming II. Stack and Queue
CMSC 132: Object-Oriented Programming II Stack and Queue 1 Stack Allows access to only the last item inserted. An item is inserted or removed from the stack from one end called the top of the stack. This
More informationLecture 9 Stacks & Queues
Lecture 9 Stacks & Queues 15-122: Principles of Imperative Computation (Spring 2018) Frank Pfenning, André Platzer, Rob Simmons In this lecture we introduce queues and stacks as data structures, e.g.,
More informationData Structure. A way to store and organize data in order to support efficient insertions, queries, searches, updates, and deletions.
DATA STRUCTURES COMP 321 McGill University These slides are mainly compiled from the following resources. - Professor Jaehyun Park slides CS 97SI - Top-coder tutorials. - Programming Challenges book. Data
More informationCS171 Midterm Exam. October 29, Name:
CS171 Midterm Exam October 29, 2012 Name: You are to honor the Emory Honor Code. This is a closed-book and closed-notes exam. You have 50 minutes to complete this exam. Read each problem carefully, and
More informationLinear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013
Linear Structure Linear Structures Chapter 4 CPTR 318 Every non-empty linear structure has A unique element called first A unique element called last Every element except last has a unique successor Every
More informationChapter 5 Data Structures Algorithm Theory WS 2016/17 Fabian Kuhn
Chapter 5 Data Structures Algorithm Theory WS 06/ Fabian Kuhn Examples Dictionary: Operations: insert(key,value), delete(key), find(key) Implementations: Linked list: all operations take O(n) time (n:
More informationCS24 Week 4 Lecture 2
CS24 Week 4 Lecture 2 Kyle Dewey Overview Linked Lists Stacks Queues Linked Lists Linked Lists Idea: have each chunk (called a node) keep track of both a list element and another chunk Need to keep track
More informationStacks, Queues (cont d)
Stacks, Queues (cont d) CSE 2011 Winter 2007 February 1, 2007 1 The Adapter Pattern Using methods of one class to implement methods of another class Example: using List to implement Stack and Queue 2 1
More informationDepartment of Computer Science and Engineering. CSE 2011: Fundamentals of Data Structures Winter 2009, Section Z
Department of omputer Science and Engineering SE 2011: Fundamentals of Data Structures Winter 2009, Section Z Instructor: N. Vlajic Date: pril 14, 2009 Midterm Examination Instructions: Examination time:
More informationImplementing a Queue with a Linked List
CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 192] Implementing a Queue with a Linked List State representation: æ Data items are kept in a linked list. æ Pointer head points to
More informationApply to be a Meiklejohn! tinyurl.com/meikapply
Apply to be a Meiklejohn! tinyurl.com/meikapply Seeking a community to discuss the intersections of CS and positive social change? Interested in facilitating collaborations between students and non-profit
More informationData Structure Advanced
Data Structure Advanced 1. Is it possible to find a loop in a Linked list? a. Possilbe at O(n) b. Not possible c. Possible at O(n^2) only d. Depends on the position of loop Solution: a. Possible at O(n)
More informationUniversity of Waterloo Department of Electrical and Computer Engineering ECE 250 Algorithms and Data Structures
University of Waterloo Department of Electrical and Computer Engineering ECE 250 Algorithms and Data Structures Final Examination (17 pages) Instructor: Douglas Harder April 14, 2004 9:00-12:00 Name (last,
More information