Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
courses:cs211:winter2012:journals:paul:home [2012/03/26 21:50] – [Chapter 6] nguyenpcourses:cs211:winter2012:journals:paul:home [2012/04/06 01:44] – [Chapter 7] nguyenp
Line 645: Line 645:
                   * else:                   * else:
                       * Find-Solution(j-1)                       * Find-Solution(j-1)
 +  * Section 6.2 - Principles of Dynamic Programming: Memoization or Iteration over Subproblems
 +      * The book first goes over what makes the previous algorithm dynamic and efficient
 +          * If I was the author, I would have put that in the last chapter. It has no business being here.
 +      * It says nothing else useful.
 +  * Section 6.3 - Segmented Least Squares: Multi-way Choices
 +      * The problem we are focusing in  this section is the one where we have a bunch of dots and want to know what line (if it even is a line) that is most similar to the data (you know what I mean)
 +      * If it is not a line, then we do not try and form a curve, instead we form a sequence of lines. 
 +      * How do we do this?
 +          * Consider the first and last points.
 +          * For every point p_n, it can only belong to one line segment
 +          * We define cost for each point as cost: c (segment cost) + error of segment
 +          * Let OPT(j) = minimum cost for points p_1, p_i+1 , … , p_j (the min cost of all the points)
 +          * Let e(i, j) = minimum sum of squares for points p_i, p_i+1 , … , p_j
 +          * How to compute OPT(j)?
 +              * Cost = e(i, j) + c + OPT(i-1).
 +              * OPT(J):
 +                  * if j=0
 +                      * return 0
 +                  * else
 +                      * min {e(i,j)+c+OPT(i,j): 1<=i<=j }
 +          * Algorithm: page 265
 +              * INPUT: n, p_1,…,p_N , c
 +              * Segmented-Least-Squares()
 +                  * M[0] = 0
 +                  * e[0][0] = 0
 +                  * for j = 1 to n
 +                      * for i = 1 to j
 +                          * e[i][j] = least square error for the segment p_i, …, p_j
 +                  * 
 +                  * for j = 1 to n
 +                      * M[j] = min 1 ≤ i ≤ j (e[i][j] + c + M[i-1])
 +                  * 
 +                  * return M[n]
 +          * The magic really lies in the math behind the least square error. That's how we determine which segment something belongs to.
 +          * Efficiency: 
 +              * The first group of nested for loops is O(n^3).
 +              * This can be reduced to quadratic if we pre-compute and write down the values for p_whatever
 +                  * YAY MEMOIZING
 +              * the last for loop is actually quadratic even though it initially looks linear because it's just a single for loop, but remember that min is a linear time function
 +          * For some reason the book likes to do things in this very not straightforward way of finding all the data we need using one algorithm and then later using that data to find the information we really need to get our results.
 +          * So, since we really need to know how to split up the points into a sequence of segments, we will go through all the segments we just assigned 
 +          * Algorithm: page 266
 +              * FindSegments(j):
 +                  * if j = 0:
 +                      * output nothing
 +                  * else:
 +                      * Find an i that minimizes ei,j + c + M[i-1]
 +                      * Output the segment {pi, …, pj}
 +                  * FindSegments(i-1)
 +  * Section 6.4 - Subset Sums and Knapsacks: Adding a variable
 +      * We're focusing on the knapsack problem
 +      * We have n things and a knapsack
 +      * item i has weight w_i (always positive) and also has a value v_i
 +      * We can only carry so much weight
 +      * We want to maximize the value (more bang for the buck type of deal)
 +      * So, for each item, we either pick it or we don't pick it
 +      * OPT(i) = max profit subset of items 1,…, i
 +      * How to compute OPT(i)?
 +          * if it does not select object i
 +              * OPT selects best of { 1, 2, …, i-1 }
 +          * if it does
 +              * Does not mean we must not pick everything lower than i
 +              * So, we don't know what to do. 
 +              * MORE SUBPROBLEMS!!! w0000ooooooo...
 +              * So, we set a new weight limit, our current available weight minus what we jsut added, i.e. w – w_i
 +              * We let OPT select the best of these
 +      * That's it! That's the algorithm
 +      * Algoriddim be on Page 269 mahn
 +          * Input: N, w_1,…,w_N, v_1,…,v_N
 +          * for w = 0 to W
 +              * M[0, w] = 0
 +          * for i = 1 to N
 +              * for w = 1 to W
 +                  * if wi > w :
 +                      * M[i, w] = M[i-1, w]
 +                  * else
 +                      * M[i, w] = max{ M[i-1, w], vi + M[i-1, w-wi] }
 +          * return M[n, W]
 +      * The reason for why this works is suprisingly simple once we realize the important of just changing the amount of weight left over and over again
 +      * Efficiency:
 +          * This is a strange one. 
 +          * It's Θ(n W)
 +          * So it both depends on the knap sack capacity and the number of objects
 +          * they call it Pseudo-polynomial. 
 +          * Weird stuff. Never thought algorithms like this existed. 
 +      * 
 +
 +====== Chapter 7 ======
 +  * Section 7.1 - The Maximum-Flow Problem and the Ford-Fulkerson Algorithm
 +      * Problem
 +          * You have a directed graph
 +          * Each edge has a capacity
 +          * Have a start node s and end node t
 +          * Greedy
 +              * Find an s-t path with highest capacity
 +              * do it!
 +              * keep doing this until you cant do it any more
 +              * Locally optimal, but not globally optimal (it's very easy to think of a counter example, counter example is on page 339 in book)
 +          * Awesome Solution: Residual Graphs
 +              * Edges have a capacity, but also a flow
 +              * We don't have to use all of it at once
 +              * Residual edge is one where you have some going one way and some going the other
 +              * The Ford-Fulkerson Algorithm uses residual edges and works awesomely (optimal)
 +                  * page 342 - 344
 +                  * Informal explanation
 +                      * graphs
 +                      * keep updating the residual edges using the following formula:
 +                          * If edge is the same as it was originally, set the forward rate to the bottle neck + original rate (which is the edge with the smallest flow rate)
 +                          * Otherwise, set the backwards residual rate to what it originally was minus the bottle neck
 +  * Section 7.2 - Maximum Flows and Minimmum Cuts in a Network
 +      * Cut: s-t cut is a partition (A,B) where s is in A and t is in B
 +      * Problem: Find an s-t cut of minimum capacity
 +      * The capacity of a cut is the sum of all the possible stuff that can go through the edge at once
 +      * Flow Value Lemma: Let f be any flow, and let (A, B) be any s-t cut. Then, the value of the flow is = f_out(A) – f_in(A).
 +      * Weak Duality: let f be any flow and let (A, B) be any s-t cut. Then the value of the flow is at most the cut’s capacity.
 +      * Corollary: Let f be any flow, and let (A, B) be any cut. If v(f) = cap(A, B), then f is a max flow and (A, B) is a min cut
 +      * Augmenting path theorem: Flow f is a max flow iff there are no augmenting paths
 +      * Max-flow min-cut theorem: The value of the max flow is equal to the value of the min cut.
 +          * More explanation on page 350
 +          * Boom and we are done. Just use the same stuff as in the past section and we have all we need. 
 +  * Section 7.5 - A First Application: Bipartite Matching Problem
 +      * Given an undirected bipartite graph
 +      * We must match so each node appears at most once on each edge
 +      * How do we find one with the max number of nodes? This is our problem.
 +      * Here's how we do it. We got a bunch of red and blue nodes. Add nodes s and t, which will be the start and end nodes. Have the start node s have a unit edge (edge of size 1) connect it to all the red nodes. Have all the blue nodes have a unit edge connect them to the end node t. Still have same edges in the middle. 
 +      * Now, use same algorithm as in the first section. BOOOM!!! Right? Yep. It's pretty intuitive, these later sections are more like practical uses rather than actual algorithms. I guess this is kind of good since pretty much all computer science problems go back to graph theory. 
 +      * 
courses/cs211/winter2012/journals/paul/home.txt · Last modified: 2012/04/06 02:17 by nguyenp
CC Attribution-Noncommercial-Share Alike 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0