Algorithms: An Introduction Section 3.1 of Rosen Spring 2011 CSCE 235 Introduction to Discrete Structures Course web-page: cse.unl.edu/~cse235 Questions:

Slides:



Advertisements
Similar presentations
22C:19 Discrete Math Algorithms and Complexity
Advertisements

College of Information Technology & Design
MATH 224 – Discrete Mathematics
Greedy Algorithms Clayton Andrews 2/26/08. What is an algorithm? “An algorithm is any well-defined computational procedure that takes some value, or set.
Greedy Algorithms Basic idea Connection to dynamic programming
Learning Objectives Explain similarities and differences among algorithms, programs, and heuristic solutions List the five essential properties of an algorithm.
Chapter 10 Algorithmic Thinking. Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Learning Objectives List the five essential.
Cs333/cutler Greedy1 Introduction to Greedy Algorithms The greedy technique Problems explored –The coin changing problem –Activity selection.
CSE115/ENGR160 Discrete Mathematics 02/28/12
Infinite Horizon Problems
Introduction to Analysis of Algorithms
1 Module 9 Recursive and r.e. language classes –representing solvable and half-solvable problems Proofs of closure properties –for the set of recursive.
Lecture 8 Recursively enumerable (r.e.) languages
CSE115/ENGR160 Discrete Mathematics 02/24/11 Ming-Hsuan Yang UC Merced 1.
CSE115/ENGR160 Discrete Mathematics 03/03/11 Ming-Hsuan Yang UC Merced 1.
Algorithms: An Introduction
Analysis of Algorithms CS 477/677
Introduction CSCE 235 Introduction to Discrete Structures Spring 2010 Instructor: Berthe Y. Choueiry (Shu-we-ri) GTA: Nobel Khandaker
DAST, Spring © L. Joskowicz 1 Data Structures – LECTURE 1 Introduction Motivation: algorithms and abstract data types Easy problems, hard problems.
10/31/02CSE Greedy Algorithms CSE Algorithms Greedy Algorithms.
TK3043 Analysis and Design of Algorithms Introduction to Algorithms.
The Fundamentals: Algorithms, the Integers & Matrices.
10/31/02CSE Greedy Algorithms CSE Algorithms Greedy Algorithms.
February 17, 2015Applied Discrete Mathematics Week 3: Algorithms 1 Double Summations Table 2 in 4 th Edition: Section th Edition: Section th.
1.1 Chapter 1: Introduction What is the course all about? Problems, instances and algorithms Running time v.s. computational complexity General description.
Discrete Structures Lecture 11: Algorithms Miss, Yanyan,Ji United International College Thanks to Professor Michael Hvidsten.
MATH 224 – Discrete Mathematics
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Sets.
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.
Introduction to Algorithms By Mr. Venkatadri. M. Two Phases of Programming A typical programming task can be divided into two phases: Problem solving.
Major objective of this course is: Design and analysis of modern algorithms Different variants Accuracy Efficiency Comparing efficiencies Motivation thinking.
CompSci 102 Discrete Math for Computer Science
Data Structure Introduction.
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.
1 Section 2.1 Algorithms. 2 Algorithm A finite set of precise instructions for performing a computation or for solving a problem.
Lecture 11 Data Structures, Algorithms & Complexity Introduction Dr Kevin Casey BSc, MSc, PhD GRIFFITH COLLEGE DUBLIN.
Algorithms & FlowchartsLecture 10. Algorithm’s CONCEPT.
CS 103 Discrete Structures Lecture 12
1 Markov Decision Processes Infinite Horizon Problems Alan Fern * * Based in part on slides by Craig Boutilier and Daniel Weld.
Algorithms 1.Notion of an algorithm 2.Properties of an algorithm 3.The GCD algorithm 4.Correctness of the GCD algorithm 5.Termination of the GCD algorithm.
UNIT-I INTRODUCTION ANALYSIS AND DESIGN OF ALGORITHMS CHAPTER 1:
ALGORITHMS.
22C:19 Discrete Math Algorithms and Integers Fall 2010 Sukumar Ghosh.
1 Discrete Structures – CNS2300 Text Discrete Mathematics and Its Applications Kenneth H. Rosen (5 th Edition) Chapter 2 The Fundamentals: Algorithms,
CES 592 Theory of Software Systems B. Ravikumar (Ravi) Office: 124 Darwin Hall.
Copyright © 2014 Curt Hill Algorithms From the Mathematical Perspective.
BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture1.
P, NP, and NP-Complete Problems Section 10.3 The class P consists of all problems that can be solved in polynomial time, O(N k ), by deterministic computers.
Greedy Algorithms Prof. Kharat P. V. Department of Information Technology.
Chapter 3 With Question/Answer Animations 1. Chapter Summary Algorithm and Growth of Function (This slide) Algorithms - Sec 3.1 – Lecture 13 Example Algorithms.
CSE15 Discrete Mathematics 03/06/17
Advanced Algorithms Analysis and Design
Math/CSE 1019C: Discrete Mathematics for Computer Science Fall 2012
Data Structures and Algorithms
Chapter 1 Introduction Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Algorithms: An Introduction
CS 2210 Discrete Structures Algorithms and Complexity
Algorithms Chapter 3 With Question/Answer Animations
Objective of This Course
CS 2210 Discrete Structures Algorithms and Complexity
Discrete Mathematics CMP-101 Lecture 12 Sorting, Bubble Sort, Insertion Sort, Greedy Algorithms Abdul Hameed
Greedy Algorithms.
CSE 2010: Algorithms and Data Structures Algorithms
Algorithms: An Introduction
CSCE 222 Discrete Structures for Computing
Chapter 1 Introduction Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
CS 2210 Discrete Structures Algorithms and Complexity
Presentation transcript:

