prev

next

of 49

View

234Download

1

Embed Size (px)

CS121

CS322Week 12 - FridayLast timeWhat did we talk about last time?Asymptotic notation

Questions?Logical warmupA pot contains 75 white beans and 150 black onesNext to the pot is a large pile of black beans.The (insane) cook removes the beans from the pot, one at a time, according to the following strange rule:He removes two beans from the pot at randomIf at least one of the beans is black, he places it on the bean-pile and drops the other bean, no matter what color, back in the potIf both beans are white, on the other hand, he discards both of them and removes one black bean from the pile and drops it in the potAfter each step of this procedure, the pot has one fewer bean in itEventually, just one bean is left in the potWhat color is it?

Exponential and Logarithmic FunctionsStudent LectureExponential and Logarithmic FunctionsExponential functionsWell, they grow fastGraph 2x for -3 x 3When considering bx, it's critically important whether b > 1 (in which case bx grows very fast in the positive direction) or 0 < b < 1 (in which case bx grows very fast in the negative direction)Graph bx when b > 1Graph bx when 0 < b < 1What happens when b = 1?What happens when b 0?

Logarithmic functionThe logarithmic function with base b, written logb is the inverse of the exponential functionThus,by = x logb x = y for b > 0 and b 1Log is a "de-exponentiating" functionLog grows very slowlyWe're interested in logb when b > 1, in which case logb is an increasing functionIf x1 < x2, logb(x1) < logb(x2), for b > 1 and positive x1 and x2Applying logHow many binary digits are needed to represent a number n?We can write n = 2k + ck-12k-1 + c222 + c12 + c0 where ci is either 0 or 1Thus, we need no more than k + 1 digits to represent nWe know that n < 2k + 1Since 2k n < 2k+1, k log2n < k+1The total number of digits we need k + 1 log2n + 1Recurrence relationsConsider the following recurrence relationa1 = 0ak = ak/2 + 1 for integers k 2What do you think its explicit formula is?It turns out that an = log nWe can prove this with strong inductionExponential and logarithmic ordersFor all real numbers b and r with b > 1 and r > 0logb x xr,for sufficiently large xxr bx,for sufficiently large xThese statements are equivalent to saying for all real numbers b and r with b > 1 and r > 0logb x is O(xr)xr is O(bx)Important thingsWe don't have time to show these things fullyxk is O(xk logb x)xk logb x is O(xk+1)The most common case you will see of this is:x is O(x log x)x log x is O(x2)In other words, x log x is between linear and quadraticlogb x is (logc x) for all b > 1 and c > 1In other words, logs are equivalent, no matter what base, in asymptotic notation1/2 + 1/3 + + 1/n is (log2 n)

ReviewRelationsRelationsRelations are generalizations of functionsIn a relation (unlike functions), an element from one set can be related to any number (from zero up to infinity) of other elementsWe can define any binary relation between sets A and B as a subset of A x BIf x is related to y by relation R, we write x R yAll relations have inverses (just reverse the order of the ordered pairs)

Properties of relationsFor relation R on set AR is reflexive iff for all x A, (x, x) RR is symmetric iff for all x, y A, if (x, y) R then (y, x) R R is transitive iff for all x, y, z A, if (x, y) R and (y, z) R then (x, z) RR is antisymmetric iff for all a and b in A, if a R b and b R a, then a = bThe transitive closure of R called Rt satisfies the following properties:Rt is transitiveR RtIf S is any other transitive relation that contains R, then Rt SEquivalence relations and partial ordersLet A be partitioned by relation RR is reflexive, symmetric, and transitive iff it induces a partition on AWe call a relation with these three properties an equivalence relationExample: congruence mod 3If R is reflexive, antisymmetric, and transitive, it is called a partial orderExample: less than or equal

Counting and ProbabilityProbability definitionsA sample space is the set of all possible outcomesAn event is a subset of the sample spaceFormula for equally likely probabilities:Let S be a finite sample space in which all outcomes are equally likely and E is an event in SLet N(X) be the number of elements in set XMany people use the notation |X| insteadThe probability of E is P(E) = N(E)/N(S)Multiplication ruleIf an operation has k steps such thatStep 1 can be performed in n1 waysStep 2 can be performed in n2 waysStep k can be performed in nk waysThen, the entire operation can be performed in n1n2 nk waysThis rule only applies when each step always takes the same number of waysIf each step does not take the same number of ways, you may need to draw a possibility treeAddition and inclusion/exclusion rulesIf a finite set A equals the union of k distinct mutually disjoint subsets A1, A2, Ak, then:N(A) = N(A1) + N(A2) + + N(Ak)

