Discrete Mathematics Algorithms. Introduction Discrete maths has developed relatively recently. Its importance and application have arisen along with.

Slides:



Advertisements
Similar presentations
MATH 224 – Discrete Mathematics
Advertisements

Garfield AP Computer Science
CSE Lecture 3 – Algorithms I
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Fundamentals of Python: From First Programs Through Data Structures
Selection and Insertion Sort Mrs. C. Furman October 1, 2008.
Chapter 9: Searching, Sorting, and Algorithm Analysis
CIS 101: Computer Programming and Problem Solving Lecture 6 Usman Roshan Department of Computer Science NJIT.
CSE115/ENGR160 Discrete Mathematics 03/03/11 Ming-Hsuan Yang UC Merced 1.
Level ISA3: Information Representation
Computer Programming Sorting and Sorting Algorithms 1.
Algorithms. Introduction Before writing a program: –Have a thorough understanding of the problem –Carefully plan an approach for solving it While writing.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 9 Searching.
The Program Design Phases
Fundamentals of C programming
ALGORITHMS AND FLOWCHARTS
Abstract Data Types (ADTs) Data Structures The Java Collections API
CSCI-100 Introduction to Computing Algorithms Part I.
Lecture # 8 ALGORITHMS AND FLOWCHARTS. Algorithms The central concept underlying all computation is that of the algorithm ◦ An algorithm is a step-by-step.
Chapter 3: The Fundamentals: Algorithms, the Integers, and Matrices
PROGRAMMING LANGUAGES Prof. Lani Cantonjos. PROGRAM - set of step-by-step instructions that tells or directs the computer what to do. PROGRAMMING LANGUAGE.
Course Web Page Most information about the course (including the syllabus) will be posted on the course wiki:
Algorithm Evaluation. What’s an algorithm? a clearly specified set of simple instructions to be followed to solve a problem a way of doing something What.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
Lesson Objective: Understand what an algorithm is and be able to use them to solve a simple problem.
Simple Iterative Sorting Sorting as a means to study data structures and algorithms Historical notes Swapping records Swapping pointers to records Description,
ALGORITHM List of instructions for carrying out some process step by step. A sequence of instructions which has a clear meaning and can performed with.
Algorithms & Flowchart
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use by MSU Dept. of Computer Science.
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.
Decision Maths 1 Sorting Algorithm Shuttle Sort A V Ali : 1.Compare items 1 and 2; swap them if necessary 2.Compare 2 and 3; swap.
Sorts Tonga Institute of Higher Education. Introduction - 1 Sorting – The act of ordering data Often, we need to order data.  Example: Order a list of.
To know and use the Bubble Sort and Shuttle Sort Algorithms.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 2.
1 Sorting (Bubble Sort, Insertion Sort, Selection Sort)
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
Algorithms Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin,
ALGORITHMS.
Sorting Algorithm Analysis. Sorting  Sorting is important!  Things that would be much more difficult without sorting: –finding a phone number in the.
In the first pass, the first two numbers are compared. The shuttle sort compares the numbers in pairs from left to right exchanging when necessary.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
Chapter 4, Part II Sorting Algorithms. 2 Heap Details A heap is a tree structure where for each subtree the value stored at the root is larger than all.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Array Applications. Objectives Design an algorithm to load values into a table. Design an algorithm that searches a table using a sequential search. Design.
CMPT 438 Algorithms.
Searching and Sorting Algorithms
Sorting With Priority Queue In-place Extra O(N) space
Discrete Mathematics Algorithms.
COP 3503 FALL 2012 Shayan Javed Lecture 15
Introduction to Search Algorithms
Chapter 9: Searching, Sorting, and Algorithm Analysis
Lecture 2 Introduction to Programming
Algorithm Analysis CSE 2011 Winter September 2018.
ALGORITHMS AND FLOWCHARTS
Algorithm design and Analysis
Shuttle Sort Example 1st pass Comparisons: 1
Sort Techniques.
Standard Version of Starting Out with C++, 4th Edition
ALGORITHMS AND FLOWCHARTS
ME 142 Engineering Computation I
Simple Sorting Methods: Bubble, Selection, Insertion, Shell
Searching and Sorting Arrays
Decision Maths Unit 7 Sorting Algorithms 3. Shell Sort.
Shuttle Sort Example 1st pass Comparisons: 1
CHAPTER 9 SORTING & SEARCHING.
Algorithms Tutorial 27th Sept, 2019.
Presentation transcript:

Discrete Mathematics Algorithms

Introduction Discrete maths has developed relatively recently. Its importance and application have arisen along with the development of computing. Discrete maths deals with discrete rather than continuous data and does not employ the continuous methods of calculus. Computers deal with procedures or algorithms to solve problems and algorithms form a substantial part of discrete maths.

Algorithms An algorithm is a procedure or set of instructions used to solve a problem. A computer programmes are algorithms written in a language which computers can interpret. The algorithm enables a person (or computer) to solve the problem without understanding the whole process

