Computer Algorithms Lecture 1 Introduction

Slides:



Advertisements
Similar presentations
MATH 224 – Discrete Mathematics
Advertisements

Chapter 2. Getting Started. Outline Familiarize you with the to think about the design and analysis of algorithms Familiarize you with the framework to.
11 Computer Algorithms Lecture 6 Recurrence Ch. 4 (till Master Theorem) Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
I Advanced Algorithms Analysis. What is Algorithm?  A computer algorithm is a detailed step-by-step method for solving a problem by using a computer.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 5.
Algorithms Recurrences. Definition – a recurrence is an equation or inequality that describes a function in terms of its value on smaller inputs Example.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Midterm Review Fri. Oct 26.
What is an Algorithm? (And how do we analyze one?)
Jan Welcome to the Course of Advanced Algorithm Design (ACS-7101/3)
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2009 Lecture 1 Introduction/Overview Text: Chapters 1, 2 Th. 9/3/2009.
CS421 - Course Information Website Syllabus Schedule The Book:
COMP 122 – Design and Analysis of Algorithms Spring 2004 MW 11:00-12:15, SN 014 Instructor:Jack Snoeyink TA: Nathan Fisher SN.
DAST, Spring © L. Joskowicz 1 Data Structures – LECTURE 1 Introduction Motivation: algorithms and abstract data types Easy problems, hard problems.
CSE 830: Design and Theory of Algorithms
Analysis of Algorithms CS 477/677 Midterm Exam Review Instructor: George Bebis.
1 Data Structures A program solves a problem. A program solves a problem. A solution consists of: A solution consists of:  a way to organize the data.
CS Main Questions Given that the computer is the Great Symbol Manipulator, there are three main questions in the field of computer science: What kinds.
What is an Algorithm? (And how do we analyze one?) COMP 122, Spring 04.
Data Structures, Spring 2004 © L. Joskowicz 1 DAST – Final Lecture Summary and overview What we have learned. Why it is important. What next.
Introduction CIS 606 Spring The sorting problem Input: A sequence of n numbers 〈 a 1, a 2, …, a n 〉. Output: A permutation (reordering) 〈 a’ 1,
DAST, Spring © L. Joskowicz 1 Data Structures – LECTURE 1 Introduction Motivation: algorithms and abstract data types Easy problems, hard problems.
Chapter 1 Introduction Definition of Algorithm An algorithm is a finite sequence of precise instructions for performing a computation or for solving.
Analysis of Algorithms CS 477/677
Computer Algorithms Lecture 11 Sorting in Linear Time Ch. 8
Instructor: Dr. Sahar Shabanah Fall Lectures ST, 9:30 pm-11:00 pm Text book: M. T. Goodrich and R. Tamassia, “Data Structures and Algorithms in.
HOW TO SOLVE IT? Algorithms. An Algorithm An algorithm is any well-defined (computational) procedure that takes some value, or set of values, as input.
CS223 Algorithms D-Term 2013 Instructor: Mohamed Eltabakh WPI, CS Introduction Slide 1.
Week 2 CS 361: Advanced Data Structures and Algorithms
Introduction to Algorithms Jiafen Liu Sept
1 Computer Algorithms Lecture 3 Asymptotic Notation Some of these slides are courtesy of D. Plaisted, UNC and M. Nicolescu, UNR.
1 Chapter 1 Analysis Basics. 2 Chapter Outline What is analysis? What to count and consider Mathematical background Rates of growth Tournament method.
Algorithm Analysis An algorithm is a clearly specified set of simple instructions to be followed to solve a problem. Three questions for algorithm analysis.
Design and Analysis of Algorithms Introduction. Class Policy Grading –Homeworks and quizzes (20%) Programming assignments –First and Second exams (20%
Jessie Zhao Course page: 1.
Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The.
ECOE 456/556: Algorithms and Computational Complexity Lecture 1 Serdar Taşıran.
Analysis of Algorithms These slides are a modified version of the slides used by Prof. Eltabakh in his offering of CS2223 in D term 2013.
The Selection Problem. 2 Median and Order Statistics In this section, we will study algorithms for finding the i th smallest element in a set of n elements.
CMPT 438 Algorithms. Why Study Algorithms? Necessary in any computer programming problem ▫Improve algorithm efficiency: run faster, process more data,
A Lecture /24/2015 COSC3101A: Design and Analysis of Algorithms Tianying Ji Lecture 1.
Major objective of this course is: Design and analysis of modern algorithms Different variants Accuracy Efficiency Comparing efficiencies Motivation thinking.
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture three Dr. Hamdy M. Mousa.
1Computer Sciences Department. Book: Introduction to Algorithms, by: Thomas H. Cormen Charles E. Leiserson Ronald L. Rivest Clifford Stein Electronic:
IT 301: Algorithm Analysis Lecture-00 Jesmin Akhter Lecturer,IIT Jahangirnagar University, Savar, DhakaBangladesh 11/29/2015.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
COSC 3101A - Design and Analysis of Algorithms 2 Asymptotic Notations Continued Proof of Correctness: Loop Invariant Designing Algorithms: Divide and Conquer.
Algorithm Analysis Part of slides are borrowed from UST.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
Midterm Review 1. Midterm Exam Thursday, October 15 in classroom 75 minutes Exam structure: –TRUE/FALSE questions –short questions on the topics discussed.
Introduction to Algorithms (2 nd edition) by Cormen, Leiserson, Rivest & Stein Chapter 2: Getting Started.
1 Ch. 2: Getting Started. 2 About this lecture Study a few simple algorithms for sorting – Insertion Sort – Selection Sort (Exercise) – Merge Sort Show.
CES 592 Theory of Software Systems B. Ravikumar (Ravi) Office: 124 Darwin Hall.
Lecture # 1 Introduction Analysis of Algorithm by Qamar Abbas Analysis of Algorithms.
Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 1 Prof. Charles E. Leiserson.
Design and Analysis of Algorithms Introduction Instructors:1. B V Kiran Mayee, 2. A Madhavi
BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture1.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 1.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 17.
CMPT 438 Algorithms.
Design and Analysis of Algorithms
Welcome to the Course of Advanced Algorithm Design
CS 583 Fall 2006 Analysis of Algorithms
CS200: Algorithms Analysis
CMPT 438 Algorithms Instructor: Tina Tian.
Algorithm Analysis, Asymptotic notations CISC4080 CIS, Fordham Univ.
Department of Computer Science & Engineering
Analysis of Algorithms CS 477/677
COMP 122 – Design and Analysis of Algorithms
Presentation transcript:

Computer Algorithms Lecture 1 Introduction Some of these slides are courtesy of D. Plaisted, UNC and M. Nicolescu, UNR

Class Information Instructor: Elena Filatova e-mail: filatova@fordham.edu Office: 334 Office hours: Tuesday, Friday 12:30 – 2:00pm Thursday 4:00 – 5:00pm Additional office hours: by appointment E-mail: 4080 in the beginning of the subject phrase Web page: blackboard It is your responsibility to check the class blackboard regularly Any questions related course material should be placed on the blackboard discussion board Main text book: Introduction to Algorithms by Cormen et al (3nd ed.)

Grading Homework assignments: 30% Midterm: 25% Final: 35% Electronic submission through blackboard Done individually Midterm: 25% Final: 35% In-class quizzes: 5% Without prior notification Based on the material from the previous class Absolutely no make-up quizzes Two worst scores will be dropped Class participation: 5% Attendance is mandatory No electronic devices is allowed in the class room (unless with special permission)

Pre-requisites Programming (CS I) Data Structures (2200) Discrete math: not necessary but very helpful

Approach Analytical Build a mathematical model of a computer Study properties of algorithms on this model Reason about algorithms Prove facts about time taken for algorithms

Course Outline Intro to algorithm design, analysis, and applications Algorithm Analysis Proof of algorithm correctness, Asymptotic Notation, Recurrence Relations, Probability & Combinatorics, Proof Techniques, Inherent Complexity. Data Structures Lists, Heaps, Graphs, Trees, Balanced Trees Sorting & Ordering Mergesort, Heapsort, Quicksort, Linear-time Sorts (bucket, counting, radix sort), Selection, Other sorting methods. Algorithmic Design Paradigms Divide and Conquer, Dynamic Programming, Greedy Algorithms, Graph Algorithms, Randomized Algorithms

Goals Be very familiar with a collection of core algorithms. CS classics A lot of examples on-line for most languages/data structures Be fluent in algorithm design paradigms: divide & conquer, greedy algorithms, randomization, dynamic programming, approximation methods. Be able to analyze the correctness and runtime performance of a given algorithm. Be intimately familiar with basic data structures. Be able to apply techniques in practical problems.

Algorithms Algorithm Input Output Informally, Example: sorting A tool for solving a well-specified computational problem. One formal definition ~ Turing Machine (4090 Theory of Computation) Example: sorting input: A sequence of numbers. output: An ordered permutation of the input. issues: correctness, efficiency, storage, etc. Algorithm Input Output

Why Study Algorithms? Necessary in any computer programming problem Improve algorithm efficiency: run faster, process more data, do something that would otherwise be impossible Solve problems of significantly large size Technology only improves things by a constant factor Compare algorithms Algorithms as a field of study Learn about a standard set of algorithms New discoveries arise Numerous application areas Learn techniques of algorithm design and analysis

Roadmap Different design paradigms Different problems Sorting Divide-and-conquer Incremental Dynamic programming Greedy algorithms Randomized/probabilistic Different problems Sorting Searching String processing Graph problems Geometric problems Numerical problems 10

Analyzing Algorithms Predict the amount of resources required: memory how much space is needed? computational time: how fast the algorithm runs? FACT: running time grows with the size of the input Input size (number of elements in the input) Size of an array, polynomial degree, # of elements in a matrix, # of bits in the binary representation of the input, vertices and edges in a graph Def: Running time = the number of primitive operations (steps) executed before termination Arithmetic operations (+, -, *), data movement, control, decision making (if, while), comparison 11

Algorithm Efficiency vs. Speed E.g.: sorting n numbers Friend’s computer = 109 instructions/second Friend’s algorithm = 2n2 instructions Your computer = 107 instructions/second Your algorithm = 50nlgn instructions Your friend = You = Sort 106 numbers (n=106)! 20 times better!! 12

Algorithm Analysis: Example Alg.: MIN (a[1], …, a[n]) m ← a[1]; for i ← 2 to n if a[i] < m then m ← a[i]; Running time: the number of primitive operations (steps) executed before termination T(n) =1 [first step] + (n) [for loop] + (n-1) [if condition] + (n-1) [the assignment in then] = 3n - 1 Order (rate) of growth: The leading term of the formula Expresses the asymptotic behavior of the algorithm

Typical Running Time Functions 1 (constant running time): Instructions are executed once or a few times logN (logarithmic) A big problem is solved by cutting the original problem in smaller sizes, by a constant fraction at each step N (linear) A small amount of processing is done on each input element N logN A problem is solved by dividing it into smaller problems, solving them independently and combining the solution

Typical Running Time Functions N2 (quadratic) Typical for algorithms that process all pairs of data items (double nested loops) N3 (cubic) Processing of triples of data (triple nested loops) NK (polynomial) 2N (exponential) Few exponential algorithms are appropriate for practical use

Why Faster Algorithms? Time units Problem size (n) Time units 16

Asymptotic Notations A way to describe behavior of functions in the limit Abstracts away low-order terms and constant factors How we indicate running times of algorithms Describe the running time of an algorithm as n grows to  O notation:  notation:  notation: asymptotic “less than and equal”: f(n) “≤” g(n) asymptotic “greater than and equal”:f(n) “≥” g(n) asymptotic “equality”: f(n) “=” g(n) 17

Mathematical Induction Used to prove a sequence of statements (S(1), S(2), … S(n)) indexed by positive integers. S(n): Proof: Basis step: prove that the statement is true for n = 1 Inductive step: assume that S(n) is true and prove that S(n+1) is true for all n ≥ 1 The key to proving mathematical induction is to find case n “within” case n+1 Correctness of an algorithm containing a loop 18

Recurrences E.g.: T(n) = T(n-1) + n Def.: Recurrence = an equation or inequality that describes a function in terms of its value on smaller inputs, and one or more base cases E.g.: T(n) = T(n-1) + n Useful for analyzing recurrent algorithms Methods for solving recurrences Iteration method Substitution method Recursion tree method Master method 19

Sorting Iterative methods: Insertion sort Bubble sort Selection sort 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, A Divide and conquer Merge sort Quicksort Non-comparison methods Counting sort Radix sort Bucket sort 20

Types of Analysis Worst case Best case Average case Provides an upper bound on running time An absolute guarantee that the algorithm would not run longer, no matter what the inputs are Best case Input is the one for which the algorithm runs the fastest Average case Provides a prediction about the running time Assumes that the input is random (e.g. cards reversely ordered) (e.g., cards already ordered) (general case) 21

Specialized Data Structures Problem: Schedule jobs in a computer system Process the job with the highest priority first Solution: HEAPS all levels are full, except possibly the last one, which is filled from left to right for any node x Parent(x) ≥ x Operations: Build Insert Extract max Increase key

Graphs Applications that involve not only a set of items, but also the connections between them Maps Schedules Computer networks Hypertext Circuits 23

Searching in Graphs Graph searching = systematically follow the edges of the graph so as to visit the vertices of the graph Two basic graph methods: Breadth-first search Depth-first search The difference between them is in the order in which they explore the unvisited edges of the graph Graph algorithms are typically elaborations of the basic graph-searching algorithms u v w x y z 24

Minimum Spanning Trees A connected, undirected graph: Vertices = houses, Edges = roads A weight w(u, v) on each edge (u, v)  E a b c d e h g f i 4 8 7 11 1 2 14 9 10 6 Find T  E such that: T connects all vertices w(T) = Σ(u,v)T w(u, v) is minimized Algorithms: Kruskal and Prim 25

Shortest Path Problems Input: Directed graph G = (V, E) Weight function w : E → R Weight of path p = v0, v1, . . . , vk Shortest-path weight from u to v: δ(u, v) = min otherwise 6 3 4 2 1 2 7 5 3 6 p w(p) : u v if there exists a path from u to v 26

Dynamic Programming An algorithm design technique (like divide and conquer) Richard Bellman, optimizing decision processes Applicable to problems with overlapping subproblems E.g.: Fibonacci numbers: Recurrence: F(n) = F(n-1) + F(n-2) Boundary conditions: F(1) = 0, F(2) = 1 Compute: F(5) = 3, F(3) = 1, F(4) = 2 Solution: store the solutions to subproblems in a table Applications: Assembly line scheduling, matrix chain multiplication, longest common sequence of two strings, 0-1 Knapsack problem

Greedy Algorithms Similar to dynamic programming, but simpler approach Also used for optimization problems Idea: When we have a choice to make, make the one that looks best right now Make a locally optimal choice in hope of getting a globally optimal solution Greedy algorithms don’t always yield an optimal solution Applications: Activity selection, fractional knapsack, Huffman codes 28

Greedy Algorithms Problem Schedule the largest possible set of non-overlapping activities for B21 Start End Activity 1 8:00am 9:15am Database systems class 2 8:30am 10:30am Movie presentation (refreshments served) 3 9:20am 11:00am Data structures class 4 10:00am noon Programming club mtg. (Pizza provided) 5 11:30am 1:00pm Computer graphics class 6 1:05pm 2:15pm Analysis of algorithms class 7 2:30pm 3:00pm Computer security class 8 4:00pm Computer games contest (refreshments served) 9 5:30pm Operating systems class      

How to Succeed in this Course Start early on all assignments. Don‘t procrastinate Complete all reading before class Participate in class Review after each class Be formal and precise on all problem sets and in-class exams

Basics Introduction to algorithms, complexity, and proof of correctness. (Chapters 1 & 2) Asymptotic Notation. (Chapter 3.1) Goals Know how to write formal problem specifications. Know about computational models. Know how to measure the efficiency of an algorithm. Know the difference between upper and lower bounds and what they convey. Be able to prove algorithms correct and establish computational complexity.

Divide-and-Conquer Goals Designing Algorithms. (Chapter 2.3) Recurrences. (Chapter 4) Quicksort. (Chapter 7) Divide-and-conquer and mathematical induction Goals Know when the divide-and-conquer paradigm is an appropriate one. Know the general structure of such algorithms. Express their complexity using recurrence relations. Determine the complexity using techniques for solving recurrences. Memorize the common-case solutions for recurrence relations.

Randomized Algorithms Probability & Combinatorics. (Chapter 5) Quicksort. (Chapter 7) Hash Tables. (Chapter 11) Goals Be thorough with basic probability theory and counting theory. Be able to apply the theory of probability to the following. Design and analysis of randomized algorithms and data structures. Average-case analysis of deterministic algorithms. Understand the difference between average-case and worst-case runtime, esp. in sorting and hashing.

Sorting & Selection Heapsort (Chapter 6) Quicksort (Chapter 7) Bucket Sort, Radix Sort, etc. (Chapter 8) Selection (Chapter 9) Goals Know the performance characteristics of each sorting algorithm, when they can be used, and practical coding issues. Know the applications of binary heaps. Know why sorting is important. Know why linear-time median finding is useful.

Search Trees Binary Search Trees – Not balanced (Chapter 12) Red-Black Trees – Balanced (Chapter 13) Goals Know the characteristics of the trees. Know the capabilities and limitations of simple binary search trees. Know why balancing heights is important. Know the fundamental ideas behind maintaining balance during insertions and deletions. Be able to apply these ideas to other balanced tree data structures.

Dynamic Programming Dynamic Programming (Chapter 15): an algorithm design technique (like divide-and-conquer) Goals Know when to apply dynamic programming and how it differs from divide and conquer. Be able to systematically move from one to the other.

Graph Algorithms Basic Graph Algorithms (Chapter 22) Goals Know how to represent graphs (adjacency matrix and edge-list representations). Know the basic techniques for graph searching. Be able to devise other algorithms based on graph-searching algorithms. Be able to “cut-and-paste” proof techniques as seen in the basic algorithms.

Greedy Algorithms Greedy Algorithms (Chapter 16) Minimum Spanning Trees (Chapter 23) Shortest Paths (Chapter 24) Goals Know when to apply greedy algorithms and their characteristics. Be able to prove the correctness of a greedy algorithm in solving an optimization problem. Understand where minimum spanning trees and shortest path computations arise in practice.

Weekly Reading Assignment Chapters 1, 2, and 3 Appendix A (Textbook: CLRS)

Insertion Sort Example Good for sorting a small number of elements Works like sorting a hand of playing cards Start with an empty hand and the cards face down on the table Then remove one card at a time from the table and insert it into the correct position in the left hand To find the correct position for a card, compare it with each of the cards already in the hand from right to left At all times, the cards that are already in the left hand a sorted and those cards were originally on the top of the pile Example

Pseudo-Code Conventions Indentation as block structure Loop and conditional constructs similar to those in Pascal or Java, such as while, for, repeat, if-then-else Symbol ► starts a comment line (no execution time) Using ← instead of = and allowing i ← j ← e Variables local to the given procedure Page 19 of the text-book

Insertion Sort: Pseudo-Code Definiteness: each instruction is clear and unambiguous Visualization: University of San Francisco

Algorithm Analysis Assumptions: Random-Access Machine (RAM) Operations are executed one after another No concurrent operations Only primitive instructions Arithmetic (+, -, /, *, floor, ceiling) Data movement (load, store, copy) Control operators (conditional/unconditional branch, subroutine call, return) Primitive instructions take constant time Interested in time complexity: amount of time to complete an algorithm

What to measure? How to measure? Input size: depends on the problem Number of items for sorting (3 or 1000) Even time for sorting sequences of the same size can vary Total number of bits for multiplying two integers Sometimes, more than one input Running time: number of primitive operations executed Machine-independent Each line of pseudo-code – constant time Constant time for each line vary

Analysis of Insertion Sort

Running Time Insertion sort running time Best-case: the input array is in the correct order Worst-case: the input array is in the reverse order Average-case Insertion sort running time Best-case: linear function Worst-case: quadratic function Average-case: best-case or worst-case?? Order (rate) of growth: The leading term of the formula Expresses the asymptotic behavior of the algorithm Given two algorithms (linear and quadratic) which one will you choose?