Problems, Algorithms, Programs

Similar documents
Searching, Sorting. Arizona State University 1

Chapter 3. The Efficiency of Algorithms INVITATION TO. Computer Science

Theory and Frontiers of Computer Science. Fall 2013 Carola Wenk

10/11/2013. Chapter 3. Objectives. Objectives (continued) Introduction. Attributes of Algorithms. Introduction. The Efficiency of Algorithms

Lecture 15: Algorithms. AP Computer Science Principles

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Searching Algorithms/Time Analysis

Section 6.1 Measures of Center

15110 Principles of Computing, Carnegie Mellon University - CORTINA. Binary Search. Required: List L of n unique elements.

COE428 Lecture Notes Week 1 (Week of January 9, 2017)

We can use a max-heap to sort data.

Analysis of Algorithms

UNIT 5B Binary Search

Divide and Conquer. Algorithm Fall Semester

Lecture 6: Divide-and-Conquer

Design and Analysis of Algorithms Prof. Madhavan Mukund Chennai Mathematical Institute. Week 02 Module 06 Lecture - 14 Merge Sort: Analysis

Depth First Search A B C D E F G A B C 5 D E F 3 2 G 2 3

Advanced Algorithms and Data Structures

CISC 1100: Structures of Computer Science

Binary Search and Worst-Case Analysis

Admin. How's the project coming? After these slides, read chapter 13 in your book. Quizzes will return

THEORY OF COMPUTATION

Sorting Pearson Education, Inc. All rights reserved.

An algorithm is a sequence of instructions that one must perform in order to solve a wellformulated

Selection (deterministic & randomized): finding the median in linear time

The Limits of Sorting Divide-and-Conquer Comparison Sorts II

Sorting. Quicksort analysis Bubble sort. November 20, 2017 Hassan Khosravi / Geoffrey Tien 1

CS 171: Introduction to Computer Science II. Quicksort

Binary Search to find item in sorted array

Greedy Algorithms 1 {K(S) K(S) C} For large values of d, brute force search is not feasible because there are 2 d {1,..., d}.

What is an algorithm? CISC 1100/1400 Structures of Comp. Sci./Discrete Structures Chapter 8 Algorithms. Applications of algorithms

Sorting. Sorting in Arrays. SelectionSort. SelectionSort. Binary search works great, but how do we create a sorted array in the first place?

CS 137 Part 8. Merge Sort, Quick Sort, Binary Search. November 20th, 2017

ECE 122. Engineering Problem Solving Using Java


Intro. Speed V Growth

Complexity of Algorithms

(Refer Slide Time: 01.26)

CSCI 2212: Intermediate Programming / C Recursion

Week - 04 Lecture - 01 Merge Sort. (Refer Slide Time: 00:02)

Today. CISC101 Reminders & Notes. Searching in Python - Cont. Searching in Python. From last time

UNIT 5C Merge Sort Principles of Computing, Carnegie Mellon University

Week 12: Running Time and Performance

CSc 110, Spring 2017 Lecture 39: searching

12/30/2013 S. NALINI,AP/CSE

UNIT 14C The Limits of Computing: Non computable Functions. Problem Classifications

CSE373: Data Structure & Algorithms Lecture 18: Comparison Sorting. Dan Grossman Fall 2013

Algorithms. Chapter 8. Objectives After studying this chapter, students should be able to:

Topics. Sorting. Sorting. 1) How can we sort data in an array? a) Selection Sort b) Insertion Sort

CSCI 270: Introduction to Algorithms and Theory of Computing Fall 2017 Prof: Leonard Adleman Scribe: Joseph Bebel

QB LECTURE #1: Algorithms and Dynamic Programming

Chapter 8 Algorithms 1

Recursion. Chapter 11. Chapter 11 1

Lecture 6: Sequential Sorting

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17

CSC 222: Object-Oriented Programming. Fall 2015

CSC 273 Data Structures

Algorithmics. Some information. Programming details: Ruby desuka?

H c. Show that n n (n!) 2 for all integers n n 1.

Lecture Notes 3: Data summarization

COMP 250 Fall recurrences 2 Oct. 13, 2017

8/5/10 TODAY'S OUTLINE. Recursion COMP 10 EXPLORING COMPUTER SCIENCE. Revisit search and sorting using recursion. Recursion WHAT DOES THIS CODE DO?

6.856 Randomized Algorithms

Algorithms. Algorithms 1.4 ANALYSIS OF ALGORITHMS

Computer Science 4U Unit 1. Programming Concepts and Skills Algorithms

Floating Point. CSC207 Fall 2017

Data Structures and Algorithms CMPSC 465

Sorting and Searching

Binary Search and Worst-Case Analysis

Lecture 2: Getting Started

Lecture 19 Sorting Goodrich, Tamassia

Test 1 Review Questions with Solutions

What s Half of a Half of a Half?

Binary Search Trees Treesort

Name: Lirong TAN 1. (15 pts) (a) Define what is a shortest s-t path in a weighted, connected graph G.

CSE 332: Data Structures & Parallelism Lecture 12: Comparison Sorting. Ruth Anderson Winter 2019

Lecture 7 Quicksort : Principles of Imperative Computation (Spring 2018) Frank Pfenning

Algorithmic problem-solving: Lecture 2. Algorithmic problem-solving: Tractable vs Intractable problems. Based on Part V of the course textbook.

Topics Recursive Sorting Algorithms Divide and Conquer technique An O(NlogN) Sorting Alg. using a Heap making use of the heap properties STL Sorting F