Sorting algorithms A frequently needed operation on a computer is a “sort” Such as sorting data into numerical order e.g. Sort the following list into numerical order starting with the smallest. (time how long it takes you) 3, 9, 10, 24, 2, 7, 1, 56, 43, 29, 36, 17, 4, 12, 77, 21, 100. A computer will do this in much less than a second. How do you know you have not made a mistake? How would you cope with a list of 100 numbers or more?

Sorting algorithms Bubble sort. This algorithm depends on successive comparisons of pairs of numbers. Compare the 1 st and 2 nd numbers in a list and swap them if the 2 nd number is smaller than the 1 st. Compare the 2 nd and 3 rd numbers and swap them if the 3 rd is smaller. Continue in this way to the end of the list. pass This procedure is called a pass

Bubble sort Example : Use a bubble sort to place the numbers in the list in order. 5, 1, 2, 6, 9, 4, 3. This represents one pass and required 6 comparisons and 4 swaps New listOriginal list

Bubble sort The complete algorithm is written as: Step 1 If there is only one number in the list then stop. Step 2 Make one pass down the list comparing numbers in pairs and swapping if necessary. Step 3 If no swaps have occurred then stop. Otherwise ignore the last element in the list and return to step 1.

Example The table shows the complete bubble sort carried out on the list given on the previous slide. Original list 1 st pass2 nd pass3 rd pass4 th pass5 th pass (The numbers in purple are the ones which are ignored.)

Shuttle Sort One disadvantage of the bubble sort is that you have to do a final pass after the list is sorted to ensure the sort is complete. The shuttle sort partially overcomes this problem. Ist pass Compare the 1 st and 2 nd numbers in the list and swap if necessary. 2 nd pass Compare the 2 nd and 3 rd numbers in the list and swap if necessary. If a swap has occurred, compare the 1 st and 2 nd numbers and swap if necessary. 3 rd pass Compare the 3 rd and 4 th numbers in the list and swap if necessary. If a swap has occurred, compare the 2 nd and 3 rd numbers, and so on up the list.

Shuttle sort Using the same list of numbers the table below shows a shuttle sort Original list 1 st pass2 nd pass3 rd pass4 th pass5 th pass6 th pass The shuttle sort has involved the same number of swaps as the bubble sort but 14 comparisons instead of 20.

Practice Questions Exercise 1A page 5

The order of an algorithm efficiency The efficiency of an algorithm is a measure of the “run- time” for the algorithm. This will often be proportional to the number of operations which have to be carried out. size The size of the problem is a measure of its complexity. E.g. in a sorting algorithm it is likely to be related to the number of numbers in the list order The order of an algorithm is a measure of the efficiency of the algorithm as a function of the size of the problem. Examples of different orders of algorithms AlgorithmSizeEfficiencyOrder An5nn or linear Bnn 2 + 7nn 2 or quadratic Cn2n 3 – 3nn 3 or cubic

Packing algorithms In business and industry, efficient packing to make best use of space is important and, for example, computerised systems are used to organise storage. First-Fit Algorithm Place each object in turn in the first available space in which it will fit. This is the simplest algorithm but rarely lead to the most efficient solution

Examples Question A small ferry has 3 lanes, each 25 metres long. The lengths of the vehicles in the queue, in the order in which they are waiting are: Using the first-fit algorithm: The final 11 m vehicle does not fit. 16 m of space is unused. The solution could be improved by putting the 9 m vehicle (no.7) in lane 3 and then the 11m vehicle (no13) will fit in lane 2. 5 m of space is unused.

Increasing efficiency First-Fit Decreasing Algorithm Order all objects in decreasing size and then apply the first-fit algorithm Using the First-Fit Decreasing Algorithm: First place the vehicles in order of decreasing size Then apply the First-Fit Algorithm

Example This is more efficient than the first-fit algorithm and accommodates all vehicles and leaving 3 m space.

Flow diagrams flow diagram A flow diagram is pictorial representation of an algorithm. The shape of the box indicates the type of instruction. Stop Oval boxes Oval boxes are used for starting and stopping, inputting and outputting data Replace m by n Rectangles Rectangles are used for calculations or instructions. Is x > 5? Yes No Diamond shapes Diamond shapes are used for questions and decisions

Notation means “take the number in pigeon hole n and put it in pigeon hole m.” The notation for this is m: = n Similarly m: = 2 means “put the number 2 in pigeon hole m.” m: = m – 1 means “ take the number already in pigeon hole m, subtract 1 and put the result back into pigeon hole m.” Pigeon holes are usually called “stores”. Replace m by n

Example An algorithm has a flow diagram below. Start Read N Is N even? R: = 0 R: = 1 N: = N-1 N: = ½ N Is N = 0Stop Write R to the left of any numbers written so far No Yes (a)What is the output if N = 57? (b)What has the algorithm been designed to do?

Solution (a)After 6 successive passes around the flow diagram, the values of N and R are as follows. PassNRWritten down The algorithm converts N into a binary number.

Practice questions Cambridge Advanced Level Maths Discrete Mathematics 1 Chapter 1 Exercise 1 A Exercise 1 B Miscellaneous Exercise 1