If A, B, C are any finite sets, then:N(A B) = N(A) + N(B) N(A B)

And:N(A B C) = N(A) + N(B) + N(C) N(A B) N(A C) N(B C) + N(A B C)

Counting guideThis is a quick reminder of all the different ways you can count k things drawn from a total of n things:

Recall that P(n,k) = n!/(n k)!And = n!/((n k)!k!)Order MattersOrder Doesn't MatterRepetition AllowednkRepetition Not AllowedP(n,k)

How many ways are there toOrder the letters in the word "replicant"Order any three letters in the word "replicant"?Order the letters in the word "neverness"If all letters are considered distinct?If identical letters cannot be told apart?Select an elite assassin team of 8 people from a squadron of 100?Select an elite assassin team of 8 people from a squadron of 100 and make one person the leader?How many ways are there toName a child if you are willing use any sequence of characters from the Latin alphabet from 1 to 10 characters long?Decide the four kinds of courses you'd like to take in a semester, if the kinds are:Computer Science coursesCore coursesElectives

Binomial theoremThe binomial theorem states:

You can easily compute these coefficients using Pascal's triangle for small values of n

Probability axiomsLet A and B be events in the sample space S0 P(A) 1P() = 0 and P(S) = 1If A B = , then P(A B) = P(A) + P(B)It is clear then that P(Ac) = 1 P(A)More generally, P(A B) = P(A) + P(B) P(A B)Expected valueExpected value is one of the most important concepts in probability, especially if you want to gambleThe expected value is simply the sum of all events, weighted by their probabilitiesIf you have n outcomes with real number values a1, a2, a3, an, each of which has probability p1, p2, p3, pn, then the expected value is:

Conditional probabilityGiven that some event A has happened, the probability that some event B will happen is called conditional probabilityThis probability is:

Graphs and TreesGraphsA graph G is made up of two finite setsVertices: V(G)Edges: E(G)Each edge is connected to either one or two vertices called its endpointsAn edge with a single endpoint is called a loopTwo edges with the same sets of endpoints are called parallelEdges are said to connect their endpointsTwo vertices that share an edge are said to be adjacentA graph with no edges is called emptySpecial graphsA simple graph does not have any loops or parallel edgesLet n be a positive integerA complete graph on n vertices, written Kn, is a simple graph with n vertices such that every pair of vertices is connected by an edgeA bipartite graph is a simple graph whose vertices can be partitioned into sets X and Y such that there are no edges from any vertex in X to another vertex in X and there are no edges from any vertex in Y to another vertex in YA complete bipartite graph on (m, n) vertices, written Km,n is a simple graph with a set of m vertices and a disjoint set of n vertices such that:There is an edge from each of the m vertices to each of the n verticesThere are no edges among the set of m verticesThere are no edges among the set of n verticesA subgraph is a graph whose vertices and edges are a subset of another graphDegreeThe degree of a vertex is the number of edges that are incident on the vertexThe total degree of a graph G is the sum of the degrees of all of its verticesWhat's the relationship between the degree of a graph and the number of edges it has?What's the degree of a complete graph with n vertices?Note that the number of vertices with odd degree must be evenExampleWhat is the maximum number of edges a simple disconnected graph with n vertices can have?Prove it.DefinitionsA walk from v to w is a finite alternating sequence of adjacent vertices and edges of G, starting at vertex v and ending at vertex wA walk must begin and end at a vertexA path from v to w is a walk that does not contain a repeated edgeA simple path from v to w is a path that does contain a repeated vertexA closed walk is a walk that starts and ends at the same vertexA circuit is a closed walk that does not contain a repeated edgeA simple circuit is a circuit that does not have a repeated vertex other than the first and lastCircuitsIf a graph is connected, non-empty, and every node in the graph has even degree, the graph has an Euler circuitAlgorithm to find one:Pick an arbitrary starting vertexMove to an adjacent vertex and remove the edge you cross from the graphWhenever you choose such a vertex, pick an edge that will not disconnected the graphIf there are still uncrossed edges, go back to Step 2A Hamiltonian circuit must visit every vertex of a graph exactly once (except for the first and the last)If a graph G has a Hamiltonian circuit, then G has a subgraph H with the following properties:H contains every vertex of GH is connectedH has the same number of edges as verticesEvery vertex of H has degree 2Matrix representations of graphsTo represent a graph as an adjacency matrix,