Presentation is loading. Please wait.

Presentation is loading. Please wait.

The material in this lecture should be review. If you have not seen it before, you should be able to learn it quickly – and on your own. So we are going.

Similar presentations


Presentation on theme: "The material in this lecture should be review. If you have not seen it before, you should be able to learn it quickly – and on your own. So we are going."— Presentation transcript:

1 The material in this lecture should be review. If you have not seen it before, you should be able to learn it quickly – and on your own. So we are going to cover it FAST!!!

2

3

4 Recall how decimal representation works. 2037 7x10 0 3x10 1 +0x10 2 +2x10 3 + 2037

5 Binary numbers are the same – just use 2 instead of 10 (hence the name “base 2”) 1011 1x2 0 1x2 1 +0x2 2 +1x2 3 + 1011 =11 10

6 Follows directly from the definition. You should know how to do this – quickly. Hint: As computer scientists you should know all powers of 2 up 1024 (2 10 )

7 Steps for converting n to binary 1. Find the largest k such that 2 k ≤ n. 2. Make the k th digit a 1. 3. Compute the binary value for n – 2 k. (Stop when n = 2 k.)

8 Convert 22 to binary: 1.Find largest k such that 2 k ≤ 22 k = 4, 2 k = 16, n’ = n-16 = 6 Number is: 1XXXX, where XXXX is the binary representation of 6 2.Find the largest k such that 2 k ≤ 6 k = 2, 2 k = 4, n’’ = n’-4 = 2 Number is: 101XX, where XX is the binary representation of 2 3.Find the largest k such that 2 k ≤ 2 k=1, 2 k = 1, n’’’ = n’’ – 2 = 0 Number is: 10110 – done!

9

10 Logarithms Question: What is the meaning of log b a? Answer: The exponent of b that results in a. In other words: the value x such that b x = a.

11 log 2 16 = log 3 9 = log 7 7 8 = 4 2 8 log 4 4 = 1 log 5 1 = 0 6 log 6 12 = 12

12

13 Floor function:  x  or floor(x) The largest integer n such that n ≤ x (  4.7  =4) Question: What is the “meaning” of  log 2 n  ? It is the largest integer k such that 2 k ≤ n. Question: When converting decimal to binary, what is the first power of 2 we will use?  log 2 n  Question: How many bits do we need to store the number n?  log 2 n  +1

14 How many digits does n have in base 10?  log 10 n  +1 How many digits does n have in base b?  log b n  +1

15 How do we solve for n when we know: 2 n = h? 2 n = h log 2 2 n = log 2 h n = log 2 h

16

17 Start with a group of n people on an island. (Assume n is a power of 2.) Step 1: Vote half of the n people off the island. Step 2: Vote half of the (remaining) people off. Step 3: Vote half of the (remaining) people off. And so on… How many steps until we have only one person? Not necessary, but makes life easier

18 How many people do we have after each step? Initially (after “step 0”): n = n/2 0 people left. After step 1:  (1/2)(n/2 0 ) = n/2 1 people left After step 2: (1/2)(n/2 1 ) = n/4 = n/2 2 left After step 3: (1/2)(n/2 2 ) = n/8 = n/2 3 left … After step i: n/2 i people left When does n/2 i = 1? When n = 2 i, or i = log 2 n

19 Consider the NCAA Basketball Tournament (a single elimination tournament – half the teams are eliminated each round). Suppose it is expanded to 512 starting teams. How many rounds are needed to finish? log 2 512 = 9 rounds

20

21 Graph : A set of nodes V and a multi-set E  V  V. v1v1 v2v2 v3v3 v4v4 v5v5 V={v 1, v 2, v 3, v 4, v 5 } E = {(v 1, v 2 ), (v 1, v 3 ), (v 1, v 4 ), (v 2, v 3 ), (v 1, v 3 ), (v 3, v 5 ), (v 4, v 5 ), (v 5, v 5 )}

22 Simple Graph : A set of nodes V and a multi-set E  V  V such that (v,v)  E for any v  V v1v1 v2v2 v3v3 v4v4 v5v5 V={v 1, v 2, v 3, v 4, v 5 } E = { (v 1, v 2 ), (v 1, v 3 ), (v 1, v 4 ), (v 2, v 3 ), (v 1, v 3 ), (v 3, v 5 ), (v 4, v 5 ), (v 5, v 5 )}