Algorithms: An Introduction Section 3.1 of Rosen Spring 2011 CSCE 235 Introduction to Discrete Structures Course web-page: cse.unl.edu/~cse235 Questions: ‘Algorithm’ is a distortion of Al-Khawarizmi, a Persian mathematician

Algorithms: An IntroductionCSCE Outline Introduction & definition Algorithms categories & types Pseudo-code Designing an algorithm – Example: Max Greedy Algorithms – Change

Algorithms: An IntroductionCSCE Computer Science is About Problem Solving A Problem is specified by 1.The givens (a formulation) A set of objects Relations between them 2.The query The information one wants to extract from the formulation, the question to answer An algorithm is a method or procedure that solves instances of a problem Real World  Computing World Objectsrepresented by…data Structures, ADTs, Classes Relationsimplemented with…relations & functions (e.g., predicates) ActionsImplemented with…algorithms: a sequence of instructions

Algorithms: An IntroductionCSCE Algorithms: Formal Definition Definition: An algorithm is a sequence of unambiguous instructions for solving a problem. Properties of an algorithm – Finite: the algorithm must eventually terminate – Complete: Always give a solution when one exists – Correct (sound): Always give a correct solution For an algorithm to be an acceptable solution to a problem, it must also be effective. That is, it must give a solution in a ‘reasonable’ amount of time Efficient= runs in polynomial time. Thus, effective  efficient There can be many algorithms to solve the same problem

Algorithms: An IntroductionCSCE Outline Introduction & definition Algorithms categories & types Pseudo-code Designing an algorithm – Example: Max Greedy Algorithms – Change

Algorithms: An IntroductionCSCE Algorithms: General Techniques There are many broad categories of algorithms – Deterministic versus Randomized (e.g., Monte-Carlo) – Exact versus Approximation – Sequential/serial versus Parallel, etc. Some general styles of algorithms include – Brute force (enumerative techniques, exhaustive search) – Divide & Conquer – Transform & Conquer (reformulation) – Greedy Techniques

Algorithms: An IntroductionCSCE Outline Introduction & definition Algorithms categories & types Pseudo-code Designing an algorithm – Example: Max Greedy Algorithms – Change

Algorithms: An IntroductionCSCE Good Pseudo-Code: Example Intersection Input: Two finite sets A, B Output: A finite set C such that C = A  B 1.C  2. If |A|>|B| 3. Then Swap( A,B) 4. End 5. For every x  A Do 6. If x  B 7. Then C  C  {x} Union (C,{x}) 8. End 9. End 10. Return C

Algorithms: An IntroductionCSCE Algorithms: Pseudo-Code Algorithms are usually presented using pseudo-code Bad pseudo-code – gives too many details or – is too implementation specific (i.e., actual C++ or Java code or giving every step of a sub-process such as set union) Good pseudo-code – Is a balance between clarity and detail – Abstracts the algorithm – Makes good use of mathematical notation – Is easy to read and – Facilitates implementation (reproducible, does not hide away important information)

