CIT 594 Basic content n n CIT 594
CIT 594
Basic content n n CIT 594 is a continuation of CIT 591 Java will be used throughout However, CIT 594 is not primarily a course about Java Topics: n Data Structures n n n Algorithms n n Supplied by Java How to create your own A sampling of some better-known algorithms Analysis of algorithms n Simple algebra is required
Types of Collection n n A collection is a structured group of objects Java supplies several types of Collection: n n Set: cannot contain duplicate elements, order is not important Sorted. Set: like a Set, but order is important List: may contain duplicate elements, order is important Java also supplies some “collection-like” things: n n Map: a “dictionary” that associates keys with values, order is not important Sorted. Map: like a Map, but order is important
The Collections hierarchy
Uniformity through interfaces n n Much of the elegance of the Collections Framework arises from the intelligent use of interfaces For example, the Collection interface specifies (among many other operations): n n n boolean add(Object o) boolean is. Empty() boolean remove() int size() Object[] to. Array() Iterator iterator()
Creating lists in Java my. List: 44 97 class Cell { int value; Cell next; Cell (int v, Cell n) { // constructor value = v; next = n; } } Cell temp = new Cell(17, null); temp = new Cell(23, temp); temp = new Cell(97, temp); Cell my. List = new Cell(44, temp); 23 17
A short list of categories n Algorithm types we will consider include: n n n n Simple recursive algorithms Backtracking algorithms Divide and conquer algorithms Dynamic programming algorithms Greedy algorithms Branch and bound algorithms Brute force algorithms Randomized algorithms
Example recursive algorithms n To count the number of elements in a list: n n If the list is empty, return zero; otherwise, Step past the first element, and count the remaining elements in the list Add one to the result To test if a value occurs in a list: n n n If the list is empty, return false; otherwise, If the first thing in the list is the given value, return true; otherwise Step past the first element, and test whether the value occurs in the remainder of the list
Example backtracking algorithm n To color a map with no more than four colors: n color(Country n): n n If all countries have been colored (n > number of countries) return success; otherwise, For each color c of four colors, n If country n is not adjacent to a country that has been colored c Color country n with color c n recursively color country n+1 n If successful, return success If loop exits, return failure n n
Example greedy algorithm n n Suppose you want to count out a certain amount of money, using the fewest possible bills and coins A greedy algorithm would do this would be: At each step, take the largest possible bill or coin that does not overshoot n Example: To make $6. 39, you can choose: n n n a $5 bill a $1 bill, to make $6 a 25¢ coin, to make $6. 25 A 10¢ coin, to make $6. 35 four 1¢ coins, to make $6. 39 For US money, the greedy algorithm always gives the optimum solution
Time and space n To analyze an algorithm means: n n n developing a formula for predicting how fast an algorithm is, based on the size of the input (time complexity), and/or developing a formula for predicting how much memory an algorithm requires, based on the size of the input (space complexity) Usually time is our biggest concern n Most algorithms require a fixed amount of space
y = x 2 + 3 x + 5, for x=1. . 20
The End
- Slides: 13