CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List, Stack and Queues Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th Apr-15 1 Stacks 4/13/2015 CSE221/ICT221 Analysis and Design of Algorithms 2 Stack Operation: Push 4/13/2015 CSE221/ICT221 Analysis and Design of Algorithms 3 Stack Operation: Pop 4/13/2015 CSE221/ICT221 Analysis and Design of Algorithms 4 Stack Operation: Top 4/13/2015 CSE221/ICT221 Analysis and Design of Algorithms 5 4/13/2015 CSE221/ICT221 Analysis and Design of Algorithms 6 Stacks Stack operation is LIFO (Last-In, First-Out) Basic operations of Stack - Adding an element to Stack (Push) - Removing an element from Stack (Pop) - Using an element of Stack (Top) Creating a Stack - using an array to represent a stack - using a Linked list to represent a Stack 4/13/2015 CSE221/ICT221 Analysis and Design of Algorithms 7 Stack represented by Linked list 4/13/2015 CSE221/ICT221 Analysis and Design of Algorithms 8 Stack represented by Linked list 4/13/2015 CSE221/ICT221 Analysis and Design of Algorithms 9 4/13/2015 CSE221/ICT221 Analysis and Design of Algorithms 10 Stack Operation: Push 4/13/2015 CSE221/ICT221 Analysis and Design of Algorithms 11 Stack Operation: Destroy 4/13/2015 CSE221/ICT221 Analysis and Design of Algorithms 12 Operations 1. 2. 3. 4. 5. 6. 7. 8. 4/13/2015 พืน้ ฐานของ Stack ที่สร้ างด้ วย Linked list Create stack: Push stack: Pop stack: Stack top: Empty stack: Full stack: Stack count: Destroy stack: allocate memory for stack head node add an element to a stack remove an element from a stack using the value on the top of stack check whether the stack is empty check whether the stack is full return number of elements in stack return all nodes of stack to system CSE221/ICT221 Analysis and Design of Algorithms 13 Stack Applications: Balancing Symbols 4/13/2015 CSE221/ICT221 Analysis and Design of Algorithms 14 Stack Applications: Infix to Postfix conversion The conversion time is O(n) 4/13/2015 CSE221/ICT221 Analysis and Design of Algorithms 15 Postfix expression evaluation The evaluation time is O(n) 4/13/2015 CSE221/ICT221 Analysis and Design of Algorithms 16 Backtracking backtracking is a general algorithm for finding all (or some) solutions to some computational problem, that incrementally builds candidates to the solutions, and abandons each partial candidate c ("backtracks") as soon as it determines that c cannot possibly be completed to a valid solution. classic example of the use of backtracking is the eight queens puzzle, that asks for all arrangements of eight queens on a standard chessboard so that no queen attacks any other. an important tool for solving constraint satisfaction problems, such as crosswords, verbal arithmetic, Sudoku, and many other puzzles. Apr-15 CSE221/ICT221 Analysis and Design of Algorithms 17 Stack Applications: Backtracking 4/13/2015 CSE221/ICT221 Analysis and Design of Algorithms 18 Stack Applications: Backtracking 4/13/2015 CSE221/ICT221 Analysis and Design of Algorithms 19 Print path to goal Running time is O(|E|+|V|) 4/13/2015 Algorithm seekGoal (val map <linked list>) This algorithm determines the path to a desired goal. Pre a graph containing the path Post path printed 1 Stack=createStack 2 pMap= pMap 3 loop (pMap not null AND goalNotFound) 1 if (pMap is goal) 1 set goalNoFound to false 2 else 1 pushStack (stack,pMap) 2 if (pMapis a branch point) 1 loop (more branch point) 1 create branchPoint node 2 pushStack (stack, branchPoint) 3 advance to next node 4 if (emptyStack (stack)) 1 print (There is no path to your goal) 5 else 1 print (The path to your goal is: ) 2 loop (not emptyStack (stack)) 1 popStack (stack, pMap) 2 if (pMap notbranchPoint) 1 print (pMAp->nodeName) 3 print (End of File) 6 destroyStack (stack) end seekGoal 20 Stack Applications: Backtracking 4/13/2015 CSE221/ICT221 Analysis and Design of Algorithms 21 Stack Applications: Backtracking 4/13/2015 CSE221/ICT221 Analysis and Design of Algorithms 22 Eight queens problem Algorithm queen8 (boardSize <integer>) Position chess queens on a game board so that no queen can capture any other queen. Pre boardSize is number of rows & collumns on board Post Queen’ position pointed createStack (stack) Set row to 1 Set col to 0 loop (row <= boardSize) loop (col <= boardSize AND row <= boardSize) add 1 to col if (not garded (row, col)) place queen at board [row] [col] pushStack (stack, [row, col]) add 1 to row set col to 0 loop (col >= boardSize) popStack (stack, [row, col]) remove queen at board[row] [col] printBoard (stack) 4/13/2015 CSE221/ICT221 Analysis and Design of Algorithms 23 Tail Recursion: bad use of recursion /** * Print List from ListNode p onwards. */ Public static void printlist (ListNode p) { /* 1*/ if (p== nul) /* 2*/ return; /* 3*/ system.out.println(p.element); /* 4*/ printList(p.next); } If the list contains 20,000 elements to print, there will be a stack of 20,000 activation records representing the nested calls of line 4. Activation records are typically large, so the program is likely to run out of stack space. 4/13/2015 CSE221/ICT221 Analysis and Design of Algorithms 24 Printing a list without recursion /** * Print List from ListNode p onward */ Public static void printList (ListNode p) { while (true) { if (p== null) return; system.out.println (p.element); p = p.next; { } Removal of tal recursion is so simple that some compilers do it automatically. 4/13/2015 CSE221/ICT221 Analysis and Design of Algorithms 25 4/13/2015 CSE221/ICT221 Analysis and Design of Algorithms 26 Queue Queue uses FIFO (First-In, First-Out) Basic operations of Queue - Enqueue :adding an element to Queue () - Dequeue: removing an element from Queue () - QueueFront: Returns a reference to the value at the front of a non-empty queue - QueueRear: Returns a reference to the value at the rear of a non-empty queue Implementing a Queue - by an Array - by Linked list 4/13/2015 CSE221/ICT221 Analysis and Design of Algorithms 27 The Queue concept 4/13/2015 CSE221/ICT221 Analysis and Design of Algorithms 28 Operation Enqueue 4/13/2015 CSE221/ICT221 Analysis and Design of Algorithms 29 Operation Dequeue 4/13/2015 CSE221/ICT221 Analysis and Design of Algorithms 30 Operation QueueFront 4/13/2015 CSE221/ICT221 Analysis and Design of Algorithms 31 Operation QueueRear 4/13/2015 CSE221/ICT221 Analysis and Design of Algorithms 32 Queue Operations 4/13/2015 CSE221/ICT221 Analysis and Design of Algorithms 33 Queue implemented by Array 4/13/2015 CSE221/ICT221 Analysis and Design of Algorithms 34 Is queue full? 4/13/2015 CSE221/ICT221 Analysis and Design of Algorithms 35 Circular Queue 4/13/2015 CSE221/ICT221 Analysis and Design of Algorithms 36 Queue implemented by linked list 4/13/2015 CSE221/ICT221 Analysis and Design of Algorithms 37 Queue data structure 4/13/2015 CSE221/ICT221 Analysis and Design of Algorithms 38 Operation algorithms on Queue 1. Create queue: create queue head based on dynamic memory 2. Enqueue: add an element on queue 3. Dequeue: remove an element from queue 4. Queue front: return an element at the front of queue 5. Queue rear: return an element at the rear of queue 6. Empty queue: returns true if queue is empty, else returns false 7. Full queue: returns true if queue is full, else returns false 8. Queue count: returns number of elements in queue 9. Destroy queue: returns memory allocated to queue to system 4/13/2015 CSE221/ICT221 Analysis and Design of Algorithms 39 Create and enqueue 4/13/2015 CSE221/ICT221 Analysis and Design of Algorithms 40 4/13/2015 CSE221/ICT221 Analysis and Design of Algorithms 41 4/13/2015 CSE221/ICT221 Analysis and Design of Algorithms 42 4/13/2015 CSE221/ICT221 Analysis and Design of Algorithms 43 Applications of Queue Queue simulation Categorizing Data 4/13/2015 CSE221/ICT221 Analysis and Design of Algorithms 44 13-Apr-15 CSE221/ICT221 Analysis and Design of Algorithms 45