CS62 - Spring 2010 - Lecture 26

  • Course reviews

  • Announcements
       - Last CS lunch today an 12 in Frank West
       - Alumni talks tomorrow (Friday) 3-4PM in Edmuns 114
          - Dreamworks, Semaphore Mobile, Zillow

  • C++ for the day: default parameters
       In Java:

       void printInt(int num){
          printInt(num, 10);
       }

       void printInt(int num, int base){
          // ...
       }

       We could then call:

       printInt(15);
       15
       printInt(15, 16);
       f
       
       In C++:
       void printInt(int num, int base = 10){
          // ...
       }

       int base = 10 is called a "default parameter" and is equivalent to the two declarations above.

       printInt(15);
       15
       printInt(15, 16);
       f

       - A few things to note:
          - default parameters must come AFTER normal parameters to avoid ambiguity. The following is NOT ok:

          void printInt(int base = 10, int num) <--- BAD

          - You can have multiple default parameters. Parameters are filled in from left to right:

          void method(int v1 = 1, int v2 = 2){
             cout << v1 << ", " << v2 << endl;
          }

          method();
          1, 2
          method(15);
          15, 2
          method(15, 20);
          15, 20

  • look at Dijkstra slides
          
  • four graph problems with code (see the handouts for the code... maybe post the code?):
       - cycles: given a graph, does the graph contain a cycle?
          - how might we do this?
             - run depth first search
             - if we visit a node that we've already visited AND we didn't just come from that node, then we've found a cycle
       - connectedness: given an undirected graph, is the graph connected?
          - how might we do this?
             - run depth first search (or breadth first search)
             - keep track of which nodes we visit
             - if we visit all of the nodes, then it's connected
       - connected components: given an undirected graph, return the largest subgraphs that are connected
          - how might we do this?
             - pick a node
             - run depth first search on that node keeping track of both the nodes we visited
             - all nodes visited during THAT dfs are one subgraph
             - pick a node that is unvisited and repeat
       - single source shortest paths: given a weighted graph and a starting vertex v, find the shortest paths from v to all other vertices
          - how would we find shortest paths in an unweighted graph?
             - use breadth first search

             enqueue start;
             while (queue not empty) {
                dequeue v;
                if (v is not visited) {
                   visit v;
                   enqueue all vŐs neighbors;
                }
             }

             we visit nodes at distance 1, then 2, then 3, etc. from the start vertex. Therefore, we always find the shortest path. At each iteration, we visit the next closest node to the vertex (or one of the next closest if there is a tie).

             - how is our problem different?
                - edges have different lengths
             - Basic idea: Same as breadth first search, we'd like to visit the next closest node to the vertex each time. Keep track of three sets of vertices:
                - visited: those we've visited and have the shortest paths for
                - frontier: those we've seen as a neighbor and we have a POSSIBLE path to them, but it may not be the shortest path
                - unseen