0% found this document useful (0 votes)
71 views

Data Structures and Algorithms 2

1. The document discusses various algorithms and data structures including algorithms for addition, multiplication, division, modular arithmetic, greatest common divisor, and Fibonacci numbers. 2. Key algorithms covered are multiplication which takes O(n^2) time, division which takes O(n^2) time, and modular exponentiation which takes O(n^3) time where n is the number of bits. 3. The greatest common divisor algorithm, Euclid's algorithm, finds the greatest divisor that divides two numbers and takes O(log n) time where n is the number of digits.

Uploaded by

espyter
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views

Data Structures and Algorithms 2

1. The document discusses various algorithms and data structures including algorithms for addition, multiplication, division, modular arithmetic, greatest common divisor, and Fibonacci numbers. 2. Key algorithms covered are multiplication which takes O(n^2) time, division which takes O(n^2) time, and modular exponentiation which takes O(n^3) time where n is the number of bits. 3. The greatest common divisor algorithm, Euclid's algorithm, finds the greatest divisor that divides two numbers and takes O(log n) time where n is the number of digits.

Uploaded by

espyter
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Data Structures and Algorithms

Wednesday, August 27, 2008


8:59 AM
www.cs.wfu.edu/~pauca
Need textbook
Read Chapter 0
HW 1 due 9/4 next Thursday
8 0.1 b d f g I k l m
9 0.2 a b c
6 0.3 a b c
What is an algorithm?
Unambiguous sequence of steps to a particular problem in a finite time
 
Fibonacci numbers:
F0=1
F1=1
Fn=Fn-1+Fn-2
"Fib(n)
if n==0 || n==1
return 1;
else
return fib(n-1)+fib(n-2)"
Exponential T(n)= 2^n
Can we do better? .
"fib2(n)
if n==0 || n==1
return 1;
f[0]=1;f[1]=0;
for i=2:n
f[i]=f[i-1]+f[i-2]
end
return f[n]".
T(n) for n<=1, T(n)<=2
For n>1, T(n)<=4+n-2
Therefore T(n)O(n) big O
Big O notation

Notation Name Intuition As

, eventually...

Big f is bounded (f(n)/g(n))<infinity, aka is a


Omicro above by g constant
n; Big (up to
O; Big constant
Oh factor)
asymptoticall
y
Big f is bounded (f(n)/g(n))=infinity
Omega below by g
(up to
Ω constant
factor)
asymptoticall
y

Big f is bounded f(n)=g(n)k


Theta both above (f(n)/g(n))=not 0 or infinity,
and below by constant
θ g
asymptoticall
y
 

Small f is
Omicro dominated
n; Small by g
O; asymptoticall
Small y
Oh
properties
1. f(n)є O(log n + (logn)3 +n^2+2n^3) …є O(n3)
2. O(cn) is exponential for all c>1
3. O(log(nc)) =O(clogn)=O(logn)/=/ )((logn)c)
Product
f1є O(g1), f2є O(g2) =>f1*f2є O(g1*g2)
f*O(g)=>O(f*g)
Sum
f1є O(g1), f2є O(g2) =>f1+f2є O(g1+g2)
 
Multiply by a constant
If k>=0 is a constant
O(k*g)=O(g)
fє O(g)=>kfє O(g)
 
f(n)=100n+log(n)
g(n)=n+(log(n))2
Same order,θ or big O both ways
 
f(n)=log2n
g(n)=log3n
f(n)=log2+logn
g(n)=log3+logn
Lim->infinity: constant/constant, theta
 
f(n)= (n2/logn)
g(n)=n(logn)2
Lim->infinity: f(n)/g(n) = …n/logn = inifinity fΩg
 
f(n)=(logn)logn
g(n)= n/logn
Lim->infinity f/g = logn(lognlogn) / n = lognlogn+1/n =
Use C and N method
 
 
 
 
"".
 
Chapter 2
Wednesday, August 27, 2008
9:00 AM
 
Read chapter 2
Algorithms with numbers
Addition x+y
T(n) where n is the number of digits in x+y
x=10111011
y=11001011
Addition is O(n), n steps with n+1 maximum digits
Optimal
 