Introduction to Computers & Programming

CS1 Lecture 30 Apr. 2, 2018

Sorting is ordering a list of objects. Here are some sorting algorithms

COSC242 Lecture 7 Mergesort and Quicksort

Data Structures and Algorithms. Roberto Sebastiani

lecture notes September 2, How to sort?

Advanced Algorithms and Data Structures

Searching for Information. A Simple Method for Searching. Simple Searching. Class #21: Searching/Sorting I

EECS 203 Spring 2016 Lecture 8 Page 1 of 6

MPATE-GE 2618: C Programming for Music Technology. Unit 4.2

Algorithm Analysis. This is based on Chapter 4 of the text.

Lecture Notes on Quicksort

Recursion. Let s start by looking at some problems that are nicely solved using recursion. First, let s look at generating The Fibonacci series.

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Lecture 2: Sorting and the Big O. Wednesday, 16 September 2009

Intro to Algorithms. Professor Kevin Gold

Binary Search APRIL 25 TH, 2014

Computer Science 210 Data Structures Siena College Fall Topic Notes: Recursive Methods

Lecture 11. Lecture 11: External Sorting

Unit-2 Divide and conquer 2016

Sorting: Given a list A with n elements possessing a total order, return a list with the same elements in non-decreasing order.

Transcription:

Algorithms

Problems, Algorithms, Programs Problem - a well defined task. Sort a list of numbers. Find a particular item in a list. Find a winning chess move.

Algorithms A series of precise steps, known to stop eventually, that solve a problem. NOT necessarily tied to computers. There can be many algorithms to solve the same problem.

Characteristics of an Algorithm Precise steps. Effective steps. Has an output. Terminates eventually.

Trivial Algorithm Computing an average: Sum up all of the values. Divide the sum by the number of values.

Problems vs. Algorithms vs. Programs There can be many algorithms that solve the same problem. There can be many programs that implement the same algorithm. We are concerned with: Analyzing the difficulty of problems. Finding good algorithms. Analyzing the efficiency of algorithms.

Example: Search Search through a list of items for a particular value. Example: Search through an array of student records for the student with ID 12345. Search through an array of address records for the address of the person with last name Doe.

Linear Search If we are searching in a list, start at the beginning and check each element until we find the one we want or reach the end. Best case? Worst case? Average case?

Binary Search If we are searching in a sorted list, we look at the middle item and then choose which half to continue looking in. We continue to cut the area we are searching in half until we find the value, or there are no more values to check. Best case? Worst case? Average case? (A little tricky)

Binary Search: Worst Case Let s say the list has1024 items and the item is the last one we check. Check midpoint of 1024 items. Check midpoint of upper or lower half (512). Check midpoint of a half of that half (128). Successive ranges we are checking have lengths 64, 32, 16, 8, 4, 2, 1. How many checks was that? 10 (log 1024 = 10)

Binary Search Aside: Note that binary search only works if the data in the list are sorted by the field on which we re searching!

Classifying Problems Problems fall into two categories. Computable problems can be solved using an algorithm. Non-computable problems have no algorithm to solve them. Historical note: Hilbert s questions in 1900: complete? Consistent? Decidable?

Classifying Problems Historical note: Hilbert posed the following questions in 1900: Is mathematics complete? Is mathematics consistent? Is every statement in mathematics decidable? In 1930, he thought the all 3 answers would be yes. Almost immediately, Gödel showed that no closed system can be both complete & consistent. By the mid-1930 s, Turing showed that the answer to the 3 rd question is no.

Classifying Problems Two categories of problems: Computable Non-computable Wouldn t it be nice to know which category a problem falls into? (Topic for later in the week: this problem itself is non-computable.)

Classifying Computable Problems Tractable There is an efficient algorithm to solve the problem. Intractable There is an algorithm to solve the problem but there is no efficient algorithm. (This is difficult to prove.)

Examples Sorting: tractable. The traveling salesperson problem: intractable. (we think ) Halting Problem: non-computable. (More on this later in the week.)

Measuring Efficiency We are (usually) concerned with the time an algorithm takes to complete. We often count the number of times blocks of code are executed, as a function of the size of the input. Why not measure time directly? Why not count the number of instructions executed?

Example Code: def afunction(array): statementa statementb statementc for x in array: statementd statemente return somevalue If the array has N elements, this function executes 4 + (2 * N) statements (i.e., 2N + 4).

Some Mathematical Background Let s see some examples

Big O The worst case running time, discounting constants and lower order terms. Example: n 3 + 2n is O(n 3 )

Exchange Sort def exchangesort(array): for indx1 in range(len(array)): for indx2 in range(indx1, len(array)): if (array[indx1] > array[indx2]): swap(array, indx1, indx2) Let s work out the big O running time

Merge Sort Given a list, split it into 2 equal piles. Then split each of these piles in half. Continue to do this until you are left with 1 element in each pile. Now merge piles back together in order.

Merge Sort An example of how the merge works: Suppose the first half and second half of an array are sorted: 5 9 10 12 17 1 8 11 20 32 Merge these by taking a new element from either the first or second subarray, choosing the smallest of the remaining elements. Big O running time?

Big O Can Be Misleading Big O analysis is concerned with worst case behavior. Sometimes we know that we are not dealing with the worst case.

Searching an Array def search(array, key): for x in array: if x == key: return key Worst case? Best case?

Algorithms Exercise