CSE 310 Lecture 01 Algorithms Counting and Data

  • Slides: 17
Download presentation
CSE 310 Lecture 01: Algorithms, Counting and Data Structures Guoliang (Larry) Xue Computer Science

CSE 310 Lecture 01: Algorithms, Counting and Data Structures Guoliang (Larry) Xue Computer Science and Engineering CIDSE Arizona State University http: //optimization. asu. edu/~xue Guoliang. Xue@asu.

Topics of this lecture n n Discussion of Syllabus Algorithms n n Counting and

Topics of this lecture n n Discussion of Syllabus Algorithms n n Counting and Induction Proof n n n definition time complexity space complexity number of operations space requirement Data Structures n n stacks and queues advanced data structures 1 Guoliang. Xue@asu.

Algorithms n An algorithm is a well-defined step by step computational procedure that is

Algorithms n An algorithm is a well-defined step by step computational procedure that is deterministic n takes some input n produces some output n stops on any input n n Examples illustrating what is an algorithm and what is not an algorithm. 2 Guoliang. Xue@asu.

Example of what is an algorithm Given a sequence of n numbers, compute the

Example of what is an algorithm Given a sequence of n numbers, compute the sum. Can we design an algorithm for this? n Given a sequence of n numbers, order them in non-decreasing order, Can we design an algorithms for this? Introduce insertion sort. n 3 Guoliang. Xue@asu.

Example of what is not an algorithm n Print out all positive integers. for

Example of what is not an algorithm n Print out all positive integers. for (n=1; n>=1; n++) { n print n; n} n n n The above is NOT an algorithm. Why? Print our the first 10, 000 positive integers. n n for (n=1; n<=10000000; n++){print n; } The above IS an algorithm. Why? 4 Guoliang. Xue@asu.

A more complex example The 3 n+1 problem. n Given a positive integer n,

A more complex example The 3 n+1 problem. n 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=3 n+1 and continue; n Is the above an algorithm? n 5 Guoliang. Xue@asu.

Another view of algorithms An algorithm is a tool to solve a welldefined computational

Another view of algorithms An algorithm is a tool to solve a welldefined computational problem. n An instance of a problem. n Example of a problem: Given a sequence of numbers, order them into nondecreasing order. n An instance of the above problem is given by the input sequence 5, 4, 3, 2, 1. n Another instance of the above problem is given by the input sequence 7, 5, 1, 3. n 6 Guoliang. Xue@asu.

Time complexity of an algorithm Given an algorithm A for solving a particular problem,

Time complexity of an algorithm Given an algorithm A for solving a particular problem, what is the time complexity of the algorithm? n Time complexity is measured using the number of operations required. n We also need to note the input size of the problem instance. n Examples: (1) summation, (2) matrix multiplication. n 7 Guoliang. Xue@asu.

Space complexity of an algorithm Given an algorithm A for solving a particular problem,

Space complexity of an algorithm Given an algorithm A for solving a particular problem, what is the space complexity of the algorithm? n Space complexity is measured using the number of memory cells required. n We also need to note the input size of the problem instance. n Examples: (1) summation, (2) matrix multiplication. n 8 Guoliang. Xue@asu.

Complexity of an algorithm Best case analysis n Average case analysis n Worst case

Complexity of an algorithm Best case analysis n Average case analysis n Worst case analysis n Amortized analysis n 9 Guoliang. Xue@asu.

Counting techniques and induction proof 1+2+3+4+. . . +n=? n 1+1/2+1/3+1/4+. . . +1/n=?

Counting techniques and induction proof 1+2+3+4+. . . +n=? n 1+1/2+1/3+1/4+. . . +1/n=? n 1+1/2+1/4+1/8+. . . +(1/2)n=? n 12+22+32+. . . +n 2=? n 10 Guoliang. Xue@asu.

Growth of functions lg (n) n sqrt(n) nn n n log(n) n n 2

Growth of functions lg (n) n sqrt(n) nn n n log(n) n n 2 n n 3 n 2 n n n! n 11 Guoliang. Xue@asu.

Data structures n What is an Abstract Data Type? A collection of data elements

Data structures n What is an Abstract Data Type? A collection of data elements n A collection of functions on the data elements n n Why data structures? to store data efficiently n to process data efficiently n n Examples of data structures: stacks and queues n binary search trees n 12 Guoliang. Xue@asu.

Using the right data structures n How to represent a polynomial of order n?

Using the right data structures n How to represent a polynomial of order n? use an array of size n n use a linked list n How to perform addition, subtraction and multiplication using the array representation? n How to perform addition, subtraction and multiplication using the linked list representation? n Which data structure is better? n 13 Guoliang. Xue@asu.

Using the right algorithm If the first 6 terms in a sequence are 1,

Using the right algorithm If the first 6 terms in a sequence are 1, 1, 2, 3, 5, 8, what is the 7 th term? What is the nth term? n This sequence is called the Fibonacci sequence. n Let us write two algorithms for computing the nth Fibonacci number. n Which algorithm is better? n 14 Guoliang. Xue@asu.

Summary What is an algorithm? n What is an ADT? n Counting. n Proof

Summary What is an algorithm? n What is an ADT? n Counting. n Proof using mathematical induction. n Problems and instances. n Comparison of algorithms. n 15 Guoliang. Xue@asu.

Readings and Exercises Most materials covered in this lecture can be found in Section

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. n You should work on Exercises 1. 1 -1 through 1. 1 -5 (p. 11) and Problems 1 -1 (pp. 14 -15). n Lecture 02 will be on asymptotic notations. To preview, read Sections 3. 1 and 3. 2. n 16 Guoliang. Xue@asu.