Product
x*y with n bits/digits
n2 steps: n additions to do and each is O(n), n*n
2n+1 digits
z=x*y
Let y be odd => y = 2k+1
z=x*y=x*2k+x=2x*floor(y/2)+x
Let y be even=> y=2k, k=floor(y/2)
z=xy=2xfloor(y/2)
 
For any y*x
If(y==0) return 0;
z=multiply(x,floor(y/2))
If y is odd
return 2*z+x
Else
return 2z
 
Complexity
Cost function T(n):
O(n) recursive calls
Each recursive call takes O(n)+O(1) which is on the order of (n)
Therefore multiplying is on order of (n2)
Division
x/y
Find 2 numbers q,r such that x=y*q+r where 0<=r<y
If x=q*y then x|y (x divides y)
Consider floor(x/2) = yq2 +r2 holds for some q2 where 0<=r2<y
If x is even, x=2k, k = floor(x/2)
x=2k=2floor(x/2)=2yq2+r2=y(2q2)+(2r2)
If 2r2>=y then r = 2r2-y q=q2+1
If x is odd, x=2k+1=2floor(x/2)+1=2yq2+2r2+1 = y(2q2) + (2r2+1)
example
8/3
8=3q+r
Floor (8/2) = 4
4=3q1+r1
q=1 r1
2=3q2+r2
q=0 r=2
1=3q3+r3
q=0 r=1 ^
0=3q4+r4
Q4=0 r4=0 send back up
Q3 is 2*q4, = 0, r3 =2r4 , =
Algorithm
"divide(x,y)
if x==o:return(q,r) = 0
(q,r)=divide(floor(x/2),y)
q=2q, r=2r
if x is odd: r=r+1
if r>=y: r=r-y,q=q+1
return q,r".
Complexity n2
 
 
Modular Arithmatic
Hardware allows 32 bit, 64 bit operations
.1e18=0.1*1018
0.1 called mantissa, 18 called exponent
A double in C++ can store about 12-14 digits as a base
Numerical error is unacceptable for many applications such as encryption
Need large numbers, 512 bit #s
We will express all numbers modulo N, %n, the remainder
x=Nq+r
x%N = r = x-Nq
Equivalence classes
Let N=4
0 4 8

1 5 9

2 6 10

3 7 11
{i+kn=k in Z}i=0:N-1
 
Two #s x s y divided by N
Have the same remainder
x%N = r = y%N
x=y are congruent
Example
24%10 = 14%10 = -6%10
Theorem
X = Y %N if N|(x-y) N divides x-y
Prove theorem
Substitution rule
If x= x' %N and y=y'%N
Then x+y=y'+x' % N
Example
x=43211
y=79254
N=4
x+y%N
x+y=122465%4
=1
x%N = 4*10802+3
y%N = 4*19813+2
x'=3
y'=2
x'+y' %N = 1
Cost of arithmetic operations modulo N
1. Modular addition x+y %N
Assume x and y are #s between 0 and N-1
Then x+y in[0,2N-2]
x+y could be larger than N
If (x+y) >= N then x+y%N = x+y-N
O(n2) where n is # of bits
Modular arithmatic facts
1. x%n=r
x=nq+r
2. x=y % n if n|(x-y)
congruent
3. Substitution rule
If x=x' %n and y=y' %n
Then x+y = x'+y'%n
xy=xy%n
Modular addition x+y%n
x,y < n
1. Regular addition x+y
1.Order nbits
2. Divide by n=> q and r
1.Order n2bits
3. x+y=nq+r
4. Return r
Complexity
Worst case x and y are n-1
x+y will be roughly < 2n
Will be O(n2) where n is bits
Modular multiplication xy%N
x and y are <N
1. Regular multiplication xy N2 = 2logN=2n bits => O(n2)
2. Divide by N
=>O(n2)
3. Return r
Total, O(n2+n2)
 
Modular exponentiation xy%n
x and y <N
If x and y have 10 bits
First idea, exponent first
Xy=(210)2^10=210240
Nope, too many
Idea 2: perform all computations %N
xx=x2%N
x,x2,x3…xy%N
y=~2m bits so complexity is =>O(n22m)
Idea 3
x2, x2^2,x2^3,….x2^m
Taking O(m) products and each product takes O(n2)=> modular
exponentiation takes O(n3) time
What if y = 25? Then break it down to be 24+23+20 ipso facto x16 x8 x1
Modular exponentiation xy%N
0<=x,y<N
 
