CPSC 212, Test #3B, December 5, 2006

  1. Adjacency Matrix vs. Adjacency List

    1. 4*N^2 bytes
    2. 4*N + 12*|E|
    3. Adjacency Matrix: 4*100*100 = 40,000 bytes
      Adjacency List: 4*100 + 12*(1500) = 18,400
      Adjacency List more space-efficient

  2. Dijkstra's Algorithm

                  dv   pv
               0   0    -
               1   3    2
               2   2    0
               3   3    0
               4   8    5
               5   7    1
          
  3. Indegrees

           public init[] calculateIndegrees (ListNode L[]) {
    
              int n = L.length;
              int ind[] = new [n];
              for (int i=0; i<n; i++)       // initialize indegree array
                 ind[i] = 0;
    
              for (int i=0; i<n; i++) {     // examine each list
    
                 ListNode curr = L[i];
                 while (curr != null) {
                    ind[curr.destination]++;
                    curr = curr.next;
                 } // while
              } // i
    
           } // calculateIndegrees
    
          
  4. Short Answers

  5. Kruskal's Algorithm

    Cost = 26
    Edges = (0,1), (1,2), (1,5), (2,6), (3,8), (4,5), (4,8), (5,7)

  6. Prim's Algorithm

                  dv   pv     Cost = 26 (should match cost of Kruskal's algorithm)
               0   3    1
               1   2    2
               2   2    6
               3   8    8
               4   3    5
               5   1    1
               6   0    -
               7   3    5
               8   4    4
          
  7. Depth-first postorder: F J E D A I C H B G , use a stack

  8. Breadth-first : B A C H D I G J E F , use a queue

  9. Genetic Algorithms

              Cost = 33
    
              Offspring:  0 4 6 7 1 2 3 5
                          6 1 2 3 0 4 7 5
          
  10. Bonus #1:

    Subset of a set that adds up to 0.
    Example: { 4, 6, -3, -2, 1, 9}. Yes. { 4, -3, -2, 1}
    Answer "yes" or "no", not find best solution.

  11. Bonus #2

    Nodes are NP-complete problems.
    Directed Edges show reduction of one problem to another
    Example: Hamiltonian Cycle can be reduced to TSP