CS 206 Introduction to Computer Science II 09 / 12 / 2008 Instructor: Michael Eckmann.

Slides:



Advertisements
Similar presentations
Algorithm Analysis Input size Time I1 T1 I2 T2 …
Advertisements

CS 206 Introduction to Computer Science II 04 / 01 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 02 / 27 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 23 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 22 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 02 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 30 / 2007 Last lecture :( Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 12 / 04 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 10 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 02 / 20 / 2009 Instructor: Michael Eckmann.
Introduction to Analysis of Algorithms
CS 106 Introduction to Computer Science I 05 / 03 / 2010 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 06 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 02 / 06 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 17 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 04 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 10 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 01 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 17 / 2008 Instructor: Michael Eckmann.
Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked.
CS 206 Introduction to Computer Science II 09 / 22 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 14 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 17 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 13 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 15 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 12 / 01 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 16 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 15 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 12 / 10 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 02 / 09 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 02 / 11 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 01 / 28 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 16 / 2009 Instructor: Michael Eckmann.
Chapter 3: Arrays, Linked Lists, and Recursion
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 04 / 06 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 19 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 02 / 25 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 28 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 30 / 2009 Instructor: Michael Eckmann.
Lecture 2 We have given O(n 3 ), O(n 2 ), O(nlogn) algorithms for the max sub-range problem. This time, a linear time algorithm! The idea is as follows:
Data Structures Week 5 Further Data Structures The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
Lecture 2 Computational Complexity
ALG0183 Algorithms & Data Structures Lecture 6 The maximum contiguous subsequence sum problem. 8/25/20091 ALG0183 Algorithms & Data Structures by Dr Andy.
CS 206 Introduction to Computer Science II 02 / 23 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 25 / 2008 Instructor: Michael Eckmann.
Algorithm Analysis Data Structures and Algorithms (60-254)
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
CS 206 Introduction to Computer Science II 11 / 16 / 2009 Instructor: Michael Eckmann.
3.3 Complexity of Algorithms
Algorithm Analysis Chapter 5. Algorithm An algorithm is a clearly specified set of instructions which, when followed, solves a problem. –recipes –directions.
CS 206 Introduction to Computer Science II 09 / 18 / 2009 Instructor: Michael Eckmann.
CS 325 Introduction to Computer Graphics 04 / 12 / 2010 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 02 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 01 / 30 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 02 / 02 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 10 / 2008 Instructor: Michael Eckmann.
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
Lecture 6 of Computer Science II
Data Structures Review Session
CS212D: Data Structures Week 5-6 Linked List.
Introduction to Data Structures
Presentation transcript:

CS 206 Introduction to Computer Science II 09 / 12 / 2008 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS Fall 2008 Today’s Topics Questions? Comments? More Algorithm Analysis (chapter 5)‏ –Example problem with multiple algorithms that solve it with different running times Linked lists

Michael Eckmann - Skidmore College - CS Fall 2008 Algorithm Analysis Some common functions (in increasing order) used in analysis are –constant functions (e.g. f(n) = 10 )‏ –logarithmic functions (e.g. f(n) = log(20n) )‏ –log squared (e.g. f(n) = log 2 (7n) )‏ –linear functions (e.g. f(n) = 3n – 9 )‏ –N log N (e.g. f(n) = 2n log n )‏ –quadratic functions (e.g. f(n) = 5n 2 + 3n )‏ –cubic functions (e.g. f(n) = 3n n 2 + (4/7)n )‏ –exponential functions (e.g. f(n) = 5 n )‏ –factorial functions (e.g. f(n) = n! )‏

Michael Eckmann - Skidmore College - CS Fall 2008 Algorithm Analysis When examining an algorithm, we usually count how many times a certain operation (or group of operations) is performed. See handout for reasonable choices of what operations we would count in different problems. This will lead us to determining the time complexity of the algorithm. We can consider best-case, worst-case and average-case scenarios.

Michael Eckmann - Skidmore College - CS Fall 2008 Algorithm Analysis Let's consider 1 problem and 3 ways to solve it (using 3 different algorithms) and we'll analyze the running times of each. The Maximum contiguous subsequence problem: –Given an integer sequence A 1, A 2,..., A N, find (and identify the sequence corresponding to) the maximum value of  j k=i A k. The maximum contiguous subsequence sum is zero if all are negative. Therefore, an empty sequence may be maximum. Example input: { -2, 11, -4, 13, -5, 2 } the answer is 20 and the sequence is { 11, -4, 13 } Another: { 1, -3, 4, -2, -1, 6 } the answer is 7, the sequence is { 4, -2, -1, 6 }

Michael Eckmann - Skidmore College - CS Fall 2008 Algorithm Analysis The simplest is an exhaustive search (brute force algorithm.)‏ –that is, simply consider every possible subsequence and compute its sum, keep doing this and save the greatest –so, we set the maxSum = 0 (b/c it is at least this big) and we start at the first element and consider ever subsequence that begins with the first element and sum them up... if any has a sum larger than maxSum, save this... –then start at second element and do the same... and so on until start at last element Advantages to this: not complex, easy to understand Disadvantages to this: slow Let's examine the algorithm. –decide what is a good thing to count –count that operation (in terms of the input size)‏

Michael Eckmann - Skidmore College - CS Fall 2008 Algorithm Analysis Let's consider this inefficient maximum continuous subsequence algorithm from page 171 in our text.

Michael Eckmann - Skidmore College - CS Fall 2008 Algorithm Analysis The exhaustive search has many unnecessary computations. Notice that  j k=i A k = A j +  j-1 k=i A k That is, if we know the sum of the first j-1 elements, then the sum of the first j elements is found just by adding in the jth element. Knowing that, the problem can be solved more efficiently by the algorithm that we are about to analyze. –We won't need to keep adding to up a sequence from scratch

Michael Eckmann - Skidmore College - CS Fall 2008 Algorithm Analysis The second algorithm that we'll analyze uses the improvement just mentioned and the running time improves (goes down.)‏

Michael Eckmann - Skidmore College - CS Fall 2008 Algorithm Analysis A further improvement can come if we realize that if a subsequence has a negative sum, it will not be the first part of the maximum subsequence. –Why? Also, all contiguous subsequences bordering a maximum contiguous subsequence must have negative or 0 sums. –why?

Michael Eckmann - Skidmore College - CS Fall 2008 Algorithm Analysis A further improvement can come if we realize that if a subsequence has a negative sum, it will not be the first part of the maximum subsequence. –Why? –A negative value will only bring the total down (see Theorem 5.2 in textbook)‏ Also, all contiguous subsequences bordering a maximum contiguous subsequence must have negative or 0 sums. –why? –If they were >0, they would be attached to the maximum sequence (thereby giving it a larger sum). Those two aren't too hard to see, but by themselves won't allow us to reduce the running time to below quadratic. Let's go a bit further.

Michael Eckmann - Skidmore College - CS Fall 2008 Algorithm Analysis There are some observations in our textbook which are then proved on page 175 in Theorem 5.3 of the textbook. This will allow us to process the sequence by just going through the sequence once from start to finish (and hence get linear time performance.)‏ Specifically, while computing the sum of a subsequence, if at any time the sum becomes negative, we start considering sequences only starting at the next element. This is less intuitive than the other observations we made, but in my opinion not worth taking class time to cover in detail, so I leave it to you, if you wish to go over the observations in our textbook. But let's look at the linear algorithm and see if it is obvious that it is linear.

Michael Eckmann - Skidmore College - CS Fall 2008 Algorithm Analysis What's the point of that exercise: –1) get a feel for how to count how much work is being done in an algorithm –2) it is sometimes possible to get the running time of an algorithm down by exploiting facts about the problem. –3) it is good to think about such things in a course that mainly deals with data structures. Any guesses as to why I say this? –4) it is sometimes difficult to exploit some things about the problem to make a more efficient algorithm

Linked lists A linked list is a data structure where every node contains data and reference(s) to other node(s.)‏ Let's look at drawings on the board. Let's figure out how to – Add to beginning of linked list – Add to end of linked list Need to find last node – Insert after a particular node – Delete a particular node Other options – Storing link to last node – Doubly linked lists (and their operations.)‏ – Circular linked lists (where the “last” node refers to the “head”).

Linked lists A doubly linked list is a data structure where every node contains – data – a reference to previous node – a reference to next node The value of the next node for the last element of a linked list is null. The value of the previous node for the first element of a linked list is also null.

Linked lists In a singly linked list every node contains data and one reference to another node. –e.g. class Node { public AnyType data; // can have more data than one public Node next; } A Linked List is maintained by keeping a reference to the head of the list. From the head, we are able to get at all the other nodes in the list by following the next reference.

Linked lists class Node { public AnyType data; // can have more data than one public Node next; // when constructing a new node, we set its data and // set the next reference to null public Node(AnyType d)‏ { data = d; next = null; }

Linked lists // All the code on the rest of the slides is outside of the Node class. Node n = new Node(somedata); Node head; head = n; // sets n to be the head of the linked list Node newnode = new Node(somedata2); // to add a newnode to the beginning of the list: newnode.next = head; head = newnode;

Linked lists Node newnode = new Node(somedata2); // to add a newnode to the end of the list: Node currnode; currnode = head; while (currnode != null)‏ { savenode = currnode; currnode = currnode.next; } savenode.next = newnode;

Linked lists Insert after a particular node // insert newnode after findnode // head currnode = head; while (!((currnode.data).equals(findnode.data)))‏ { currnode = currnode.next; } newnode.next = currnode.next; currnode.next = newnode;

Linked lists // delete findnode // head Node prevnode; Node currnode = head; while (!((currnode.data).equals(findnode.data)))‏ { prevnode = currnode; currnode = currnode.next; } prevnode.next = currnode.next; currnode = null; // the data originally refered-to to be garbage collected // if we didn't have currnode = null; here, it would get garbage // collected once the currnode went out of scope.

Linked lists (chap. 4)‏ What's deficient in singly linked lists?

Linked lists (chap. 4)‏ Let's consider operations for a doubly linked list on the board. A doubly linked list is a data structure where every node has – data – a reference to previous node (prev)‏ – a reference to next node (next)‏ We maintain a head node and a tail node. The value of the next node for the last (tail) element of a linked list is null. The value of the previous node for the first (head) element of a linked list is also null. When construct a new node, set the data and next and prev default to null