# Can you design an algorithm that searches for the maximum value in the list?

Save this PDF as:

Size: px
Start display at page:

Download "Can you design an algorithm that searches for the maximum value in the list?"

## Transcription

1 The Science of Computing I Lesson 2: Searching and Sorting Living with Cyber Pillar: Algorithms Searching One of the most common tasks that is undertaken in Computer Science is the task of searching. We can search for many things: a value (like a phone number), a number (like seven), a position (like who finished the race in second place), or an object (like an image of a dog). Typically, searching will require doing a specific task over and over again (i.e., searching in one position, and then searching in another position), until we find what we are looking for. This requires repetition. We have already learned how to deal with and represent repetition. Our first task is to design an algorithm that can be used to search for a given value. Activity 1 For this activity, you will participate in the demonstration of an algorithm to find some value (specifically, the maximum value) in a list of values. Approximately ten students will be asked to stand in front of the class. These will be known as memory students; that is, they represent a subset of a computer's memory. Each memory student will secretly write a number of their choosing on a post-it note. Collectively, this represents a list of numbers stored in the computer's memory. One other student, known as the computer student, will represent the computer as it executes the algorithm. This student will follow instructions (like a computer would) and keep track of a single value representing the current maximum value in the list of numbers. This student will have a few postit notes (or a small dry-erase board) to keep up with the current maximum value. The computer student understands several instructions: START instructs the computer student to go to the beginning of the memory student line; MOVE instructs the computer student to move to the next memory student in the line; COMPARE instructs the computer student to compare the number of the current memory student to the one currently recorded and respond with greater, if the current memory student's number is greater, or less, otherwise; STORE instructs the computer student to record the current memory student's number; and DISPLAY instructs the computer student to display the currently recorded value. Can you design an algorithm that searches for the maximum value in the list? Gourd, Kiremire, O'Neal 1 Last modified: 13 Oct 2015

2 Sequential search The activity above was a demonstration of the sequential search. Sequential searching has a lot of applications in computer science. Definition: Sequential search (also known as linear search) is the process of locating a value in a list of values by checking each element, one at a time, until either the value is located or the end of the list has been reached. The algorithm described in the activity above can be represented more generally in pseudocode as follows: 1: i 1 2: max value of item i in the list 3: repeat 4: increment i 5: if value of item i in the list > max 6: then 7: max value of item i in the list 8: end 9: until i = the length of the list 10: display max This algorithm searches for (and displays) the largest value in a list of numbers. How could it be modified to search for (and display) the smallest value in a list of numbers? Try to modify the algorithm above in the space below: 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: The sequential search can also be used to find a specific value (such as 100) instead of something more general as we did above (such as the maximum value). How could the algorithm that finds the maximum value be modified to search for a specified value and display whether it was found (or not)? Try to modify the algorithm above in the space below: Gourd, Kiremire, O'Neal 2 Last modified: 13 Oct 2015

3 Is this algorithm efficient? What happens if the value searched for is not in the list? What happens if the value searched for is near the beginning of the list? We observe that, if the value searched for is near the beginning of the list, the algorithm sets the variable found to true but continues to look through the remainder of the list. This is not very efficient. Consider a list of 1 million values such that the value searched for happens to be found at the beginning of the list. The algorithm above would absolutely find the value at the beginning of the list; however, it would then continue to needlessly search through the remaining 999,999 values in the list. There is no stop condition in the case that the value searched for is found. We can fix this quite easily, however, by slightly modifying the algorithm: 1: n value to search for 2: i 1 3: found false 4: repeat 5: if value of item i in the list = n 6: then 7: found true 8: else 9: increment i 10: end 11: until i > the length of the list or found = true 12: display found By adding an additional exit condition (something that terminates the repetition), we can capture the case that the value is found at any point in the list and immediately terminate the search. When designing algorithms, we must carefully examine them to find inconsistencies, problems, and to ensure that they, in fact, solve the problem. In addition, we must not forget to consider efficiency, performance, and scalability. For a very short list, efficiency may not matter; however, we don't usually deal with small amounts of data in practice. Here is an example of the sequential search for the value 80 applied to a list containing the following values: 10, 20, 30, 40, 50, 60, 70, 80, 90, 100: Search List Comparison Action 10, 20, 30, 40, 50, 60, 70, 80, 90, Target not found; continue 10, 20, 30, 40, 50, 60, 70, 80, 90, Target not found; continue Gourd, Kiremire, O'Neal 3 Last modified: 13 Oct 2015

5 requires searching through the entire list). On average, the algorithm would have to search through about half of the list. Consider the scenario of searching for a name that corresponds to a specific phone number in a phone book. This is the reverse of what is normally done (i.e., searching for a phone number that corresponds to a given name). How could this be done? The only way is to perform a sequential search, starting at the beginning of the phone book. This could take a long time, and it would be utterly depressing if the phone number was not found in the phone book. While the sequential search is effective, there are scenarios (like this one) where a more efficient way of searching is preferable. Consider the normal approach to searching a phone book for a phone number that corresponds to a given name. A sequential search would require starting the search at the beginning of the phone book and continue until either the name is found or until the entire phone book is exhausted. However, a phone book has a specific quality that can be taken advantage of which makes searching significantly easier and faster: it is ordered in a meaningful way. The names in a phone book are arranged in alphabetical order. This makes searching easier in that only a small subset of the names have to actually be searched through. When searching through a phone book, the usual process is to thumb through it until the first letter of the given name is reached. From there, a sequential search (of sorts) is performed to search for the given name. A method that may be better suited for computing is to open the phone book in the middle. If the given name starts with a letter in the alphabet that comes before the one that is there, then the right half of the phone book can be ignored. With a single comparison, half of the phone book has been eliminated. This strategy can be continued by shifting to the part of the phone book that represents the halfway point in the first half of the phone book. Another comparison is performed to determine if the given name appears before or after this point. This continues until the given name is found. With each check, half of the remaining portion is eliminated. This strategy can be used to guess a number from one to 1,000 much more efficiently than by doing a sequential search. So how is the halfway point (or middle value) calculated? Given a list of n items, we calculate the middle as follows: n 2 +1 Note that the brackets represent the floor function which means to round down to the largest previous integer. For example, the floor of 3.14 is 3, and the floor of 27.9 is 27. Some of you may have naturally implemented this algorithm in the activity above. Let's formally define it now in pseudocode: 1: repeat 2: n number of items in the current portion of the list 3: mid floor(n / 2) + 1 4: guess mid 5: if response is HIGHER 6: then 7: discard the left half of the list 8: else if response is LOWER Gourd, Kiremire, O'Neal 5 Last modified: 13 Oct 2015

