Algorithms An algorithm is a set of instructions that enable you, step-by-step, to achieve a particular goal. Computers use algorithms to solve problems.

Slides:



Advertisements
Similar presentations
College of Information Technology & Design
Advertisements

 Review: The Greedy Method
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Chapter 9: Searching, Sorting, and Algorithm Analysis
CPSC 171 Introduction to Computer Science More Efficiency of Algorithms.
Ver. 1.0 Session 5 Data Structures and Algorithms Objectives In this session, you will learn to: Sort data by using quick sort Sort data by using merge.
CS 280 Data Structures Professor John Peterson. Project Not a work day but I’ll answer questions as long as they keep coming! I’ll try to leave the last.
CSE115/ENGR160 Discrete Mathematics 02/24/11 Ming-Hsuan Yang UC Merced 1.
Discrete Mathematics Algorithms. Introduction Discrete maths has developed relatively recently. Its importance and application have arisen along with.
CSE115/ENGR160 Discrete Mathematics 03/03/11 Ming-Hsuan Yang UC Merced 1.
Chapter 8 Search and Sort Asserting Java ©Rick Mercer.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Copyright © Cengage Learning. All rights reserved. CHAPTER 11 ANALYSIS OF ALGORITHM EFFICIENCY ANALYSIS OF ALGORITHM EFFICIENCY.
D1: Bin Packing Algorithms. D1: Bin-Packing Algorithms Bin-packing algorithms can be used to find ways to complete a number of tasks in given time slots,
Sorting and Searching Arrays CSC 1401: Introduction to Programming with Java Week 12 – Lectures 1 & 2 Wanda M. Kunkle.
The Fundamentals: Algorithms, the Integers & Matrices.
Chapter 3: The Fundamentals: Algorithms, the Integers, and Matrices
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Approximation schemes Bin packing problem. Bin Packing problem Given n items with sizes a 1,…,a n  (0,1]. Find a packing in unit-sized bins that minimizes.
Randomized Turing Machines
Sorting HKOI Training Team (Advanced)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 9: Algorithm Efficiency and Sorting Data Abstraction &
Introduction to Algorithms Jiafen Liu Sept
TECH Computer Science NP-Complete Problems Problems  Abstract Problems  Decision Problem, Optimal value, Optimal solution  Encodings  //Data Structure.
Lesson Objective: Understand what an algorithm is and be able to use them to solve a simple problem.
Major objective of this course is: Design and analysis of modern algorithms Different variants Accuracy Efficiency Comparing efficiencies Motivation thinking.
Summary Algorithms Flow charts Bubble sort Quick sort Binary search Bin Packing.
Sorting CS 110: Data Structures and Algorithms First Semester,
Chapter Algorithms 3.2 The Growth of Functions 3.3 Complexity of Algorithms 3.4 The Integers and Division 3.5 Primes and Greatest Common Divisors.
Algorithms and their Applications CS2004 ( ) Professor Jasna Kuljis (adapted from Dr Steve Swift) 6.1 Classic Algorithms - Sorting.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 2.
Chapter 8 Search and Sort ©Rick Mercer. Outline Understand how binary search finds elements more quickly than sequential search Sort array elements Implement.
Algorithms Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin,
UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a.
ALGORITHMS.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
The bin packing problem. For n objects with sizes s 1, …, s n where 0 < s i ≤1, find the smallest number of bins with capacity one, such that n objects.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
1 Discrete Structures – CNS2300 Text Discrete Mathematics and Its Applications Kenneth H. Rosen (5 th Edition) Chapter 2 The Fundamentals: Algorithms,
Introduction to design and analysis algorithm
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
Onlinedeeneislam.blogspot.com1 Design and Analysis of Algorithms Slide # 1 Download From
Sorting Lower Bounds n Beating Them. Recap Divide and Conquer –Know how to break a problem into smaller problems, such that –Given a solution to the smaller.
DECISION 1. How do you do a Bubble Sort? Bubble Sort:  You compare adjacent items in a list;  If they are in order, leave them.  If they are not in.
1 Introduction to design and analysis algorithm. 2.
Conceptual Foundations © 2008 Pearson Education Australia Lecture slides for this course are based on teaching materials provided/referred by: (1) Statistics.
Searching and Sorting Searching algorithms with simple arrays
CSE15 Discrete Mathematics 03/06/17
Searching and Sorting Algorithms
Bin Packing First fit decreasing algorithm
Discrete Mathematics Algorithms.
IGCSE 6 Cambridge Effectiveness of algorithms Computer Science
Algorithms Chapter 3 With Question/Answer Animations
Algorithm An algorithm is a finite set of steps required to solve a problem. An algorithm must have following properties: Input: An algorithm must have.
Objective of This Course
Unit-2 Divide and Conquer
Chapter 8 Search and Sort
“Human Sorting” It’s a “Problem Solving” game:
Bin Packing First fit decreasing algorithm
Search,Sort,Recursion.
Bin Packing First fit decreasing algorithm
Searching and Sorting Arrays
Bin Packing First fit decreasing algorithm
Algorithm Efficiency and Sorting
Introduction to Algorithms
“Human Sorting” It’s a “Problem Solving” game:
Bin Packing and Binary Search
Algorithms Tutorial 27th Sept, 2019.
Presentation transcript:

