Download review.basic_tools

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Approximations of π wikipedia , lookup

Location arithmetic wikipedia , lookup

Addition wikipedia , lookup

Positional notation wikipedia , lookup

Elementary mathematics wikipedia , lookup

Transcript
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!!!
Recall how decimal representation works.
2037
2x103 + 0x102 + 3x101 + 7x100
Binary numbers are the same –
just use 2 instead of 10
(hence the name “base 2”)
1011
1x23 + 0x22 + 1x21 + 1x20
=1110
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 (210)
1. Find the largest k such
that 2k ≤ n.
Steps for converting
n to binary
2. Make the kth digit a 1.
3. Compute the binary
value for n – 2k.
(Stop when n = 2k.)
Convert 22 to binary:
1. Find largest k such that 2k ≤ 22
k = 4, 2k = 16, n’ = n-16 = 6
Number is: 1XXXX, where XXXX is the binary representation of 6
2. Find the largest k such that 2k ≤ 6
k = 2, 2k = 4, n’’ = n’-4 = 2
3.
Number is: 101XX, where XX is the binary representation of 2
Find the largest k such that 2k ≤ 2
k=1, 2k = 1, n’’’ = n’’ – 2 = 0
Number is: 10110 – done!
Logarithms
Question: What is the meaning of logba?
Answer: The exponent of b that results in
a.
In other words: the value x such that bx =
a.
log2 16 = 4
log3 9 = 2
log44 = 1
log51 = 0
8
log77 = 8
6 log612 =12
Floor function: x or floor(x)
The largest integer n such that n ≤ x (4.7 =4)
Question: What is the “meaning” of log2n?
It is the largest integer k such that 2k ≤ n.
Question: When converting decimal to binary,
what is the first power of 2 we will use?
log2n
Question: How many bits do we need to store
the number n?
log2n+1
How many digits does n have in base 10?
log10n+1
How many digits does n have in base b?
logbn+1
How do we solve for n when we know:
2n = h?
2n = h
log22n = log2h
n = log2h
Solve: 16n = 1024
16n =1024
log1616n = log161024
n = log161024
n = log21024 / log216
n = 10 / 4
n = 2.5
Not
necessary,
but makes
life easier
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?
How many people do we have after each step?
Initially (after “step 0”): n = n/20 people left.
After step 1: (1/2)(n/20) = n/21 people left
After step 2: (1/2)(n/21) = n/4 = n/22 left
After step 3: (1/2)(n/22) = n/8 = n/23 left
…
After step i: n/2i people left
When does n/2i = 1?
When n = 2i, or i = log2 n
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?
log2 512 = 9 rounds
Graph: A set of nodes V and a multi-set E VV.
V={v1, v2, v3, v4, v5}
v1
v2
E = {(v1, v2), (v1, v3),
(v1, v4), (v2, v3), (v1, v3),
(v3, v5), (v4, v5), (v5, v5)}
v3
v4
v5
Simple Graph: A set of nodes V and a
multi-set E VV such that (v,v)E for any v
V
V={v1, v2, v3, v4, v5}
v1
v2
E = { (v1, v2), (v1, v3),
(v1, v4), (v2, v3), (v1, v3),
(v3, v5), (v4, v5), (v5, v5)}
v3
v4
v5
In this course: all graphs will be simple –
we will not bother saying it
Simple Graph: A set of nodes V and a set E
VV such that (v,v)E for any v  V
V={v1, v2, v3, v4, v5}
v1
v2
E = {(v1, v2), (v1, v3),
(v1, v4), (v2, v3), (v3, v5),
(v4, v5)}
v3
v4
v5
Nodes u and v are adjacent if they share an
edge (e.g. if (u,v)  E)
Correct: “v1 and
v3 are adjacent”
v1
v2
Incorrect: “v1 and
v5 are adjacent”
v3
v4
v5
A path from u to v is a sequence of adjacent
nodes starting at u and ending at v
Sample path:
v 1 , v 2 , v3 , v 5
v1
v2
v3
v4
v5
A cycle is a path from a node to itself using
at least one edge.
v1
v2
v3
v4
v5
A graph is connected if there is a path
between every pair of nodes
Two connected components
NotStill
connected
Connected
connected
v1
v2
v3
v4
v5
A graph is a acyclic if it has no cycles.
A graph is a tree if it is connected and acyclic.
v1
v2
v3
v4
Not
A
tree.
acyclic.
Acyclic,
but
not a tree.
v5
Make sure you
can define
these terms
(Simple) Graph
Adjacent nodes
Path
Cycle
Connected
Connected Component
Acyclic
Tree
Proof: A logic-based argument that a
given fact is true.
Does not have to
contain algebra
or complicated
mathematical
terminology
Must be clear and
easily understood.
Should primarily
consist of
straight-forward
English
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.
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
: “There exists…”
Example:  nN n = n2
Translation: “There exists a natural
number that is equal to its own square.”
!: “There exists a unique…”
Example: ! nN n = n2 - 2
Translation: There exists exactly one
element in N that is two less then its
own square.
: “For all…”
Example: nN n2 ≥ n
Translation: “For all elements n in N it is
true that n2 ≥ n.”
True or False?
nN x x < n and x2 = n
False: Not true for n = 0
 nN kN k < n, k2 = n
True: n=4, k=2
 nN ! kN k < n, k2 = n
True: For n=4, k=2 is the only such k
! nN kN k < n , k2 = n
False: Can be made true for both n=4 and n=9
: “If/then”, “implies” or “therefore”
Example: n  N  n2  n
Translation: “If n is a natural number
then n2 is at least as large as n”
or
“n  N implies that n2  n”
or
“n  N therefore n2  n”
: “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.”
Pseudocode is a
way of writing out
algorithms in
“simplified code”
(on paper only)
Should be easy to
convert it to code
(by you or someone
else) – the missing
details should be
obvious
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
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
Sample Pseudocode: Bubble Sort
Parameters type declarations help
with clarity, but are not required
Assignment operator
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])
simple enough that its not
worth writing out
We will assume we have
a built in  value.
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
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