7 then to 2,500, then to 1,250, then to 625, then to 313, then to 157, then to 79, then to 40, then to 20, then to 10, then to 5, then to 3, then to 2, and finally to 1 (14 total splits). We can actually calculate this precisely by solving the following equation: 2 n = We can visualize how the binary search works to find a value in a list of 10,000 values by illustrating each guess (the middle value): Comparison List size Items on left Middle value Items on right Remaining items 1 10,000 5,000 5,001 4,999 5, ,000 2,500 2,501 2,499 2, ,500 1,250 1,251 1,249 1, , Consider a simpler problem of guessing a number from 1 to 1,000. How many guesses would that take? We know that 2 10 = 1024 and that 2 9 = 512; therefore, 1,000 can be divided by two between 9 and 10 times. However, it's evident that it's closer to 10 than it is to 9. In fact, it is actually 9.97 times. Recall that, on average, it would take 500 guesses if the sequential search were used instead. The binary search is therefore 50 times faster than the sequential search for a list of 1,000 values (500 guesses / 10 guesses = 50). What about the comparison for a list of 1 billion values? The sequential search would take, on average, 500 million comparisons. The binary search would take, at worst, 30 comparisons. That's almost 17 million times faster! Did you know? To solve for n in 2 n = 1000, we can use the formula that expresses the inverse of raising two to a power: log 2 n = Logarithms represent the power to which some number, called the base (in this case, 2), must be raised to produce a given number (in this case, 1,000). Most calculators do not have a log 2 function; however, they typically do have a log 10 function (note that most calculators denote this as log and omit the base). We can convert easily by using the following conversion (where b is the given base and d is the desired base): Gourd, Kiremire, O'Neal 7 Last modified: 13 Oct 2015

8 log b x= log d x log d b In the example above where the base is 2, the desired base is 10, and x is 1000, we can convert as follows: log = log log 10 2 =9.97 For giggles, how many tries would it take to guess a number from one to 1 billion? log 2 1billion= log 1billion 10 =29.9 log 10 2 On average, searching for a target value in a list containing n items requires, at maximum, the following number of comparisons: log 2 (n+1) Note that the brackets represent the ceiling function which means to round up to the smallest following integer. For example, the ceiling of 3.14 is 4, and the ceiling of 27.1 is 28. The following table shows the number of comparisons required to search through a list of values ranging from 0 to 10,000 items using both the sequential and binary search. It also includes a performance measure that compares how much better the binary search is when compared to the sequential search. It is amazing to see the huge difference in the performance of the two algorithms: Number of items Sequential search Binary search Performance , ,000 1, ,000 1, ,000 2, ,000 2, ,000 3, ,000 3, ,000 4, ,000 4, ,000 5, For a list of 1,000 items, the binary search is roughly 50 times faster than the sequential search, and for a list of 10,000 items, the binary search is roughly 376 times faster. This, however, does not take into Gourd, Kiremire, O'Neal 8 Last modified: 13 Oct 2015

9 consideration that the binary search takes extra calculations (i.e., calculating the middle of the current portion of the list, discarding half of the list, etc). However, suppose that each binary search comparison takes 1/10 of a second and each sequential search comparison takes 1/1000 of a second. We can still observe that the binary search ridiculously outperforms the sequential search as illustrated in Figure 1. Also note that the time it takes to initially order the list of numbers is not considered. Since the binary search requires the list to be ordered, it is worthwhile to investigate various ordering methods and their performance. Sorting We have seen that the binary search is significantly faster than the sequential search. Why then do we even need to know about the sequential search? Why not use the binary search every time we need to search for something? What weaknesses does the binary search have? What scenarios can you imagine where a sequential search would be preferable to a binary search? The answer lies in the fact that a binary search can only be used when the data to be searched through is ordered in some meaningful way. This does not occur naturally (i.e., we must order the data first, prior to searching). Humans seem to have a basic desire to structure and organize the world around them. One of the most basic ways of organizing a collection of objects is to arrange them according to some common characteristic such as size, weight, cost, or age. A group of objects is sorted when the objects are arranged according to some rule that involves the use of orderable keys. While the term orderable will not be rigorously defined, intuitively it means keys on which the concepts of less than, greater than, and equal to make sense, or on which precedes and follows have meaning. Orderable keys include numbers such as weight, height, age, income, and social security number. Strings of characters such as words, names, and addresses are also orderable. Numeric keys are generally arranged from smallest to largest, or, occasionally, largest to smallest. Sorting is a very important problem. If information is not organized in some fashion, retrieval can be quite time consuming. To find a particular item in an unordered (or unsorted) list, we are forced to use Gourd, Kiremire, O'Neal 9 Last modified: 13 Oct 2015

