Theory of Algorithms: Introduction James Gain and Edwin Blake {jgain | Department of Computer Science University of Cape Town August.

Slides:



Advertisements
Similar presentations
Lecture 24 Coping with NPC and Unsolvable problems. When a problem is unsolvable, that's generally very bad news: it means there is no general algorithm.
Advertisements

Algorithms + L. Grewe.
CS 345 Ch 4. Algorithmic Methods Spring, Misconceptions about CS Computer Science is the study of Computers. “Computer Science is no more about.
Analysis & Design of Algorithms (CSCE 321)
Theory of Algorithms: Brute Force James Gain and Edwin Blake {jgain | Department of Computer Science University of Cape Town August.
Design and Analysis of Algorithms - Chapter 1
CSE 830: Design and Theory of Algorithms
What is an algorithm? An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate.
The Design and Analysis of Algorithms
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Design and Analysis of Algorithms - Chapter 11 Algorithm An algorithm is a.
TK3043 Analysis and Design of Algorithms Introduction to Algorithms.
Unit 1. Sorting and Divide and Conquer. Lecture 1 Introduction to Algorithm and Sorting.
Algorithms. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The main aim of this lecture.
Design and Analysis of Algorithms
Design & Analysis of Algorithms Introduction. Introduction Algorithms are the ideas behind computer programs. An algorithm is the thing which stays the.
Teaching Teaching Discrete Mathematics and Algorithms & Data Structures Online G.MirkowskaPJIIT.
CS223 Algorithms D-Term 2013 Instructor: Mohamed Eltabakh WPI, CS Introduction Slide 1.
Course Web Page Most information about the course (including the syllabus) will be posted on the course wiki:
Theory of Algorithms: Divide and Conquer James Gain and Edwin Blake {jgain | Department of Computer Science University of Cape Town.
Introduction Algorithms and Conventions The design and analysis of algorithms is the core subject matter of Computer Science. Given a problem, we want.
Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The main aim of this.
Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The.
Introduction to Algorithms By Mr. Venkatadri. M. Two Phases of Programming A typical programming task can be divided into two phases: Problem solving.
Theory of Algorithms: Brute Force. Outline Examples Brute-Force String Matching Closest-Pair Convex-Hull Exhaustive Search brute-force strengths and weaknesses.
Major objective of this course is: Design and analysis of modern algorithms Different variants Accuracy Efficiency Comparing efficiencies Motivation thinking.
Design & Analysis of Algorithms Lecture 1 Introduction.
Introduction to Problem Solving. Steps in Programming A Very Simplified Picture –Problem Definition & Analysis – High Level Strategy for a solution –Arriving.
FIRST QUESTIONS FOR ALGORITHM ANALYSIS. WHAT IS AN ALGORITHM? From the text (p. 3): “An algorithm is a sequence of unambiguous instructions for solving.
Data Structure Introduction.
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. Chapter 1 Introduction.
Design and Analysis of Algorithms - Chapter 11 Algorithm b An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining.
1 BIM304: Algorithm Design Time: Friday 9-12am Location: B4 Instructor: Cuneyt Akinlar Grading –2 Midterms – 20% and 30% respectively –Final – 30% –Projects.
UNIT-I INTRODUCTION ANALYSIS AND DESIGN OF ALGORITHMS CHAPTER 1:
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3 rd ed., Ch. 1 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
CS404 Design and Analysis of Algorithms BBy DDr. M V S Peri Sastry BB.E, PhD(BITS-Pilani)
Theory of Algorithms: Greedy Techniques James Gain and Edwin Blake {jgain | Department of Computer Science University of Cape Town.
DATA STRUCTURES (CS212D) Overview & Review Instructor Information 2  Instructor Information:  Dr. Radwa El Shawi  Room: 
Onlinedeeneislam.blogspot.com1 Design and Analysis of Algorithms Slide # 1 Download From
Why do we study algorithms?. 2 First results are about bats and dolphins.
BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture1.
Ch3 /Lecture #4 Brute Force and Exhaustive Search 1.
Design and Analysis of Algorithms Faculty Name : Ruhi Fatima Course Description This course provides techniques to prove.
Problemløsning: hvordan? PROGRAMMERING SOM HÅNDVÆRK V/ Wilfred Gluud I Samarbejde med: Maj 2003Copyright Wilfred Gluud Programmering How to Solve IT.
RAIK 283 Data Structures and Algorithms
Advanced Algorithms Analysis and Design
TK3043 Analysis and Design of Algorithms
The Design and Analysis of Algorithms
Unit 1. Sorting and Divide and Conquer
Chapter 1 Introduction Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
What is an algorithm? An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate.
What is an algorithm? An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate.
What is an algorithm? An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate.
Introduction to the Design and Analysis of Algorithms
Introduction to The Design & Analysis of Algorithms
What is an algorithm? An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate.
Courtsey & Copyright: DESIGN AND ANALYSIS OF ALGORITHMS Courtsey & Copyright:
What is an algorithm? An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate.
Chapter 1.
What is an algorithm? An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate.
Objective of This Course
3. Brute Force Selection sort Brute-Force string matching
3. Brute Force Selection sort Brute-Force string matching
Introduction to Algorithms
Algorithms Presented By:- Mr. Anup Ashok Shinde BBA (C.A) Dept.
Design and Analysis of Algorithms
Algorithm Course Algorithms Lecture 3 Sorting Algorithm-1
Chapter 1 Introduction Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
3. Brute Force Selection sort Brute-Force string matching
Advanced Analysis of Algorithms
What is an algorithm? An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate.
Presentation transcript:

Theory of Algorithms: Introduction James Gain and Edwin Blake {jgain | Department of Computer Science University of Cape Town August - October 2004

Objectives lTo define an algorithm lTo introduce:  Problem types  The Process of Algorithm Design  Solution strategies  Ways of Analysing Algorithms lTo cover the structure of the course, including practicals

Definitions lAn algorithm is a sequence of unambiguous instructions for solving a problem  For obtaining a required output for any legitimate input in a finite amount of time  Does not require implementation in software  Not an answer but a method for deriving an answer lHistorical Perspective:  Named after Muhammad ibn Musa al-Khwarizmi – 9 th century mathematician 

Notion of algorithm “computer” problem algorithm input output Each step of the algorithm must be unambiguous The range of inputs must be specified carefully The same algorithm can be represented in different ways Several algorithms for solving the same problem may exist - with different properties

What is an algorithm? lRecipe, process, method, technique, procedure, routine,… with following requirements: 1.Finiteness bTerminates after a finite number of steps 2.Definiteness bRigorously and unambiguously specified 3.Input bValid inputs are clearly specified 4.Output bCan be proved to produce the correct output given a valid input 5.Effectiveness bSteps are sufficiently simple and basic

Example: Sorting lStatement of problem:  Input: A sequence of n numbers  Output: A reordering of the input sequence so that a ´ i ≤ a ´ j whenever i < j lInstance: The sequence lAlgorithms: -Selection sort -Insertion sort -Merge sort -(many others)

Selection Sort Input: array a[1],..,a[n] Output: array a sorted in non-decreasing order lAlgorithm: for i=1 to n swap a[i] with smallest of a[i],…,a[n] for i  1 to n do min  i for j  i+1 to n do if a[j] < a[min] min  j swap a[i] and a[min]

Exercise: Bridge Puzzle lProblem:  4 People want to cross a bridge. You have 17 minutes to get them across lConstraints:  It is night and you have 1 flashlight. Max of 2 on the bridge at one time. All start on the same side  Those crossing must have the flashlight with them. The flashlight must be walked back and forth (no throwing)  People walk at different speeds: person A = 1 minute to cross, person B = 2 minutes, person C = 5 minutes, person D = 10 minutes  A pair walks at the speed of the slower person’s pace lRumour: this problem is given to Microsoft interviewees

Solution: Bridge Puzzle lStart (0 min): A B C D lAB Across (2 min): A B C D lA Back (1 min): B A C D lCD Across (10 min): B C D A lB Back (2 min): C D A B lAB Across (2 min): A B C D lTotal Time = 17 minutes

Extension Exercise lThis is an instance of a problem. How would you generalise it? lCan you derive an algorithm to solve this generalised problem?  Must show the sequence of moves  Must output the minimum time required for crossing  Are there any special cases to watch out for?  Are there any constraints on the input?

Extension Solution lInput: a list a of crossing times for n people, numbered 1, …, n lOutput: total time to cross lStrategy: use 1 & 2 as shuttles and send the others across in pairs for i  2 to n/2 do t  a[2] // 1 & 2 across t  t + a[1] // 1 back t  t + a[i*2] // i*2 & (i*2)-1 across t  t + a[2] // 2 back t  a[2] // 1 & 2 across return t

Extension Problems lThis is an inadequate solution lIt falsely assumes certain inputs lList may not be sorted in ascending order  Sort a ln may not be even numbered  Alter final iteration of loop ln > 3 not guaranteed  Special case for n = 1, 2, 3 lIs not optimal for all inputs, e.g. 1, 20, 21, 22  Can you quantify the nature of these inputs? Suggest an alternative. lFinal solution is left as an exercise. Attempt to make your solution elegant

Fundamentals of Algorithmic Problem Solving lUnderstanding the Problem  Make sure you are solving the correct problem and for all legitimate inputs lAscertaining the Capabilities of a Computational Device  Sequential vs. Parallel.  What are the speed and memory limits? lChoosing between exact and approximate Problem Solving  Is absolute precision required? Sometimes this may not be possible lDeciding on Appropriate Data Structures  Algorithms often rely on carefully structuring the data  Fundamental Data Structures: array, linked list, stacks, queues, heaps, graphs, trees, sets

Fundamentals of Algorithm Design lApplying an Algorithm Design Technique  Using a general approach to problem solving that is applicable to a variety of problems lSpecifying the Algorithm  Pseudocode is a mixture of natural language and programming constructs that has replaced flowcharts lProving an Algorithms Correctness  Prove that an algorithm yields a required result for legitimate inputs in finite time lAnalyzing an Algorithm  Consider time efficiency, space efficiency, simplicity, generality, optimality  Analysis can be empirical or theoretical lCoding an Algorithm

Well known Computational Problems lSorting lSearching lString Processing  String Matching lGraph Problems  Graph Traversal, Shortest Path, Graph Colouring lCombinatorial Problems  Find a combinatorial object - permutation, combination, subset - subject to constraints lGeometric Problems  Closest-Pair, Convex-Hull lNumerical Problems  Solving systems of equations, computing definite integrals, evaluating functions, etc.

Algorithm Design Strategies lBrute force  A straightforward approach to solving a problem, usually directly based on the problem’s statement lDivide and conquer  Divide a problem into smaller instances, solve smaller instances (perhaps recursively), combine lDecrease and conquer  Exploit relationship between the problem and a smaller instance reduced by some factor (often 1) lTransform and conquer  Transform the problem to a simpler instance, another representation or an instance with a known solution

More Algorithm Design Strategies lGreedy approach  Make locally optimal steps which (hopefully) lead to a globally optimal solution for an optimization problem lDynamic programming  Technique for solving problems with overlapping sub- domains lBacktracking and Branch and bound  A way of tackling difficult optimization and combinatorial problems without exploring all state-space lSpace and time tradeoffs  Preprocess the input and store additional information to accelerate solving the problem

How to Solve It: Understanding the Problem lTaken from G. Polya, “How to Solve It”, 2nd edition. A classic textbook on problem solving for mathematics 1.You have to understand the problem.  What is the unknown? What are the data? Is the problem statement sufficient, redundant, contradictory  Draw a figure. Introduce suitable notation  Separate the various parts of the problem. Can you write them down?

Devising a Plan lFind the connection between the data and the unknown. You may be obliged to consider auxiliary problems if an immediate connection cannot be found. You should obtain eventually a plan of the solution. lHave you seen it before? Or have you seen the same problem in a slightly different form? lDo you know a related problem? Do you know a theorem that could be useful? lLook at the unknown! And try to think of a familiar problem having the same or a similar unknown. lCould you restate the problem? Could you restate it still differently? Go back to definitions. lIf you cannot solve the proposed problem try to solve first some related problem. Are the unknown and the new data nearer to each other? lDid you use all the data? Did you use the whole condition? Have you taken into account all essential notions involved in the problem?

Carrying it Through 3.Carry out the Plan  Carrying out your plan of the solution, check each step. Can you see clearly that the step is correct? Can you prove that it is correct? 4.Looking Back  Can you check the result? Can you check the argument?  Can you derive the solution differently? Can you see it at a glance?  Can you use the result, or the method, for some other problem?

Analysis of Algorithms lHow good is the algorithm?  Correctness  Time efficiency  Space efficiency  Simplicity lDoes there exist a better algorithm?  Lower bounds  Optimality

Why Study Algorithms? lTheoretical importance  The core of computer science lPractical importance  A practitioner’s toolkit of known algorithms  Framework for designing and analyzing algorithms for new problems  Useful mindset

Course Structure lFundamentals of the Analysis of Algorithms (Ch. 2)  Asymptotic notations, analysis of recursive and non-recursive algorithms, empirical analysis lAlgorithmic Strategies (Ch. 3-9)  Brute force, Divide-and-Conquer, Decrease-and-Conquer, Transform-and-Conquer, Space and Time Tradeoffs, Greedy Techniques, Biologically-inspired techniques, Dynamic Programming lLimitations of Algorithms (Ch handouts)  Turing Machines, Computability, Problem Classification lCoping with Limitations on Algorithms (Ch. 11)  Backtracking and Branch and Bound lAnany Levitin, “Introduction to the Design and Analysis of Algorithms”, International Edition, Addison-Wesley, 2003

Practicals lWeekly mini prac exams lGiven a problem specification that is solvable using the algorithm design strategies presented in the course  Design Algorithm  Code it in C++  Submit it for automatic marking lAfter the 3-hour lab session will be asked to do a short analysis of the solution