Algorithms: An IntroductionCSCE Writing Pseudo-Code: Advice Input/output must properly defined All your variables must be properly initialized, introduced Variables are instantiated, assigned using  All `commands' (while, if, repeat, begin, end) bold face \bf For i  1 to n Do All functions in small caps Union (s,t) \sc All constants in courier: pi  3.14 \tt All variables in italic: temperature  78 (\it, \em) LaTeX: Several algorithm formatting packages exist on WWW

Algorithms: An IntroductionCSCE Outline Introduction & definition Algorithms categories & types Pseudo-code Designing an algorithm – Example: Max Greedy Algorithms – Change

Algorithms: An IntroductionCSCE Designing an Algorithm A general approach to designing algorithms is as follows – Understanding the problem, assess its difficulty – Choose an approach (e.g., exact/approximate, deterministic/ probabilistic) – (Choose appropriate data structures) – Choose a strategy – Prove 1.Termination 2.Completeness 3.Correctness/soundness – Evaluate complexity – Implement and test it – Compare to other known approach and algorithms

Algorithms: An IntroductionCSCE Algorithm Example: Max When designing an algorithm, we usually give a formal statement about the problem to solve Problem – Given: a set A={a 1,a 2,…,a n } of integers – Question: find the index i of the maximum integer a i A straightforward idea is – Simply store an initial maximum, say a 1 – Compare the stored maximum to every other integer in A – Update the stored maximum if a new maximum is ever encountered

Algorithms: An IntroductionCSCE Pseudo-code of Max Max Input: A finite set A={a 1,a 2,…,a n } of integers Output: The largest element in the set 1. temp  a 1 2. For i =2 to n Do 3. If a i > temp 4. Then temp  a i 5. End 6. End 7. Return temp

Algorithms: An IntroductionCSCE Algorithms: Other Examples Check Bubble Sort and Insertion Sort in your textbooks … which you should have seen ad nauseum in CSE 155 and CSE 156 And which you will see again in CSE 310 Let us know if you have any questions

Algorithms: An IntroductionCSCE Outline Introduction & definition Algorithms categories & types Pseudo-code Designing an algorithm – Example: Max Greedy Algorithms – Change

Algorithms: An IntroductionCSCE Greedy Algorithms In many problems, we wish to not only find a solution, but to find the best or optimal solution A simple technique that works for some optimization problems is called the greedy technique As the name suggests, we solve a problem by being greedy: – Choose what appears now to be the best choice – Choose the most immediate best solution (i.e., think locally) Greedy algorithms – Work well on some (simple) algorithms – Usually they are not guaranteed to produce the best globally optimal solution

Algorithms: An IntroductionCSCE Change-Making Problem We want to give change to a customer but we want to minimize the number of total coins we give them Problem – Given: An integer n an a set of coin denominations (c 1,c 2,…,c r ) with c 1 >c 2 >…>c r – Query: Find a set of coins d 1,d 2,…,d k such that  i=1 k d i = n and k is minimized

Algorithms: An IntroductionCSCE Greedy Algorithm: Change Change Input: An integer n and a set of coin denominations {c 1,c 2,…,c r } with c 1 > c 2 > … >c r Output: A set of coins d 1,d 2,…,d k such that  i=1 k d i = n and k is minimized 1.C   2. For i =1 to r Do 3. While n  c i Do 4. C  C  {c i } 5. n  n - c i 6. End 7. Return C

Algorithms: An IntroductionCSCE Change : Analysis (1) Will the algorithm always produce an optimal answer? Example –Consider a coinage system where c 1 =20, c 2 =15, c 3 =7, c 4 =1 –We want to give 22 ‘cents’ in change What is the output of the algorithm? Is it optimal? It is not optimal because it would give us two c4 and one c1 (3 coins). The optimal change is one c2 and one c3 (2 coins)

Algorithms: An IntroductionCSCE Change : Analysis (2) What about the US currency system: is the algorithm correct in this case? Yes, in fact it is. We can prove it by contradiction. For simplicity, let us consider c 1 =25, c 2 =10, c 3 =5, c 4 =1

Algorithms: An IntroductionCSCE Optimality of Change (1) Let C={d 1,d 2,…,d k } be the solution given by the greedy algorithm for some integer n. By way of contradiction, assume there is a better solution C’={d’ 1,d’ 2,…,d’ l } with l<k Consider the case of quarters. Say there are q quarters in C and q’ in C’. 1.If q’>q, the greedy algorithm would have used q’ by construction. Thus, it is impossible that the greedy uses q<q’. 2.Since the greedy algorithms uses as many quarters as possible, n=q(25)+r, where r<25. If q’<q, then, n=q’(25)+r’ where r’  25. C’ will have to use more smaller coins to make up for the large r’. Thus C’ is not the optimal solution. 3.If q=q’, then we continue the argument on the smaller denomination (e.g., dimes). Eventually, we reach a contradiction. Thus, C=C’ is our optimal solution

Algorithms: An IntroductionCSCE Optimality of Change (2) But, how about the previous counterexample? Why (and where) does this proof? We need the following lemma: If n is a positive integer, then n cents in change using quarters, dimes, nickels, and pennies using the fewest coins possible – Has at most two dimes, – Has at most one nickel – Has at most four pennies, and – Cannot have two dimes and a nickel The amount of change in dimes, nickels, and pennies cannot exceed 24 cents

Algorithms: An IntroductionCSCE Greedy Algorithm: Another Example Check the problem of Scenario I, page 8 in the slides IntroductiontoCSE235.ppt IntroductiontoCSE235.ppt We discussed then (remember?) a greedy algorithm for accommodating the maximum number of customers. The algorithm – terminates, is complete, sound, and satisfies the maximum number of customers (finds an optimal solution) – runs in time linear in the number of customers

Algorithms: An IntroductionCSCE Summary Introduction & definition Algorithms categories & types Pseudo-code Designing an algorithm – Example: Max Greedy Algorithms – Example: Change