Algorithms An algorithm is a set of instructions that enable you, step-by-step, to achieve a particular goal. Computers use algorithms to solve problems on a large scale, as they are able to follow millions of simple instructions per second. The algorithms you learn in D1 are not conceptually difficult, yet they do represent and give an insight into the tools developed by computer programmers. This section considers algorithms that: Sort values into size order Locate desired values in a list Maximise usage of materials All of these require you to follow a few simple steps, but before that we look at algorithms in the form of flowcharts designed to achieve a range of ‘goals’.

Euclid’s Algorithm r = 0? Start Stop The Greek Mathematician Euclid devised an algorithm for finding the Highest Common Factor of 2 numbers: Eg find the HCF of 240 and 90 a b P q r r=0? 240 90 2 180 60 no 90 60 1 60 30 no 60 30 2 60 yes Output = HCF(240,90) = 30 Computers use algorithms to do everything! Is r = 0? Yes Stop No This may seem cumbersome, but imagine dealing with very large numbers and introduce a machine that can run the algorithm for you… Eg find the HCF of 7609800 and 54810068

Eg An algorithm is described by the flowchart shown. (a) Given that S = 25 000, complete the table to show the results obtained at each step when the algorithm is applied. S T R R > 0? Output 25000 17000 yes 3400 7000 yes 4450 -5000 no 4450 This algorithm is designed to model a possible system of income tax, T, on an annual salary, £S. (b) Write down the amount of income tax paid by a person with an annual salary of £ 25 000. £4450 (c) Find the maximum annual salary of a person who pays no tax. A person pays no tax if when T = 0 So maximum salary is £8000

Eg An algorithm is described by the flowchart shown. (a) Given that S = 25 000, complete the table to show the results obtained at each step when the algorithm is applied. S T R R > 0? Output This algorithm is designed to model a possible system of income tax, T, on an annual salary, £S. (b) Write down the amount of income tax paid by a person with an annual salary of £ 25 000. (c) Find the maximum annual salary of a person who pays no tax.

You may not need to use all the rows in this table. a b c Let P = 2, 3, 5, 7, 11, 13, … WB1(a) Starting with a = 90, implement this algorithm. Show your working in the table below. You may not need to use all the rows in this table. a b c Integer? Output List a = b? 90 2 45 y 2 n 45 2 22.5 n 45 3 15 y 3 n 15 2 7.5 n 15 3 5 y 3 n 5 2 2.5 n 5 3 1.66… n 5 5 1 y 5 y (b) Explain the significance of the output list. The prime factors of a (c) Write down the final value of c for any initial value of a. 1

Bubble sort Sort complete A list can also be ordered using a bubble sort, which compares adjacent values sequentially Eg The list of numbers below is to be sorted into ascending order. Perform a bubble sort to obtain the sorted list, giving the state of the list after each completed pass. 45 56 37 79 46 18 90 81 51 45 56 37 79 46 18 90 81 51 45 37 56 46 18 79 81 51 90 37 45 46 18 56 79 51 81 90 37 45 18 46 56 51 79 81 90 37 18 45 46 51 56 79 81 90 Sort complete

Bubble sort Sort complete WB3. The list of numbers below is to be sorted into descending order. Perform a bubble sort to obtain the sorted list, giving the state of the list after each completed pass. 52 48 50 45 64 47 53 52 48 50 45 64 47 53 52 50 48 64 47 53 45 52 50 64 48 53 47 45 52 64 50 53 48 47 45 64 52 53 50 48 47 45 Sort complete

Quick sort Sort complete A list can be ordered using a quick sort, which splits the list to obtain pivots 45 32 51 75 56 47 61 70 28 The list of numbers above is to be sorted into ascending order. Perform a Quick Sort to obtain the sorted list, giving the state of the list after each pass, indicating the pivot elements. 45 32 51 75 56 47 61 70 28 45 32 51 47 28 56 75 61 70 45 32 47 28 51 61 75 70 45 32 28 47 70 75 28 32 45 75 28 45 Why do you think it is called a quick sort? Sort complete

