The Fundamentals: Algorithms, the Integers & Matrices.

Slides:



Advertisements
Similar presentations
SortingTechniques. Bubble-sort: One of the simplest sorting methods. The basic idea is the weight of the record. The records are kept in an array held.
Advertisements

22C:19 Discrete Math Algorithms and Complexity
Introduction to Algorithms
College of Information Technology & Design
Algorithm Analysis.
MATH 224 – Discrete Mathematics
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
DIVIDE AND CONQUER APPROACH. General Method Works on the approach of dividing a given problem into smaller sub problems (ideally of same size).  Divide.
CompSci 102 Discrete Math for Computer Science
CSE115/ENGR160 Discrete Mathematics 02/28/12
Chapter 2: Fundamentals of the Analysis of Algorithm Efficiency
DAST, Spring © L. Joskowicz 1 Data Structures – LECTURE 1 Introduction Motivation: algorithms and abstract data types Easy problems, hard problems.
CSE115/ENGR160 Discrete Mathematics 03/03/11 Ming-Hsuan Yang UC Merced 1.
CSE115/ENGR160 Discrete Mathematics 03/10/11
10/31/02CSE Greedy Algorithms CSE Algorithms Greedy Algorithms.
Lecture 3: Algorithms and Complexity Study Chapter COMP 555 Bioalgorithms Fall 2014.
10/31/02CSE Greedy Algorithms CSE Algorithms Greedy Algorithms.
CSE 373 Data Structures Lecture 15
Describing algorithms in pseudo code To describe algorithms we need a language which is: – less formal than programming languages (implementation details.
Chapter Complexity of Algorithms –Time Complexity –Understanding the complexity of Algorithms 1.
1 Chapter 1 Analysis Basics. 2 Chapter Outline What is analysis? What to count and consider Mathematical background Rates of growth Tournament method.
Algorithms Section 3.1. Problems and Algorithms In many domains there are key general problems that ask for output with specific properties when given.
Design and Analysis of Algorithms - Chapter 21 Analysis of Algorithms b Issues: CorrectnessCorrectness Time efficiencyTime efficiency Space efficiencySpace.
Chapter 12 Recursion, Complexity, and Searching and Sorting
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
Chapter 19: Searching and Sorting Algorithms
Chapter 3 Sec 3.3 With Question/Answer Animations 1.
Jessie Zhao Course page: 1.
22C:19 Discrete Structures Algorithms and Complexity Fall 2014 Sukumar Ghosh.
Counting Coins. The Basics Quarter 25 cents Dime 10 cents.
CompSci 102 Discrete Math for Computer Science
1 Today’s Material Iterative Sorting Algorithms –Sorting - Definitions –Bubble Sort –Selection Sort –Insertion Sort.
Foundation of Computing Systems
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.
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.
Section 3.1. Section Summary Properties of Algorithms Algorithms for Searching and Sorting Greedy Algorithms Halting Problem.
Example { 1 – –1} Use Bubble Sort (sort in increasing order} After first pass { –1 50} After Second Pass { } After.
Name the United States Coins Count the Pennies 10 ¢
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
CS 103 Discrete Structures Lecture 12
22C:19 Discrete Math Algorithms and Integers Fall 2010 Sukumar Ghosh.
1 UNIT-I BRUTE FORCE ANALYSIS AND DESIGN OF ALGORITHMS CHAPTER 3:
Searching Topics Sequential Search Binary Search.
Copyright © 2014 Curt Hill Algorithms From the Mathematical Perspective.
1 Chapter 2 Program Performance. 2 Concepts Memory and time complexity of a program Measuring the time complexity using the operation count and step count.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
SORTING Sorting is storage of data in some order, it can be in ascending or descending order. The term Sorting comes along-with the term Searching. There.
Discrete Mathematics Chapter 2 The Fundamentals : Algorithms, the Integers, and Matrices. 大葉大學 資訊工程系 黃鈴玲.
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
Algorithms (Section 3.1) The Growth of Functions (Section 3.2)
Algorithms Chapter 3.
CSE15 Discrete Mathematics 03/06/17
CSE15 Discrete Mathematics 03/13/17
Problem solving sequence
Lecture – 2 on Data structures
CS 2210 Discrete Structures Algorithms and Complexity
CS 2210 Discrete Structures Algorithms and Complexity
Name the United States Coins
Discrete Mathematics CMP-101 Lecture 12 Sorting, Bubble Sort, Insertion Sort, Greedy Algorithms Abdul Hameed
3. Brute Force Selection sort Brute-Force string matching
Applied Combinatorics, 4th Ed. Alan Tucker
Analysis of Algorithms
CSCE 222 Discrete Structures for Computing
3. Brute Force Selection sort Brute-Force string matching
Algorithms Chapter 3 With Question/Answer Animations.
Algorithm Course Algorithms Lecture 3 Sorting Algorithm-1
3. Brute Force Selection sort Brute-Force string matching
CS 2210 Discrete Structures Algorithms and Complexity
Presentation transcript:

The Fundamentals: Algorithms, the Integers & Matrices

Algorithms 2 Sorting Goal: “Order the elements of a list”. For example, sorting the list 7, 2, 1, 4, 5, 9 produces the list 1, 2, 4,, 5, 7, 9. Similarly, sorting the list d, h, c, a, f produces a, c, d, f, h.

3 The Bubble sort Example: Sort the list 3, 2, 4, 1, 5 into increasing order using the Bubble sort st pass 2 nd pass 3 rd pass4 th pass Steps of the Bubble sort = ordered = permute

Algorithms 4 Procedure Bubblesort (a 1, …, a n ) for i := 1 to n-1 {count number of passes} for j := 1 to n-i if a j > a j+1 then interchange a j and a j+1 {a 1, …, a n is the increasing order}

Algorithms 5 Greedy algorithms Goal: Solving optimization problems. Find a solution to the given problem that either minimizes or maximizes the value of some parameter Some examples that involves optimization: Find a route between 2 cities with smallest total mileage Determine a way to encode messages using the fewest bits possible Find a set of fiber links between networks nodes using the least amount of fiber

Algorithms 6 The change making problem Problem statement: Consider the problem of making n cents change with quarters, dimes, nickels and pennies, and using the least total number of coins. For example, to make change for 67 cents, we do the following: 1. Select a quarter, leaving 42 cents 2. Select a second quarter, leaving 17 cents 3. Select a dime, leaving 7 cents 4. Select a nickel, leaving 2 cents 5. Select a penny, leaving 1 cent 6. Select a penny.

Algorithms 7 Greedy change making Procedure change (c 1, c 2, …, c r : values of denominations of coins where c 1 > c 2 >…> c r ; n: positive integer) For i := 1 to r while n  c i begin add a coin with value c i to the change n := n-c i end

Algorithms 8 Remark: if we have only quarters, dimes and pennies  the change for 30 cents would be made using 6 coins = 1 quarter + 5 pennies. Whereas a better solution is equal to 3 coins = 3 dimes! Therefore: “The greedy algorithm selects the best choice at each step, instead of considering all sequences of steps that may lead to an optimal solution. The greedy algorithm often leads to a solution!”

Complexity of Algorithms 9 Time Complexity: Determine the approximate number of operations required to solve a problem of size n. Space Complexity: Determine the approximate memory required to solve a problem of size n.

Complexity of Algorithms 10 Time Complexity Use the Big-O notation Count the expensive operations only Basic operations: searching algorithms - key comparisons sorting algorithms - list component comparisons numerical algorithms - floating point ops. (flops) - multiplications/divisions and/or additions/subtractions

Complexity of Algorithms 11 Worst Case: maximum number of operations Average Case: mean number of operations assuming an input probability distribution

Complexity of Algorithms 12 Examples: Multiply an n x n matrix A by a scalar c to produce the matrix B: procedure (n, c, A, B) for i from 1 to n do for j from 1 to n do B[i, j] = c * A[i, j] end do Analysis (worst case): Count the number of floating point multiplications. n 2 elements requires n 2 multiplications. time complexity is O(n 2 ) or quadratic complexity.

Complexity of Algorithms 13 Bubble sort: L is a list of elements to be sorted. We assume nothing about the initial order The list is in ascending order upon completion. Analysis (worst case): Count the number of list comparisons required. Method: If the jth element of L is larger than the (j + 1)st, swap them. Note: this is not an efficient implementation of the algorithm

Complexity of Algorithms 14 procedure bubble (n, L) /* - L is a list of n elements - swap is an intermediate swap location */ for i from n - 1 to 1 by -1 do for j from 1 to i do if L(j) > L(j + 1) do swap = L(j + 1) L(j + 1) = L (j) L(j) = swap end do

Complexity of Algorithms 15 Bubble the largest element to the 'top' by starting at the bottom - swap elements until the largest in the top position. Bubble the second largest to the position below the top. Continue until the list is sorted. n-1 comparison on the first pass n-2 comparisons on the second pass. 1 comparison on the last pass Total: (n - 1)+ (n - 2) = O(n 2 ) or quadratic complexity (what is the leading coefficient?)