10 the sequential search rather than the more efficient binary search. While the sequential search approach may be acceptable for a small numbers of items, it becomes impractical when dealing with large numbers of objects. For this reason, computer scientists have focused a great deal of attention on the sorting problem and have devised a number of sorting algorithms. Over the years, these algorithms have been closely studied and the efficiency of each carefully analyzed. This section presents three common approaches to the sorting problem: the bubble sort, the selection sort, and the insertion sort. As was the case with sequential and binary searches, each of the three sorts will be compared to determine their efficiency. In order to develop estimates of the runtimes of the three sort algorithms, we will use a computer capable of executing one million comparisons per second. While this may seem like a lot and to be very fast, today s computers are many times more powerful. In fact, they are capable of performing billions of basic low-level instructions per second. The number of higher-level operations they can perform each second depends on many factors other than CPU speed. In the case of sorting, the number of comparisons per second depends on factors such as whether the list being sorted is in main memory or on disk, and whether the computer is running other programs besides the sort procedure. The take home message is not to place too much emphasis on the numbers that will be generated for predicted runtimes as they are highly dependent on the assumptions made concerning computing hardware. Instead, what is important is the relative performance of the algorithms, which is not affected by underlying hardware assumptions. Bubble sort The bubble sort processes a list of items from left-to-right. The sorted list is built, in place, from rightto-left; that is, the largest value in the list is placed in its final position first, and so on, in the same list. It works by repeatedly comparing two neighboring elements (i.e., in positions i and i+1). If the two neighboring elements are out of order, they are swapped. The algorithm advances the position i and repeats. If, at the end of one iteration of the entire list (also called a pass) any swaps were made, the process is repeated. If no swaps were made, it means the list is sorted, and the process terminates. The following is an example of the bubble sort on the list [ ], with the underlined elements showing where the comparisons are made in each pass and red indicating the sorted portion of the list: Pass 1 List Comparison Action < 9 no swap > 3 swap > 5 swap > 1 swap done with this pass Pass 2 List Comparison Action > 3 swap > 5 swap > 1 swap Gourd, Kiremire, O'Neal 10 Last modified: 13 Oct 2015

12 < 5 no swap > 4 swap done with this pass Pass 2 List Comparison Action < 2 no swap < 3 no swap < 4 no swap done with the sort Since no swaps were made in pass 2, then the sort is immediately terminated. Terminating the bubble sort during a pass if no swaps are made is a tweak on the original bubble sort called the optimized bubble sort. Can you design an algorithm in pseudocode for the optimized bubble sort? Try it in the space below: The method illustrated on the sort the list [ ] in the table above is great for showing each comparison and illustrating the bubble sort in detail; however, it is overkill when simply showing how a list is sorted. We can slightly deviate from the method used previously and instead summarize each pass in a single row of the table. The underlined items in the list at each pass represent the unsorted portion of the list: Pass List original list Gourd, Kiremire, O'Neal 12 Last modified: 13 Oct 2015

13 Show how the bubble sort works on the list [ ] by completing the table below: Did you know? Pass List original list Note the for-next construct in the pseudocode for the bubble sort above. It is used to repeat a fixed or known number of times. You have already seen the repeat-until construct that repeats until some condition is true. A for-next construct can be rewritten into a repeat-until construct as follows: The for-next version: for i 1..n... next The repeat-until version: i 1 repeat... i i + 1 until i = n It is not always possible to rewrite a repeat-until construct into a for-next construct; for example (assuming that the length of the list is unknown): repeat remove item from list until the list is empty In this case, there is no way of knowing when the list will be empty if we don't originally know the length of the list (as assumed). Selection sort The selection sort is probably the most natural approach to sorting. Most people would likely come up with this algorithm when asked to design one that, for example, reorders a line of people based on their height. The selection sort processes a list of items from left-to-right. The sorted list is also built, in place, from left-to-right; that is, the smallest value in the list is placed in its final position first, and so on. Since the list is sorted in place, we keep track of both an unsorted portion and a sorted portion. And Gourd, Kiremire, O'Neal 13 Last modified: 13 Oct 2015

14 since the list is built from left-to-right, then the sorted portion is on the left. The selection sort works by repeatedly finding the smallest value in the unsorted portion of the list and swapping it with the first value in the unsorted portion. This action increases the sorted portion of the list and shrinks the unsorted portion of the list by one value. This is repeated until there are no values in the unsorted portion of the list. The following is an example of the selection sort on the list [ ] with the underlined elements showing where the comparisons are made in each pass and red indicating the sorted portion of the list: Pass 1 List Comparison Smallest so far Action < > < > done with this pass swap 7 and 1 Pass 2 List Comparison Smallest so far Action > < < done with this pass swap 9 and 3 Pass 3 List Comparison Smallest so far Action > < done with this pass swap 9 and 5 Pass 4 List Comparison Smallest so far Action > done with the sort swap 9 and 7 Note that the selection sort also required four passes through the list of items. For a list of n items, the selection sort requires n-1 passes through the list. Again, note that as each pass was made, fewer and fewer elements in the list were compared. If we consider all passes in aggregate, on average about half of the values were compared; therefore, for a list of n items, an average of n /2 comparisons are made at each pass. We can now calculate the number of comparisons made by using the formula defined above: Gourd, Kiremire, O'Neal 14 Last modified: 13 Oct 2015

