170 likes | 217 Views
CSE310 Lecture01: Algorithms, Counting and Data Structures. Guoliang (Larry) Xue Computer Science and Engineering CIDSE Arizona State University http://optimization.asu.edu/~xue. Topics of this lecture. Discussion of Syllabus Algorithms definition time complexity space complexity
E N D
CSE310 Lecture01:Algorithms, Counting and Data Structures Guoliang (Larry) Xue Computer Science and Engineering CIDSE Arizona State University http://optimization.asu.edu/~xue
Topics of this lecture • Discussion of Syllabus • Algorithms • definition • time complexity • space complexity • Counting and Induction Proof • number of operations • space requirement • Data Structures • stacks and queues • advanced data structures
Algorithms • An algorithm is a well-defined step by step computational procedure that • is deterministic • takes some input • produces some output • stops on any input • Examples illustrating what is an algorithm and what is not an algorithm.
Example of what is an algorithm • Given a sequence of n numbers, compute the sum. Can we design an algorithm for this? • Given a sequence of n numbers, order them in non-decreasing order, Can we design an algorithms for this?Introduce insertion sort.
Example of what is not an algorithm • Print out all positive integers. • for (n=1; n>=1; n++) { • print n; • } • The above is NOT an algorithm. Why? • Print our the first 10,000,000 positive integers. • for (n=1; n<=10000000; n++){print n;} • The above IS an algorithm. Why?
A more complex example • The 3n+1 problem. • Given a positive integer n, if n=1 output n and stop; if n is even, output n=n/2 and continue; if n is odd, output n=3n+1 and continue; • Is the above an algorithm?
Another view of algorithms • An algorithm is a tool to solve a well-defined computational problem. • An instance of a problem. • Example of a problem: Given a sequence of numbers, order them into non-decreasing order. • An instance of the above problem is given by the input sequence 5, 4, 3, 2, 1. • Another instance of the above problem is given by the input sequence 7, 5, 1, 3.
Time complexity of an algorithm • Given an algorithm A for solving a particular problem, what is the time complexity of the algorithm? • Time complexity is measured using the number of operations required. • We also need to note the input size of the problem instance. • Examples: (1) summation, (2) matrix multiplication.
Space complexity of an algorithm • Given an algorithm A for solving a particular problem, what is the space complexity of the algorithm? • Space complexity is measured using the number of memory cells required. • We also need to note the input size of the problem instance. • Examples: (1) summation, (2) matrix multiplication.
Complexity of an algorithm • Best case analysis • Average case analysis • Worst case analysis • Amortized analysis
Counting techniques and induction proof • 1+2+3+4+...+n=? • 1+1/2+1/3+1/4+...+1/n=? • 1+1/2+1/4+1/8+...+(1/2)n=? • 12+22+32+...+n2=?
Growth of functions • lg (n) • sqrt(n) • n • n log(n) • n2 • n3 • 2n • n!
Data structures • What is an Abstract Data Type? • A collection of data elements • A collection of functions on the data elements • Why data structures? • to store data efficiently • to process data efficiently • Examples of data structures: • stacks and queues • binary search trees
Using the right data structures • How to represent a polynomial of order n? • use an array of size n • use a linked list • How to perform addition, subtraction and multiplication using the array representation? • How to perform addition, subtraction and multiplication using the linked list representation? • Which data structure is better?
Using the right algorithm • If the first 6 terms in a sequence are 1, 1, 2, 3, 5, 8, what is the 7th term? What is the nth term? • This sequence is called the Fibonacci sequence. • Let us write two algorithms for computing the nth Fibonacci number. • Which algorithm is better?
Summary • What is an algorithm? • What is an ADT? • Counting. • Proof using mathematical induction. • Problems and instances. • Comparison of algorithms.
Readings and Exercises • Most materials covered in this lecture can be found in Section 1.1, Section 1.2, Section 2.1 and 2.2. • You should work on Exercises 1.1-1 through 1.1-5 (p.11) and Problems 1-1 (pp.14-15). • Lecture 02 will be on asymptotic notations. To preview, read Sections 3.1 and 3.2.