CS62 - Spring 2010 - Lecture 25

  • Exercise 16.1?

  • Admin: any seniors?

  • C++ for the day: initializer lists
       - anything inefficient about?

       MyClass(string s, vector<int> v){
          this->s = s;
          this->v = v;
          data = 0;
       }

       ...

       private string s;
       private vector<int> v;
       private int data;

       - Two basic problems:
          - our private members must be initialized to something before the constructor is called (what if we didn't assign to them... they'd still need to be valid class objects)
             - all the private members that are classes would have to have a zero parameter constructor
          - The parameters are passed to the constructor using call-by value
             - we'll call the copy constructor and when the parameters are passed
             - then we'll call the operator= method to again copy the data from our formal parameter v to our private member variable
             - We don't actually need the first copy
       - Initializer lists:
          - follow the parameter list, but before the opening curly brace of the constructor
          - start with a ':'
          - look list normal constructor calls
          - comma separated
       
          MyClass(string s, vector<int> v) :
             s(s), v(v), data(0);
          {}

          - notice that we can do it for built-in data types as well
          - also notice that it's not just for using the copy constructor, you can use constants, etc.
       - In general, good practice
       - They are required in three cases:
          - one of the classes doesn't have a zero value constructor
          - constant data members
          - call-by reference: since we can't do copy-by reference (e.g. streams)
          
  • 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

  • Assignment 11
       - two parts
          - three graph algorithms (closely related to what we looked at today)
          - netflix data
             - some I/O
             - applying algorithms to real data
       - lab is important this week for getting primed for the assignment
          - if you can't make the lab, make sure to look at the material covered in the lab (in particular, the second part)
       - due May 5 at 5pm (the last day of classes)... though I'll accept it up to midnight
       - I've provided you with code to generate some test graphs. Use it!