Presentation is loading. Please wait.

Presentation is loading. Please wait.

Winter 2006CISC121 - Prof. McLeod1 Stuff Deadline for assn 3 extended to Monday, the 13 th. Please note that the testing class for assn 3 has changed (last.

Similar presentations


Presentation on theme: "Winter 2006CISC121 - Prof. McLeod1 Stuff Deadline for assn 3 extended to Monday, the 13 th. Please note that the testing class for assn 3 has changed (last."— Presentation transcript:

1 Winter 2006CISC121 - Prof. McLeod1 Stuff Deadline for assn 3 extended to Monday, the 13 th. Please note that the testing class for assn 3 has changed (last Sunday). You will need to download a new copy, if you have not done so this week already. Solution to midterm posted shortly.

2 Winter 2006CISC121 - Prof. McLeod2 Dept. Math. & Stats. Colloquium Today!! Friday, March 10, 2:30 p.m., Jeffery Hall 234 (bad vibes?) Speaker: Olgica Milenkovic University of Colorado Title: Two problems in analysis of algorithms (see next slide for abstract…)

3 Winter 2006CISC121 - Prof. McLeod3 Abstract: We address the problem of analyzing the complexity of two algorithms from the field of computer algebra and bioinformatics. The first algorithm, known as Gosper's method for indefinite summation of hypergeometric terms, is investigated in the asymptotic, average-case complexity regime. We show that for several classes of input functions represented in terms of urn models, the complexity of Gosper's algorithm is related to the extremal behavior of a class of generalized random-walks. The second algorithm we discuss is concerned with the problem of sorting signed permutations by reversals. Algorithms for sorting by reversals represent one of the most important tools in analyzing rearrangements in the mouse vs. human genome. We point out some shortcomings of the current approach to modeling reversals and propose two new extensions for the notion of reversal distance. We investigate the complexity of a Viterbi and M-type algorithm for computing cost-constrained reversal distances. The first part of the talk is a joint work with Kevin J. Compton from the University of Michigan.

4 Winter 2006CISC121 - Prof. McLeod4 Last Time (Tutorial!) Before that: –Doubly linked lists. –Circular lists. –Skip lists. –Self – organizing lists.

5 Winter 2006CISC121 - Prof. McLeod5 Today Replacing sparse tables with linked lists. LinkList in java.util Stacks (if we have time) Next time: Queues

6 Winter 2006CISC121 - Prof. McLeod6 Sparse Tables A sparse table is defined as a table where the available cells are only partly populated. For example, consider a table where the columns are student ID’s and the rows are courses taken, for the entire University: –A cell can contain a grade for a course taken by the student. –Of course, not all students take all courses, so only a small portion of each column is taken up with data. –Such a “sparse table” is probably not an efficient use of memory.

7 Winter 2006CISC121 - Prof. McLeod7 Sparse Tables – Cont. Replace the table by a system of linked lists. Here is one possible design: –Have an ArrayList of course Objects. Each course Object contains all the necessary info about each course and a link to the first student enrolled in the course. –Have an ArrayList of student Objects. Each student Object contains the necessary student information and has a link to the first course taken by that student. –However, make the node design in such a way that the nodes are shared by both lists!

8 Winter 2006CISC121 - Prof. McLeod8 Sparse Tables – Cont. –Each node contains: student number class number grade code (0 is “A”, 9 is “F”) link to next student link to next course –One node for each course the student has taken. No empty nodes. See the structure on the next slide: Data Links

9 Winter 2006CISC121 - Prof. McLeod9 1

10 Winter 2006CISC121 - Prof. McLeod10 Sparse Tables – Cont. (I don’t know why SN and course# have to be in each node in the diagram…) How to navigate through this structure? –Start with a student or start with a course. –But from any given node you can flip the direction of your navigation. At the moment, you have to follow links from oldest to newest in one direction – this would be a painful way of finding recent students in a course that has been around for a while! How would you fix this?

11 Winter 2006CISC121 - Prof. McLeod11 Sparse Tables – Cont. More efficient use of memory because there are no empty table elements. Uses 17% of the memory used by the sparse table for a typical University setting. Can easily grow as required.

12 Winter 2006CISC121 - Prof. McLeod12 Linked Lists in java.util java.util contains a class called “ LinkedList ”. ( E is the element type to be stored in the list.) As does the ArrayList class, LinkedList contains many useful pre-defined methods. LinkedList implements a linked list as a “generic doubly-linked list with references to the head and tail”. Many of the LinkedList methods throw Exceptions for illegal parameters. LinkedList only stores “ Objects ” of type E.

13 Winter 2006CISC121 - Prof. McLeod13 Linked Lists in java.util – Cont. Methods include (“ ob ” is an object of type E ): void add(ob) // adds ob to end of list. void add(pos, ob) // adds ob at pos. void addFirst(ob) // adds ob at beginning of list. void addLast(ob) // same as add(ob). void clear() // removes all objects from the list. boolean contains(ob) // returns true if the list contains ob.

14 Winter 2006CISC121 - Prof. McLeod14 Linked Lists in java.util – Cont. Object get(pos) // returns the object at pos. Object getFirst() // returns first object in list. Object getLast() // returns last object in list. int indexOf(ob) // returns position of first occurrence of ob, or –1 if ob is not found. boolean isEmpty() // returns true if list is empty, false otherwise.

15 Winter 2006CISC121 - Prof. McLeod15 Linked Lists in java.util – Cont. Iterator iterator() // generates and returns an iterator for the list. LinkedList() // creates an empty linked list. boolean remove(ob) // removes first occurrence of ob and returns true. Object removeFirst() // removes and returns first element in list.

16 Winter 2006CISC121 - Prof. McLeod16 Linked Lists in java.util – Cont. Object removeLast() // removes and returns last element in list. int size() // returns number of elements in list.

17 Winter 2006CISC121 - Prof. McLeod17 Stacks What is a stack? For example a “PEZ” candy container:

18 Winter 2006CISC121 - Prof. McLeod18 Stacks – Cont. Another example are those plate dispensers in cafeterias. A stack follows the “Last In, First Out” or “LIFO” principle. A stack would have the following operations: –clear() – clear the stack. –isEmpty() – check to see if the stack is empty. –isFull() – check to see if the stack is full. –push(element) - put the element on top of the stack. –pop() – take the topmost element from the stack. –peek() – return the topmost element in the stack without removing it.

19 Winter 2006CISC121 - Prof. McLeod19 Stacks – Cont. Any other methods would not be “legal” – you would no longer be modeling a stack. This is a restrictive data structure! Why bother?

20 Winter 2006CISC121 - Prof. McLeod20 Stacks – Cont. A stack is modeled with another data structure “under the hood”. If you used a linked list: –What kind of a linked list would you use? –What linked list methods would be used to carry out the following stack methods:? push pop clear peek

21 Winter 2006CISC121 - Prof. McLeod21 Stacks – Cont. See IntStack.java for a linked list implementation. Some features of IntStack: –An inner, inner class for the node. –An inner class for the linked list. –Only public methods are: clear () boolean isEmpty () push (int) int pop () int peek ()

22 Winter 2006CISC121 - Prof. McLeod22 Stacks – Cont. A stack can also be implemented with an ArrayList or even an array (But not as well, IMHO). Note that none of the stack operations require iteration through the linked list.

23 Winter 2006CISC121 - Prof. McLeod23 ArrayList Stack Implementation See ALStack.java (Note that we are assuming the user will check to see if the stack is empty before calling a “pop()” operation. What else could we do?) “Features”: –We need to keep track of position in the ArrayList. –We could use (but did not) the automatic “un-boxing” and boxing feature in Java 5.0 (huh?).

24 Winter 2006CISC121 - Prof. McLeod24 Array Stack Implementation See ArrayStack.java A bit clumsy? Of the three implementations, which is the best?

25 Winter 2006CISC121 - Prof. McLeod25 A Stack in Use How to add numbers that are too large to be stored in a long type variable? See LongAddition.java

26 Winter 2006CISC121 - Prof. McLeod26 The Stack Class in java.util java.util has a “ Stack ” class that implements the above methods using a Vector as the storage object. (A Vector is like an ArrayList…) Stack is a sub-class of Vector, and as such, inherits all the Vector methods.

27 Winter 2006CISC121 - Prof. McLeod27 The Stack Class – Cont. Unique Stack methods: boolean empty() // same as isEmpty Object peek() Object pop() Object push(element) int search(target) // returns position of target in stack, if not found –1 is returned. Stack() // constructor

28 Winter 2006CISC121 - Prof. McLeod28 The Stack Class – Cont. In Stack, “ push ” also returns a reference to the Object added to the stack, so both peek and push can change that topmost object on the stack. Since Stack “is a” Vector, methods like “ setElementAt ” and “ removeElementAt ” can be used on stack elements – but these methods would be illegal by our definition of what a stack is! Also, when Vector ’s are used to implement the stack, re-sizing of the Vector can greatly slow down the push method.

29 Winter 2006CISC121 - Prof. McLeod29 The Stack Class – Cont. For these reasons it is better to implement a stack as we have done above, using a private linked list (best) or a private array(next best) as a data object within the definition of the class. Implementing a stack using a linked list defined using an inner class for the list and an “inner, inner” class for the node provides better information hiding than the Stack class.


Download ppt "Winter 2006CISC121 - Prof. McLeod1 Stuff Deadline for assn 3 extended to Monday, the 13 th. Please note that the testing class for assn 3 has changed (last."

Similar presentations


Ads by Google