CSC 202 Analysis and Design of Algorithms Lecture 06: CSC 202 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List, Stack and.

Slides:



Advertisements
Similar presentations
Data Structures Through C
Advertisements

Stacks, Queues, and Linked Lists
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.
Ceng-112 Data Structures I Chapter 5 Queues.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Stacks  a data structure which stores data in a Last-in First-out manner (LIFO)  has a pointer called TOP  can be implemented by either Array or Linked.
E.G.M. Petrakislists, stacks, queues1 Stacks Stack: restricted variant of list –Elements may by inserted or deleted from only one end  LIFO lists –Top:
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
1 CSC 211 Data Structures Lecture 22 Dr. Iftikhar Azim Niaz 1.
 Balancing Symbols 3. Applications
E.G.M. Petrakisstacks, queues1 Stacks  Stack: restricted variant of list  elements may by inserted or deleted from only one end : LIFO lists  top: the.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Stacks, Queues & Deques CSC212.
TCSS 342, Winter 2005 Lecture Notes
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
© 2004 Pearson Addison-Wesley. All rights reserved12-1 Chapter 12 : Collections Intermediate Java Programming Summer 2007.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Objectives of these slides:
DS.L.1 Lists, Stacks, and Queues (Review) Chapter 3 Overview Abstract Data Types Linked Lists, Headers, Circular Links Cursor (Array) Implementation Stacks.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Ceng-112 Data Structures ITurgut Kalfaoglu 1 Chapter 3 Stacks.
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,
Stack and Queue.
Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy.
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.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R1. Elementary Data Structures.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 18 Stacks and Queues.
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
A data structure is a type of data storage ….similar to an array. There are many data structures in Java (Stacks, Queues, LinkedList, Sets, Maps, HashTables,
Data Structures Using C++
1 Data Structures - Part II CS215 Lecture #8. Stacks  Last-In-First-Out (LIFO)  Stacks are often used in programming when data will need to be used.
Foundation of Computing Systems Lecture 3 Stacks and Queues.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
Stacks And Queues Chapter 18.
The Abstract Data Type Queue A queue New items enter at the back, or rear, of the queue Items leave from the front of the queue First-in, first-out (FIFO)
1 Stacks & Queues CSC Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.
Lecture 20 Stacks and Queues. Stacks, Queues and Priority Queues Stacks – first-in-last-out structure – used for function evaluation (run-time stack)
Linear Data Structures
Chapter 17: Stacks and Queues. Objectives In this chapter, you will: – Learn about stacks – Examine various stack operations – Learn how to implement.
Stacks & Queues. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy to use.
CSCI 62 Data Structures Dr. Joshua Stough October 7, 2008.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
Data Structures: A Pseudocode Approach with C1 Chapter 3 Objectives Upon completion you will be able to Explain the design, use, and operation of a stack.
3/3/20161 Stacks and Queues Introduction to Data Structures Ananda Gunawardena.
Lecture 21 Data Structures, Algorithms and Complexity Stacks and Queues GRIFFITH COLLEGE DUBLIN.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
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,
Stacks Chapter 3 Objectives Upon completion you will be able to
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.
STACKS & QUEUES for CLASS XII ( C++).
Stacks and Queues Chapter 4.
CC 215 Data Structures Queue ADT
Chapter 15 Lists Objectives
Objectives In this lesson, you will learn to: Define stacks
Stacks and Queues.
Queues Chapter 4.
CSC 172 DATA STRUCTURES.
Stacks Stack: restricted variant of list
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
Stack A data structure in which elements are inserted and removed only at one end (called the top). Enforces Last-In-First-Out (LIFO) Uses of Stacks Evaluating.
Stacks and Queues 1.
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
Presentation transcript:

CSC 202 Analysis and Design of Algorithms Lecture 06: CSC 202 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List, Stack and Queues Asst.Prof.Dr.Surasak Mungsing Sep-151

9/17/ Stacks

9/17/ Stack Operation: Push

9/17/ Stack Operation: Pop

9/17/ Stack Operation: Top

9/17/2015 6

7Stacks  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

9/17/ Stack represented by Linked list

9/17/ Stack represented by Linked list

9/17/

9/17/ Stack Operation: Push

9/17/ Stack Operation: Destroy

9/17/ Operations พื้นฐานของ Stack ที่สร้างด้วย Linked 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

9/17/ Stack Applications: Balancing Symbols

9/17/ Stack Applications: Infix to Postfix conversion The conversion time is O(n)

9/17/ Postfix expression evaluation The evaluation time is O(n)

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. Sep-15 17

9/17/ Stack Applications: Backtracking

9/17/ Stack Applications: Backtracking

9/17/ Print path to goal Algorithm seekGoal (val map ) This algorithm determines the path to a desired goal. Prea graph containing the path Postpath 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 Running time is O(|E|+|V|)

9/17/ Stack Applications: Backtracking

9/17/ Stack Applications: Backtracking

9/17/ Eight queens problem Algorithm queen8 (boardSize ) 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)

9/17/ /** * 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 recursion 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.

9/17/ 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.

9/17/

9/17/ 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

9/17/ The Queue concept

9/17/ Operation Enqueue

9/17/ Operation Dequeue

9/17/ Operation QueueFront

9/17/ Operation QueueRear

9/17/ Queue Operations

9/17/ Queue implemented by Array

9/17/ Is queue full?

9/17/ Circular Queue

9/17/ Queue implemented by linked list

9/17/ Queue data structure

9/17/ 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

9/17/ Create and enqueue

9/17/

9/17/

9/17/

9/17/ Applications of Queue  Queue simulation  Categorizing Data

17-Sep-15 45