8 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
9 Now lets use algorithms… To solve Puzzles!Towers of HanoiDining PhilosophersTravelling salesmanEight queens
10 Towers of HanoiYou 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?
12 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 Bmake the legal move between pegs A and Cmake the legal move between pegs B and Crepeat until completeFor an odd number of disks:In each case, a total of 2n-1 moves are made.
13 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.
14 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?
15 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.
17 Welcome to the world of Data Structures StacksQueuesLinked ListsTreesHave Fun
18 Applications of Stacks Direct applicationsPage-visited history in a Web browsUndo sequence in a text editorIndirect applicationsComponent of other data structures
19 Applications of Queues Direct applicationWaiting linesAccess to shared resources (e.g., printer)MultiprogrammingIndirect applicationsComponent of other data structures
20 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
21 Queue with a Singly Linked List 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
22 Doubly Linked ListA doubly linked list provides a natural implementation of the List ADTNodes implement Position and store:-element-link to the previous node-link to the next nodeSpecial trailer and header nodes
24 TreesIn computer science, a tree is an abstract model of a hierarchicalstructure-A tree consists of nodes with a parent-childrelationApplications:-Organization charts-File systems-Programming environments
25 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 internalnode left child and right child- Alternative recursive definition: abinary 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 treeApplications:-arithmetic expressions-decision processes-searching
27 Tree Traversals (power of recursion) Depth First SearchPre-order(NLR): Root Node- Left child- Right childA-B-D-E-H-I-C-F-GIn-order(LNR): Left child - Root Node - Right childD-B-H-E-I-A-F-C-GPost-order(LRN): Left child - Right child – Root NodeD-H-I-E-B-F-G-C-ABreadth First TraversalLevel order Traversal: Traverse each node level by levelA-B-C-D-E-F-G-H-I