15 comparisons = number of passes * average number of comparisons per pass For a list of n items, n-1 passes are required, each of which on average makes n/2 comparisons. In total, the runtime performance of the selection sort is then: (n 1) n 2 = 1 2 n (n 1 ) = 1 2 ( n2 n ) For the list above of five items, the number of comparisons is then: 1 2 (52 5) = 1 (20 ) = 10 2 Again, we can show how the selection sort works by summarizing each pass in a single row of the table. The underlined items in the list at each pass represent the unsorted portion of the list: Pass List original list This is sufficient to show how the selection sort works. Can you design an algorithm in pseudocode for the selection sort? Try it in the space below: Gourd, Kiremire, O'Neal 15 Last modified: 13 Oct 2015

16 Show how the selection sort works on the list [ ] by completing the table below: Pass List original list Insertion sort The insertion sort is the procedure that most people use to arrange a hand of cards. To begin to understand the algorithm, think of the list as having both a sorted portion and an unsorted portion (as we did in the previous sorts). The first item of the list is considered to be a sorted list one item long, with the rest of the list (items 2 through n) forming an unsorted portion. The insertion sort removes the first item from the unsorted portion of the list and marks it as the item to be inserted. It then works its way from the back to the front of the sorted portion of the list, at each step comparing the item to be inserted with the current item. As long as the current item is larger than the item to be inserted, the algorithm continues moving backward through the sorted portion of the list. Eventually it will either reach the beginning of the sorted portion of the list or encounter an item that is less than or equal to the item to be inserted. When that happens the algorithm inserts the item at the current insertion point. The entire process of selecting the first item from the unsorted portion of the list and scanning backwards through the sorted portion of the list for the insertion point is then repeated. Eventually, the unsorted portion of the list will be empty since all of the items will have been inserted into the sorted portion of the list. When this occurs, the sort is complete. The following is an example of the selection sort on the list [ ] with the underlined elements showing where the comparisons are made in each pass, the bold value indicating the current first item in the unsorted portion of the list, and red indicating the sorted portion of the list: Pass 1 List First item Comparison Action < done with this pass insert 9 Pass 2 List First item Comparison Action > 3 slide 9 over > 3 slide 7 over done with this pass insert 3 Gourd, Kiremire, O'Neal 16 Last modified: 13 Oct 2015

17 Pass 3 List First item Comparison Action > 5 slide 9 over > 5 slide 7 over < done with this pass insert 5 Pass 4 List First item Comparison Action > 1 slide 9 over > 1 slide 7 over > 1 slide 5 over > 1 slide 3 over done with the sort insert 1 Again, we can show how the insertion sort works by summarizing each pass in a single row of the table. The underlined items in the list at each pass represent the unsorted portion of the list: Pass List original list Show how the insertion sort works on the list [ ] by completing the table below: Pass List original list Note that the insertion sort required four passes through the list of items. For a list of n items, the insertion sort requires n-1 passes through the list, which is the same as both the bubble sort and the selection sort. However, the number of comparisons made at each pass is not so simple to calculate because it depends on the original state of the list. Sometimes, the first item in the unsorted portion of Gourd, Kiremire, O'Neal 17 Last modified: 13 Oct 2015

20 in about 25 seconds (precisely seconds). The bubble and selection sorts are both expected to take about 50 seconds on the same problem (precisely seconds) or about twice as long. Figure 2: Runtime comparison of bubble, selection, and insertion sorts Gourd, Kiremire, O'Neal 20 Last modified: 13 Oct 2015

### APP INVENTOR. Test Review

APP INVENTOR Test Review Main Concepts App Inventor Lists Creating Random Numbers Variables Searching and Sorting Data Linear Search Binary Search Selection Sort Quick Sort Abstraction Modulus Division

### CHAPTER 3 Numbers and Numeral Systems

CHAPTER 3 Numbers and Numeral Systems Numbers play an important role in almost all areas of mathematics, not least in calculus. Virtually all calculus books contain a thorough description of the natural,

### Data Structures. Topic #12

Data Structures Topic #12 Today s Agenda Sorting Algorithms insertion sort selection sort exchange sort shell sort radix sort As we learn about each sorting algorithm, we will discuss its efficiency Sorting

### Zabin Visram Room CS115 CS126 Searching. Binary Search

Zabin Visram Room CS115 CS126 Searching Binary Search Binary Search Sequential search is not efficient for large lists as it searches half the list, on average Another search algorithm Binary search Very

### 6. Standard Algorithms

6. Standard Algorithms The algorithms we will examine perform Searching and Sorting. 6.1 Searching Algorithms Two algorithms will be studied. These are: 6.1.1. inear Search The inear Search The Binary

### CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team Lecture Summary In this lecture, we learned about the ADT Priority Queue. A

### Intro. to the Divide-and-Conquer Strategy via Merge Sort CMPSC 465 CLRS Sections 2.3, Intro. to and various parts of Chapter 4

Intro. to the Divide-and-Conquer Strategy via Merge Sort CMPSC 465 CLRS Sections 2.3, Intro. to and various parts of Chapter 4 I. Algorithm Design and Divide-and-Conquer There are various strategies we

### Divide-and-Conquer Algorithms Part Four

Divide-and-Conquer Algorithms Part Four Announcements Problem Set 2 due right now. Can submit by Monday at 2:15PM using one late period. Problem Set 3 out, due July 22. Play around with divide-and-conquer

### Analysis of Binary Search algorithm and Selection Sort algorithm

Analysis of Binary Search algorithm and Selection Sort algorithm In this section we shall take up two representative problems in computer science, work out the algorithms based on the best strategy to

### 1 2-3 Trees: The Basics

