Presentation on theme: "Data Structures and Algorithms (60-254)"— Presentation transcript:
1 Data Structures and Algorithms (60-254) Linear ListsData Structures and Algorithms (60-254)
2 TopicsStacksQueuesLinked Lists (Singly and Doubly Linked Lists)
3 Stacks A stack is a LIFO (Last In First Out) list of elements. Operations allowed in a stack:Addition (Push)Deletion (Pop)Inspect TopAllowed 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.
4 GraphicallyAdditions and Deletions at the stacktop
5 Balanced Parentheses Checking Given a string ofOpen “(” parentheses, andClosed “)” 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 ()swhere s isa string of balanced parentheses, orthe empty string.
6 Examples Examples of strings of balanced parentheses: and many more… ()()()()()()()()((((((((())))))))(()((()()())(())))and many more…Examples of strings of non-balanced parentheses:)(((()()())))(((((((()))))))()()())()()()
7 GoalWe 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 bracketPush it onto the stack.If character is closing bracket, and stack is emptyThe string is not balanced.OtherwisePop the opening bracket from stack.If scanning ends and stack is not empty
8 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.
9 Postfix Arithmetic Expression Evaluation Three ways of writing arithmetic expressions:Prefix: + a bOperands are preceded by operator.Infix: a + bUsual way of writing:Operand, then Operator, and finally Operand.Postfix: a b +Operands are followed by operator.
10 Examples of Arithmetic Expressions Infix Postfixa + 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.
11 Postfix Expression Evaluation Algorithm is simple:Scans characters in given expression from left to right.If character is an operandPush it onto stackIf character is an operatorPop two operands from stack.Apply operator to these two operands.Push the result onto stack.
12 ExampleGiven 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
13 Another ExampleGiven 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
14 Implementation of a Stack Using a variety of data structures …available in programming language used.Possible data structures:ArrayLinked ListComplexity of stack operations:Independent of data structure and programming language.Push and Pop can be done in O(1) timeArray-based implementation:Array’s size needs to be pre-determined.Specify a maximum for the size, say N = 1,000Our stack is an N-element array, A.
15 Array Implementation The stack represented by elements from 0 … t Pop an element:if t 0element A[t]t t – 1elsestack is empty
16 Array ImplementationPush an element: if t < N - 1 t t + 1 A[t] element else Stack is full
17 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 = 1234Output is m = 4321An algorithm for this problem?
18 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
19 Examplen = 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 Pop 2, m 2 * = 21 Pop 3, m 3 * = 321 Pop 4, m 4 * = 43211234
20 Time Complexity of Algorithm ReverseDigit Assumption: Let k be the number of digits in the value nOperators div, mod, + and * take constant time, O(1)Number of steps of first while loop:2 * kNumber of steps of second while loop:3 * kAlso, k = log10 nBut, we know that log10 n = log2 n / log2 10, and log2 10 3.32Then,
21 Queues A queue is a FIFO (First In First Out) list of elements. It is a close “cousin” of the stackAddition 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:
22 Real-life examples of queues A queue at a bus-stopA line at a bank machineA printer queueFood 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?
23 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.StartFinish
24 Using a queueFrom 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, andsaves positions at the back of the queue.Then, it takes all positions (coordinates) from the front of queue, andAssuming this position is labeled i,It marks all positions it can visit with i + 1It continues until it visits the FINISH square.
25 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
26 GraphicallyTraversal of example mesh looks like: This shows the worst case; among all squares labeled 5, the one adjacent to square FINISH is examined last.Start12345Finish6
27 Array Implementations of a Queue Some problems arise in implementing a queue when using an arrayLet’s see a solution (Fixed Front Approach):Fix the front of the queue at 0Let the back be movable.9516-132-71234678frontback
28 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..A[back-1] one position to the left. Set back to back - 1 Quite inefficient!!!
29 Moveable Front Approach Can we do better?Yes… Make the front of the queue movable as well.9516-132-71234678frontback
30 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…
31 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!
32 Can we do even better? Circular array based Yes… ImplementA “circular” queue,or a “circular” array.We will have two configurations…Normal configuration: front < back9516-132-71234678frontback
33 Circular Queue “Wrapped around” configuration: front > back -1 32 -795161234678backfront
34 ImplementationImplementation 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
35 Implementation To dequeue: If (queue not empty) Increment front as: (front + 1) mod NQueue empty? …when back = frontNow, unused cells will be:at the extremes of the array, orin the middle portion of the array.
36 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.
37 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.
38 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 lastEach node has two links:next element,previous element
39 Inserting an elementFind 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 …
40 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
41 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.
42 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.
43 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)
44 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 nodeWhat does the algorithm look like?Again, we can start from first, or can start from lastHere is the algorithm starting from first:
45 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”
46 Example: Delete 16 Observations: How does it work starting from last? Homework …What is time complexity of DeleteElement? …
47 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.