پشته Stack ساختمان داده ها و الگوريتمها. 2 آشنايي مجموعه پويا (Dynamic Set ): مجموعه اي است كه تركيب و تعداد عناصر آن ممكن است در طول زمان اجراي برنامه.

Slides:



Advertisements
Similar presentations
مهدی آذرنوش مبحث ششم : آرایه ها و رشته ها حسابگری الگوریتمی.
Advertisements

در جامعه، افرادي که موقعيت و مسؤليت خود را مي‌دانند از راحتي و امنيت بيشتري برخوردارند هر کلاس از جامعه، مجموعه‌اي از امکانات و تواناييهاي مجاز برخوردار.
برنامه سازي پيشرفته 5 Inheritance. وراثت Inheritance allows a software developer to derive a new class from an existing one The existing class is called.
دوران شكل، رسم اشكال 3D مركز تحصيلات تكميلي در علوم پايه زنجان بخش فن‌آوري اطلاعات گرافيك كامپيوتري قسمت پنجم سري آموزش هاي OpenGL.
دستور العمل نحوه محاسبه امتیاز مقالات ISI اعضای هیأت علمی دانشگاه صنعتی اصفهان بر اساس تعداد استنادات در پايگاه اسكاپوس شهریور ماه 1388 نفیسه دهقان.
1 آزمايشگاه سيستم های هوشمند ( Domain-specific Architecture.
مهندسی نرم افزار مبتنی بر عامل
ارائه روشي براي شناسايي کاراکترهاي دستنويس، برپايه شبکه LVQ.
ارائه درس روباتيکز Extended Kalman Filter فريد ملازم استاد مربوطه دکتر شيري دانشگاه امير کبير – دانشکده کامپيوتر و فناوري اطلاعات.
Part-B1 Stacks. Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations.
طراحي و ساخت سيستم‌هاي تجارت الکترونيک ساخت سيستم‌هاي تجارت الکترونيک ECSE.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
نام و نام خانوادگي : فريد ملازم 1 آزمايشکاه سيستم هاي هوشمند ( موضوع ارائه Process and Deployment Design.
شاخص هاي فرايند و پروژه درس مهندسي نرم‌افزار 2
مرتضي صاحب الزماني Memory Modeling. مرتضي صاحب الزماني مدل ساده package body Mem_Pkg is constant DataWidth_c : Natural := 8; constant AddrWidth_c : Natural.
Artificial Intelligent Systems Laboratory 1 الگو‌هاي فرايند (Process Patterns) فصل 2 درس مهندسي نرم‌افزار 2 دكتر احمد عبداله زاده بارفروش تهيه كننده :
Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only.
Stacks CSE, POSTECH. 2 2 Stacks Linear list One end is called top. Other end is called bottom. Additions to and removals from the top end only.
In the name of God Sharif University of Technology, International Branch, Kish Island Dr. Mohsen Sadighi Moshkenani Chapter 12.
Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only.
1 Binary storage & registers. CS 151 Binary Variables Recall that the two binary values have different names: –True/False –On/Off –Yes/No –1/0 We use.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
1 Stacks. 2 A stack has the property that the last item placed on the stack will be the first item removed Commonly referred to as last-in, first-out,
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
Stacks Chapter 8. Objectives In this chapter, you will: Learn about stacks Examine various stack operations Learn how to implement a stack as an array.
SNU IDB Lab. 1 Ch9. Stacks © copyright 2006 SNU IDB Lab.
ساختارهاي تقسيم كار پروژه
Applications of Stacks and Queues Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only.
 Hassan jamali In the name of God Grade: three CD شامل : مكالمه لغات جديد درس نقاط گرامري و تمرين است.
Solving problems by searching Chapter 3 Modified by Vali Derhami.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
شرط و تصميم اصول كامپيوتر 1. 2 الگوريتم اقليدس E1: [find remainder] Divide m by n and let r be the remainder. Clearly, 0
1/19 Informed search algorithms Chapter 4 Modified by Vali Derhami.
Department of Computer Eng. & IT Amirkabir University of Technology (Tehran Polytechnic) Data Structures Lecturer: Abbas Sarraf Order.
Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only.
Stacks 황승원 Fall 2010 CSE, POSTECH.
Stacks Linear list. One end is called top. Other end is called bottom.
Stacks.
مدارهای منطقی فصل چهارم و پنجم - مدارهاي منطقي تركيبي ماجولي
ساختمان داده ها و الگوريتمها
مرتب سازي مقايسه اي مرتب سازي خطي
آرايه ها و ساختارها.
آرايه ها.
ANOVA: Analysis Of Variance
ليست هاي پيوندي.
فصل سوم : صف وپشته اهداف آشنايي با پشته آشنايي با صف ارزشيابي عبارات 1.
ANOVA: Analysis Of Variance
وحید حقيقت دوست دانشکده فنی و مهندسی دانشگاه شاهد
Quick Sort مرتب سازي سريع.
Stacks template<class T> class stack { public:
لغت نامه و جدول درهم سازي Dictionaries and Hash Tables
آشنايي با برنامه نويسي به زبان C++
معرفي درس ساختمان داده ها و الگوريتمها
دکتر حسين بلندي/ دکتر سید مجید اسماعیل زاده / دکتر بهمن قربانی واقعی
تبدیل فوریه (Fourier Transform)
نمايش اعداد در کامپيوتر چهار عمل اصلي
Similarity transformation
Stacks template<class T> class stack { public:
گزگز و خواب رفتگي انگشتان دست
سيستمهاي اطلاعات مديريت
هیدرولیک جریان در کانالهای باز
فيلتر كالمن معرفي : فيلتر كالمن تخمين بهينه حالت‌ها است كه براي سيستم‌هاي ديناميكي با اختلال تصادفي در سال 1960 بزاي سيستم‌هاي گسسته و در سال 1961 براي.
عمليات آهنگري.
Stacks template<class T> class stack { public:
Test آزمون نرم افزار Mansooreh Jalalyazdi.
با تشکر از دکتر جواد سلیمی
مثال : فلوچارتي رسم كنيد كه دو عدد از ورودي دريافت كرده بزرگترين عدد
در اين درس مباني ساختمان داده ها و الگوريتم ها تدریس میشود.
Stacks public interface Stack { public boolean empty();
Stacks template<class T> class stack { public:
Presentation transcript:

پشته Stack ساختمان داده ها و الگوريتمها

2 آشنايي مجموعه پويا (Dynamic Set ): مجموعه اي است كه تركيب و تعداد عناصر آن ممكن است در طول زمان اجراي برنامه تغيير كند. مجموعه هاي پويا كاربردهاي زيادي در پياده سازي انواع الگوريتمهاي محاسباتي، نرم افزارهاي شبيه سازي و خود سيستم‌عامل كامپيوترها دارند. عمليات مختلفي مانند حذف واضافه نمودن اعضا، پيدا كردن يك عضو يا عضو قبل و بعد آن، تعداد اعضا، بزرگترين و كوچكترين عضو و... بر روي مجموعه هاي پويا قابل انجام است پشته، صف، صف اولويت دار و ليستهاي پيوندي ساختمان داده هايي براي نگهداري و مديريت مجموعه هاي پويا هستند.

3 پشته Stack هر عنصر جديدي كه به پشته وارد شود در بالاي آن قرار مي گيرد تنها بالاترين عضو پشته مجاز است از آن خارج شود پشته مثل همه ساختمان داده هاي ديگر، محدوديت اندازه دارد و ممكن است پر يا خالي باشد پشته ساختار Last In First Out است (LIFO)

4 مثال Stack

5 Stack ADT class Stack{ const MAX-SIZE= 100 ; //Max stack size int top = -1 ; // default : stack is empty int members[0... MAX-SIZE-1] void push(int x ) { if (top < MAX-SIZE-1){ top ++ ; members[top]= x ; } else { raise_error() ; }; }

6 Stack ADT class Stack{ //…. int pop(){ if ( top > -1 ) { int temp = members[top] ; top -- ; return temp ; }else { raise_error() ; } }

7 Stack ADT class Stack{ const MAX-SIZE= 100 ; //Max stack size int top = -1 ; // default : stack is empty int members[0... MAX-SIZE-1] int push(int x ) { …} int pop(){..} int numElements(){ return top ; } bool isEmpty() { return ( top == -1) ; } bool isFull() { return ( top == MAX-SIZE-1) ; } }

8 isFull, isEmpty اين دو تابع براي خواندن دو وضعيت ويژه پشته استفاده مي شوند. با استفاده از اين دو تابع مي توان قبل از خواندن يا نوشتن يك عضو وضعيت پشته را بررسي كرده و از بروز خطاي احتمالي جلو گيري كرد: if ( stack.isFull() == 0 ) stack.push(n) if ( stack.isEmpty() == 0 )n = stack.pop()

The Interface Stack public interface Stack { public boolean empty(); public Object peek(); public void push(Object theObject); public Object pop(); }

تطبيق پرانتزها در عبارت زير پرانتزهاي بازو بسته متناظر را پيدا کنيد: (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n) عبارت داده شده را از راست به چپ ببينيد با يافتن پرانتز باز، محل آن را در پشته يادداشت کنيد با يافتن پرانتز بسته، محل پرانتز باز نظير آن را از پشته بخوانيد

مثال (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n) 0 1 2

مثال (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n) 0 1 (2,6)(1,13) 15

مثال (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n) 0 1 (2,6)(1,13)(15,19) 21

مثال (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n) 0 1 (2,6)(1,13)(15,19)(21,25) 27

مثال (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n) 0 1 (2,6)(1,13)(15,19)(21,25)(27,31)(0,32) and so on

Towers Of Hanoi/Brahma ABC gold disks to be moved from tower A to tower C each tower operates as a stack cannot place big disk on top of a smaller one

Towers Of Hanoi/Brahma 3-disk Towers Of Hanoi/Brahma A BC 1 2 3

Towers Of Hanoi/Brahma 3-disk Towers Of Hanoi/Brahma A BC 1 2 3

Towers Of Hanoi/Brahma 3-disk Towers Of Hanoi/Brahma A BC 123

Towers Of Hanoi/Brahma 3-disk Towers Of Hanoi/Brahma A BC 12 3

Towers Of Hanoi/Brahma 3-disk Towers Of Hanoi/Brahma A BC 12 3

Towers Of Hanoi/Brahma 3-disk Towers Of Hanoi/Brahma A BC 123

Towers Of Hanoi/Brahma 3-disk Towers Of Hanoi/Brahma A BC 1 2 3

Towers Of Hanoi/Brahma 3-disk Towers Of Hanoi/Brahma A BC disk moves

Recursive Solution ABC 1 n > 0 gold disks to be moved from A to C using B move top n-1 disks from A to B using C

Recursive Solution A BC 1 move top disk from A to C

Recursive Solution A BC 1 move top n-1 disks from B to C using A

Recursive Solution A BC 1 moves(n) = 0 when n = 0 moves(n) = 2*moves(n-1) + 1 = 2 n -1 when n > 0

Towers Of Hanoi/Brahma moves(64) = 1.8 * (approximately) Performing 10 9 moves/second, a computer would take about 570 years to complete. At 1 disk move/min, the monks will take about 3.4 * years.

Method Invocation And Return public void a() { …; b(); …} public void b() { …; c(); …} public void c() { …; d(); …} public void d() { …; e(); …} public void e() { …; c(); …} return address in a() return address in b() return address in c() return address in d() return address in e() return address in c() return address in d()

Try-Throw-Catch When you enter a try block, push the address of this block on a stack. When an exception is thrown, pop the try block that is at the top of the stack (if the stack is empty, terminate). If the popped try block has no matching catch block, go back to the preceding step. If the popped try block has a matching catch block, execute the matching catch block.

Rat In A Maze

Move order is: right, down, left, up Block positions to avoid revisit.

Rat In A Maze Move order is: right, down, left, up Block positions to avoid revisit.

Rat In A Maze Move backward until we reach a square from which a forward move is possible.

Rat In A Maze Move down.

Rat In A Maze Move left.

Rat In A Maze Move down.

Rat In A Maze Move backward until we reach a square from which a forward move is possible.

Rat In A Maze Move backward until we reach a square from which a forward move is possible. Move downward.

Rat In A Maze Move right. Backtrack.

Rat In A Maze Move downward.

Rat In A Maze Move right.

Rat In A Maze Move one down and then right.

Rat In A Maze Move one up and then right.

Rat In A Maze Move down to exit and eat cheese. Path from maze entry to current position operates as a stack.

پياده سازي Stack public interface Stack { public boolean empty(); public Object peek(); public void push(Object theObject); public Object pop(); }

Derive From A Linear List Class پشته همانند يك ليست خطي مجموعه اي از عناصر هم نوع را نگهداري مي كند – ورود و خروج اعضا به اين ليست تنها از انتهاي ليست ممكن است بنابراين، منطقي است كه پشته با ارث بري، از يكي از كلاسهاي پياده سازي كننده ليست خطي مشتق شود ArrayLinearList Chain

Derive From ArrayLinearList – stack top is either left end or right end of linear list – empty() => isEmpty() O(1) time – peek() => get(0) or get(size() - 1) O(1) time abcde

Derive From ArrayLinearList when top is left end of linear list – push(theObject) => add(0, theObject) – O(size) time – pop() => remove(0) – O(size) time abcde

Derive From ArrayLinearList – when top is right end of linear list push(theObject) => add(size(), theObject) O(1) time pop() => remove(size()-1) O(1) time – use right end of list as top of stack abcde

Derive From Chain – stack top is either left end or right end of linear list – empty() => isEmpty() O(1) time abcde null firstNode

Derive From Chain abcde null firstNode – when top is left end of linear list  peek() => get(0)  O(1) time  push(theObject) => add(0, theObject)  O(1) time  pop() => remove(0)  O(1) time

Derive From Chain abcde null firstNode – when top is right end of linear list peek() => get(size() - 1) O(size) time push(theObject) => add(size(), theObject) O(size) time pop() => remove(size()-1) O(size) time – use left end of list as top of stack

Derive From ArrayLinearList package dataStructures; import java.util.*; // has stack exception public class DerivedArrayStack extends ArrayLinearList implements Stack { // constructors come here // Stack interface methods come here }

Constructors /** create a stack with the given initial * capacity */ public DerivedArrayStack(int initialCapacity) {super(initialCapacity);} /** create a stack with initial capacity 10 */ public DerivedArrayStack() {this(10);}

empty() And peek() public boolean empty() {return isEmpty();} public Object peek() { if (empty()) throw new EmptyStackException(); return get(size() - 1) } abcde

push(theObject) And pop() public void push(Object theElement) {add(size(), theElement);} public Object pop() { if (empty()) throw new EmptyStackException(); return remove(size() - 1); } abcde

ارزيابي روش پياده سازي ارث بري از ليست – مزاياي استفاده از ArrayLinearList كد نويسي ساده و كم – مديريت كد از جمله توسعه يا اشكال زدايي آن راحتتر است – معايب اين روش تمام متدهاي public كلاس ArrayLinearList‌ بر روي stack‌نيز قابل انجام هستند – با تعريف پشته متناقض است متدهاي اضافه را بايد override كرد براي انجام عمليات مورد نياز، كد اضافي اجرا مي شود – pop() خالي نبودن پشته را بررسي مي كند سپس متد remove را فراخواني مي كند. اين متد نيز خالي نبودن پشته را بررسي مي كند پياده سازي بعنوان كلاس جديد براي تسريع اجراي كد – زمان و هزينه توليد نرم افزار

پياده سازي پشته از آرايه يك بعدي با نوع Object استفاده مي كند – متغير top براي نگهداري انتهاي پشته تعريف مي شود – اعضاي پشته در آرايه stack[0:top] قرار مي گيرند – Top element is in stack[top] – Bottom element is in stack[0]. – Stack is empty iff top = -1. – تعداد اعضاي پشته برابر top+1 است

پياده سازي پشته package dataStructures; import java.util.EmptyStackException; import utilities.*; // ChangeArrayLength public class ArrayStack implements Stack { // data members int top; // current top of stack Object [] stack; // element array // constructors come here // Stack interface methods come here }

Constructors public ArrayStack(int initialCapacity) { if (initialCapacity < 1) throw new IllegalArgumentException ("initialCapacity must be >= 1"); stack = new Object [initialCapacity]; top = -1; } public ArrayStack() {this(10);}

push(…) public void push(Object theElement) { // increase array size if necessary if (top == stack.length - 1) stack = ChangeArrayLength.changeLength1D (stack, 2 * stack.length); // put theElement at the top of the stack stack[++top] = theElement; } abcde top

pop() public Object pop() { if (empty()) throw new EmptyStackException(); Object topElement = stack[top]; stack[top--] = null; // enable garbage collection return topElement; } abcde top

java.util.Stack Derives from java.util.Vector. java.util.Vector is an array implementation of a linear list.

Performance 500,000 pop, push, and peek operations initial capacity Class ,000 ArrayStack 0.44s 0.22s DerivedArrayStack 0.60s 0.38s DerivedArrayStackWithCatch 0.55s 0.33s java.util.Stack 1.15s - DerivedLinkedStack 3.20s 3.20s LinkedStack 2.96s 2.96s

67 تمرين با استفاده از ليست پيوندي، پشته را پياده سازي كنيد تمرينهاي بخش 10.1 را حل كنيد.