Xy= if y is even: (xfloor(y/2))2
If y is odd:(xfloor(y/2))2
Modexp(x,y,N)
If y==0
Return 1
z=modexp(x,floor(y/2), N)
If y is even
Return Z2 %N O(N2)
Else
Return z2x %N O(N2)
 
Complexity, worst case
O(N) recursive calls
At each recursion, O(N2)
Therefore the algorithm takes O(N3)
 

Greatest common divisor


If d divides a exactly, d|a, and d divides b exactly, d|b,
If g=gcd(a,b) then d<=g
Euclid's rule
If a and b are integers, and a>b, then gcd(a,b) =gcd(a%b, b)
Gcd(a,b)
a>b
If b==0 //gcd(a,0)
Return a
Return (gcd(b,a%b)
 
Example
Gcd(64,21)
Gcd(21,1)
Gcd(1,0)
Return 1
Lemma
If d|a and d|b and d=ax+by for some integers X and y then d is the gcd(a,b)
Extended euclid algorithm to find d,x,y (d=ax+by)
x,y,d= extended euclid(a,b)
//precondition a>b
If b==0
Return [1,0,a]
[x',y', d']=extendedeuclid(b,a%b);
Return [y ',x'-floor(a/b)*y ', d]
O(n3)
 
Example
Down Up

[x,y,d] = extended euclid(45,27) -1,2,9

[x',y',d'] = extended euclid(27,18) 1,-1,9

[x',y',d'] = extended euclid(18,9) 0,1,9

[x',y',d'] = extended euclid(9,0) 1,0,9


Modular division x/y(mod N)
a/b = a*(1/b)
b*b-1= 1
In modular division
b-1 or x, the inverse is the scalar that when multiplied by b, and then modded by N, =1
b*x=1
Use the x, to multiply a to make it a*x %N =remainder of 1
X is called the inverse of a, a-1
 
Division a/b (mod N) expressed as a product a*b-1
Find b-1 (use extended Euclid)
Compute a*b-1 (mod N) use modular multiplication
In modular arithmetic division
The inverse of a(mod N): x=a-1+: a*x %N=1
And x sort of has to be less than N, if it exists
There are numbers that don’t have an inverse?
a=4, N=10
If they both are even, then they don't have an inverse
Proof
a& N are even, then a-1 doesn’t exist
a=2i for all i
N=2j for all j
Remainder = ax%N=ax-Nq
=2i*x-2j*q
=2(ix-jq)
Always even
For 2#s a and N if d=gcd(a,N)=1, then a-1 exists
Conversely, if it does not = 1, then a-1 does not exist
Proof
Since d = gcd(a,N)Assume a=d*q1 for some q1
N=d*q2 for some q2
ax(mod N) = ax-Nq
=d*q1*x-d*q2*q
=d(q1*x-q2*q)
=d(q1*x-q2*q) can't =1 if d doesn’t = 1 for all x
If d=1=g(a,N) then _x such that (q1*x-q2*q)=1
To compute a-1 mod N
1. [a,y,d] =extended euclid(a,N)
d=ax+Ny and d=gcd(a, N)
2. Check if d==1, then a-1 exists
1=ax+Ny mod N
=ax mod N
3. To divide b/a (mod N)
Multiply(b,x)
Complexity
Extended euclid (a,N) is O(n3)
Multiplication of b*a-1 is O(n2)
Modular division b/a mod N is O(n3+n2)…O(n3)
Primality testing
Fermat's little theorem
If p is prime then for every value 1<=a<p
Then ap-1=1 %p
Example
p=5
For a = 1, 1^4 = 1 , which mod p is = 1
a=2, 2^4 = 16 % p = 1
a=3, 3^4 = 81 % p = 1
a=4, 4^4 = 256 % p = 1
Check
Proof
Let p=5, s={1,2,…p-1}
s={1,2,3,4}
1*1 mod p = 1
2*1 mod p = 2
3*1 mod p = 3
4*1 mod p = 4
Multiply all numbers in s{} by one number in s{} and mod P, it will be mapped
to the same set of mods
Say you pick
1*3 mod p = 3
2*3 mod p = 1
3*3 mod p = 4
4*3 mod p = 2
4!*3^4= 4!(mod p)Can be simplified
3^4=1 mod 5
 
But we can only do this if 4! And 5 are relatively prime
(p-1)! And p are relatively prime
We already know p is prime, so the only options for gcd(p-1! And p) can only
be p or 1
Since the left is p-1! There's no p in it, therefore the gcd is 1 and they are
relatively prime
If p is prime, then for all 1<=a<p, ap-1=1 mod p
Also, if p isn't prime, it's still ok if a is relatively prime and <p
There are numbers that pass fermat's test but are not prime
Carmichael #s
For any composite number that is not prime or a Carmichael #, there must be some
value a, 1<=a<p such that aN-1 mod p DOES NOT = 1
Primality
Primality (n)
"//return true or false
Choose a in [1,N-1] randomly
If aN-1%N = 1
Return true //N is prime
Else
Return false //N is not prime".
This program is rather shitty since it only tries 1 number
Lemma
If an-1%n does not = 1, for some a relatively prime to n, then an-1%n does not = 1
for at least half the choices of a<n
proof
s{1,2,3,n-1}
Pick aN-1 that doesn’t equal 1 % N and is relatively prime with N. pick all b in S
such that bN-1 = 1 %N
b=> c =a*b %N
cN-1 =aN-1 bN-1 %N=aN-1 %N DOES NOT = 1
C1=c2
=ab1=ab2 /a
b1=b2
N=15
All mod 9:
1^8=1
2^8=4
3^8=0
4^8=7
5^8=7
6^8=0
7^8=4
8^8=1
Proof
Consider some a such that an-1mod N doesn’t = 1
Now for all b <N st that bn-1 mod n =1
c=a*b
cN-1=(a*b)N-1 %N
=aN-1 bN-1 %N
=aN-1 (1) %N
=aN-1
There are numbers b and =/ b2 <N, such that b1N-1 %N=1 and b2N-1 %N=1
No proof
If N is prime then aN-1 %N = 1 for all a < N
If N is not prime then aN-1 % N =1 for at most half the values of a<N
For Primality (N)
If N is not prime then probability(primality(n)-true) = 1
If N is not prime then Probability (primality(N)<=1/2
Primality2 (n)
"//return true or false
Choose different values a1, a2,… ak <N randomly
For i= 1…k
If aiN-1%N doesn’t = 1 //use modexp() function
Return false //N is not prime
Return true //N is prime".
 
If N is not prime then probability(primality2(n)-true) = 1
If N is not prime then Probability (primality2(N)<=1/(2k)
For larger values of K, it's going to be pretty accurate
Generating random primes
A random n-bit# has 1/n chance of being prime.
Lugrange prime # then: let pi(x) be the number of primes <= x then pi(x) ~x/(ln(x))
Therefore they are abundant
Strategy
1. Pick a random # N of n bits
2. Run primality2(N)
3. If it returns false, go back to #1
Encryption
Private key encryption
r=101010 key
m=100011 message
q=r(+)m=001001 encoded (+) = exclusive or
q(+)r=100011
 
Public encryption
Publish a public key, e()
Message is x.
Message is sent encoded with public key: e(x)
Private key for decoding
d(e(x))
RSA encryption idea (Rivest Shamir and Adleman)
1. Choose 2 large k-bit primes, p and q
2. Compute N=p*q s=(p-1)(q-1)
3. Choose 1<e <S gcd(e,s)=1 (relatively prime)
1.If we choose e to be prime then they are going to be relatively prime
2.Make e be a small prime and you don't have to test for relatively prime,
4. Compute d such that d*e=1 % S
 
Public key: (N,e)
Private key(N,d)
Sending and receiving encrypted messages
Suppose message is m
c=me%N encrypted message
Transmit c
Receiving
To decrypt, cd%N=> message
Example
1. p=5 q=7
2. N= 77 and S=60
3. e=7
4. d*7=1 Mod 60
[x,y,d]=extendedEuclid(60,7) D=43
[x',y',d']=extended euclid 7,4
…. 4,3 ...

…3,1 1,0-1*1,1

….1,0 Up:1,0,1
The public key is 77,7
Private key is 77,43
Message is 30
Encrypted: c =me(mod N), = 307mod 77
Mod exp: x,y,n
30,7,77 Send up 2, which is answer, 2 =%77

30,3,77 Send up 50 because 30^3%77=50

30,1,77 Send up 30 (30^1)%77 = 30

30,0,77 Send up 1
Proof for RSA encryption
Given (N,e) for some m<n
c=me%modN
Then cd = med mod N
d*e mod S =1
mod (p-1)(q-1) = 1
d*e=1 mod (p-1)
d*e=1 mod q-1
Now consider
Med = mk(p-1)+1 for some k
= mk(p-10)*m mod p
Fermats little theorem
Med= (1)k * m mod p
=m
Med =mk2(q-1)+1 for some k2
=m(q-1)k2m mod q
Fermat's theorem: m(q-1)=1 mod q for some m<q
=m
By chinese remaineder theorem
Med= m mod p*q = m mod N
For sending lab to Pauca
….txt
N \n
e
 
 
Hash tables
Test Material
Open book
Big O notation
Recurrence
RSA encryption
How many ints have modulo 113 inverses
Know algorithms for gcd and extended and complexity of them
 
O(n2) algorithm for x*y nbit #s n = 2k
x=xl2n/2+xr
y=yl 2n/2 + yr
Then
x*y =xlyl2n+(xlyl+xryr)2n/2+yrxr
T(n)<=4T(n/2)+403n
4T(n/2)=42T(n/22)+41*3 n/2

4k-1T(n/2k-1)=4k(n/2k)+4k-13 n/2k-1
Add together
T(n)<=4T(n/2)+403n
4T(n/2)=42T(n/22)+41*3 n/2

4k-1T(n/2k-1)=4k(n/2k)+4k-13 n/2k-1
T(n)=4k+3n sumk-1i=0 4i/2i
=4k+3n (1+2i+22…+2k-1)
=4k+3n (2k-1/1)

4n2-3n
O(n2)
P1=xlyl
P2=xryr
P3=(xl+xr)*(yl+yr)
=(yl+xl+yl+xr+yr+xl+yr+xr)
=p3-p2-p1
3 products 4 additions
T(n)=3k+4n sumk-1i=0 3i/2i

O(nlog2(3))=O(n1.59)
 
 
 
 
"".
This row is an equivalence class
 
Search
Thursday, October 16, 2008
9:36 AM
If the list is ordered length n, binary search
Takes O(logn) time to find a value in the list worst case
 
Sorting
Bubblesort
O(n2)
Mergesort
Split in middle , sort each side, then merge
"mergeSort(a[1…n])
If n>1
Return merge(mergesort(a[1,2…
floor(n/2)]),mergesort(a[floor n/2…n])
Return a
Merge(x[1…k], y[1…l])
If k==0 return y
If l == 0 return x
If x[1]<=y[1]
Return x[1]*merge(x[2…k],y[1…l])
Else
Return y[1]*merge(x[1…k],y[2…l])".
Quick sort
Worst case O(N2)
Average case O(NlogN)
Can we do better than nlogn?
No
Proof
List of length n
Depth is n
# of leaves: n! (# of permutations)
Binary tree
At most 2logn! Leaves
Logn! Comparisons, bounded below by c*n*logn
 
"".
 
 
Chapter 2 Cont'd
Tuesday, October 07, 2008
9:47 AM
 
Accessing rockola
Need to download "putty" and "xming"
Terminal
%Ssh - x rockola.cs.wfu.edu -"username"
[rockola]%vim modular arithmetic.h
 
Chapter 2 Divide and conquer
Basic multiplication: x*y
O(n2) : n max # bits in x & y
Algorithm by Gaus O(n1.59)
Multiplication of complex numbers
Z1 = a+bi i=root(2)
z2=c+di
Z1*z2=(a+bi)(c+di)=ac+adi+bci+bdi2
=(ac-bd)+(ad+bc)i
ad+bc=(a+b)(c+d)-ac-bd
 
4 multiplications
2 additions
Multiplication of real #s x*y
Assume x & y are n - bit #s
n is 2k n=8
x=xL*2n/2 +xR -
y=yL*2n/2 +yR -
x*y=xl*yl*2^n+xlyr2^n/2+xryl2^n/2+xr*yr+xl*yl*2^n+(xl*yr+xr*yl)2^n/2+xr*yr
Write a divide and conquer algorithm to compute x*y
z=multiply(x,y)
"mult(x,y)
If x<=1 or y<= 1
Return x&y
Else
{
Int n = bits(x)
Int xl xr yr yl;
Xl=x>>n/2
Xr=x-xl*2^n/2
Yl=y>>n/2
Yr=y-yl^2n/2
Return mult(xl,yl)<<n + (mult(xl,yr)
+mult(xr,yl))<<n/2+mult(xr,yr)".
 
Complexity
Recurrence equations to find complex.
T(n) represents time for input size n
T (n)=4T(2/2)+3n
T(1)=1

T(n)=…
Medians
How to find the median in the list
7,2,5,25,35
Mean 15
Median 7
Given a list of # s, what is the complexity of finding s
Sort (s) )(nlogn)
Then select the middle one, O(1) to find median
Consider selection problem
Given list S and #k return kth smallest element of s
Selection (s,k)
Choose v randomly from S
Sort S, on split V
Sl contains all S<v
Sv contains all s==v
Sr contains all S>v
s{35,9,9,25,7,10,1,19,14,6}
v=9
Sl{7,1,6}
Sv{9,9}
Sr{35,25,10,19}
If k<=length(Sl)
Return selection(Sl,k)
Else if (k>length(Sl) && k <= length(sl) + length(sv)
Return v //v is now the median or whatever kth element you are looking
for
Else
Return selection (Sr, k-|sl|-|sv|) //k-(sl + sr) because once you've found
k is in the right, and //you're only using sr, so the index #s are (sl + sr)
less than they were before
Worst case
Pick largest or smallest element of s as V, only reducing by 1 each time
T(n)= T(n-1)+O(n)=O(n2)
N recdursive steps
Takes O(n) to split the list
O(n2)
Best case
V is the kth smallest, O(n) on first try
Average case
Length of Sl < 1/4 of n, or Sr <1/4 n
Bad v
Ideally v is the median
Good v
Length of Sl < 3/4 of n, and Sr <3/4 n
1/2 chance of choosing a good v vs a bad v
Let T(n) be expected running time of selection(s,k)
T(n) <=T(3/4 n)
Master theorem
a=1
b=3/4
d=1
d vs logba
1 0
O(nd) = O(n)
nice
Master theorem
Pick an a recursive calls, b factor of list, reducing problem by ,d cost of things back to
gether
d>logba
O(nd)
d=logba
O(ndlogbn)
d<logba
O(nlogba)
The master theorem concerns recurrence relations of the form:

In the application to the analysis of a recursive algorithm, the constants and function take on the
following significance:
 n is the size of the problem.
 a is the number of subproblems in the recursion.
 n/b is the size of each subproblem. (Here it is assumed that all subproblems are essentially the
same size.)
 f (n) or d is the cost of the work done outside the recursive calls, which includes the cost of
dividing the problem and the cost of merging the solutions to the subproblems.
 
Quick Sort
Worst case O(N2)
Average case O(NlogN)
Algorithm
If # of elements in array is 0, do nothing (base case)
Else choose pivot element v in the array
Partition the array S-{v} into two disjoint sets S1 S2
S1={x in S-{v}|x<=v}
S2={x in S-{v}|x>=v}
Return Quicksort (S1 cat v cat S2)

 
Pasted from <http://www.mycsresource.net/articles/image?id=32>
 
Worst case of QuickSort T(N)
Pivot is first element and array is already sorted
T(N) = T(i) + T(N-i-1)+cN
i = S1
j =S2, N-i-1
T(N)=T(N-1)+cN
T(N-1)=T(N-2)+cN
T(N-2)=T(N-3)+cN
… telescoping
T(2)=T(1)+c2
Sum all up and cancel out stuff
T(N) = T(1) +c Σni=2 i
= O(N2)
Best case
Pivot in middle
T(N) = 2T(N/2) +cN
T(N)/N=(2T(N/2))/N + c
T(N)/N = T(N/2)/(N/2) +c
T(N/2)/(N/2)=T(N/4)/(N/4) +c

T(2)/2=T(1)/1+c
Sum up and cancel stuff

T(N)=N+cNlogN
O(NlogN)
 
Linear algebra
Dot product
O(n)
Matrix vector dot product
O(n2)
Matrix-matrix multiplication
O(n3)
Divide and conquer algorithm
Split the matrices into 4 equal quadrants
 

8 recursive steps,
 
and then only 4 additions left to do
 
Complexity is still O(n3), but there's a way to only 8 multiplications and save a little
time
 
Fast Fourier Transform (a,w)
a is an array
n is a power of 2
w nth root of unity
F is an n*n matrix based on wij
Output is b=F*a
Size of input, n
Time will be measured in floating point operations
What is the cost of the divide; what is the cost of the conquer
T(n)=2T(n/2)+4*(n/2)
The 4* is how many operations are in the for loop
T(1) = O(1)
Solve the equation
T(n)=2T(n/2)+4*(n/2)
T(n)=2(2T(n/4)+n)+4*(n/2)
T(n)=4T(n/4)+4(n)
….
2kT(n/2k)+2kn
K = log2n pick k, so that it will create what will reduce it to T(1)
T(n)=2 log2n T(n/2 log2n)+4*log2n*(n/2)
=nT(1)+2nlog2n
=n+nlogn
O(nlogn)
Could have used master theorem
 
 
 
 
"".
 
Do split "in place" without allocating new memory
Split (s&,v)
Create sl contains all S<v
Create Sv contains all s==v
Create Sr contains all S>v
Int indexsl =0
Int indexsv=0+size of sl
Int indexsr= indexsv+size of sv
Copy sl sv and sr into S
Delete sl sv sr
Return index sl, sv, sr
 
Chap 3
Thursday, October 30, 2008
10:29 AM
Graphs
Def: G={V,E} is a graph where V={v1,v2…vn} is a set of vertices in G and
E={(u,v): for all u,v there exists an edge between u and v in G}
|v|:# of vertices in G
|E|: # of edges in G
Undirected graph
G is undirected if (u,v)=(v,u) for all v,u
Directed graph
G is a directed if for any edge e in E, e=(u,v) does not equal (v,u)
Ex:
G={V,E} undirected
V={a b c d e f}
e={(a c) (a d) (b c) (b f) (c e) (d e) (e f)}
A B C
 
D E F
 

 
 

Graph Representation
Adjacency matrices
Put a 1 in the column of the edge that points to an edge in the row
A=(aij)= 1 if (vi vj are in V)
=0 if no edge exists
For an undirected graph, the matrix is symmetric, A=AT
Linked lists
Adjacency lists
e={(a c) (a d) (b c) (b f) (c e) (d e) (e f)}
A ->C >D
B->c>f
C->b->e
d->a>e
e->c>d>f
f->b>e
Use linked lists
|V| Link list (L list)
Given e=(v,m) put e in L list (v)
In an undirected graph, each edge (u,v) appears exactly twice; one in the linked list
for u, the other in v
Storage O(|E|+|V|)
Search: is edge (u,v) in the graph?
Adjacency matrices: O(1)
Linked lists: # elements in L list (u)
Given vertex u, find all vertices in the reach from u
Explore(graph g, vertex u)
//precondition u in V in graph G, u has not been visited
//postcondition returns "list" of all vertices reachable
Create a boolean array list, size is # of edges, visited[e]
Visited[u]=true;
For all edges (u,v)
If visited[v] == false
Explore(G,v)
Complexity
In an undirected graph each edge is visited
Takes O(|E|+|V|)
Depth first search
Given graph G, compute reach-ability for all V in the graph
Use explore
Dfs(G)
For all vertices v
Visited [v] = false
For all v in V
If visited = false
Explore(G,v)
Complexity
Still O(|E|+|V|)
Connectivity in an undirected graph
G is connected if any u and v in V there is a set of edges that connects them
Connected components of G
Subgraph that is internally connected if u in V then v belong to only 1
connected component of G
Use dfs modified
Dfs(G)
For all vertices v
Visited [v] = false
Ccnum[v]=0;
Cc=0;
For all v in V
If visited = false
Cc++
Explore(G,v,cc)
Mark a vertex with a counter at the first time(pre()) you see it and the last
(post()), that way you get a range for each node, say [1-10] then you can easily
tell which nodes are connected to it (any that are between 2 and 9)
Two nodes, the pre and post fall within each other or they are totally disjoint.
Can't overlap
Types of edges
Tree edge, edges traveled during algorithm to find shit
When a tree is made from traversing a graph, cross edge, edges that connect
vertices not in direct ancestor or descendent
Back edge (can be in undirected graphs; any non-tree edges) and edge that points to
a vertex higher up in tree
Forward edge (only directed graphs) and edge that points to a vertex lower down on
the tree
Directed acyclic graph DAG
A directed graph that doesn’t contain cycles
DAGs have no back edges
A graph has a cycle if DFS reveals a back edge
In a DAG, every edge leads to a vertex with lower post #
Every DAG has at least one source and sink node
Strongly connected components
If there is a path to get from one vertex back to itself, then that vertex and any
along the way are SSG
If we run explore on a node u, by the time explore ends, you get everything
reachable from u if u is a sink
Note with highest post # in DFS (G) must be the source
Max post # of source ssg will be > than sink ssg
DFS(Greversed)
Post # for all nodes
Run DFSmod(G)
For all v in V
Visited[v] = false
For all v in V in decreasing post #
If !visited[v]
Explore(G,v) marks nodes with cc
DAG of SSG
Count SSGs as one node, then draw edges that connect them, leaves you with a
DAG,
3.8 in hw, algorithm for pouring
What are verteces and what are edges,
A vertex is a state, so that would be how much is currently in it
The rule allows you only to go to certain other states.
 
Paths in graphs
What is the shortest path between any two nodes in a graph
u->v
Path length # hops
Distance(u,v) length of shortest path between u and v
Can Depth First Search give shortest paths?
Generally no
Mark distance for all nodes from node x as infinity
Then mark x as 0, and start looking at closest nodes, mark dist as 1
Breadth first search(G,start node)
Initialize all to infinity
Dist [s] = 0
q+=[s] //put s into queue
While Q not empty
u=eject(q)
For all (u,v,) in E
If dist (v) == infinite
Dist(v)=dist u+1
Inject (q,v)
Complexity
O(|V|+|E|)
Proof that we get the shortest distance
 
What if edges have lengths associated with them
Have a compare for current stored dist and dist using current path, store the shorter
one and the path used
Dijkstra(G, l, s)
For all u in V
Dist[u] = inf.
Prev[u] = nil
Dist [s] = 0;
H = makequeue(V)
While H not empty
U = delemin(H) //remove node with smallest dist
For all edges (u,v) in E,
If dist(v)>dist(u)+l(u,v)
Dist[v] = dist(u)+l(u,v)
Prev[v] = u
Decrease key(H,V)
}
}
}
 
 
 
 
Chap 5
Tuesday, December 02, 2008
10:20 AM
 
Minimum Spanning Trees
Spanning tree; fully connected graph
Def: given G= {V,E} and weights We
An MST is a tree T=(V,E') E' is contained in E such that weight (T) = sum 0 to # of
elements in E' of We
Least number of edges with lowest weight required to reach all nodes
Kruskal's algorithm
Sort all edges according to weight
Add edge with minimum weight to T without forming a cycle until all vertices are
connected
Complexity O(|V|log|E|)
Prim's algorithm
Edges of T form a single subtree, not a forest
 
Chapter 8
NP complete problem
Search problems
Sorting
Searching
Graphs
The input I to a problem A solution S
Large # of possible solutions given I
We have found polynomial algorithms to solve theses search problems
There is a large set of problems w/o known polynomial time algorithms to solve them
Satisfactionality
Boolean formula SAT
Literals (variables) x,y,z values (true/false)
X v Y clause
z- negation of a literal
Horm formulas
All clauses contain at most one positive literal
Solvabe in polynomial (Nconstant)time using greedy approaches of chapter 5
2SAT
All clauses contain at most two literals
3SAT
Traveling sales person
Return to starting city, travel to all cities under certain budget, only
travel to each city once
Similar to MST but can only leave a node by one path
NP set of all search problems
 

You might also like