Instructor : Zuhair Qadir Lecture # 1 Design & Analysis of Algorithms CS3444 – Design & Analysis of Algorithms 16-September-2014 1 Instructor Introduction • Education MS Computer Sciences from LUMS 2011- 2013 BS Computer Engineering from UET, Lahore 2006-2010 • Industry Experience • Systems Limited 2010-2012 Design & Analysis of Algorithms • Zuhair Qadir • Core Interests • Computer Vision, Network Security and Algorithms 2 • • • • • • • • Course Overview: Goals & Objectives Class Protocols Outline of topics to be discussed in the course Understanding a problem Purpose of a Algorithms—setting the stage for Algo concepts and principles What is an Algorithm? Reading Suggestions Design & Analysis of Algorithms Outline 3 Course Title Design & Analysis of Algorithms Course Code CS3444 Credit Hours 3 credit hours Instructor Zuhair Qadir Contact Office Office Hours Sections Course Website Muhammad.Qadir@cs.uol.edu.pk Room-102 Saturday 12:00pm – 2:00pm BSCS – Section B Design & Analysis of Algorithms Course Information www.zuhairqadir.info/UOL/ALGO/ 4 Course Information • Lectures • There will be 2 lectures a week of one and a half hour each. • Refer to the timetable for class timings/venue. Design & Analysis of Algorithms • No Lab 5 Course Objective • We will study fundamental algorithms for solving a variety of problems, including sorting, searching and graph algorithms. More importantly, we will focus on general design and analysis techniques that underlie these algorithms. For example, we will examine divideand-conquer, dynamic programming, greediness, and probabilistic approaches. With an understanding of these techniques, you will be prepared to design some of your own algorithms. • We will learn techniques for analyzing time and space complexity of algorithms and will use these to evaluate tradeoffs between different algorithms. We will also see that problems can be organized into a hierarchy that measures their inherent difficulty by the efficiency of the best possible algorithm for solving them. Design & Analysis of Algorithms • Introduce the classic algorithms in various domains, and techniques for designing efficient algorithms. 6 • • • • • • • • • • • • • • Algorithm design Algorithm analysis Sorting Algorithms Divide-and-conquer approach Greedy approach Graph algorithms Graph searching Minimum spanning tree Shortest paths Backtracking and its applications in games String matching Dynamic programming Longest common subsequence Theory of NP-completeness Design & Analysis of Algorithms Course Contents 7 Text Books • Course Book • Reference Books • The Algorithm Design Manual - Steven S. • Algorithms – Dasgupta, Papadimitriou, Vazirani • Some relevant important material from internet as well Design & Analysis of Algorithms • Introduction to Algorithms, 3rd edition by Cormen, Leisersen, Rivest and Stein. 8 Assessment Weightage Mid Term Exam 30% Final Term Exam 40% Assignments 10% Quizzes 20% Design & Analysis of Algorithms Grading Policy 9 Class Honor Code • Copying code from someone else’s screen or notes • Providing your own written code to another student • Letting another student type solutions for you on the machine you are working on, for assignment submission • Looking at someone else’s answer sheet during a quiz or an exam • Downloading code from the internet or any other source and submitting portions of it as your own. • Writing code, whether on a computer or a sheet of paper, for someone else’s assignment and providing it to him or her. • Emailing your own code to someone else. • Debugging someone else’s code. Some examples of what does not constitute cheating and plagiarism • Answering questions on the web forum • Discussing ideas with another student • Helping other students understand concepts If in doubt, discuss with your instructor. • Try to come in class on time Design & Analysis of Algorithms We expect all UOL students to act honestly at all times. Cheating and plagiarism will not be tolerated. Resorting to unfair means may result in an F in the course and Disciplinary Committee Case from UOL, even for first-time offenders. Some examples of what constitutes cheating and plagiarism are listed below, though this is not an exhaustive list: 10 Lecture # 1 Design & Analysis of Algorithms Design & Analysis of Algorithms 11 Introduction • What are Algorithms ? • An algorithm is the thing which stays the same whether the program is in C++ running on a PC in New York or is in Android running on a Smartphone in Lahore! • An algorithm has to solve a general, specified problem. An algorithmic problem is specified by describing the set of instances/input it must work on and what desired properties the output must have. Design & Analysis of Algorithms • Algorithms are the ideas behind computer programs. 12 Introduction • Naïve Definition: • An algorithm is a set of instructions to be followed to solve a problem. an algorithm is a complete set of rules that transform the input into the output in a finite number of steps. • Characterisics of an Algorithm • There can be more than one solution (more than one algorithm) to solve a given problem. • An algorithm can be implemented using different programming languages on different platforms. • An algorithm should correctly solve the problem. • e.g., for sorting, this means even if (1) the input is already sorted, or (2) it contains repeated elements. • Once we have a correct algorithm for a problem, we have to determine the efficiency of that algorithm. Design & Analysis of Algorithms • Concrete Definition: 13 Introduction • Key features of Algorithms: • Algorithms are judged not only by how well they solve a problem, but also by how effectively they use resources like time and space. Design & Analysis of Algorithms • Algorithms are recipes for solving computational problems. 14 Aspects of studying algorithms: 1. Designing algorithms: • putting the pieces of the puzzles together, • choosing data structures, • selecting the basic approaches to the solution of the problem, • The most popular design strategies are divide&conquer, greedy, dynamic prog., backtracking, and branch&bound. 2. Expressing and implementing the algorithm Concerns are: • clearness • conciseness • Effectiveness • etc. Design & Analysis of Algorithms Design & Analysis of Algorithms 15 Design & Analysis of Algorithms 3. Analyzing the algorithm 4. Compare UB and LB to see if your solution is good enough Analyzing the algorithm gives us the upper bound to solve the problem Analyzing the problem gives us the lower bound to solve the problem 5. Validate the algorithm We show that the algorithm computes the correct answer for all possible legal (or given) inputs. In contrast with the verification it is concerned with the external things. “are you building the right thing ?” Design & Analysis of Algorithms Algorithm analysis is assessing the time and space resources required by an algorithm as a function of the size of the problem, without actually implementing the algorithm. 16 Design & Analysis of Algorithms 6. Verifying the algorithm (or program) 7. Testing algorithms There are two phases; Debugging: The process of executing programs on sample data sets to determine if faulty results occur, and if so, to correct them. “Debugging can only point to the presence of errors, but not to their absence” Profiling: the process of executing a correct program on various data sets and measuring the time (and space) it takes to compute the results. Design & Analysis of Algorithms An algorithm is said to be correct (verified) if, for every input instance, it halts with the correct output. In contrast with validation it is concerned with the internal things. “are you building it right?” 17 Algorithmic Performance • Instructions take time. • How fast does the algorithm perform? • What affects its runtime? • Space • Data structures take space • What kind of data structures can be used? • How does choice of data structure affect the runtime? • We will focus on time: • How to estimate the time required for an algorithm • How to reduce the time required Design & Analysis of Algorithms There are two aspects of algorithmic performance: • Time 18 Algorithmic Analysis • How do we compare the time efficiency of two algorithms that solve the same problem? Naïve Approach: implement these algorithms in a programming language (i.e., C++), and run them to compare their time requirements. Design & Analysis of Algorithms • Analysis of Algorithms is the area of computer science that provides tools to analyze the efficiency of different methods of solutions. 19 Sorting • Input : 6, 3, 1, 8, 2, 4 Design & Analysis of Algorithms • How to Sort ? 20 Bubble Sort • Input : 6, 3, 1, 8, 2, 4 Design & Analysis of Algorithms • Sort ? 21 Readings for Today • Google: • Google Search Engine Algorithm • Why do we need algorithms ? Why analysis is important ? Design & Analysis of Algorithms • History of Algorithms , inventor of algorithms 22