WB2a) The following list gives the names of some students who have represented Britain in the International Mathematics Olympiad. Roper (R), Palmer (P), Boase (B), Young (Y), Thomas (T), Kenney (K), Morris (M), Halliwell (H), Wicker (W), Garesalingam (G). (a) Use the quick sort algorithm to sort the names above into alphabetical order. R P B Y T K M H W G B H G K R P Y T M W B G H R P M T Y W B G M P R W Y B M R Y Sort complete

The list of numbers below is to be sorted into asscending order. Perform: a bubble sort to obtain the sorted list, giving the state of the list after each completed pass. a quick sort to obtain the sorted list, giving the state of the list after each completed pass. 8 4 13 2 17 9 15 8 4 13 2 17 9 15 2 8 4 13 17 9 15 8 4 13 9 15 17 8 4 9 13 15 4 8 9 15 8 9 8 Quick sort 8 4 13 2 17 9 15 4 8 2 13 9 15 17 4 2 8 9 13 15 17 2 4 8 9 13 15 17 Bubble sort

Desired values in a list can be located using a binary search, which uses a process of elimination to find the value Binary Search Eg A list of numbers, in ascending order, is 7, 23, 31, 37, 41, 44, 50, 62, 71, 73, 94 Use the binary search algorithm to locate the number 73 in this list. 1st 7 2nd 23 Reject 7 to 44 3rd 31 4th 37 Reject 50 to 71 5th 41 6th 44 7th 50 Reject 94 8th 62 9th 71 Leaving Number found, search complete 10th 73 11th 94

Binary Search WB2b) The following list gives the names of some students who have represented Britain in the International Mathematics Olympiad. Use the binary search algorithm to locate the name Kenney 1st Boase (B) 2nd Garesalingam (G) Reject P to Y 3rd Halliwell (H) 4th Kenney (K) Reject B to H 5th Morris (M) 6th Palmer (P) Reject M 7th Roper (R) 8th Thomas (T) Leaving Name found, search complete 9th Wicker (W) 10th Young (Y)

Bin Packing: first fit decreasing Suppose you need some expensive wood, in various lengths, for a DIY project. It only comes in set lengths and you want to minimise the number of lengths you buy and therefore minimise the total cost. Bin packing can be used to do this. WB4 Nine pieces of wood are required to build a small cabinet. The lengths, in cm, of the pieces of wood are listed below. 20, 20, 20, 35, 40, 50, 60, 70, 75 Planks, one metre in length, can be purchased at a cost of £3 each. The first fit decreasing algorithm is used to determine how many of these planks are to be purchased to make this cabinet. Find the total cost and the amount of wood wasted. Planks of wood can also be bought in 1.5 m lengths, at a cost of £4 each. The cabinet can be built using a mixture of 1 m and 1.5 m planks. b) Find the minimum cost of making this cabinet. Justify your answer.

Bin Packing 20, 20, 20, 35, 40, 50, 60, 70, 75 To see if a solution is optimal: Bin Lengths of wood Waste calculate the total of all values 1 35 20 20 50 20 75 60 70 40 5 divide this by the bin capacity 2 10 round to the next integer If optimal, this value matches the number of bins you used 3 4 15 If value is smaller, the solution may not be optimal 5 80 Amount needed = 390 Bin capacity = 100 Total waste = 110cm So minimum 4 bins required Total cost = 5 x £3 = £15 Solution may not be optimal

Bin Packing There are 3 bin packing algorithms you must be able to apply: 8 7 14 9 6 9 5 15 6 7 8 Eg The numbers represent the lengths, in cm, of pieces to be cut from 20cm rods First-fit First-fit decreasing Full-bin Bin Lengths 15 14 9 9 8 8 7 7 6 6 5 8 7 14 9 6 9 5 15 6 7 8 1 8 7 5 Bin Lengths Bin Lengths 2 14 6 1 15 5 1 15 5 15 + 5 = 20 3 9 9 2 14 6 2 14 6 14 + 6 = 20 4 15 3 9 9 3 7 7 6 7 + 7 + 6 = 20 5 6 7 4 8 8 4 8 9 6 8 5 7 7 6 5 9 8 Fit values into the first bin with enough space Put values in descending size order, then apply first-fit algorithm Group values into totals to fill bins, then apply first-fit algorithm There is no guarantee that any of the algorithms will give an optimal solution, but the full-bin method is most likely to be optimal and the first-fit method is least likely. Which is best?