CS10: Data Structures and Object-Oriented Design (Fall 2013) November 1, 2013: 2-3 Trees: Inserting and Deleting Scribes: CS 10 Teaching Team Lecture Summary In this class, we investigated 2-3 Trees in

### Knowledge of searching using linear search. around 15 minutes, can be used within a longer workshop activity

20 Questions Age group: Abilities assumed: Time: Size of group: adult Knowledge of searching using linear search around 5 minutes, can be used within a longer workshop activity anything from 2 to hundreds

### Regular Expressions and Automata using Haskell

Regular Expressions and Automata using Haskell Simon Thompson Computing Laboratory University of Kent at Canterbury January 2000 Contents 1 Introduction 2 2 Regular Expressions 2 3 Matching regular expressions

### Record Storage and Primary File Organization

Record Storage and Primary File Organization 1 C H A P T E R 4 Contents Introduction Secondary Storage Devices Buffering of Blocks Placing File Records on Disk Operations on Files Files of Unordered Records

### Formal Languages and Automata Theory - Regular Expressions and Finite Automata -

Formal Languages and Automata Theory - Regular Expressions and Finite Automata - Samarjit Chakraborty Computer Engineering and Networks Laboratory Swiss Federal Institute of Technology (ETH) Zürich March

### Lecture Notes on Linear Search

Lecture Notes on Linear Search 15-122: Principles of Imperative Computation Frank Pfenning Lecture 5 January 29, 2013 1 Introduction One of the fundamental and recurring problems in computer science is

### Outline. Introduction Linear Search. Transpose sequential search Interpolation search Binary search Fibonacci search Other search techniques

Searching (Unit 6) Outline Introduction Linear Search Ordered linear search Unordered linear search Transpose sequential search Interpolation search Binary search Fibonacci search Other search techniques

### Chapter 13: Query Processing. Basic Steps in Query Processing

Chapter 13: Query Processing! Overview! Measures of Query Cost! Selection Operation! Sorting! Join Operation! Other Operations! Evaluation of Expressions 13.1 Basic Steps in Query Processing 1. Parsing

### 12 Abstract Data Types

12 Abstract Data Types 12.1 Source: Foundations of Computer Science Cengage Learning Objectives After studying this chapter, the student should be able to: Define the concept of an abstract data type (ADT).

### CHAPTER 5 Round-off errors

CHAPTER 5 Round-off errors In the two previous chapters we have seen how numbers can be represented in the binary numeral system and how this is the basis for representing numbers in computers. Since any

### EFFICIENT EXTERNAL SORTING ON FLASH MEMORY EMBEDDED DEVICES

ABSTRACT EFFICIENT EXTERNAL SORTING ON FLASH MEMORY EMBEDDED DEVICES Tyler Cossentine and Ramon Lawrence Department of Computer Science, University of British Columbia Okanagan Kelowna, BC, Canada tcossentine@gmail.com

### This puzzle is based on the following anecdote concerning a Hungarian sociologist and his observations of circles of friends among children.

0.1 Friend Trends This puzzle is based on the following anecdote concerning a Hungarian sociologist and his observations of circles of friends among children. In the 1950s, a Hungarian sociologist S. Szalai

### The Graphical Method: An Example

The Graphical Method: An Example Consider the following linear program: Maximize 4x 1 +3x 2 Subject to: 2x 1 +3x 2 6 (1) 3x 1 +2x 2 3 (2) 2x 2 5 (3) 2x 1 +x 2 4 (4) x 1, x 2 0, where, for ease of reference,

### Symbol Tables. Introduction

Symbol Tables Introduction A compiler needs to collect and use information about the names appearing in the source program. This information is entered into a data structure called a symbol table. The

### CSC148 Lecture 8. Algorithm Analysis Binary Search Sorting

CSC148 Lecture 8 Algorithm Analysis Binary Search Sorting Algorithm Analysis Recall definition of Big Oh: We say a function f(n) is O(g(n)) if there exists positive constants c,b such that f(n)

### 3 Some Integer Functions

3 Some Integer Functions A Pair of Fundamental Integer Functions The integer function that is the heart of this section is the modulo function. However, before getting to it, let us look at some very simple

### Preliminary Mathematics

Preliminary Mathematics The purpose of this document is to provide you with a refresher over some topics that will be essential for what we do in this class. We will begin with fractions, decimals, and

CS787: Advanced Algorithms Lecture 14: Online algorithms We now shift focus to a different kind of algorithmic problem where we need to perform some optimization without knowing the input in advance. Algorithms

### Chapter 11 Number Theory

Chapter 11 Number Theory Number theory is one of the oldest branches of mathematics. For many years people who studied number theory delighted in its pure nature because there were few practical applications

### Pseudo code Tutorial and Exercises Teacher s Version

Pseudo code Tutorial and Exercises Teacher s Version Pseudo-code is an informal way to express the design of a computer program or an algorithm in 1.45. The aim is to get the idea quickly and also easy

### Efficiency of algorithms. Algorithms. Efficiency of algorithms. Binary search and linear search. Best, worst and average case.

Algorithms Efficiency of algorithms Computational resources: time and space Best, worst and average case performance How to compare algorithms: machine-independent measure of efficiency Growth rate Complexity

### Theory of Computation Prof. Kamala Krithivasan Department of Computer Science and Engineering Indian Institute of Technology, Madras

Theory of Computation Prof. Kamala Krithivasan Department of Computer Science and Engineering Indian Institute of Technology, Madras Lecture No. # 31 Recursive Sets, Recursively Innumerable Sets, Encoding

### Heaps & Priority Queues in the C++ STL 2-3 Trees

