Data Structures and Algorithms (60-254)

Slides:



Advertisements
Similar presentations
Data Structures Through C
Advertisements

CHP-5 LinkedList.
Stack & Queues COP 3502.
§3 The Stack ADT 1. ADT A stack is a Last-In-First-Out (LIFO) list, that is, an ordered list in which insertions and deletions are.
Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Stacks - 3 Nour El-Kadri CSI Evaluating arithmetic expressions Stack-based algorithms are used for syntactical analysis (parsing). For example.
COSC 2006 Chapter 7 Stacks III
More on Stacks and Queues. As we mentioned before, two common introductory Abstract Data Type (ADT) that worth studying are Stack and Queue Many problems.
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title : Overview of Stack.
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
 Abstract Data Type Abstract Data Type  What is the difference? What is the difference?  Stacks Stacks  Stack operations Stack operations  Parsing.
Topic 15 Implementing and Using Stacks
Stacks Chapter 5. Chapter 5: Stacks2 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop, peek, and empty.
CS 206 Introduction to Computer Science II 03 / 06 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 17 / 2008 Instructor: Michael Eckmann.
Infix, Postfix, Prefix.
Lecture 4 Abstract Data Types (ADT). Usual Data Types Integer, Double, Boolean You do not know the implementation of these. There are some operations.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
CS 206 Introduction to Computer Science II 10 / 15 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
Transforming Infix to Postfix
Stack: Linked List Implementation Push and pop at the head of the list New nodes should be inserted at the front of the list, so that they become the top.
Chapter 6 Stacks. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-2 Chapter Objectives Examine stack processing Define a stack abstract.
Implementing and Using Stacks
Topic 15 Implementing and Using Stacks
Lecture 4 Abstract Data Types (ADT). Usual Data Types Integer, Double, Boolean You do not know the implementation of these. There are some operations.
CS Data Structures Chapter 3 Stacks and Queues.
CS 206 Introduction to Computer Science II 10 / 28 / 2009 Instructor: Michael Eckmann.
Chapter 16 Stacks and Queues Saurav Karmakar Spring 2007.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
Objectives of these slides:
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 5: Stacks and Queues.
Fundamentals of Python: From First Programs Through Data Structures Chapter 14 Linear Collections: Stacks.
Comp 245 Data Structures Stacks. What is a Stack? A LIFO (last in, first out) structure Access (storage or retrieval) may only take place at the TOP NO.
Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy.
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Chapter 4 Stacks Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving. Its called.
Computer Science Department Data Structure & Algorithms Problem Solving with Stack.
Data Structures Week 4 Our First Data Structure The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
ELEMENTARY DATA STRUCTURES Stacks, Queues, and Linked Lists.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture6.
For more notes and topics VISIT: IMPLEMENTATION OF STACKS eITnotes.com.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues (part 2)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
CHP-3 STACKS.
Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure.
3/3/20161 Stacks and Queues Introduction to Data Structures Ananda Gunawardena.
Lecture 10 b Stacks b Queues. 2 Stacks b A stack ADT is linear b Items are added and removed from only one end of a stack b It is therefore LIFO: Last-In,
CC 215 DATA STRUCTURES MORE ABOUT STACK APPLICATIONS Dr. Manal Helal - Fall 2014 Lecture 6 AASTMT Engineering and Technology College 1.
Applications of Stack Maitrayee Mukerji. Stacks Last In First Out (LIFO List) ◦ FILO? Insertions and Deletions from the same end called the Top Push(),
 In general, Queue is line of person waiting for their turn at some service counter like ticket window at cinema hall, at bus stand or at railway station.
1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
 Chapter 7 introduces the stack data type.  Several example applications of stacks are given in that chapter.  This presentation shows another use called.
Chapter 15 Lists Objectives
MEMORY REPRESENTATION OF STACKS
DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING IN C++
Objectives In this lesson, you will learn to: Define stacks
Stacks and Queues.
Data Structures Interview / VIVA Questions and Answers
PART II STACK APPLICATIONS
CMSC 341 Lecture 5 Stacks, Queues
The List, Stack, and Queue ADTs
Data Structures and Algorithms for Information Processing
Topic 15 Implementing and Using Stacks
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
LINEAR DATA STRUCTURES
Presentation transcript:

Data Structures and Algorithms (60-254) Linear Lists Data Structures and Algorithms (60-254)

Topics Stacks Queues Linked Lists (Singly and Doubly Linked Lists)

Stacks A stack is a LIFO (Last In First Out) list of elements. Operations allowed in a stack: Addition (Push) Deletion (Pop) Inspect Top Allowed at the end of the list, known as stacktop. Real-life examples of stacks are very common. Here are a few: A stack of dishes, A stack of paper, A stack of books.

Graphically Additions and Deletions at the stacktop

Balanced Parentheses Checking Given a string of Open “(” parentheses, and Closed “)” parentheses. An interesting problem: To design an algorithm that checks if balanced. When is a string of parentheses balanced?   If it is either of the form: (s) s() or ()s where s is a string of balanced parentheses, or the empty string.

