Here are my slides for my preparation class for possible Master students in Electrical Engineering and Computer Science (Specialization in Computer Science)... for the entrance examination here at Cinvestav GDL.
Here are my slides for my preparation class for possible Master students in Electrical Engineering and Computer Science (Specialization in Computer Science)... for the entrance examination here at Cinvestav GDL.
Here are my slides for my preparation class for possible students in the Master in Electrical Engineering and Computer Science (Specialization in Computer Science)... for the entrance examination here at Cinvestav GDL.
Here are my slides for my preparation class for possible Master students in Electrical Engineering and Computer Science (Specialization in Computer Science)... for the entrance examination here at Cinvestav GDL.
Big picture of category theory in scala with deep dive into contravariant and...Piotr Paradziński
A big picture of category theory in Scala - starting from regular functors with additional structure (Apply, Applicative, Monad) to Comonads. Usually, we think about structures like Monoids in a monoidal category with particular tensor. In here I analyze just signatures of different abstractions.
Exploration of Contravariant functors as a way to model computation "backward" or abstract over input with the ability to prepend operation. Examples for predicates, sorting, show and function input (or any other function parameter except the last one).
Profunctors as abstraction unifying Functors and Contravariant functors to model both input and output. Example for Profunctor - function with one argument.
Relation to Bifunctors, Kan extensions, Adjunctions, and Free constructions.
Here are my slides for my preparation class for possible Master students in Electrical Engineering and Computer Science (Specialization in Computer Science)... for the entrance examination here at Cinvestav GDL.
Here are my slides for my preparation class for possible students in the Master in Electrical Engineering and Computer Science (Specialization in Computer Science)... for the entrance examination here at Cinvestav GDL.
Here are my slides for my preparation class for possible Master students in Electrical Engineering and Computer Science (Specialization in Computer Science)... for the entrance examination here at Cinvestav GDL.
Big picture of category theory in scala with deep dive into contravariant and...Piotr Paradziński
A big picture of category theory in Scala - starting from regular functors with additional structure (Apply, Applicative, Monad) to Comonads. Usually, we think about structures like Monoids in a monoidal category with particular tensor. In here I analyze just signatures of different abstractions.
Exploration of Contravariant functors as a way to model computation "backward" or abstract over input with the ability to prepend operation. Examples for predicates, sorting, show and function input (or any other function parameter except the last one).
Profunctors as abstraction unifying Functors and Contravariant functors to model both input and output. Example for Profunctor - function with one argument.
Relation to Bifunctors, Kan extensions, Adjunctions, and Free constructions.
Functional Core and Imperative Shell - Game of Life Example - Haskell and ScalaPhilip Schwarz
See a program structure flowchart used to highlight how an FP program breaks down into a functional core and imperative shell
View a program structure flowchart for the Game of Life
See the code for Game of Life’s functional core and imperative shell, both in Haskell and in Scala.
Code:
https://github.com/philipschwarz/functional-core-imperative-shell-scala
https://github.com/philipschwarz/functional-core-imperative-shell-haskell
download for better quality - Learn about the sequence and traverse functions
through the work of Runar Bjarnason and Paul Chiusano, authors of Functional Programming in Scala https://www.manning.com/books/functional-programming-in-scala, and others (Martin Odersky, Derek Wyatt, Adelbert Chang)
Download for better quality.
Functor Composition - including (starting from) the definition in Bartosz Milewski's Category Theory book. https://github.com/hmemcpy/milewski-ctfp-pdf
After a recap of implicits I introduce the type class mechanics in Scala. Then I have a look at ways for good non-intrusive type class design. The main focus of this presentation are type classes in Scala. In the last chapter I show the Haskell implementation of my example.
Download for better quality.
Monads do not Compose. Not in a generic way - There is no general way of composing monads.
A comment from Rúnar Bjarnason, coauthor of FP in Scala: "They do compose in a different generic way. For any two monads F and G we can take the coproduct which is roughly Free of Either F or G (up to normalization)".
Another comment from Sergei Winitzki (which caused me to upload https://www.slideshare.net/pjschwarz/addendum-to-monads-do-not-compose): "It is a mistake to think that a traversable monad can be composed with another monad. It is true that, given `Traversable`, you can implement the monad's methods (pure and flatMap) for the composition with another monad (as in your slides 21 to 26), but this is a deceptive appearance. The laws of the `Traversable` typeclass are far insufficient to guarantee the laws of the resulting composed monad. The only traversable monads that work correctly are Option, Either, and Writer. It is true that you can implement the type signature of the `swap` function for any `Traversable` monad. However, the `swap` function for monads needs to satisfy very different and stronger laws than the `sequence` function from the `Traversable` type class. I'll have to look at the "Book of Monads"; but, if my memory serves, the FPiS book does not derive any of these laws." See https://www.linkedin.com/feed/update/urn:li:groupPost:41001-6523141414614814720?commentUrn=urn%3Ali%3Acomment%3A%28groupPost%3A41001-6523141414614814720%2C6532108273053761536%29
Game of Life - Polyglot FP - Haskell - Scala - Unison - Part 3Philip Schwarz
(download for picture-perfect quality) Follow along as Trampolining is used to overcome Stack Overflow issues with the simple IO monad, deepening you understanding of the IO monad in the process. See Game of Life IO actions migrated to the Cats Effect IO monad, which is trampolined in its flatMap evaluation.
Errata:
slide 33: "impure factorial function" should be "initial factorial function"
slide 34: there is a pointless short vertical bar inside a speech bubble
slide 39: "it is till annoying" should be "it is still annoying"
slide 44: "values that we are interested" should be "values that we are interested in"
Computer Graphics in Java and Scala - Part 1Philip Schwarz
Computer Graphics in Java and Scala - Part 1.
Continuous (Logical) and Discrete (Device) Coordinates,
with a simple yet pleasing example involving concentric triangles.
Scala code: https://github.com/philipschwarz/computer-graphics-50-triangles-scala
Errata:
1. Scala classes TrianglesPanel and Triangles need not be classes, they could just be objects.
Scala 3 enum for a terser Option Monad Algebraic Data TypePhilip Schwarz
(download for flawless slides)
* Explore a terser definition of the Option Monad that uses a Scala 3 enum as an Algebraic Data Type.
* In the process, have a tiny bit of fun with Scala 3 enums.
* Get a refresher on the Functor and Monad laws.
* See how easy it is to use Scala 3 extension methods, e.g. to add convenience methods and infix operators.
The diagrams for function composition and Kleisli composition were made using https://q.uiver.app/ by https://twitter.com/varkora.
Source code: https://github.com/philipschwarz/scala-3-enum-for-terser-option-monad-algebraic-data-type
Errata:
slide 14 is an unwanted leftover - it is the same as slide 15 minus a diagram.
on slide 19, the colons in the extension method declarations are not needed
In this chapter we are going to get familiar with some of the basic presentations of data in programming: lists and linear data structures. Very often in order to solve a given problem we need to work with a sequence of elements. For example, to read completely this book we have to read sequentially each page, i.e. to traverse sequentially each of the elements of the set of the pages in the book. Depending on the task, we have to apply different operations on this set of data. In this chapter we will introduce the concept of abstract data types (ADT) and will explain how a certain ADT can have multiple different implementations. After that we shall explore how and when to use lists and their implementations (linked list, doubly-linked list and array-list). We are going to see how for a given task one structure may be more convenient than another. We are going to consider the structures "stack" and "queue", as well as their applications. We are going to get familiar with some implementations of these structures.
The introduction to my class on machine learning. The subjects covered in this class go from:
1.- Linear Classifiers
2.- Non Linear Classifiers
3.- Graphical Models
4.- Clustering
5.- Etc
I am planning to upload the rest once I feel they are at the level.
Functional Core and Imperative Shell - Game of Life Example - Haskell and ScalaPhilip Schwarz
See a program structure flowchart used to highlight how an FP program breaks down into a functional core and imperative shell
View a program structure flowchart for the Game of Life
See the code for Game of Life’s functional core and imperative shell, both in Haskell and in Scala.
Code:
https://github.com/philipschwarz/functional-core-imperative-shell-scala
https://github.com/philipschwarz/functional-core-imperative-shell-haskell
download for better quality - Learn about the sequence and traverse functions
through the work of Runar Bjarnason and Paul Chiusano, authors of Functional Programming in Scala https://www.manning.com/books/functional-programming-in-scala, and others (Martin Odersky, Derek Wyatt, Adelbert Chang)
Download for better quality.
Functor Composition - including (starting from) the definition in Bartosz Milewski's Category Theory book. https://github.com/hmemcpy/milewski-ctfp-pdf
After a recap of implicits I introduce the type class mechanics in Scala. Then I have a look at ways for good non-intrusive type class design. The main focus of this presentation are type classes in Scala. In the last chapter I show the Haskell implementation of my example.
Download for better quality.
Monads do not Compose. Not in a generic way - There is no general way of composing monads.
A comment from Rúnar Bjarnason, coauthor of FP in Scala: "They do compose in a different generic way. For any two monads F and G we can take the coproduct which is roughly Free of Either F or G (up to normalization)".
Another comment from Sergei Winitzki (which caused me to upload https://www.slideshare.net/pjschwarz/addendum-to-monads-do-not-compose): "It is a mistake to think that a traversable monad can be composed with another monad. It is true that, given `Traversable`, you can implement the monad's methods (pure and flatMap) for the composition with another monad (as in your slides 21 to 26), but this is a deceptive appearance. The laws of the `Traversable` typeclass are far insufficient to guarantee the laws of the resulting composed monad. The only traversable monads that work correctly are Option, Either, and Writer. It is true that you can implement the type signature of the `swap` function for any `Traversable` monad. However, the `swap` function for monads needs to satisfy very different and stronger laws than the `sequence` function from the `Traversable` type class. I'll have to look at the "Book of Monads"; but, if my memory serves, the FPiS book does not derive any of these laws." See https://www.linkedin.com/feed/update/urn:li:groupPost:41001-6523141414614814720?commentUrn=urn%3Ali%3Acomment%3A%28groupPost%3A41001-6523141414614814720%2C6532108273053761536%29
Game of Life - Polyglot FP - Haskell - Scala - Unison - Part 3Philip Schwarz
(download for picture-perfect quality) Follow along as Trampolining is used to overcome Stack Overflow issues with the simple IO monad, deepening you understanding of the IO monad in the process. See Game of Life IO actions migrated to the Cats Effect IO monad, which is trampolined in its flatMap evaluation.
Errata:
slide 33: "impure factorial function" should be "initial factorial function"
slide 34: there is a pointless short vertical bar inside a speech bubble
slide 39: "it is till annoying" should be "it is still annoying"
slide 44: "values that we are interested" should be "values that we are interested in"
Computer Graphics in Java and Scala - Part 1Philip Schwarz
Computer Graphics in Java and Scala - Part 1.
Continuous (Logical) and Discrete (Device) Coordinates,
with a simple yet pleasing example involving concentric triangles.
Scala code: https://github.com/philipschwarz/computer-graphics-50-triangles-scala
Errata:
1. Scala classes TrianglesPanel and Triangles need not be classes, they could just be objects.
Scala 3 enum for a terser Option Monad Algebraic Data TypePhilip Schwarz
(download for flawless slides)
* Explore a terser definition of the Option Monad that uses a Scala 3 enum as an Algebraic Data Type.
* In the process, have a tiny bit of fun with Scala 3 enums.
* Get a refresher on the Functor and Monad laws.
* See how easy it is to use Scala 3 extension methods, e.g. to add convenience methods and infix operators.
The diagrams for function composition and Kleisli composition were made using https://q.uiver.app/ by https://twitter.com/varkora.
Source code: https://github.com/philipschwarz/scala-3-enum-for-terser-option-monad-algebraic-data-type
Errata:
slide 14 is an unwanted leftover - it is the same as slide 15 minus a diagram.
on slide 19, the colons in the extension method declarations are not needed
In this chapter we are going to get familiar with some of the basic presentations of data in programming: lists and linear data structures. Very often in order to solve a given problem we need to work with a sequence of elements. For example, to read completely this book we have to read sequentially each page, i.e. to traverse sequentially each of the elements of the set of the pages in the book. Depending on the task, we have to apply different operations on this set of data. In this chapter we will introduce the concept of abstract data types (ADT) and will explain how a certain ADT can have multiple different implementations. After that we shall explore how and when to use lists and their implementations (linked list, doubly-linked list and array-list). We are going to see how for a given task one structure may be more convenient than another. We are going to consider the structures "stack" and "queue", as well as their applications. We are going to get familiar with some implementations of these structures.
The introduction to my class on machine learning. The subjects covered in this class go from:
1.- Linear Classifiers
2.- Non Linear Classifiers
3.- Graphical Models
4.- Clustering
5.- Etc
I am planning to upload the rest once I feel they are at the level.
Here are my slides for my preparation class for possible Master students in Electrical Engineering and Computer Science (Specialization in Computer Science)... for the entrance examination here at Cinvestav GDL.
Here is the basic introduction to the probability used in my Analysis of Algorithms course at the Cinvestav Guadalajara. They go from the basic axioms to the Expected Value and Variance.
Here are my slides for my preparation class for possible students for the Master in Electrical Engineering and Computer Science (Specialization in Computer Science)... for the entrance examination here at Cinvestav GDL.
Here are my slides in some basic algorithms in Computational Geometry:
1.- Line Intersection
2.- Sweeping Line
3.- Convex Hull
They are the classic one, but there is still a lot for anybody wanting to get in computer graphics to study. I recomend
Mark de Berg, Otfried Cheong, Marc van Kreveld, and Mark Overmars. 2008. Computational Geometry: Algorithms and Applications (3rd ed. ed.). TELOS, Santa Clara, CA, USA.
Reactive Programming with RxJava has widely been adopted by both backend services and Android applications alike. Yet, the steep learning curve leaves many developers hesitant about adding it to their own Software tool belt. I was one such developer. Over the past two years, I’ve watched countless videos, read numerous blog posts and attended several conference talks on the subject. Yet, I often left each experience feeling only slightly more knowledgeable, but not quite empowered to start using RxJava in my apps. That’s not going to happen today!
In this talk, we’re going to cover the bare minimum concepts you need to grok, in order to start using RxJava today. In particular, we’re going to focus on:
The 3 O’s: Observable, Observer and Operator
The most common Operators: map(), flatMap(), and filter()
Understanding those Marble Diagrams
Reactive Programming is not going away any time soon. It’s a powerful way to create asynchronous, event-based applications. It allows developers the ability to craft applications that can easily combine multiple network calls, gracefully handle failures, all while providing a snappy user experience. I want everyone to feel comfortable with the basic concepts of RxJava. Today can be your first step...
Implementation of checking the validity of an arithmetic expression, conversion from infix to postfix form, evaluation of a postfix expression (C code)
Good morning Salma Hayek you have to do is your purpose of the best time to plant grass seed in the morning Salma Hayek you have to do is your purpose of the best time to plant grass seed in the morning Salma Hayek you have to do is your purpose of the best time to plant grass seed in the morning Salma Hayek you want me potter to plant in spring I will be there in the morning Salma Hayek you have a nice weekend with someone legally allowed in spring a contract for misunderstanding and tomorrow I hope it was about the best msg you want me potter you want me potter you want to do is your purpose of the best time to plant grass seed in the morning Salma Hayek you have to do it up but what do you think about the pros of the morning Salma good mornings are you doing well and tomorrow I hope it goes well and I hope you to do it goes well and tomorrow I have to be there at both locations in spring a nice day service and I hope it goes away soon as I can you have to be to get a I hope it goes away soon I hope it goes away soon I hope it goes away soon as I can you have to be to work at a time I can do is
Using a circularly-linked list made the solution trivial. The solution would have been more difficult if an array had been used. This illustrates the fact that the choice of the appropriate data structures can significantly simplify an algorithm. It can make the algorithm much faster and efficient.
1- The design of a singly-linked list below is a picture of the functi (1).pdfafgt2012
1. The design of a singly-linked list
below is a picture of the function that needs to be used
below is the code of the above picture:
#include <string>
#include <iostream>
#include <cmath>
#include <cstdio>
#include <algorithm>
using namespace std;
#define defaultSize 100
void Assert(bool val, string s)
{
if (!val)
{ // Assertion failed -- close the program
cout << "Assertion Failed: " << s << endl;
exit(-1);
}
}
template <typename E>
class Link {
public:
E element; // Value for this node
Link *next; // Pointer to next node in list
// Constructors
Link(const E& elemval, Link<E>* nextval = NULL)
{ element = elemval; next = nextval; }
Link(Link<E>* nextval =NULL) { next = nextval; }
};
template <typename E>
class LList: public Link<E> {
private:
Link<E>* head;// Intialization helper method
Link<E>* tail;// Pointer to last element
Link<E>* curr;// Access to current element
int cnt;// Size of list
void init(){// Intialization helper method
curr = tail = head = new Link<E>;
cnt = 0;
}
void removeall() {// Return link nodes to free store
while(head != NULL) {
curr = head;
head = head->next;
delete curr;
}
}
public:
LList(int size=defaultSize) { init(); }// Constructor
~LList() { removeall(); }// Destructor
void print() const;// Print list contents
void clear() { removeall(); init(); }// Clear list
// Insert "it" at current position
void insert(const E& it) {
curr->next = new Link<E>(it, curr->next);
if (tail == curr) tail = curr->next; // New tail
cnt++;
}
void append(const E& it) { // Append "it" to list
tail = tail->next = new Link<E>(it, NULL);
cnt++;
}
// Remove and return current element
E remove() {
Assert(curr->next != NULL, "No element");
E it = curr->next->element; // Remember value
Link<E>* ltemp = curr->next; // Remember link node
if (tail == curr->next) tail = curr; // Reset tail
curr->next = curr->next->next; // Remove from list
delete ltemp; // Reclaim space
cnt--; // Decrement the count
return it;
}
void moveToStart()// Place curr at list start
{ curr = head; }
void moveToEnd() // Place curr at list end
{ curr = tail; }
void prev(){
if (curr == head) return;
Link<E>* temp = head;
while (temp->next!=curr) temp=temp->next;
curr = temp;
}
void next(){ if (curr != tail) curr = curr->next; }
int length() const { return cnt; }
int currPos() const {
Link<E>* temp = head;
int i;
for (i=0; curr != temp; i++)
temp = temp->next;
return i;
}
void moveToPos(int pos){
Assert ((pos>=0)&&(pos<=cnt), "Position out of range");
curr = head;
for(int i=0; i<pos; i++) curr = curr->next;
}
const E& getValue() const {
Assert(curr->next != NULL, "No value");
return curr->next->element;
}
};
completed this code to fulfill the requirement below:
Write a function to insert an integer into a singly-linked list of elements arranged from largest to
smallest. Requires that elements remain ordered after insertion.
2. The design of array-based stack
below is a picture of the function that needs to be used
below is the code of the above picture:
#include <string.h>
#.
Kotlin is a statically-typed programming language for the JVM and the browser, built by a team at JetBrains and designed as a pragmatic and easy-to-use Java alternative. In this presentation we'll explore the most notable Kotlin features, such as:
- Extension functions
- Null-safety
- Smart casts
- and many more
Check out and see that programming in Kotlin is fun!
final Year Projects, Final Year Projects in Chennai, Software Projects, Embedded Projects, Microcontrollers Projects, DSP Projects, VLSI Projects, Matlab Projects, Java Projects, .NET Projects, IEEE Projects, IEEE 2009 Projects, IEEE 2009 Projects, Software, IEEE 2009 Projects, Embedded, Software IEEE 2009 Projects, Embedded IEEE 2009 Projects, Final Year Project Titles, Final Year Project Reports, Final Year Project Review, Robotics Projects, Mechanical Projects, Electrical Projects, Power Electronics Projects, Power System Projects, Model Projects, Java Projects, J2EE Projects, Engineering Projects, Student Projects, Engineering College Projects, MCA Projects, BE Projects, BTech Projects, ME Projects, MTech Projects, Wireless Networks Projects, Network Security Projects, Networking Projects, final year projects, ieee projects, student projects, college projects, ieee projects in chennai, java projects, software ieee projects, embedded ieee projects, "ieee2009projects", "final year projects", "ieee projects", "Engineering Projects", "Final Year Projects in Chennai", "Final year Projects at Chennai", Java Projects, ASP.NET Projects, VB.NET Projects, C# Projects, Visual C++ Projects, Matlab Projects, NS2 Projects, C Projects, Microcontroller Projects, ATMEL Projects, PIC Projects, ARM Projects, DSP Projects, VLSI Projects, FPGA Projects, CPLD Projects, Power Electronics Projects, Electrical Projects, Robotics Projects, Solor Projects, MEMS Projects, J2EE Projects, J2ME Projects, AJAX Projects, Structs Projects, EJB Projects, Real Time Projects, Live Projects, Student Projects, Engineering Projects, MCA Projects, MBA Projects, College Projects, BE Projects, BTech Projects, ME Projects, MTech Projects, M.Sc Projects, Final Year Java Projects, Final Year ASP.NET Projects, Final Year VB.NET Projects, Final Year C# Projects, Final Year Visual C++ Projects, Final Year Matlab Projects, Final Year NS2 Projects, Final Year C Projects, Final Year Microcontroller Projects, Final Year ATMEL Projects, Final Year PIC Projects, Final Year ARM Projects, Final Year DSP Projects, Final Year VLSI Projects, Final Year FPGA Projects, Final Year CPLD Projects, Final Year Power Electronics Projects, Final Year Electrical Projects, Final Year Robotics Projects, Final Year Solor Projects, Final Year MEMS Projects, Final Year J2EE Projects, Final Year J2ME Projects, Final Year AJAX Projects, Final Year Structs Projects, Final Year EJB Projects, Final Year Real Time Projects, Final Year Live Projects, Final Year Student Projects, Final Year Engineering Projects, Final Year MCA Projects, Final Year MBA Projects, Final Year College Projects, Final Year BE Projects, Final Year BTech Projects, Final Year ME Projects, Final Year MTech Projects, Final Year M.Sc Projects, IEEE Java Projects, ASP.NET Projects, VB.NET Projects, C# Projects, Visual C++ Projects, Matlab Projects, NS2 Projects, C Projects, Microcontroller Projects, ATMEL Projects, PIC Projects, ARM Projects, DSP Projects, VLSI Projects, FPGA Projects, CPLD Projects, Power Electronics Projects, Electrical Projects, Robotics Projects, Solor Projects, MEMS Projects, J2EE Projects, J2ME Projects, AJAX Projects, Structs Projects, EJB Projects, Real Time Projects, Live Projects, Student Projects, Engineering Projects, MCA Projects, MBA Projects, College Projects, BE Projects, BTech Projects, ME Projects, MTech Projects, M.Sc Projects, IEEE 2009 Java Projects, IEEE 2009 ASP.NET Projects, IEEE 2009 VB.NET Projects, IEEE 2009 C# Projects, IEEE 2009 Visual C++ Projects, IEEE 2009 Matlab Projects, IEEE 2009 NS2 Projects, IEEE 2009 C Projects, IEEE 2009 Microcontroller Projects, IEEE 2009 ATMEL Projects, IEEE 2009 PIC Projects, IEEE 2009 ARM Projects, IEEE 2009 DSP Projects, IEEE 2009 VLSI Projects, IEEE 2009 FPGA Projects, IEEE 2009 CPLD Projects, IEEE 2009 Power Electronics Projects, IEEE 2009 Electrical Projects, IEEE 2009 Robotics Projects, IEEE 2009 Solor Projects, IEEE 2009 MEMS Projects, IEEE 2009 J2EE P
Senior data scientist and founder of the company Intelligentia Data I+D SA de CV. We are offering consultancy services, development of projects and products in Machine Learning, Big Data, Data Sciences and Artificial Intelligence.
My first set of slides (The NN and DL class I am preparing for the fall)... I included the problem of Vanishing Gradient and the need to have ReLu (Mentioning btw the saturation problem inherited from Hebbian Learning)
It has been almost 62 years since the invention of the term Artificial Intelligence by Samuel and Minsky et al. at the Dartmouth workshop College in 1956 (“Dartmouth Summer Research Project on Artificial Intelligence”) where this new area of Computer Science was invented. However, the history of Artificial Intelligence goes back to previous millennia, when the Greeks in their Myths spoke about golden robots at Hephaestus, and the Galatea of Pygmalion. They were the first automatons known at the dawn of history, and although these first attempts were only myths, automatons were invented and built through multiple civilizations in history. Nevertheless, these automatons resembled in quite limited way their final objectives, representing animals and humans. In spite of that, the greatest illusion of an automaton, the Turk by Wolfgang von Kempelen, inspired many people, trough its exhibitions, as Alexander Graham Bell and Charles Babbage to develop inventions that would change forever human history. Thus, the importance of the concept “Artificial Intelligence” as a driver of our technological dreams. And although Artificial Intelligence has never been defined in a precise practical way, the amount of research and methods that have been developed to tackle some of its basics tasks have been and are quite humongous. Thus, the importance of having an introduction to the concepts of Artificial Intelligence, thus the dream can continue.
A review of one of the most popular methods of clustering, a part of what is know as unsupervised learning, K-Means. Here, we go from the basic heuristic used to solve the NP-Hard problem to an approximation algorithm K-Centers. Additionally, we look at variations coming from the Fuzzy Set ideas. In the future, we will add more about On-Line algorithms in the line of Stochastic Gradient Ideas...
Here a Review of the Combination of Machine Learning models from Bayesian Averaging, Committees to Boosting... Specifically An statistical analysis of Boosting is done
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdfTechSoup
In this webinar you will learn how your organization can access TechSoup's wide variety of product discount and donation programs. From hardware to software, we'll give you a tour of the tools available to help your nonprofit with productivity, collaboration, financial management, donor tracking, security, and more.
The French Revolution, which began in 1789, was a period of radical social and political upheaval in France. It marked the decline of absolute monarchies, the rise of secular and democratic republics, and the eventual rise of Napoleon Bonaparte. This revolutionary period is crucial in understanding the transition from feudalism to modernity in Europe.
For more information, visit-www.vavaclasses.com
Introduction to AI for Nonprofits with Tapp NetworkTechSoup
Dive into the world of AI! Experts Jon Hill and Tareq Monaur will guide you through AI's role in enhancing nonprofit websites and basic marketing strategies, making it easy to understand and apply.
2024.06.01 Introducing a competency framework for languag learning materials ...Sandy Millin
http://sandymillin.wordpress.com/iateflwebinar2024
Published classroom materials form the basis of syllabuses, drive teacher professional development, and have a potentially huge influence on learners, teachers and education systems. All teachers also create their own materials, whether a few sentences on a blackboard, a highly-structured fully-realised online course, or anything in between. Despite this, the knowledge and skills needed to create effective language learning materials are rarely part of teacher training, and are mostly learnt by trial and error.
Knowledge and skills frameworks, generally called competency frameworks, for ELT teachers, trainers and managers have existed for a few years now. However, until I created one for my MA dissertation, there wasn’t one drawing together what we need to know and do to be able to effectively produce language learning materials.
This webinar will introduce you to my framework, highlighting the key competencies I identified from my research. It will also show how anybody involved in language teaching (any language, not just English!), teacher training, managing schools or developing language learning materials can benefit from using the framework.
A Strategic Approach: GenAI in EducationPeter Windle
Artificial Intelligence (AI) technologies such as Generative AI, Image Generators and Large Language Models have had a dramatic impact on teaching, learning and assessment over the past 18 months. The most immediate threat AI posed was to Academic Integrity with Higher Education Institutes (HEIs) focusing their efforts on combating the use of GenAI in assessment. Guidelines were developed for staff and students, policies put in place too. Innovative educators have forged paths in the use of Generative AI for teaching, learning and assessments leading to pockets of transformation springing up across HEIs, often with little or no top-down guidance, support or direction.
This Gasta posits a strategic approach to integrating AI into HEIs to prepare staff, students and the curriculum for an evolving world and workplace. We will highlight the advantages of working with these technologies beyond the realm of teaching, learning and assessment by considering prompt engineering skills, industry impact, curriculum changes, and the need for staff upskilling. In contrast, not engaging strategically with Generative AI poses risks, including falling behind peers, missed opportunities and failing to ensure our graduates remain employable. The rapid evolution of AI technologies necessitates a proactive and strategic approach if we are to remain relevant.
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...Levi Shapiro
Letter from the Congress of the United States regarding Anti-Semitism sent June 3rd to MIT President Sally Kornbluth, MIT Corp Chair, Mark Gorenberg
Dear Dr. Kornbluth and Mr. Gorenberg,
The US House of Representatives is deeply concerned by ongoing and pervasive acts of antisemitic
harassment and intimidation at the Massachusetts Institute of Technology (MIT). Failing to act decisively to ensure a safe learning environment for all students would be a grave dereliction of your responsibilities as President of MIT and Chair of the MIT Corporation.
This Congress will not stand idly by and allow an environment hostile to Jewish students to persist. The House believes that your institution is in violation of Title VI of the Civil Rights Act, and the inability or
unwillingness to rectify this violation through action requires accountability.
Postsecondary education is a unique opportunity for students to learn and have their ideas and beliefs challenged. However, universities receiving hundreds of millions of federal funds annually have denied
students that opportunity and have been hijacked to become venues for the promotion of terrorism, antisemitic harassment and intimidation, unlawful encampments, and in some cases, assaults and riots.
The House of Representatives will not countenance the use of federal funds to indoctrinate students into hateful, antisemitic, anti-American supporters of terrorism. Investigations into campus antisemitism by the Committee on Education and the Workforce and the Committee on Ways and Means have been expanded into a Congress-wide probe across all relevant jurisdictions to address this national crisis. The undersigned Committees will conduct oversight into the use of federal funds at MIT and its learning environment under authorities granted to each Committee.
• The Committee on Education and the Workforce has been investigating your institution since December 7, 2023. The Committee has broad jurisdiction over postsecondary education, including its compliance with Title VI of the Civil Rights Act, campus safety concerns over disruptions to the learning environment, and the awarding of federal student aid under the Higher Education Act.
• The Committee on Oversight and Accountability is investigating the sources of funding and other support flowing to groups espousing pro-Hamas propaganda and engaged in antisemitic harassment and intimidation of students. The Committee on Oversight and Accountability is the principal oversight committee of the US House of Representatives and has broad authority to investigate “any matter” at “any time” under House Rule X.
• The Committee on Ways and Means has been investigating several universities since November 15, 2023, when the Committee held a hearing entitled From Ivory Towers to Dark Corners: Investigating the Nexus Between Antisemitism, Tax-Exempt Universities, and Terror Financing. The Committee followed the hearing with letters to those institutions on January 10, 202
Operation “Blue Star” is the only event in the history of Independent India where the state went into war with its own people. Even after about 40 years it is not clear if it was culmination of states anger over people of the region, a political game of power or start of dictatorial chapter in the democratic setup.
The people of Punjab felt alienated from main stream due to denial of their just demands during a long democratic struggle since independence. As it happen all over the word, it led to militant struggle with great loss of lives of military, police and civilian personnel. Killing of Indira Gandhi and massacre of innocent Sikhs in Delhi and other India cities was also associated with this movement.
Biological screening of herbal drugs: Introduction and Need for
Phyto-Pharmacological Screening, New Strategies for evaluating
Natural Products, In vitro evaluation techniques for Antioxidants, Antimicrobial and Anticancer drugs. In vivo evaluation techniques
for Anti-inflammatory, Antiulcer, Anticancer, Wound healing, Antidiabetic, Hepatoprotective, Cardio protective, Diuretics and
Antifertility, Toxicity studies as per OECD guidelines
3. Images/cinvestav-
Introduction
Definition of a stack
It is a linear list where:
One end is called top
Other end is called bottom
Additionally, adds and removes are at the top end only.
3 / 130
4. Images/cinvestav-
Introduction
Definition of a stack
It is a linear list where:
One end is called top
Other end is called bottom
Additionally, adds and removes are at the top end only.
3 / 130
5. Images/cinvestav-
Introduction
Definition of a stack
It is a linear list where:
One end is called top
Other end is called bottom
Additionally, adds and removes are at the top end only.
3 / 130
6. Images/cinvestav-
Introduction
Definition of a stack
It is a linear list where:
One end is called top
Other end is called bottom
Additionally, adds and removes are at the top end only.
3 / 130
7. Images/cinvestav-
What is a stack?
First
Stores a set of elements in a particular order.
Second
Stack principle: LAST IN FIRST OUT = LIFO
Meaning
It means: the last element inserted is the first one to be removed
4 / 130
8. Images/cinvestav-
What is a stack?
First
Stores a set of elements in a particular order.
Second
Stack principle: LAST IN FIRST OUT = LIFO
Meaning
It means: the last element inserted is the first one to be removed
4 / 130
9. Images/cinvestav-
What is a stack?
First
Stores a set of elements in a particular order.
Second
Stack principle: LAST IN FIRST OUT = LIFO
Meaning
It means: the last element inserted is the first one to be removed
4 / 130
33. Images/cinvestav-
Stacks ADT
Interface
p u b l i c i n t e r f a c e Stack<Item>
{
p u b l i c boolean empty ( ) ;
p u b l i c Item peek ( ) ;
p u b l i c void push ( Item TheObject ) ;
p u b l i c Item pop ( ) ;
}
28 / 130
34. Images/cinvestav-
Explanation of the ADT I
peek()
This method allows to look at the top of the stack without removing it!!!
For Example
29 / 130
35. Images/cinvestav-
Explanation of the ADT I
peek()
This method allows to look at the top of the stack without removing it!!!
For Example
A
B
C
D peek()
D
29 / 130
39. Images/cinvestav-
Explanation of the ADT III
push()
This method allows to push stuff to the top of the stack!!!
For Example
A
B
C
push( DD )
31 / 130
41. Images/cinvestav-
Stack Applications
Real life
Pile of books
Plate trays
More applications related to computer science
Program execution stack (You will know about this in OS or CA)
Evaluating expressions
33 / 130
42. Images/cinvestav-
Stack Applications
Real life
Pile of books
Plate trays
More applications related to computer science
Program execution stack (You will know about this in OS or CA)
Evaluating expressions
33 / 130
43. Images/cinvestav-
Stack Applications
Real life
Pile of books
Plate trays
More applications related to computer science
Program execution stack (You will know about this in OS or CA)
Evaluating expressions
33 / 130
44. Images/cinvestav-
Stack Applications
Real life
Pile of books
Plate trays
More applications related to computer science
Program execution stack (You will know about this in OS or CA)
Evaluating expressions
33 / 130
45. Images/cinvestav-
What do we do now?
First
Instead of going toward the implementations!!!
Why not examples?
1 Parentheses Matching
2 Towers Of Hanoi/Brahma
3 Switch Box Routing
4 Try-Throw-Catch in Java
5 Rat In A Maze
34 / 130
46. Images/cinvestav-
What do we do now?
First
Instead of going toward the implementations!!!
Why not examples?
1 Parentheses Matching
2 Towers Of Hanoi/Brahma
3 Switch Box Routing
4 Try-Throw-Catch in Java
5 Rat In A Maze
34 / 130
47. Images/cinvestav-
What do we do now?
First
Instead of going toward the implementations!!!
Why not examples?
1 Parentheses Matching
2 Towers Of Hanoi/Brahma
3 Switch Box Routing
4 Try-Throw-Catch in Java
5 Rat In A Maze
34 / 130
48. Images/cinvestav-
What do we do now?
First
Instead of going toward the implementations!!!
Why not examples?
1 Parentheses Matching
2 Towers Of Hanoi/Brahma
3 Switch Box Routing
4 Try-Throw-Catch in Java
5 Rat In A Maze
34 / 130
49. Images/cinvestav-
What do we do now?
First
Instead of going toward the implementations!!!
Why not examples?
1 Parentheses Matching
2 Towers Of Hanoi/Brahma
3 Switch Box Routing
4 Try-Throw-Catch in Java
5 Rat In A Maze
34 / 130
50. Images/cinvestav-
What do we do now?
First
Instead of going toward the implementations!!!
Why not examples?
1 Parentheses Matching
2 Towers Of Hanoi/Brahma
3 Switch Box Routing
4 Try-Throw-Catch in Java
5 Rat In A Maze
34 / 130
52. Images/cinvestav-
Parentheses Matching
Example - Input
(((a+b)*c+d-e)/(f+g)-(h+j))
( ( ( a + b ) ∗ c + d − e ) / ...
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...
( f + g ) − ( h + j ) )
15 16 17 18 19 20 21 22 23 24 25 26
Output
Output pairs (u,v) such that the left parenthesis at position u is matched
with the right parenthesis at v.
Or
(2,6) (1,13) (15,19) (21,25) (0,26)
36 / 130
53. Images/cinvestav-
Parentheses Matching
Example - Input
(((a+b)*c+d-e)/(f+g)-(h+j))
( ( ( a + b ) ∗ c + d − e ) / ...
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...
( f + g ) − ( h + j ) )
15 16 17 18 19 20 21 22 23 24 25 26
Output
Output pairs (u,v) such that the left parenthesis at position u is matched
with the right parenthesis at v.
Or
(2,6) (1,13) (15,19) (21,25) (0,26)
36 / 130
54. Images/cinvestav-
Parentheses Matching
Example - Input
(((a+b)*c+d-e)/(f+g)-(h+j))
( ( ( a + b ) ∗ c + d − e ) / ...
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...
( f + g ) − ( h + j ) )
15 16 17 18 19 20 21 22 23 24 25 26
Output
Output pairs (u,v) such that the left parenthesis at position u is matched
with the right parenthesis at v.
Or
(2,6) (1,13) (15,19) (21,25) (0,26)
36 / 130
78. Images/cinvestav-
Thus
We need a flag to send down the recursion!!!
To tell the logic if we saw a left parenthesis
Ok
For simple problems fine!!! However...
44 / 130
79. Images/cinvestav-
Thus
We need a flag to send down the recursion!!!
To tell the logic if we saw a left parenthesis
Ok
For simple problems fine!!! However...
44 / 130
83. Images/cinvestav-
Recursive Solution - You assume a list of characters
p u b l i c s t a t i c boolean Balanced ( C h a i n L i n e a r L i s t L i s t ,
i n t Counter ){
i f ( L i s t . isEmpty ( ) ) { // Check empty
i f ( Counter == 0) // Check Counter
r e t u r n t r u e ;
e l s e
r e t u r n f a l s e ;}
i f ( L i s t . get (0)== ’ ( ’ ) // Case (
{
Counter++;
L i s t . remove ( 0 ) ;
r e t u r n Balanced ( L i s t , Counter ) ;
} e l s e i f ( L i s t . get (0)== ’ [0 −9]|[+ −] ’ ) // Case Number or −+
{
L i s t . remove ( 0 ) ;
r e t u r n Balanced ( L i s t , Counter ) ;
} e l s e i f ( L i s t . get (0)== ’ ) ’ ) // Case )
{
i f ( Counter > 0){
Counter −−;
L i s t . remove ( 0 ) ;
r e t u r n Balanced ( L i s t , Counter )
} e l s e r e t u r n f a l s e ;
} e l s e r e t u r n f a l s e
}
47 / 130
89. Images/cinvestav-
So, we can simulate the recursion using what?
First a way to...
ITERATE
A memory storage
A Stack
So somebody?
Can give a process for it?
53 / 130
90. Images/cinvestav-
So, we can simulate the recursion using what?
First a way to...
ITERATE
A memory storage
A Stack
So somebody?
Can give a process for it?
53 / 130
91. Images/cinvestav-
So, we can simulate the recursion using what?
First a way to...
ITERATE
A memory storage
A Stack
So somebody?
Can give a process for it?
53 / 130
92. Images/cinvestav-
Iterative Solution - You assume a list of characters
p u b l i c s t a t i c boolean Balanced ( C h a i n L i n e a r L i s t L i s t ){
Stack I t = new Stack ( ) ;
i f ( L i s t . isEmpty ( ) )
r e t u r n t r u e
w h i l e ( ! L i s t . isEmpty ( ) ) { // Use a loop
i f ( L i s t . get (0)== ’ ( ’ )
{
I t . push ( L i s t . get ( 0 ) ) ;
L i s t . remove ( 0 ) ;
}
e l s e i f ( L i s t . get (0)== ’ [0 −9]|[+ −] ’ )
{ L i s t . remove (0) }
e l s e i f ( L i s t . get (0)== ’ ) ’ )
{
i f ( ! I t . empty ( ) )
I t . pop ( ) ;
e l s e
r e t u r n f a l s e ;
L i s t . remove ( 0 ) ; // Yes remove the
// l a s t ")"
}
i f ( I t . empty ( ) )
r e t u r n t r u e ;
r e t u r n f a l s e
}
54 / 130
94. Images/cinvestav-
The Puzzle
The Leyend
There is a story about an Indian temple in Kashi Vishwanath which
contains a large room with three time-worn posts in it surrounded by
64 golden disks.
Something Notable
Brahmin priests, acting out the command of an ancient prophecy,
have been moving these disks, in accordance with the immutable rules
of the Brahma, since that time.
So
According to the legend, when the last move of the puzzle will be
completed, the world will end.
56 / 130
95. Images/cinvestav-
The Puzzle
The Leyend
There is a story about an Indian temple in Kashi Vishwanath which
contains a large room with three time-worn posts in it surrounded by
64 golden disks.
Something Notable
Brahmin priests, acting out the command of an ancient prophecy,
have been moving these disks, in accordance with the immutable rules
of the Brahma, since that time.
So
According to the legend, when the last move of the puzzle will be
completed, the world will end.
56 / 130
96. Images/cinvestav-
The Puzzle
The Leyend
There is a story about an Indian temple in Kashi Vishwanath which
contains a large room with three time-worn posts in it surrounded by
64 golden disks.
Something Notable
Brahmin priests, acting out the command of an ancient prophecy,
have been moving these disks, in accordance with the immutable rules
of the Brahma, since that time.
So
According to the legend, when the last move of the puzzle will be
completed, the world will end.
56 / 130
105. Images/cinvestav-
The Rules
First One
Only one disk can be moved at a time.
Second One
Each move consists of taking the upper disk from one of the stacks and
placing it on top of another stack i.e. a disk can only be moved if it is the
uppermost disk on a stack.
Third One
No disk may be placed on top of a smaller disk.
65 / 130
106. Images/cinvestav-
The Rules
First One
Only one disk can be moved at a time.
Second One
Each move consists of taking the upper disk from one of the stacks and
placing it on top of another stack i.e. a disk can only be moved if it is the
uppermost disk on a stack.
Third One
No disk may be placed on top of a smaller disk.
65 / 130
107. Images/cinvestav-
The Rules
First One
Only one disk can be moved at a time.
Second One
Each move consists of taking the upper disk from one of the stacks and
placing it on top of another stack i.e. a disk can only be moved if it is the
uppermost disk on a stack.
Third One
No disk may be placed on top of a smaller disk.
65 / 130
108. Images/cinvestav-
Properties
With Three disk
You can solve it in seven moves
Thus, the minimum number of moves required for n disks
2n
− 1 (1)
So how we solve the problem recursively?
Ideas?
66 / 130
109. Images/cinvestav-
Properties
With Three disk
You can solve it in seven moves
Thus, the minimum number of moves required for n disks
2n
− 1 (1)
So how we solve the problem recursively?
Ideas?
66 / 130
110. Images/cinvestav-
Properties
With Three disk
You can solve it in seven moves
Thus, the minimum number of moves required for n disks
2n
− 1 (1)
So how we solve the problem recursively?
Ideas?
66 / 130
112. Images/cinvestav-
Next
The other things
Let n be the total number of discs.
Number the discs from 1 (smallest, topmost) to n (largest,
bottommost)
How we solve this problem?
We can use the technique of “Divide and Conquer”
68 / 130
113. Images/cinvestav-
Next
The other things
Let n be the total number of discs.
Number the discs from 1 (smallest, topmost) to n (largest,
bottommost)
How we solve this problem?
We can use the technique of “Divide and Conquer”
68 / 130
114. Images/cinvestav-
Next
The other things
Let n be the total number of discs.
Number the discs from 1 (smallest, topmost) to n (largest,
bottommost)
How we solve this problem?
We can use the technique of “Divide and Conquer”
68 / 130
115. Images/cinvestav-
Divide and Conquer
Divide and Conquer
It is an important algorithm design paradigm based on multi-branched
recursion.
Divide
This phase of the algorithm works by recursively breaking down a
problem into two or more sub-problems of the same (or related) type.
The Conquer I
Then these subproblems become simple enough to be solved directly.
69 / 130
116. Images/cinvestav-
Divide and Conquer
Divide and Conquer
It is an important algorithm design paradigm based on multi-branched
recursion.
Divide
This phase of the algorithm works by recursively breaking down a
problem into two or more sub-problems of the same (or related) type.
The Conquer I
Then these subproblems become simple enough to be solved directly.
69 / 130
117. Images/cinvestav-
Divide and Conquer
Divide and Conquer
It is an important algorithm design paradigm based on multi-branched
recursion.
Divide
This phase of the algorithm works by recursively breaking down a
problem into two or more sub-problems of the same (or related) type.
The Conquer I
Then these subproblems become simple enough to be solved directly.
69 / 130
120. Images/cinvestav-
A Simple Divide Phase
Move everything
The things that are in front of disk n.
In the previous case n = 3
Ok
How the recursion looks as simple logic steps?
72 / 130
121. Images/cinvestav-
A Simple Divide Phase
Move everything
The things that are in front of disk n.
In the previous case n = 3
Ok
How the recursion looks as simple logic steps?
72 / 130
122. Images/cinvestav-
The Initial Recursion
Logical Steps
To move n discs from tower A to tower C:
1 Move n − 1 discs from A to B using C as a temporary!!!
2 This leaves disc n alone on tower A move disc n from A to C.
3 Move n − 1 discs from B to C so they sit on disc n using A as
temporary!!!
73 / 130
123. Images/cinvestav-
The Initial Recursion
Logical Steps
To move n discs from tower A to tower C:
1 Move n − 1 discs from A to B using C as a temporary!!!
2 This leaves disc n alone on tower A move disc n from A to C.
3 Move n − 1 discs from B to C so they sit on disc n using A as
temporary!!!
73 / 130
124. Images/cinvestav-
The Initial Recursion
Logical Steps
To move n discs from tower A to tower C:
1 Move n − 1 discs from A to B using C as a temporary!!!
2 This leaves disc n alone on tower A move disc n from A to C.
3 Move n − 1 discs from B to C so they sit on disc n using A as
temporary!!!
73 / 130
125. Images/cinvestav-
The Initial Recursion
Logical Steps
To move n discs from tower A to tower C:
1 Move n − 1 discs from A to B using C as a temporary!!!
2 This leaves disc n alone on tower A move disc n from A to C.
3 Move n − 1 discs from B to C so they sit on disc n using A as
temporary!!!
73 / 130
126. Images/cinvestav-
Recursive Solution
Code
// Assume the l i s t i n A i s i n d e c r e a s i n g order
p u b l i c s t a t i c S t r i n g TH( i n t n , Char Origin ,
Char Destination ,
Char Temp){
S t r i n g r e s u l t ;
i f ( n = = 1){
r e t u r n "Move␣ Disk ␣"+n+"␣from␣"+ Or ig in +
"␣ to ␣" + D e s t i n a t i o n + "n" ;
}
r e s u l t = TH(n−1, Origin , Temp , D e s t i n a t i o n ) ;
r e s u l t+= "Move␣ Disk ␣"+n+"␣from␣"+ O ri gi n +
"␣ to ␣" + D e s t i n a t i o n + "n" ;
r e s u l t+= TH(n−1,Temp , Destination , Or ig i n ) ;
r e t u r n r e s u l t s ;
}
74 / 130
127. Images/cinvestav-
What do we need for the iterative solution?
Storage for the disks
Use one stack per tower!!!
A while loop to simulate the recursion
Doing What?
75 / 130
128. Images/cinvestav-
What do we need for the iterative solution?
Storage for the disks
Use one stack per tower!!!
A while loop to simulate the recursion
Doing What?
75 / 130
129. Images/cinvestav-
Simple Logic to the Iterative Version
Logic
A simple solution for the toy puzzle:
1 Alternate moves between the smallest piece and a non-smallest piece.
2 When moving the smallest piece, always move it to the next position
in the same direction.
3 If there is no tower position in the chosen direction, move the piece to
the opposite end, but then continue to move in the correct direction.
76 / 130
130. Images/cinvestav-
Simple Logic to the Iterative Version
Logic
A simple solution for the toy puzzle:
1 Alternate moves between the smallest piece and a non-smallest piece.
2 When moving the smallest piece, always move it to the next position
in the same direction.
3 If there is no tower position in the chosen direction, move the piece to
the opposite end, but then continue to move in the correct direction.
76 / 130
131. Images/cinvestav-
Simple Logic to the Iterative Version
Logic
A simple solution for the toy puzzle:
1 Alternate moves between the smallest piece and a non-smallest piece.
2 When moving the smallest piece, always move it to the next position
in the same direction.
3 If there is no tower position in the chosen direction, move the piece to
the opposite end, but then continue to move in the correct direction.
76 / 130
132. Images/cinvestav-
Simple Logic to the Iterative Version
Logic
A simple solution for the toy puzzle:
1 Alternate moves between the smallest piece and a non-smallest piece.
2 When moving the smallest piece, always move it to the next position
in the same direction.
3 If there is no tower position in the chosen direction, move the piece to
the opposite end, but then continue to move in the correct direction.
76 / 130
134. Images/cinvestav-
We have two cases
When there are n disks
n is even
When there are n disk
n is odd
Then a really simple solution involving 2n−1
− 1 moves
You have three stacks: A, B, C.
78 / 130
135. Images/cinvestav-
We have two cases
When there are n disks
n is even
When there are n disk
n is odd
Then a really simple solution involving 2n−1
− 1 moves
You have three stacks: A, B, C.
78 / 130
136. Images/cinvestav-
We have two cases
When there are n disks
n is even
When there are n disk
n is odd
Then a really simple solution involving 2n−1
− 1 moves
You have three stacks: A, B, C.
78 / 130
139. Images/cinvestav-
Process
IterativeHT using three stacks : towerA, towerB, towerC
1 for i = 1 to 2n−1
1 If (n%2 == 0)
1 Select the smallest disk at the top of the stacks
2 Move the smallest disk one position in the direction of the cyclic order
for even order.
3 Move the next largest disk to the only possible stack
2 else
1 Select the smallest disk at the top of the stacks
2 Move the smallest disk one position in the direction of the cyclic order
for odd order.
3 Move the next largest disk to the only possible stack
81 / 130
140. Images/cinvestav-
Process
IterativeHT using three stacks : towerA, towerB, towerC
1 for i = 1 to 2n−1
1 If (n%2 == 0)
1 Select the smallest disk at the top of the stacks
2 Move the smallest disk one position in the direction of the cyclic order
for even order.
3 Move the next largest disk to the only possible stack
2 else
1 Select the smallest disk at the top of the stacks
2 Move the smallest disk one position in the direction of the cyclic order
for odd order.
3 Move the next largest disk to the only possible stack
81 / 130
141. Images/cinvestav-
Process
IterativeHT using three stacks : towerA, towerB, towerC
1 for i = 1 to 2n−1
1 If (n%2 == 0)
1 Select the smallest disk at the top of the stacks
2 Move the smallest disk one position in the direction of the cyclic order
for even order.
3 Move the next largest disk to the only possible stack
2 else
1 Select the smallest disk at the top of the stacks
2 Move the smallest disk one position in the direction of the cyclic order
for odd order.
3 Move the next largest disk to the only possible stack
81 / 130
142. Images/cinvestav-
Process
IterativeHT using three stacks : towerA, towerB, towerC
1 for i = 1 to 2n−1
1 If (n%2 == 0)
1 Select the smallest disk at the top of the stacks
2 Move the smallest disk one position in the direction of the cyclic order
for even order.
3 Move the next largest disk to the only possible stack
2 else
1 Select the smallest disk at the top of the stacks
2 Move the smallest disk one position in the direction of the cyclic order
for odd order.
3 Move the next largest disk to the only possible stack
81 / 130
143. Images/cinvestav-
Process
IterativeHT using three stacks : towerA, towerB, towerC
1 for i = 1 to 2n−1
1 If (n%2 == 0)
1 Select the smallest disk at the top of the stacks
2 Move the smallest disk one position in the direction of the cyclic order
for even order.
3 Move the next largest disk to the only possible stack
2 else
1 Select the smallest disk at the top of the stacks
2 Move the smallest disk one position in the direction of the cyclic order
for odd order.
3 Move the next largest disk to the only possible stack
81 / 130
144. Images/cinvestav-
Process
IterativeHT using three stacks : towerA, towerB, towerC
1 for i = 1 to 2n−1
1 If (n%2 == 0)
1 Select the smallest disk at the top of the stacks
2 Move the smallest disk one position in the direction of the cyclic order
for even order.
3 Move the next largest disk to the only possible stack
2 else
1 Select the smallest disk at the top of the stacks
2 Move the smallest disk one position in the direction of the cyclic order
for odd order.
3 Move the next largest disk to the only possible stack
81 / 130
145. Images/cinvestav-
Process
IterativeHT using three stacks : towerA, towerB, towerC
1 for i = 1 to 2n−1
1 If (n%2 == 0)
1 Select the smallest disk at the top of the stacks
2 Move the smallest disk one position in the direction of the cyclic order
for even order.
3 Move the next largest disk to the only possible stack
2 else
1 Select the smallest disk at the top of the stacks
2 Move the smallest disk one position in the direction of the cyclic order
for odd order.
3 Move the next largest disk to the only possible stack
81 / 130
146. Images/cinvestav-
Process
IterativeHT using three stacks : towerA, towerB, towerC
1 for i = 1 to 2n−1
1 If (n%2 == 0)
1 Select the smallest disk at the top of the stacks
2 Move the smallest disk one position in the direction of the cyclic order
for even order.
3 Move the next largest disk to the only possible stack
2 else
1 Select the smallest disk at the top of the stacks
2 Move the smallest disk one position in the direction of the cyclic order
for odd order.
3 Move the next largest disk to the only possible stack
81 / 130
147. Images/cinvestav-
Process
IterativeHT using three stacks : towerA, towerB, towerC
1 for i = 1 to 2n−1
1 If (n%2 == 0)
1 Select the smallest disk at the top of the stacks
2 Move the smallest disk one position in the direction of the cyclic order
for even order.
3 Move the next largest disk to the only possible stack
2 else
1 Select the smallest disk at the top of the stacks
2 Move the smallest disk one position in the direction of the cyclic order
for odd order.
3 Move the next largest disk to the only possible stack
81 / 130
148. Images/cinvestav-
What about the Most Efficient Version?
You need a node state
1 Number of Disks
2 Origin Tower
3 Destination Tower
4 Temporary Tower
82 / 130
149. Images/cinvestav-
What about the Most Efficient Version?
You need a node state
1 Number of Disks
2 Origin Tower
3 Destination Tower
4 Temporary Tower
82 / 130
150. Images/cinvestav-
What about the Most Efficient Version?
You need a node state
1 Number of Disks
2 Origin Tower
3 Destination Tower
4 Temporary Tower
82 / 130
151. Images/cinvestav-
What about the Most Efficient Version?
You need a node state
1 Number of Disks
2 Origin Tower
3 Destination Tower
4 Temporary Tower
82 / 130
152. Images/cinvestav-
We have then the following logic procedure
Iterative-Hanoi(n)
1 let S be an stack
2 let Result be a empty string
3 S.push(Node(n, A, C, B, Result))
4 while S is not empty
5 v = S.pop()
6 if (v.n == 1)
7 print "Move one from v.Origin to v.Destination n"
8 else
9 S.push(Node(v.n-1, v.Temp, v.Destination, v.Origin))
10 S.push(Node(1, v.Origin, v.Destination, v.Temp))
11 S.push(Node(v.n-1,v.Origin, v.Temp, v.Destination))
83 / 130
153. Images/cinvestav-
We have then the following logic procedure
Iterative-Hanoi(n)
1 let S be an stack
2 let Result be a empty string
3 S.push(Node(n, A, C, B, Result))
4 while S is not empty
5 v = S.pop()
6 if (v.n == 1)
7 print "Move one from v.Origin to v.Destination n"
8 else
9 S.push(Node(v.n-1, v.Temp, v.Destination, v.Origin))
10 S.push(Node(1, v.Origin, v.Destination, v.Temp))
11 S.push(Node(v.n-1,v.Origin, v.Temp, v.Destination))
83 / 130
154. Images/cinvestav-
We have then the following logic procedure
Iterative-Hanoi(n)
1 let S be an stack
2 let Result be a empty string
3 S.push(Node(n, A, C, B, Result))
4 while S is not empty
5 v = S.pop()
6 if (v.n == 1)
7 print "Move one from v.Origin to v.Destination n"
8 else
9 S.push(Node(v.n-1, v.Temp, v.Destination, v.Origin))
10 S.push(Node(1, v.Origin, v.Destination, v.Temp))
11 S.push(Node(v.n-1,v.Origin, v.Temp, v.Destination))
83 / 130
155. Images/cinvestav-
We have then the following logic procedure
Iterative-Hanoi(n)
1 let S be an stack
2 let Result be a empty string
3 S.push(Node(n, A, C, B, Result))
4 while S is not empty
5 v = S.pop()
6 if (v.n == 1)
7 print "Move one from v.Origin to v.Destination n"
8 else
9 S.push(Node(v.n-1, v.Temp, v.Destination, v.Origin))
10 S.push(Node(1, v.Origin, v.Destination, v.Temp))
11 S.push(Node(v.n-1,v.Origin, v.Temp, v.Destination))
83 / 130
156. Images/cinvestav-
We have then the following logic procedure
Iterative-Hanoi(n)
1 let S be an stack
2 let Result be a empty string
3 S.push(Node(n, A, C, B, Result))
4 while S is not empty
5 v = S.pop()
6 if (v.n == 1)
7 print "Move one from v.Origin to v.Destination n"
8 else
9 S.push(Node(v.n-1, v.Temp, v.Destination, v.Origin))
10 S.push(Node(1, v.Origin, v.Destination, v.Temp))
11 S.push(Node(v.n-1,v.Origin, v.Temp, v.Destination))
83 / 130
157. Images/cinvestav-
We have then the following logic procedure
Iterative-Hanoi(n)
1 let S be an stack
2 let Result be a empty string
3 S.push(Node(n, A, C, B, Result))
4 while S is not empty
5 v = S.pop()
6 if (v.n == 1)
7 print "Move one from v.Origin to v.Destination n"
8 else
9 S.push(Node(v.n-1, v.Temp, v.Destination, v.Origin))
10 S.push(Node(1, v.Origin, v.Destination, v.Temp))
11 S.push(Node(v.n-1,v.Origin, v.Temp, v.Destination))
83 / 130
158. Images/cinvestav-
We have then the following logic procedure
Iterative-Hanoi(n)
1 let S be an stack
2 let Result be a empty string
3 S.push(Node(n, A, C, B, Result))
4 while S is not empty
5 v = S.pop()
6 if (v.n == 1)
7 print "Move one from v.Origin to v.Destination n"
8 else
9 S.push(Node(v.n-1, v.Temp, v.Destination, v.Origin))
10 S.push(Node(1, v.Origin, v.Destination, v.Temp))
11 S.push(Node(v.n-1,v.Origin, v.Temp, v.Destination))
83 / 130
159. Images/cinvestav-
We have then the following logic procedure
Iterative-Hanoi(n)
1 let S be an stack
2 let Result be a empty string
3 S.push(Node(n, A, C, B, Result))
4 while S is not empty
5 v = S.pop()
6 if (v.n == 1)
7 print "Move one from v.Origin to v.Destination n"
8 else
9 S.push(Node(v.n-1, v.Temp, v.Destination, v.Origin))
10 S.push(Node(1, v.Origin, v.Destination, v.Temp))
11 S.push(Node(v.n-1,v.Origin, v.Temp, v.Destination))
83 / 130
160. Images/cinvestav-
We have then the following logic procedure
Iterative-Hanoi(n)
1 let S be an stack
2 let Result be a empty string
3 S.push(Node(n, A, C, B, Result))
4 while S is not empty
5 v = S.pop()
6 if (v.n == 1)
7 print "Move one from v.Origin to v.Destination n"
8 else
9 S.push(Node(v.n-1, v.Temp, v.Destination, v.Origin))
10 S.push(Node(1, v.Origin, v.Destination, v.Temp))
11 S.push(Node(v.n-1,v.Origin, v.Temp, v.Destination))
83 / 130
161. Images/cinvestav-
We have then the following logic procedure
Iterative-Hanoi(n)
1 let S be an stack
2 let Result be a empty string
3 S.push(Node(n, A, C, B, Result))
4 while S is not empty
5 v = S.pop()
6 if (v.n == 1)
7 print "Move one from v.Origin to v.Destination n"
8 else
9 S.push(Node(v.n-1, v.Temp, v.Destination, v.Origin))
10 S.push(Node(1, v.Origin, v.Destination, v.Temp))
11 S.push(Node(v.n-1,v.Origin, v.Temp, v.Destination))
83 / 130
162. Images/cinvestav-
What about the Legend?
To solve the towers of Hanoi for 64 disks
We need ≈ 1.8 ∗ 1019 moves
With a computer making 109
moves/second
A computer would take about 570 years to complete.
At 1 disk move/min
The monks will take about 3.4 × 1013 years.
The sun will destroy the life on earth in 2.8 × 109.
84 / 130
163. Images/cinvestav-
What about the Legend?
To solve the towers of Hanoi for 64 disks
We need ≈ 1.8 ∗ 1019 moves
With a computer making 109
moves/second
A computer would take about 570 years to complete.
At 1 disk move/min
The monks will take about 3.4 × 1013 years.
The sun will destroy the life on earth in 2.8 × 109.
84 / 130
164. Images/cinvestav-
What about the Legend?
To solve the towers of Hanoi for 64 disks
We need ≈ 1.8 ∗ 1019 moves
With a computer making 109
moves/second
A computer would take about 570 years to complete.
At 1 disk move/min
The monks will take about 3.4 × 1013 years.
The sun will destroy the life on earth in 2.8 × 109.
84 / 130
166. Images/cinvestav-
Chess
As in the Tower of Hanoi
It is possible to devise a massive search solution based in the actual state
of the chess board.
State 0
86 / 130
167. Images/cinvestav-
Chess
As in the Tower of Hanoi
It is possible to devise a massive search solution based in the actual state
of the chess board.
State 0
86 / 130
168. Images/cinvestav-
Then
Something Notable
If you put 1 penny for the first square, 2 for next, 4 for next, 8 for
next, and so on.
You have
$3.6 ∗ 1017 (federal budget ~ 2 ∗ 1012) .
87 / 130
169. Images/cinvestav-
Then
Something Notable
If you put 1 penny for the first square, 2 for next, 4 for next, 8 for
next, and so on.
You have
$3.6 ∗ 1017 (federal budget ~ 2 ∗ 1012) .
87 / 130
175. Images/cinvestav-
Try-Throw-Catch
Thus
1 When you enter a try block, push the address of this block on a stack.
2 When an exception is thrown, pop the try block that is at the top of
the stack (if the stack is empty, terminate).
3 If the popped try block has no matching catch block, go back to the
preceding step.
4 If the popped try block has a matching catch block, execute the
matching catch block.
93 / 130
176. Images/cinvestav-
Try-Throw-Catch
Thus
1 When you enter a try block, push the address of this block on a stack.
2 When an exception is thrown, pop the try block that is at the top of
the stack (if the stack is empty, terminate).
3 If the popped try block has no matching catch block, go back to the
preceding step.
4 If the popped try block has a matching catch block, execute the
matching catch block.
93 / 130
177. Images/cinvestav-
Try-Throw-Catch
Thus
1 When you enter a try block, push the address of this block on a stack.
2 When an exception is thrown, pop the try block that is at the top of
the stack (if the stack is empty, terminate).
3 If the popped try block has no matching catch block, go back to the
preceding step.
4 If the popped try block has a matching catch block, execute the
matching catch block.
93 / 130
178. Images/cinvestav-
Try-Throw-Catch
Thus
1 When you enter a try block, push the address of this block on a stack.
2 When an exception is thrown, pop the try block that is at the top of
the stack (if the stack is empty, terminate).
3 If the popped try block has no matching catch block, go back to the
preceding step.
4 If the popped try block has a matching catch block, execute the
matching catch block.
93 / 130
190. Images/cinvestav-
Derivation From ArrayLinearList
We can do the following
Stack top is either left end or right end of linear list
For any possible top
empty() =⇒ isEmpty()
O(1) time
peek() =⇒ get(0) or get(size() - 1)
O(1) time
105 / 130
191. Images/cinvestav-
Derivation From ArrayLinearList
We can do the following
Stack top is either left end or right end of linear list
For any possible top
empty() =⇒ isEmpty()
O(1) time
peek() =⇒ get(0) or get(size() - 1)
O(1) time
105 / 130
192. Images/cinvestav-
Derivation From ArrayLinearList
We can do the following
Stack top is either left end or right end of linear list
For any possible top
empty() =⇒ isEmpty()
O(1) time
peek() =⇒ get(0) or get(size() - 1)
O(1) time
105 / 130
193. Images/cinvestav-
Derivation From ArrayLinearList
We can do the following
Stack top is either left end or right end of linear list
For any possible top
empty() =⇒ isEmpty()
O(1) time
peek() =⇒ get(0) or get(size() - 1)
O(1) time
105 / 130
194. Images/cinvestav-
Derivation From ArrayLinearList
We can do the following
Stack top is either left end or right end of linear list
For any possible top
empty() =⇒ isEmpty()
O(1) time
peek() =⇒ get(0) or get(size() - 1)
O(1) time
105 / 130
205. Images/cinvestav-
Thus
The Moral of the Story
You must use the right end of list as top of stack
That is way
STACK
DATA
TEXT
HEAP
0
High Memory
Growing Directions
108 / 130
207. Images/cinvestav-
Derivation from a Chain
Stack top is either left end or right end of linear list
CA EDB
firstNode NULL
Thus
empty() =⇒ isEmpty()
O(1) time
110 / 130
208. Images/cinvestav-
Derivation from a Chain
Stack top is either left end or right end of linear list
CA EDB
firstNode NULL
Thus
empty() =⇒ isEmpty()
O(1) time
110 / 130
209. Images/cinvestav-
Derivation from a Chain
Stack top is either left end or right end of linear list
CA EDB
firstNode NULL
Thus
empty() =⇒ isEmpty()
O(1) time
110 / 130
210. Images/cinvestav-
Derivation from a Chain
When top is left end of linear list
CA EDB
firstNode NULL
Thus
peek() =⇒ get(0)
O(1) time
push(theObject) =⇒ add(0, theObject)
O(1) time
pop() =⇒ remove(0)
O(1) time
111 / 130
211. Images/cinvestav-
Derivation from a Chain
When top is left end of linear list
CA EDB
firstNode NULL
Thus
peek() =⇒ get(0)
O(1) time
push(theObject) =⇒ add(0, theObject)
O(1) time
pop() =⇒ remove(0)
O(1) time
111 / 130
212. Images/cinvestav-
Derivation from a Chain
When top is left end of linear list
CA EDB
firstNode NULL
Thus
peek() =⇒ get(0)
O(1) time
push(theObject) =⇒ add(0, theObject)
O(1) time
pop() =⇒ remove(0)
O(1) time
111 / 130
213. Images/cinvestav-
Derivation from a Chain
When top is left end of linear list
CA EDB
firstNode NULL
Thus
peek() =⇒ get(0)
O(1) time
push(theObject) =⇒ add(0, theObject)
O(1) time
pop() =⇒ remove(0)
O(1) time
111 / 130
214. Images/cinvestav-
Derivation from a Chain
When top is left end of linear list
CA EDB
firstNode NULL
Thus
peek() =⇒ get(0)
O(1) time
push(theObject) =⇒ add(0, theObject)
O(1) time
pop() =⇒ remove(0)
O(1) time
111 / 130
215. Images/cinvestav-
Derivation from a Chain
When top is left end of linear list
CA EDB
firstNode NULL
Thus
peek() =⇒ get(0)
O(1) time
push(theObject) =⇒ add(0, theObject)
O(1) time
pop() =⇒ remove(0)
O(1) time
111 / 130
216. Images/cinvestav-
Derivation from a Chain
When top is left end of linear list
CA EDB
firstNode NULL
Thus
peek() =⇒ get(0)
O(1) time
push(theObject) =⇒ add(0, theObject)
O(1) time
pop() =⇒ remove(0)
O(1) time
111 / 130
217. Images/cinvestav-
Derivation from a Chain
When top is left end of linear list
CA EDB
firstNode NULL
Thus
peek() =⇒ get(size()-1)
O(size) time
push(theObject) =⇒ add(size(), theObject)
O(size) time
pop() =⇒ remove(size()-1)
O(size) time
112 / 130
218. Images/cinvestav-
Derivation from a Chain
When top is left end of linear list
CA EDB
firstNode NULL
Thus
peek() =⇒ get(size()-1)
O(size) time
push(theObject) =⇒ add(size(), theObject)
O(size) time
pop() =⇒ remove(size()-1)
O(size) time
112 / 130
219. Images/cinvestav-
Derivation from a Chain
When top is left end of linear list
CA EDB
firstNode NULL
Thus
peek() =⇒ get(size()-1)
O(size) time
push(theObject) =⇒ add(size(), theObject)
O(size) time
pop() =⇒ remove(size()-1)
O(size) time
112 / 130
220. Images/cinvestav-
Derivation from a Chain
When top is left end of linear list
CA EDB
firstNode NULL
Thus
peek() =⇒ get(size()-1)
O(size) time
push(theObject) =⇒ add(size(), theObject)
O(size) time
pop() =⇒ remove(size()-1)
O(size) time
112 / 130
221. Images/cinvestav-
Derivation from a Chain
When top is left end of linear list
CA EDB
firstNode NULL
Thus
peek() =⇒ get(size()-1)
O(size) time
push(theObject) =⇒ add(size(), theObject)
O(size) time
pop() =⇒ remove(size()-1)
O(size) time
112 / 130
222. Images/cinvestav-
Derivation from a Chain
When top is left end of linear list
CA EDB
firstNode NULL
Thus
peek() =⇒ get(size()-1)
O(size) time
push(theObject) =⇒ add(size(), theObject)
O(size) time
pop() =⇒ remove(size()-1)
O(size) time
112 / 130
223. Images/cinvestav-
Derivation from a Chain
When top is left end of linear list
CA EDB
firstNode NULL
Thus
peek() =⇒ get(size()-1)
O(size) time
push(theObject) =⇒ add(size(), theObject)
O(size) time
pop() =⇒ remove(size()-1)
O(size) time
112 / 130
225. Images/cinvestav-
Derive From ArrayLinearList
Code
package MyStack ;
import java . u t i l . ∗ ; // I t has stack e x c e p t i o n
p u b l i c c l a s s DerivedArrayStack extends
A r r a y L i n e a r L i s t <Item>
{
// c o n s t r u c t o r s come here
// Stack i n t e r f a c e methods come here
}
114 / 130
226. Images/cinvestav-
Derive From ArrayLinearList
Constructors
/∗∗ c r e a t e a stack with the given i n i t i a l
∗ c a p a c i t y ∗/
p u b l i c DerivedArrayStack ( i n t i n i t i a l C a p a c i t y )
{ super ( i n i t i a l C a p a c i t y ) ; }
/∗∗ c r e a t e a stack with i n i t i a l c a p a c i t y 10 ∗/
p u b l i c DerivedArrayStack ()
{ t h i s ( 1 0 ) ; }
115 / 130
227. Images/cinvestav-
empty() And peek()
Code
p u b l i c boolean empty ()
{ r e t u r n isEmpty ( ) ; }
p u b l i c Object peek ()
{
i f ( empty ( ) )
throw new EmptyStackException ( ) ;
r e t u r n get ( s i z e () − 1 ) ;
}
116 / 130
228. Images/cinvestav-
push(theObject) And pop()
Code
p u b l i c void push ( Object theElement )
{add ( s i z e ( ) , theElement ) ; }
p u b l i c Object pop ()
{
i f ( empty ( ) )
throw new EmptyStackException ( ) ;
r e t u r n remove ( s i z e () − 1 ) ;
}
117 / 130
229. Images/cinvestav-
The Pros
The merits of deriving from ArrayLinearList
Code for derived class is quite simple and easy to develop.
Code is expected to require little debugging.
Code for other stack implementations such as a linked
implementation are easily obtained.
Just replace extends ArrayLinearList with extends Chain.
For efficiency reasons we must also make changes to use the left end of
the list as the stack top rather than the right end.
118 / 130
230. Images/cinvestav-
The Pros
The merits of deriving from ArrayLinearList
Code for derived class is quite simple and easy to develop.
Code is expected to require little debugging.
Code for other stack implementations such as a linked
implementation are easily obtained.
Just replace extends ArrayLinearList with extends Chain.
For efficiency reasons we must also make changes to use the left end of
the list as the stack top rather than the right end.
118 / 130
231. Images/cinvestav-
The Pros
The merits of deriving from ArrayLinearList
Code for derived class is quite simple and easy to develop.
Code is expected to require little debugging.
Code for other stack implementations such as a linked
implementation are easily obtained.
Just replace extends ArrayLinearList with extends Chain.
For efficiency reasons we must also make changes to use the left end of
the list as the stack top rather than the right end.
118 / 130
232. Images/cinvestav-
The Pros
The merits of deriving from ArrayLinearList
Code for derived class is quite simple and easy to develop.
Code is expected to require little debugging.
Code for other stack implementations such as a linked
implementation are easily obtained.
Just replace extends ArrayLinearList with extends Chain.
For efficiency reasons we must also make changes to use the left end of
the list as the stack top rather than the right end.
118 / 130
233. Images/cinvestav-
The Pros
The merits of deriving from ArrayLinearList
Code for derived class is quite simple and easy to develop.
Code is expected to require little debugging.
Code for other stack implementations such as a linked
implementation are easily obtained.
Just replace extends ArrayLinearList with extends Chain.
For efficiency reasons we must also make changes to use the left end of
the list as the stack top rather than the right end.
118 / 130
234. Images/cinvestav-
The Cons
Then
All public methods of ArrayLinearList are performed following the
stack structure:
get(0) . . . get bottom element
remove(5)... pop
add(3, x) ... push
So we do not have a true stack implementation.
We must override undesired methods.
119 / 130
235. Images/cinvestav-
The Cons
Then
All public methods of ArrayLinearList are performed following the
stack structure:
get(0) . . . get bottom element
remove(5)... pop
add(3, x) ... push
So we do not have a true stack implementation.
We must override undesired methods.
119 / 130
236. Images/cinvestav-
The Cons
Then
All public methods of ArrayLinearList are performed following the
stack structure:
get(0) . . . get bottom element
remove(5)... pop
add(3, x) ... push
So we do not have a true stack implementation.
We must override undesired methods.
119 / 130
237. Images/cinvestav-
The Cons
Then
All public methods of ArrayLinearList are performed following the
stack structure:
get(0) . . . get bottom element
remove(5)... pop
add(3, x) ... push
So we do not have a true stack implementation.
We must override undesired methods.
119 / 130
238. Images/cinvestav-
The Cons
Then
All public methods of ArrayLinearList are performed following the
stack structure:
get(0) . . . get bottom element
remove(5)... pop
add(3, x) ... push
So we do not have a true stack implementation.
We must override undesired methods.
119 / 130
239. Images/cinvestav-
The Cons
Unnecessary work is done by the code.
Examples
Example I
peek() verifies that the stack is not empty before get is invoked.
The index check done by get is, therefore, not needed.
Example II
add(size(), theElement) does an index check and a for loop that is
not entered.
Neither is needed.
120 / 130
240. Images/cinvestav-
The Cons
Unnecessary work is done by the code.
Examples
Example I
peek() verifies that the stack is not empty before get is invoked.
The index check done by get is, therefore, not needed.
Example II
add(size(), theElement) does an index check and a for loop that is
not entered.
Neither is needed.
120 / 130
241. Images/cinvestav-
The Cons
Unnecessary work is done by the code.
Examples
Example I
peek() verifies that the stack is not empty before get is invoked.
The index check done by get is, therefore, not needed.
Example II
add(size(), theElement) does an index check and a for loop that is
not entered.
Neither is needed.
120 / 130
242. Images/cinvestav-
The Cons
Unnecessary work is done by the code.
Examples
Example I
peek() verifies that the stack is not empty before get is invoked.
The index check done by get is, therefore, not needed.
Example II
add(size(), theElement) does an index check and a for loop that is
not entered.
Neither is needed.
120 / 130
243. Images/cinvestav-
The Cons
Unnecessary work is done by the code.
Examples
Example I
peek() verifies that the stack is not empty before get is invoked.
The index check done by get is, therefore, not needed.
Example II
add(size(), theElement) does an index check and a for loop that is
not entered.
Neither is needed.
120 / 130
245. Images/cinvestav-
Moral of the Story
First
Code developed from scratch will run faster but will take more time (cost)
to develop.
Second
Tradeoff between software development cost and performance.
Third
Tradeoff between time to market and performance.
Fourth
It could be easy to develop the code first and later refine it to improve
performance.
122 / 130
246. Images/cinvestav-
Moral of the Story
First
Code developed from scratch will run faster but will take more time (cost)
to develop.
Second
Tradeoff between software development cost and performance.
Third
Tradeoff between time to market and performance.
Fourth
It could be easy to develop the code first and later refine it to improve
performance.
122 / 130
247. Images/cinvestav-
Moral of the Story
First
Code developed from scratch will run faster but will take more time (cost)
to develop.
Second
Tradeoff between software development cost and performance.
Third
Tradeoff between time to market and performance.
Fourth
It could be easy to develop the code first and later refine it to improve
performance.
122 / 130
248. Images/cinvestav-
Moral of the Story
First
Code developed from scratch will run faster but will take more time (cost)
to develop.
Second
Tradeoff between software development cost and performance.
Third
Tradeoff between time to market and performance.
Fourth
It could be easy to develop the code first and later refine it to improve
performance.
122 / 130
249. Images/cinvestav-
Example
A slow pop
1 if (empty())
2 throw new EmptyStackException();
3 return remove(size() - 1);
Faster Code
1 try {return remove(size() - 1);}
2 catch(IndexOutOfBoundsException e)
3 {throw new EmptyStackException();}
123 / 130
250. Images/cinvestav-
Example
A slow pop
1 if (empty())
2 throw new EmptyStackException();
3 return remove(size() - 1);
Faster Code
1 try {return remove(size() - 1);}
2 catch(IndexOutOfBoundsException e)
3 {throw new EmptyStackException();}
123 / 130
251. Images/cinvestav-
Example
A slow pop
1 if (empty())
2 throw new EmptyStackException();
3 return remove(size() - 1);
Faster Code
1 try {return remove(size() - 1);}
2 catch(IndexOutOfBoundsException e)
3 {throw new EmptyStackException();}
123 / 130
252. Images/cinvestav-
Example
A slow pop
1 if (empty())
2 throw new EmptyStackException();
3 return remove(size() - 1);
Faster Code
1 try {return remove(size() - 1);}
2 catch(IndexOutOfBoundsException e)
3 {throw new EmptyStackException();}
123 / 130
253. Images/cinvestav-
Example
A slow pop
1 if (empty())
2 throw new EmptyStackException();
3 return remove(size() - 1);
Faster Code
1 try {return remove(size() - 1);}
2 catch(IndexOutOfBoundsException e)
3 {throw new EmptyStackException();}
123 / 130
254. Images/cinvestav-
Example
A slow pop
1 if (empty())
2 throw new EmptyStackException();
3 return remove(size() - 1);
Faster Code
1 try {return remove(size() - 1);}
2 catch(IndexOutOfBoundsException e)
3 {throw new EmptyStackException();}
123 / 130
255. Images/cinvestav-
Code From Scratch
First
Use a 1D array stack whose data type is Item.
same as using array element in ArrayLinearList
Second
Use an int variable top.
Stack elements are in stack[0:top].
Top element is in stack[top].
Bottom element is in stack[0].
Stack is empty iff top = -1.
Number of elements in stack is top+1.
124 / 130
256. Images/cinvestav-
Code From Scratch
First
Use a 1D array stack whose data type is Item.
same as using array element in ArrayLinearList
Second
Use an int variable top.
Stack elements are in stack[0:top].
Top element is in stack[top].
Bottom element is in stack[0].
Stack is empty iff top = -1.
Number of elements in stack is top+1.
124 / 130
257. Images/cinvestav-
Code From Scratch
First
Use a 1D array stack whose data type is Item.
same as using array element in ArrayLinearList
Second
Use an int variable top.
Stack elements are in stack[0:top].
Top element is in stack[top].
Bottom element is in stack[0].
Stack is empty iff top = -1.
Number of elements in stack is top+1.
124 / 130
258. Images/cinvestav-
Code From Scratch
First
Use a 1D array stack whose data type is Item.
same as using array element in ArrayLinearList
Second
Use an int variable top.
Stack elements are in stack[0:top].
Top element is in stack[top].
Bottom element is in stack[0].
Stack is empty iff top = -1.
Number of elements in stack is top+1.
124 / 130
259. Images/cinvestav-
Code From Scratch
First
Use a 1D array stack whose data type is Item.
same as using array element in ArrayLinearList
Second
Use an int variable top.
Stack elements are in stack[0:top].
Top element is in stack[top].
Bottom element is in stack[0].
Stack is empty iff top = -1.
Number of elements in stack is top+1.
124 / 130
260. Images/cinvestav-
Code From Scratch
First
Use a 1D array stack whose data type is Item.
same as using array element in ArrayLinearList
Second
Use an int variable top.
Stack elements are in stack[0:top].
Top element is in stack[top].
Bottom element is in stack[0].
Stack is empty iff top = -1.
Number of elements in stack is top+1.
124 / 130
261. Images/cinvestav-
Code From Scratch
First
Use a 1D array stack whose data type is Item.
same as using array element in ArrayLinearList
Second
Use an int variable top.
Stack elements are in stack[0:top].
Top element is in stack[top].
Bottom element is in stack[0].
Stack is empty iff top = -1.
Number of elements in stack is top+1.
124 / 130
262. Images/cinvestav-
Code From Scratch
First
Use a 1D array stack whose data type is Item.
same as using array element in ArrayLinearList
Second
Use an int variable top.
Stack elements are in stack[0:top].
Top element is in stack[top].
Bottom element is in stack[0].
Stack is empty iff top = -1.
Number of elements in stack is top+1.
124 / 130
263. Images/cinvestav-
Code From Scratch
Data Memember
package Stack ;
import java . u t i l . EmptyStackException ;
import u t i l i t i e s . ∗ ;
p u b l i c c l a s s ArrayStack<Item>
implements Stack<Item>
{
// data members
i n t top ; // c u r r e n t top of stack
Item [ ] stack ; // element a r r a y
// Etc
}
125 / 130
264. Images/cinvestav-
Constructors
Code
p u b l i c ArrayStack ( i n t i n i t i a l C a p a c i t y )
{
i f ( i n i t i a l C a p a c i t y < 1)
throw new I l l e g a l A r g u m e n t E x c e p t i o n
( " i n i t i a l C a p a c i t y ␣must␣be␣>=␣1" ) ;
t h i s . stack = ( Item [ ] ) new Object [ i n i t i a l C a p a c i t y ] ;
top = −1;
}
p u b l i c ArrayStack ()
{ t h i s ( 1 0 ) ; }
126 / 130
265. Images/cinvestav-
Push(...)
Code
p u b l i c void push ( Object theElement )
{
// i n c r e a s e a r r a y s i z e i f n e c e s s a r y
i f ( top == stack . le ngth − 1)
stack = ChangeArrayLength . changeLength1D
( stack , 2 ∗ stack . l ength ) ;
// put theElement at the top of the stack
stack[++top ] = theElement ;
}
Thus
127 / 130
266. Images/cinvestav-
Pop()
Code
p u b l i c Item pop ()
{
i f ( empty ( ) )
throw new EmptyStackException ( ) ;
Object topElement = stack [ top ] ;
stack [ top −−] = n u l l ; // enable garbage c o l l e c t i o n
r e t u r n topElement ;
}
Thus
128 / 130
267. Images/cinvestav-
Actually
We have the following
java.util.Stack
It derives from java.util.Vector.
java.util.Vector is an array implementation of a linear list.
129 / 130
268. Images/cinvestav-
Performance of 500,000 pop, push, and peek operations
We have
Class 500,000
DerivedArrayStack 0.38 s
Scratch Array Stack 0.22 s
DerivedArrayStackWithCatch 0.33 s
DerivedLinkedStack 3.20 s
Scratch LinkedStack 2.96 s
130 / 130