Hashtable & clone

  1. Searching efficiently
    1. Unsorted list - linear search, O(n)
    2. Sorted list - binary search, O(log n)
    3. Hashtable - O(1)!
    4. H : keyset -> valueSet
      1. Need a hash function that maps a value onto a key
      2. Use the key to access a bucket, store the value in the bucket
      3. If more than one value in a bucket (called a collision)... many solutions
        1. chaining - create a linked (or other) list
        2. rehash - apply another hash function
        3. etc, etc...
      4. But, modern programming languages have built-in hashtables; Java's is called HashMap
  2. Make an exact, deep, copy of an Object: clone()! But why?
    1. In NaryTree() we have the code:
      void addKids() {
              if (!root.gameOver()) {
                  for (Board nextBoard : root.generateNextLegalBoards()) {
                      kids.add(new NaryTree(nextBoard));
                  }
              }
          }
    2. See psvm in Board in examplesFromClass/April6_GameProto/src/april6_gameproto/Board.java for a truly hideous bug (worth an hour or two...)
  3. Back at bioinformatics... Example: AT & CAT
    For this example:
        gap penalty = -4
        mismatch = -1
        match = 2
    
    Given two strings
    Put them on two edges of a table (with the fronts near each other! and a 0 in between) like this:
         T
         A
           0
              C  A  T
    
    Fill in the cells straight to the right and up from the 0 with increasing gap penalties            
         T -8
         A -4 
            0 -4  -8 -12
              C   A   T
    For each of the blank cells, there are three ways you might get there: 
            1) Horizontally or 
            2) Vertically, both create a gap, so score change is the gap penalty
            3) Diagonally: advances one in each string; score change is either match or mismatch
        Select the one with the highest score and fill that in.
    
    Deduce the route by which you arrived at the upper right cell.
    Use it to generate the optimal alignment.
            
  4. Another example? TACCT & AT, with match=5, gap=-1, mismatch=-10