Examples Examples of strings of balanced parentheses: and many more… () ()()()()()()() ((((((((()))))))) (()((()()())(()))) and many more…   Examples of strings of non-balanced parentheses: )( ((()()()))) (((((((())))))) ()()())()()()

Goal We want to design a stack-based algorithm that checks if a string of parentheses is balanced. How does it work? Scan the string from left to right. If character is opening bracket Push it onto the stack. If character is closing bracket, and stack is empty The string is not balanced. Otherwise Pop the opening bracket from stack. If scanning ends and stack is not empty

Example The string ((()(())()) is not balanced. An opening bracket will be left on the stack.   The string )() is not balanced either. A closing bracket found and stack is empty. The string (()) is balanced. Both scanning will end and stack will be empty.

Postfix Arithmetic Expression Evaluation Three ways of writing arithmetic expressions: Prefix: + a b Operands are preceded by operator. Infix: a + b Usual way of writing: Operand, then Operator, and finally Operand. Postfix: a b + Operands are followed by operator.

Examples of Arithmetic Expressions Infix Postfix a + b a b + a + b * c a b c * + (a + b) * c a b + c *   Advantages of infix form: Can use brackets to change operator precedence. Stacks lead to an interesting algorithm to evaluate arithmetic expressions in postfix form.

Postfix Expression Evaluation Algorithm is simple: Scans characters in given expression from left to right.   If character is an operand Push it onto stack If character is an operator Pop two operands from stack. Apply operator to these two operands. Push the result onto stack.

Example Given postfix expression: a b c * + Push a Push b Push c Pop c Pop b d = b * c Push d Pop d Pop a e = a + d Push e

Another Example Given postfix arithmetic expression: a b + c * Push a Push b Pop b Pop a d = a + b Push d Push c Pop c Pop d e = c * d Push e

Implementation of a Stack Using a variety of data structures … available in programming language used. Possible data structures: Array Linked List Complexity of stack operations: Independent of data structure and programming language. Push and Pop can be done in O(1) time Array-based implementation: Array’s size needs to be pre-determined. Specify a maximum for the size, say N = 1,000 Our stack is an N-element array, A.

Array Implementation The stack represented by elements from 0 … t   Pop an element: if t  0 element  A[t] t  t – 1 else stack is empty

Array Implementation Push an element: if t < N - 1 t  t + 1 A[t]  element else Stack is full

Reversing Digits of a Number A very interesting problem. It involves the use of stacks.   Given a non-negative integer, n. Want to output a number m … whose digits are the reverse of those of n. For example: Input is n = 1234 Output is m = 4321 An algorithm for this problem?

Algorithm ReverseDigit Input: Non-negative integer, n Output: Non-negative integer, m, whose digits are the reverse of digits of n. While (n > 0) Push n mod 10 to stack n  n div 10 m  0; i  1 while (stack not empty) Pop d from stack m  d * i + m i  i * 10 Print m and STOP

Example n = 1234 Push 1234 mod 10 = 4, n  1234 div 10 = 123 Push 123 mod 10 = 3, n  123 div 10 = 12 Push 12 mod 10 = 2, n  12 div 10 = 1 Push 1 mod 10 = 1, n  1 div 10 = 0 Stack = Pop 1, m  1 * 1 + 0 = 1 Pop 2, m  2 * 10 + 1 = 21 Pop 3, m  3 * 100 + 21 = 321 Pop 4, m  4 * 1000 + 321 = 4321 1 2 3 4

Time Complexity of Algorithm ReverseDigit Assumption: Let k be the number of digits in the value n Operators div, mod, + and * take constant time, O(1) Number of steps of first while loop: 2 * k Number of steps of second while loop: 3 * k Also, k = log10 n But, we know that log10 n = log2 n / log2 10, and log2 10  3.32 Then,

Queues A queue is a FIFO (First In First Out) list of elements. It is a close “cousin” of the stack   Addition to this list (Enqueue) is done at one end, called the BACK of the queue. Deletion (Dequeue) is done at the other end, called the FRONT of the queue. Graphically:

Real-life examples of queues A queue at a bus-stop A line at a bank machine A printer queue Food for thought: A bank has three tellers. Would it be better to have a queue for each teller, or one queue that feeds all three tellers. How could you set up an experiment to test your hypothesis?

Application of Queues: Pathfinding A rat, started off at the START square of the maze. Has to find the shortest path to the FINISH square. Shaded squares are blocked off to the rat. Length of path is the number of squares the rat visits. Start   Finish

Using a queue From a given square, the rat can move left, right, up or down.   Assumption: “our” rat knows about the data structure. It does the following: It labels all the squares it can visit, from the START square being number 0, and saves positions at the back of the queue. Then, it takes all positions (coordinates) from the front of queue, and Assuming this position is labeled i, It marks all positions it can visit with i + 1 It continues until it visits the FINISH square.