23 Simple Graph : A set of nodes V and a set E  V  V such that (v,v)  E for any v  V v1v1 v2v2 v3v3 v4v4 v5v5 V={v 1, v 2, v 3, v 4, v 5 } E = {(v 1, v 2 ), (v 1, v 3 ), (v 1, v 4 ), (v 2, v 3 ), (v 3, v 5 ), (v 4, v 5 )} In this course: all graphs will be simple – we will not bother saying it

24 Nodes u and v are adjacent if they share an edge (e.g. if (u,v)  E ) v1v1 v2v2 v3v3 v4v4 v5v5 Correct: “v 1 and v 3 are adjacent” Incorrect: “v 1 and v 5 are adjacent”

25 A path from u to v is a sequence of adjacent nodes starting at u and ending at v v4v4 v1v1 v2v2 v3v3 v5v5 Sample path: v 1, v 2, v 3, v 5 v1v1 v2v2 v3v3 v5v5

26 A cycle is a path from a node to itself using at least one edge. v4v4 v5v5 v1v1 v2v2 v3v3 v1v1 v2v2 v3v3

27 A graph is connected if there is a path between every pair of nodes v1v1 v2v2 v3v3 v4v4 v5v5 Connected Still connected Not connected Two connected components

28 A graph is a acyclic if it has no cycles. A graph is a tree if it is connected and acyclic. v1v1 v2v2 v3v3 v4v4 v5v5 Not acyclic. Acyclic, but not a tree. A tree.

29 Make sure you can define these terms (Simple) Graph Adjacent nodes Path Cycle Connected Connected Component Acyclic Tree

30

31 Proof: A logic-based argument that a given fact is true. Does not have to contain algebra or complicated mathematical terminology Should primarily consist of straight-forward English Must be clear and easily understood.

32 Mathematical notation is not necessary, but it is useful. It is essentially short-hand that conveys specific information. It is important that you are comfortable with both reading and using it.

33 N (“Natural numbers”): {0, 1, 2, …} E (“Evens”): {0, 2, 4, …} O (“Odds”): {1, 3, 5, …} Z (“Integers”): - N  N = {…, -2, -1, 0, 1, 2, …}  (“Real numbers”): Set of all real numbers

34  : “There exists…” Example:  n  N n = n 2 Translation: “There exists a natural number that is equal to its own square.”  !: “There exists a unique…” Example:  ! n  N n = n 2 - 2 Translation: There exists exactly one element in N that is two less then its own square.

35  : “For all…” Example:  n  N n 2 ≥ n Translation: “For all elements n in N it is true that n 2 ≥ n.”

36 True or False?  n  N  x  x < n and x 2 = n False: Not true for n = 0  n  N  k  N k < n, k 2 = n True: n=4, k=2  n  N  ! k  N k < n, k 2 = n True: For n=4, k=2 is the only such k  ! n  N  k  N k < n, k 2 = n False: Can be made true for both n=4 and n=9

37  : “If/then”, “implies” or “therefore” Example: n  N  n 2  n Translation: “If n is a natural number then n 2 is at least as large as n” or “n  N implies that n 2  n” or “n  N therefore n 2  n”

38  : “if and only if” or “iff” Meaning: a  b equivalent to “a  b and b  a” Example:  n  N : n  E  n/2  N Translation: “For all natural numbers n: n is even if and only if n/2 is a natural number.” or “For all natural numbers n: n is even iff n/2 is a natural number.”

39

40 Pseudocode is a way of writing out algorithms in “simplified code” (on paper only) Skips the niggling details of an actual program in Java/C/ C++/Perl/Python/ Whatever Should be a easy read by you or anyone else Should be easy to convert it to code (by you or someone else) – the missing details should be obvious

41 We can think of pseudocode as a “basic” language that supports the minimum needed amount of functionality Rules: Should be easy to read Should not be bogged down in details Each line should be easily implemented

42 Sample Pseudocode: Bubble Sort BubbleSort(Array A, int n) 1.for i  0 to n-1 2.for j  0 to n-1-i 3.if A[j] < A[j+1] 4.swap(A[j], A[j+1]) Parameters type declarations help with clarity, but are not required Assignment operator simple enough that its not worth writing out

43 FindMax(Array A, int n) 1.max  -  2.for i  0 to n-1 3.if A[i] > max 4.max = A[i] 5.return max We will assume we have a built in  value.

44 In this course we will be writing algorithms, not programs You will describe them with pseudocode Must be clear Should convey sufficient detail for implementation Should not assume existence of complicated functions unless we have done them in class


Download ppt "The material in this lecture should be review. If you have not seen it before, you should be able to learn it quickly – and on your own. So we are going."

Similar presentations


Ads by Google