Heaps & Priority Queues in the C++ STL 2-3 Trees CS 3 Data Structures and Algorithms Lecture Slides Friday, April 7, 2009 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks

### The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

The Tower of Hanoi Recursion Solution recursion recursion recursion Recursive Thinking: ignore everything but the bottom disk. 1 2 Recursive Function Time Complexity Hanoi (n, src, dest, temp): If (n >

### Second Order Linear Nonhomogeneous Differential Equations; Method of Undetermined Coefficients. y + p(t) y + q(t) y = g(t), g(t) 0.

Second Order Linear Nonhomogeneous Differential Equations; Method of Undetermined Coefficients We will now turn our attention to nonhomogeneous second order linear equations, equations with the standard

### The Number Line: An Overview

The Number Line: An Overview Learning to Think Mathematically with the Number Line One of the most overlooked tools of the elementary and middle school classroom is the number line. Typically displayed

### Algorithms, Integers

CHAPTER 3 Algorithms, Integers 3.1. Algorithms Consider the following list of instructions to find the maximum of three numbers a, b, c: 1. Assign variable x the value of a. 2. If b > x then assign x the

### Session 7 Fractions and Decimals

Key Terms in This Session Session 7 Fractions and Decimals Previously Introduced prime number rational numbers New in This Session period repeating decimal terminating decimal Introduction In this session,

### Introduction to Diophantine Equations

Introduction to Diophantine Equations Tom Davis tomrdavis@earthlink.net http://www.geometer.org/mathcircles September, 2006 Abstract In this article we will only touch on a few tiny parts of the field

### COMP 250 Fall 2012 lecture 2 binary representations Sept. 11, 2012

Binary numbers The reason humans represent numbers using decimal (the ten digits from 0,1,... 9) is that we have ten fingers. There is no other reason than that. There is nothing special otherwise about

### CMPS 102 Solutions to Homework 1

CMPS 0 Solutions to Homework Lindsay Brown, lbrown@soe.ucsc.edu September 9, 005 Problem..- p. 3 For inputs of size n insertion sort runs in 8n steps, while merge sort runs in 64n lg n steps. For which

### Analysis of Micromouse Maze Solving Algorithms

1 Analysis of Micromouse Maze Solving Algorithms David M. Willardson ECE 557: Learning from Data, Spring 2001 Abstract This project involves a simulation of a mouse that is to find its way through a maze.

### Q&As: Microsoft Excel 2013: Chapter 2

Q&As: Microsoft Excel 2013: Chapter 2 In Step 5, why did the date that was entered change from 4/5/10 to 4/5/2010? When Excel recognizes that you entered a date in mm/dd/yy format, it automatically formats

### Recursive void Methods. Chapter 11. A recursive method is a method that includes a call to itself Recursion is based on the general problem.

Chapter 11 Recursion Recursive void Methods A recursive method is a method that includes a call to itself Recursion is based on the general problem solving technique of breaking down a task into subtasks

### OA4-13 Rounding on a Number Line Pages 80 81

OA4-13 Rounding on a Number Line Pages 80 81 STANDARDS 3.NBT.A.1, 4.NBT.A.3 Goals Students will round to the closest ten, except when the number is exactly halfway between a multiple of ten. PRIOR KNOWLEDGE

### Binary Heap Algorithms

CS Data Structures and Algorithms Lecture Slides Wednesday, April 5, 2009 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks CHAPPELLG@member.ams.org 2005 2009 Glenn G. Chappell

### Access Tutorial 1: Introduction to Microsoft Access

Access Tutorial 1: Introduction to Microsoft Access The purpose of these tutorials is not to teach you Microsoft Access, but rather to teach you some generic information systems concepts and skills using

### NODAL ANALYSIS. Circuits Nodal Analysis 1 M H Miller

NODAL ANALYSIS A branch of an electric circuit is a connection between two points in the circuit. In general a simple wire connection, i.e., a 'short-circuit', is not considered a branch since it is known

### Everything you wanted to know about using Hexadecimal and Octal Numbers in Visual Basic 6

Everything you wanted to know about using Hexadecimal and Octal Numbers in Visual Basic 6 Number Systems No course on programming would be complete without a discussion of the Hexadecimal (Hex) number

### 1. Microsoft Excel Basics:

1. Microsoft Excel Basics: This section introduces data manipulation using Microsoft Excel, including importing, copying and pasting data and entering equations. A basic understanding of computer operating

### KWIC Exercise. 6/18/2007 2007, Spencer Rugaber 1

KWIC Exercise On a subsequent slide, you will be given the description of a simple program for which you will be asked to devise two architectures. For the purposes of this exercise, you should imagine

### B-Trees. Algorithms and data structures for external memory as opposed to the main memory B-Trees. B -trees

B-Trees Algorithms and data structures for external memory as opposed to the main memory B-Trees Previous Lectures Height balanced binary search trees: AVL trees, red-black trees. Multiway search trees:

### Short-Run Production and Costs

Short-Run Production and Costs The purpose of this section is to discuss the underlying work of firms in the short-run the production of goods and services. Why is understanding production important to

### DATABASE DESIGN - 1DL400

DATABASE DESIGN - 1DL400 Spring 2015 A course on modern database systems!! http://www.it.uu.se/research/group/udbl/kurser/dbii_vt15/ Kjell Orsborn! Uppsala Database Laboratory! Department of Information

### Technical Terms Algorithm, computational thinking, algorithmic thinking, efficiency, testing.

The Swap Puzzle Age group: Abilities assumed: Time: 7 adult Nothing Size of group: 8 to 30 50-60 minutes, Focus What is an algorithm? Testing Efficiency of algorithms Computational Thinking: algorithmic

### Merge Sort. 2004 Goodrich, Tamassia. Merge Sort 1

Merge Sort 7 2 9 4 2 4 7 9 7 2 2 7 9 4 4 9 7 7 2 2 9 9 4 4 Merge Sort 1 Divide-and-Conquer Divide-and conquer is a general algorithm design paradigm: Divide: divide the input data S in two disjoint subsets

### Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb

Binary Search Trees Lecturer: Georgy Gimel farb COMPSCI 220 Algorithms and Data Structures 1 / 27 1 Properties of Binary Search Trees 2 Basic BST operations The worst-case time complexity of BST operations

### 1. Sorting (assuming sorting into ascending order) a) BUBBLE SORT

DECISION 1 Revision Notes 1. Sorting (assuming sorting into ascending order) a) BUBBLE SORT Make sure you show comparisons clearly and label each pass First Pass 8 4 3 6 1 4 8 3 6 1 4 3 8 6 1 4 3 6 8 1

