45
CSE 221/ICT221 CSE 221/ICT221 Analysis and Design of Analysis and Design of Algorithms Algorithms Lecture 06: Lecture 06: Analysis of Algorithm using List, Stack and Queues Dr.Surasak Mungsing Dr.Surasak Mungsing E-mail: [email protected] 06/18/22 1

CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List,

Embed Size (px)

Citation preview

Page 1: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List,

CSE 221/ICT221 CSE 221/ICT221 Analysis and Design of AlgorithmsAnalysis and Design of Algorithms

Lecture 06:Lecture 06:Analysis of Algorithm using List, Stack and Queues

Dr.Surasak MungsingDr.Surasak MungsingE-mail: [email protected]

04/19/23 1

Page 2: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List,

04/19/232

Stacks

CSE221/ICT221 Analysis and Design of Algorithms

Page 3: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List,

04/19/233

Stack Operation: Push

CSE221/ICT221 Analysis and Design of Algorithms

Page 4: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List,

04/19/234

Stack Operation: Pop

CSE221/ICT221 Analysis and Design of Algorithms

Page 5: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List,

04/19/235

Stack Operation: Top

CSE221/ICT221 Analysis and Design of Algorithms

Page 6: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List,

04/19/236

CSE221/ICT221 Analysis and Design of Algorithms

Page 7: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List,

04/19/237

StacksStacks

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

CSE221/ICT221 Analysis and Design of Algorithms

Page 8: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List,

04/19/238

Stack represented by Linked list

CSE221/ICT221 Analysis and Design of Algorithms

Page 9: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List,

04/19/239

Stack represented by Linked list

CSE221/ICT221 Analysis and Design of Algorithms

Page 10: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List,

04/19/2310

CSE221/ICT221 Analysis and Design of Algorithms

Page 11: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List,

04/19/2311

Stack Operation: Push

CSE221/ICT221 Analysis and Design of Algorithms

Page 12: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List,

04/19/2312

Stack Operation: Destroy

CSE221/ICT221 Analysis and Design of Algorithms

Page 13: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List,

04/19/2313

OperationsOperations พื้��นฐานของพื้��นฐานของ StackStack ที่��สร้�างด้�วยที่��สร้�างด้�วย Linked listLinked list

1.     Create stack: allocate memory for stack head node

2.     Push stack: add an element to a stack

3.     Pop stack: remove an element from a stack

4.     Stack top: using the value on the top of stack

5.     Empty stack: check whether the stack is empty

6.     Full stack: check whether the stack is full 7.     Stack count: return number of elements in stack

8.     Destroy stack: return all nodes of stack to system

CSE221/ICT221 Analysis and Design of Algorithms

Page 14: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List,

04/19/2314

Stack Applications: Balancing Symbols

CSE221/ICT221 Analysis and Design of Algorithms

Page 15: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List,

04/19/2315

Stack Applications: Infix to Postfix conversion

The conversion time is O(n)

CSE221/ICT221 Analysis and Design of Algorithms

Page 16: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List,

04/19/2316

Postfix expression evaluation

The evaluation time is O(n)CSE221/ICT221 Analysis and Design of Algorithms

Page 17: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List,

BacktrackingBacktracking

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.

04/19/2317

CSE221/ICT221 Analysis and Design of Algorithms

Page 18: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List,

04/19/2318

Stack Applications: Backtracking

CSE221/ICT221 Analysis and Design of Algorithms

Page 19: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List,

04/19/2319

Stack Applications: Backtracking

CSE221/ICT221 Analysis and Design of Algorithms

Page 20: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List,

04/19/2320

Print path Print path to goalto goal

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=createStack2 pMap= pMap3 loop (pMap not null AND goalNotFound)

1 if (pMap is goal) 1 set goalNoFound to false2 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

Running time is O(|E|+|V|)

Page 21: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List,

04/19/2321

Stack Applications: Backtracking

CSE221/ICT221 Analysis and Design of Algorithms

Page 22: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List,

04/19/2322

Stack Applications: Backtracking

CSE221/ICT221 Analysis and Design of Algorithms

Page 23: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List,

04/19/2323

Eight queens problemEight 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 boardPost Queen’ position pointed

createStack (stack)Set row to 1Set col to 0loop (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)

CSE221/ICT221 Analysis and Design of Algorithms

Page 24: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List,

04/19/2324

/** * 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);

}

Tail Recursion: bad use of Tail Recursion: bad use of recursionrecursion

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.

CSE221/ICT221 Analysis and Design of Algorithms

Page 25: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List,

04/19/2325

Printing a list without recursionPrinting 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.

CSE221/ICT221 Analysis and Design of Algorithms

Page 26: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List,

04/19/2326

CSE221/ICT221 Analysis and Design of Algorithms

Page 27: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List,

04/19/2327

QueueQueue 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

CSE221/ICT221 Analysis and Design of Algorithms

Page 28: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List,

04/19/2328

The Queue concept

CSE221/ICT221 Analysis and Design of Algorithms

Page 29: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List,

04/19/2329

Operation Enqueue

CSE221/ICT221 Analysis and Design of Algorithms

Page 30: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List,

04/19/2330

Operation Dequeue

CSE221/ICT221 Analysis and Design of Algorithms

Page 31: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List,

04/19/2331

Operation QueueFront

CSE221/ICT221 Analysis and Design of Algorithms

Page 32: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List,

04/19/2332

Operation QueueRear

CSE221/ICT221 Analysis and Design of Algorithms

Page 33: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List,

04/19/2333

QueueOperations

CSE221/ICT221 Analysis and Design of Algorithms

Page 34: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List,

04/19/2334

Queue implemented by Array

CSE221/ICT221 Analysis and Design of Algorithms

Page 35: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List,

04/19/2335

Is queue full?

CSE221/ICT221 Analysis and Design of Algorithms

Page 36: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List,

04/19/2336

Circular Queue

CSE221/ICT221 Analysis and Design of Algorithms

Page 37: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List,

04/19/2337

Queue implemented by linked list

CSE221/ICT221 Analysis and Design of Algorithms

Page 38: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List,

04/19/2338

Queue data structure

CSE221/ICT221 Analysis and Design of Algorithms

Page 39: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List,

04/19/2339

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

CSE221/ICT221 Analysis and Design of Algorithms

Page 40: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List,

04/19/2340

Create and enqueue

CSE221/ICT221 Analysis and Design of Algorithms

Page 41: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List,

04/19/2341

CSE221/ICT221 Analysis and Design of Algorithms

Page 42: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List,

04/19/2342

CSE221/ICT221 Analysis and Design of Algorithms

Page 43: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List,

04/19/2343

CSE221/ICT221 Analysis and Design of Algorithms

Page 44: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List,

04/19/2344

Applications of Applications of Queue Queue

Queue simulation

Categorizing Data

CSE221/ICT221 Analysis and Design of Algorithms

Page 45: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List,

Apr 19, 202345

CSE221/ICT221 Analysis and Design of Algorithms