Download Presentation
## Gyan from the Geek

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -

**Gyan from the Geek**ApoorvaVadi M.S Information Systems New York University**Algorithms…**What are those things?**An algorithm…is like a recipe**Problem solving as an analogy to cooking: 1. Inputs 2. Recipe/Set of defined rules (or an Algorithm) 3. Outputs**Now lets use algorithms…**To solve Puzzles! • Towers of Hanoi • Dining Philosophers • Travelling salesman • Eight queens**Towers of Hanoi**You have a stack of discs, from largest to smallest, that slide on to the first peg of a three peg board. Your goal is to move the entire stack of discs from the first peg to the third peg. However, you can only move the topmost disc of any peg, and smaller discs must always be placed on larger discs. How many moves will it take?**Now lets write a recipe for this…**Alternating between the smallest and the next-smallest disks, follow the steps for the appropriate case: • For an even number of disks: • make the legal move between pegs A and B • make the legal move between pegs A and C • make the legal move between pegs B and C • repeat until complete For an odd number of disks: • make the legal move between pegs A and C • make the legal move between pegs A and B • make the legal move between pegs B and C • repeat until complete • In each case, a total of 2n-1 moves are made.**But that was too long… lets try recursion…**Step 1: Move N−1 discs from A to B. This leaves Nth disc alone on peg A. Step 2: Move Nth disc from A to C Step 3: Move N−1 discs from B to C so they sit on disc N.**Dining Philosophers Problem**Lets try something ‘non-arkymalarky’: Five philosophers sit around a circular table. In front of each philosopher is a large plate of rice. The philosophers alternate their time between eating and thinking. There is one chopstick between each philosopher, to their immediate right and left. In order to eat, a given philosopher needs to use both chopsticks. How can you ensure all the philosophers can eat reliably without starving to death?**So this is a classic example of a common computing problem**in concurrency… Issues: • Deadlock - cycle of unwarranted requests. Every philosopher picked up a left fork and waits for a right fork (forever). • Resource Starvation – one philosopher might have to wait extended amounts of time. • Mutual exclusion – multiple processes accessing sets of data.**Welcome to the world of Data Structures**• Stacks • Queues • Linked Lists • Trees Have Fun**Applications of Stacks**• Direct applications Page-visited history in a Web brows Undo sequence in a text editor • Indirect applications Component of other data structures**Applications of Queues**• Direct application Waiting lines Access to shared resources (e.g., printer) Multiprogramming • Indirect applications Component of other data structures**List**• A singly linked list is a concrete data structure • consisting of a sequence of nodes • -Each node stores element • -link to the next node**We can implement a queue with a singly linked list -The**front element is stored at the first node-The rear element is stored at the last node Queue with a Singly Linked List**Doubly Linked List**• A doubly linked list provides a natural implementation of the List ADT • Nodes implement Position and store: -element -link to the previous node -link to the next node • Special trailer and header nodes**Trees**• In computer science, a tree is an abstract model of a hierarchical • structure • -A tree consists of nodes with a parent-child • relation • Applications: • -Organization charts • -File systems • -Programming environments**Binary Trees**• A binary tree is a tree with the following properties: -Each internal node has two children -The children of a node are an ordered pair - We call the children of an internal • node left child and right child - Alternative recursive definition: a • binary tree is either -a tree consisting of a single node, or - a tree whose root has an ordered pair of children, each of which is a binary tree • Applications: -arithmetic expressions -decision processes -searching**Tree Traversals(power of recursion)**Depth First Search • Pre-order(NLR): Root Node- Left child- Right child A-B-D-E-H-I-C-F-G • In-order(LNR): Left child - Root Node - Right child D-B-H-E-I-A-F-C-G • Post-order(LRN): Left child - Right child – Root Node D-H-I-E-B-F-G-C-A Breadth First Traversal • Level order Traversal: Traverse each node level by level A-B-C-D-E-F-G-H-I**Links**• http://www.youtube.com/watch?v=jq_EcstLlfE&feature=related • http://www.youtube.com/watch?v=GYAPIxiTjww&playnext_from=TL&videos=b-s7rzbuXy0