Finding the shortest path It does this by moving backwards, as follows.   If the FINISH square would have been labeled k, It moves back to square labeled k – 1, then, to a square labeled k – 2, and so on, until it reaches the START square. Each move is possible, since to reach square k one would have to reach square k – 1

Graphically Traversal of example mesh looks like: This shows the worst case; among all squares labeled 5, the one adjacent to square FINISH is examined last. Start 1 2 3 4   5 Finish 6

Array Implementations of a Queue Some problems arise in implementing a queue when using an array Let’s see a solution (Fixed Front Approach): Fix the front of the queue at 0 Let the back be movable. 9 5 16 -1 32 -7   1 2 3 4 6 7 8 front back

Time complexity of enqueue and dequeue? Enqueue operation: Takes constant, O(1), time. Why? Very simple: Assign element to A[back] Set back to back + 1 = 7 Dequeue operation: Takes linear, O(n), time. Complicated: Shift elements A[1]..A[back-1] one position to the left. Set back to back - 1 Quite inefficient!!!

Moveable Front Approach Can we do better? Yes… Make the front of the queue movable as well.   9 5 16 -1 32 -7 1 2 3 4 6 7 8 front back

Time complexity of enqueue and dequeue? Enqueue operation: Takes constant, O(1), time. Why? Very simple: Assign element to A[back] Set back to back + 1 = 9 Dequeue operation: Takes constant, O(1), time !!! Why? Easy: Assign A[front] to element Set front to front + 1 Much more efficient, but… still a problem…

Problems We need to keep lots of unused positions: On the right of array … to enqueue.   And, will have lots of unused positions: on the left of the array … after dequeueing. Still BIG Problems: Lots of unused positions in the array. Queue may become full, and still unused positions on the left!

Can we do even better? Circular array based Yes… Implement A “circular” queue, or a “circular” array.   We will have two configurations… Normal configuration: front < back   9 5 16 -1 32 -7 1 2 3 4 6 7 8 front back

Circular Queue “Wrapped around” configuration: front > back -1 32 -7   9 5 16 1 2 3 4 6 7 8 back front

Implementation Implementation of operations: Simple How to enqueue? If (queue not full) Increment back as: (back + 1) mod N Queue full? … when (back + 1) mod n = front

Implementation To dequeue: If (queue not empty) Increment front as: (front + 1) mod N Queue empty? … when back = front   Now, unused cells will be: at the extremes of the array, or in the middle portion of the array.

Time complexity of enqueue and dequeue? Enqueueing: Constant time, O(1).   Dequeueing: Constant time, O(1), too!!! Note: full queue still has one wasted slot. It is possible to design this so no slots are wasted by keeping a separate count variable to distinguish empty and full.

Singly linked lists A typical example of a linked list: Every node can be accessed via first. Each node has a link to the next element.

Doubly linked Lists A typical example: In a doubly linked list, we have: Two “unused” nodes for first and last – Why?. Every node in List can be accessed: either via first or via last Each node has two links: next element, previous element

Inserting an element Find the node … after which element is to be inserted. Create a new node Set the new node’s element to element Establish the corresponding links: Set next of node and prev of next of node to new node We can start searching from first, or can start from last Here is the an algorithm starting from first …

Algorithm InsertElement Input: An element, e Create new node n’; n’.elem  e n  first while (n.next <> last and n.next.elem < e) n  n.next n.next.prev  n’ n’.next  n.next n.next  n’ n’.prev  n

Example: Insert 12 last Observations: How does it work starting from last? Homework… Other types of objects can be stored in the list. Examples: Strings, float’s, double’s, or even any Object.

Time complexity of Algorithm InsertElement While loop is executed at most n times, where n is the number of elements Thus, it takes O(n) to find and insert the element. Insertion only takes O(1) !!! What is time complexity if we started from last? Homework… What if we had an array instead? Find takes O(n) and then insert takes O(n) too. Also, an advantage of using a linked list: Array’s size is fixed, and list’s is not.

Advantage of using doubly linked lists If the list is sorted, and  want to find the maximum (or minimum) of the list: it takes O(n) in a linked list, and O(1) in a doubly linked list!   For any order of elements:  want to insert an element at the end: it takes O(n) in a linked list, whereas in a doubly linked list: O(1)

Deleting an element Find the node … containing element to be removed.   Just modify the corresponding links: Change next of previous of node and previous of next of node What does the algorithm look like? Again, we can start from first, or can start from last Here is the algorithm starting from first:

Algorithm DeleteElement Input: An element, e n  first.next while (n <> last and n.elem <> e) n  n.next if n <> last n.next.prev  n.prev n.prev.next  n.next else Print e + “is not in the list”

Example: Delete 16 Observations: How does it work starting from last? Homework …   What is time complexity of DeleteElement? …

Time complexity of DeleteElement Again, To find the element, it takes O(n) (the while loop in Algorithm DeleteElement) But, To delete the element, it takes O(1) In an array implementation: It takes O(n) just to delete the element!! Why? Homework: Write the algorithm that starts from last.