### 1 Review of Newton Polynomials

cs: introduction to numerical analysis 0/0/0 Lecture 8: Polynomial Interpolation: Using Newton Polynomials and Error Analysis Instructor: Professor Amos Ron Scribes: Giordano Fusco, Mark Cowlishaw, Nathanael

### Chapter 2: Algorithm Discovery and Design. Invitation to Computer Science, C++ Version, Third Edition

Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition Objectives In this chapter, you will learn about: Representing algorithms Examples of algorithmic problem

### Database design basics

Microsoft Office Access Database design basics Applies to: Microsoft Office Access 2007 A properly designed database provides you with access to up-to-date, accurate information. Because a correct design

### Enterprise Discovery Best Practices

Enterprise Discovery Best Practices 1993-2015 Informatica Corporation. No part of this document may be reproduced or transmitted in any form, by any means (electronic, photocopying, recording or otherwise)

### CSE 326: Data Structures B-Trees and B+ Trees

Announcements (4//08) CSE 26: Data Structures B-Trees and B+ Trees Brian Curless Spring 2008 Midterm on Friday Special office hour: 4:-5: Thursday in Jaech Gallery (6 th floor of CSE building) This is

### INTRODUCTION The collection of data that makes up a computerized database must be stored physically on some computer storage medium.

Chapter 4: Record Storage and Primary File Organization 1 Record Storage and Primary File Organization INTRODUCTION The collection of data that makes up a computerized database must be stored physically

### MS SQL Performance (Tuning) Best Practices:

MS SQL Performance (Tuning) Best Practices: 1. Don t share the SQL server hardware with other services If other workloads are running on the same server where SQL Server is running, memory and other hardware

### Binary search algorithm

Binary search algorithm Definition Search a sorted array by repeatedly dividing the search interval in half. Begin with an interval covering the whole array. If the value of the search key is less than

### Induction. Margaret M. Fleck. 10 October These notes cover mathematical induction and recursive definition

Induction Margaret M. Fleck 10 October 011 These notes cover mathematical induction and recursive definition 1 Introduction to induction At the start of the term, we saw the following formula for computing

### PRINCIPLES OF PROBLEM SOLVING

PRINCIPLES OF PROBLEM SOLVING There are no hard and fast rules that will ensure success in solving problems. However, it is possible to outline some general steps in the problem-solving process and to

### Using Proportions to Solve Percent Problems I

RP7-1 Using Proportions to Solve Percent Problems I Pages 46 48 Standards: 7.RP.A. Goals: Students will write equivalent statements for proportions by keeping track of the part and the whole, and by solving

### ENUMERATION OF UNIQUELY SOLVABLE OPEN SURVO PUZZLES

ENUMERATION OF UNIQUELY SOLVABLE OPEN SURVO PUZZLES SEPPO MUSTONEN 30 OCTOBER 2007 1. Introduction In an m n Survo puzzle an m n table has to be filled by integers 1, 2,..., mn in such a way that the their

### SMMG December 2 nd, 2006 Dr. Edward Burger (Williams College) Discovering Beautiful Patterns in Nature and Number. Fun Fibonacci Facts

SMMG December nd, 006 Dr. Edward Burger (Williams College) Discovering Beautiful Patterns in Nature and Number. The Fibonacci Numbers Fun Fibonacci Facts Examine the following sequence of natural numbers.

### Petrel TIPS&TRICKS from SCM

Petrel TIPS&TRICKS from SCM Knowledge Worth Sharing Tips for Writing Workflows The Process Manager is the tool used to write workflows. A workflow manages Petrel processes (hence the name), files, graphics,

### 2x + y = 3. Since the second equation is precisely the same as the first equation, it is enough to find x and y satisfying the system

1. Systems of linear equations We are interested in the solutions to systems of linear equations. A linear equation is of the form 3x 5y + 2z + w = 3. The key thing is that we don t multiply the variables

### , each of which contains a unique key value, say k i , R 2. such that k i equals K (or to determine that no such record exists in the collection).

The Search Problem 1 Suppose we have a collection of records, say R 1, R 2,, R N, each of which contains a unique key value, say k i. Given a particular key value, K, the search problem is to locate the

### 8. Query Processing. Query Processing & Optimization

ECS-165A WQ 11 136 8. Query Processing Goals: Understand the basic concepts underlying the steps in query processing and optimization and estimating query processing cost; apply query optimization techniques;

### The Essentials of Computer Organization and Architecture. Linda Null and Julia Lobur Jones and Bartlett Publishers, 2003

The Essentials of Computer Organization and Architecture Linda Null and Julia Lobur Jones and Bartlett Publishers, 2003 Chapter 2 Instructor's Manual Chapter Objectives Chapter 2, Data Representation,

### 7 Gaussian Elimination and LU Factorization

7 Gaussian Elimination and LU Factorization In this final section on matrix factorization methods for solving Ax = b we want to take a closer look at Gaussian elimination (probably the best known method

### 20-30 minutes, can be used within a longer activity

Locked-in 1 Age group successfully used with: Abilities assumed: Time: Size of group: 11 adult None 20-30 minutes, can be used within a longer activity anything from 2 to hundreds Focus What is an algorithm?

### Solving a System of Equations

11 Solving a System of Equations 11-1 Introduction The previous chapter has shown how to solve an algebraic equation with one variable. However, sometimes there is more than one unknown that must be determined

### 2. Compressing data to reduce the amount of transmitted data (e.g., to save money).

Presentation Layer The presentation layer is concerned with preserving the meaning of information sent across a network. The presentation layer may represent (encode) the data in various ways (e.g., data

### CSC 180 H1F Algorithm Runtime Analysis Lecture Notes Fall 2015

1 Introduction These notes introduce basic runtime analysis of algorithms. We would like to be able to tell if a given algorithm is time-efficient, and to be able to compare different algorithms. 2 Linear

### Arrays. number: Motivation. Prof. Stewart Weiss. Software Design Lecture Notes Arrays

Motivation Suppose that we want a program that can read in a list of numbers and sort that list, or nd the largest value in that list. To be concrete about it, suppose we have 15 numbers to read in from

### 1. The Fly In The Ointment

Arithmetic Revisited Lesson 5: Decimal Fractions or Place Value Extended Part 5: Dividing Decimal Fractions, Part 2. The Fly In The Ointment The meaning of, say, ƒ 2 doesn't depend on whether we represent

### Session 7 Bivariate Data and Analysis

Session 7 Bivariate Data and Analysis Key Terms for This Session Previously Introduced mean standard deviation New in This Session association bivariate analysis contingency table co-variation least squares

### Count the Dots Binary Numbers

Activity 1 Count the Dots Binary Numbers Summary Data in computers is stored and transmitted as a series of zeros and ones. How can we represent words and numbers using just these two symbols? Curriculum

### Fractions. If the top and bottom numbers of a fraction are the same then you have a whole one.

What do fractions mean? Fractions Academic Skills Advice Look at the bottom of the fraction first this tells you how many pieces the shape (or number) has been cut into. Then look at the top of the fraction

### Project and Production Management Prof. Arun Kanda Department of Mechanical Engineering Indian Institute of Technology, Delhi

Project and Production Management Prof. Arun Kanda Department of Mechanical Engineering Indian Institute of Technology, Delhi Lecture - 15 Limited Resource Allocation Today we are going to be talking about

### Homework 5 Solutions

Homework 5 Solutions 4.2: 2: a. 321 = 256 + 64 + 1 = (01000001) 2 b. 1023 = 512 + 256 + 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = (1111111111) 2. Note that this is 1 less than the next power of 2, 1024, which

### Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

Binary Heaps A binary heap is another data structure. It implements a priority queue. Priority Queue has the following operations: isempty add (with priority) remove (highest priority) peek (at highest

### Section 1.5 Exponents, Square Roots, and the Order of Operations

Section 1.5 Exponents, Square Roots, and the Order of Operations Objectives In this section, you will learn to: To successfully complete this section, you need to understand: Identify perfect squares.

### 2 Programs: Instructions in the Computer

2 2 Programs: Instructions in the Computer Figure 2. illustrates the first few processing steps taken as a simple CPU executes a program. The CPU for this example is assumed to have a program counter (PC),

### Hill s Cipher: Linear Algebra in Cryptography

Ryan Doyle Hill s Cipher: Linear Algebra in Cryptography Introduction: Since the beginning of written language, humans have wanted to share information secretly. The information could be orders from a

### Data Structures. Algorithm Performance and Big O Analysis

Data Structures Algorithm Performance and Big O Analysis What s an Algorithm? a clearly specified set of instructions to be followed to solve a problem. In essence: A computer program. In detail: Defined

### Converting a Number from Decimal to Binary

Converting a Number from Decimal to Binary Convert nonnegative integer in decimal format (base 10) into equivalent binary number (base 2) Rightmost bit of x Remainder of x after division by two Recursive

### Base Conversion written by Cathy Saxton

Base Conversion written by Cathy Saxton 1. Base 10 In base 10, the digits, from right to left, specify the 1 s, 10 s, 100 s, 1000 s, etc. These are powers of 10 (10 x ): 10 0 = 1, 10 1 = 10, 10 2 = 100,

### Binary Numbers. Bob Brown Information Technology Department Southern Polytechnic State University

Binary Numbers Bob Brown Information Technology Department Southern Polytechnic State University Positional Number Systems The idea of number is a mathematical abstraction. To use numbers, we must represent

### Equations and Inequalities

Rational Equations Overview of Objectives, students should be able to: 1. Solve rational equations with variables in the denominators.. Recognize identities, conditional equations, and inconsistent equations.

### Quick Sort. Implementation

Implementation Next, recall that our goal is to partition all remaining elements based on whether they are smaller than or greater than the pivot We will find two entries: One larger than the pivot (staring

### 6.3 Conditional Probability and Independence

222 CHAPTER 6. PROBABILITY 6.3 Conditional Probability and Independence Conditional Probability Two cubical dice each have a triangle painted on one side, a circle painted on two sides and a square painted