Docsity

Randomized Algorithms: QuickSort, Motif Finding with RandomizedQuickSort - Prof. Wei Wang, Study notes of Computer Science

Toggle sidebar
bg1
11/20/2008 Comp 590/Comp 790-90 Fall 2008 1
Lecture 24:
Lecture 24:
Randomized Algorithms
Randomized Algorithms
Chapter 12
Chapter 12
11/20/2008 Comp 590/Comp 790-90 Fall 2008 2
Randomized Algorithms
Randomized Algorithms
•Randomized algorithms incorporate random,
rather than deterministic, decisions
•Commonly used in situations where no exact
and/or fast algorithm is known
•Main advantage is that no input can reliably
produce worst-case results because the
algorithm runs differently each time.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f

Partial preview of the text

Download Randomized Algorithms: QuickSort, Motif Finding with RandomizedQuickSort - Prof. Wei Wang and more Study notes Computer Science in PDF only on Docsity!

11/20/2008 Comp 590/Comp 790-90 Fall 2008 1

Lecture 24:Lecture 24:

Randomized AlgorithmsRandomized Algorithms

Chapter 12Chapter 12

Randomized Algorithms Randomized Algorithms

• Randomized algorithms incorporate random,

rather than deterministic, decisions

• Commonly used in situations where no exact

and/or fast algorithm is known

• Main advantage is that no input can reliably

produce worst-case results because the

algorithm runs differently each time.

11/20/2008 Comp 590/Comp 790-90 Fall 2008 3

QuickSort QuickSort

  • QuickSort is a simple approach to sorting:
    • Select an element m from unsorted array c and divide

the array into two subarrays:

c small - elements smaller than m and

c large - elements larger than m.

  • Recursively quicksort the subarrays and

combine them together into the c sorted

Example of QuickSort Example ofQuickSort

Given an array: c = { 5, 2, 8, 4, 3, 1, 7, 6, 9 }

Step 1: Choose the first element as m

c = { 6, 3, 2, 8, 4, 5, 1, 7, 0, 9 }

Our Selection

11/20/2008 Comp 590/Comp 790-90 Fall 2008 7

Example of QuickSort Example ofQuickSort (cont’(cont’d)d)

Step 4: Combine the two arrays with m working

back out of the recursion and as we build

together the sorted array.

C small = { 0, 1, 2, 3, 4, 5 } C large = { 7, 8, 9 }
{ 0, 1 } 2 { empty }
{ empty } 4 { 5 }
{ 0 } 1 { empty }

Example of QuickSort Example ofQuickSort (cont’(cont’d)d)

Finally we can assemble c small and c large with our

original choice of m , creating the sorted array.

C small = { 0, 1, 2, 3, 4, 5 } m = 6 C large = { 7, 8, 9 }
C sorted = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }

11/20/2008 Comp 590/Comp 790-90 Fall 2008 9

The QuickSort Algorithm The QuickSort Algorithm

  1. QuickSort(c)
  2. if c consists of a single element
  3. return c

4. m Å c 1

5. Determine the set of elements csmall smaller thanm

6. Determine the set of elements clarge larger thanm

  1. QuickSort(csmall)
  2. QuickSort(clarge )

9. Combine csmall,m, and clarge into a single array, csorted

  1. return csorted

QuickSort Analysis: Optimistic Outlook QuickSort Analysis: Optimistic Outlook

  • Runtime depends on our selection of m :

-A good selection will split c evenly such that

| c small | = | c large |, then the runtime is O( n log n ).

-For a good selection, the recurrence relation is:

T ( n ) = 2T ( n /2) + α · n

Time it takes to split the

array into 2 parts where

const is a positive constant

The time it takes to

sort two smaller

arrays of size n /

Same recurrence as Merge Sort

11/20/2008 Comp 590/Comp 790-90 Fall 2008 13

A Randomized Approach A Randomized Approach

  • To improve QuickSort, randomly select m.
  • Since half of the elements will be good splitters,

if we choose m at random we will get a 50%

chance that m will be a good choice.

  • This approach will make sure that no matter

what input is received, the expected running

time is small.

The RandomizedQuickSort Algorithm The RandomizedQuickSort Algorithm

  1. RandomizedQuickSort(c)
  2. if c consists of a single element
  3. return c

4. Choose elementm uniformly at random from c

5. Determine the set of elements csmall smaller thanm

6. Determine the set of elements clarge larger thanm

  1. RandomizedQuickSort(csmall)
  2. RandomizedQuickSort(clarge )

9. Combine csmall,m, and clarge into a single array, csorted

  1. return csorted

*Lines Highlighted in red indicate the differences between QuickSort and RandomizedQuickSort

11/20/2008 Comp 590/Comp 790-90 Fall 2008 15

RandomizedQuickSort Analysis RandomizedQuickSort Analysis

  • Worst case runtime: O( n^2 )
  • Expected runtime : O( n log n ).
  • Expected runtime is a good measure of the

performance of randomized algorithms, often

more informative than worst case runtimes.

  • RandomizedQuickSort will always return the

correct answer, which offers a way to classify

Randomized Algorithms.

Two Types of Randomized Algorithms Two Types of Randomized Algorithms

  • Las Vegas Algorithms – always produce the

correct solution (ie. RandomizedQuickSort)

  • Monte Carlo Algorithms – do not always return

the correct solution.

  • Las Vegas Algorithms are always preferred, but

they are often hard to come by.

11/20/2008 Comp 590/Comp 790-90 Fall 2008 19

Profiles Revisited Profiles Revisited

  • Let s =( s 1 ,..., s t) be the set of starting positions for

l- mers in our t sequences.

  • The substrings corresponding to these starting

positions will form:

  • t x l alignment matrix and
  • 4 x l profile matrix * P.
*We make a special note that the profile matrix will be defined in terms of the
frequency of letters, and not as the count of letters.
  • Prob ( a | P ) is defined as the probability that an l -

mer a was created by the Profile P.

  • If a is very similar to the consensus string of P

then Prob ( a | P ) will be high

  • If a is very different, then Prob ( a | P ) will be low.

n

Prob ( a | P ) =Π p a i

, (^) i

i=

Scoring Strings with a Profile Scoring Strings with a Profile

11/20/2008 Comp 590/Comp 790-90 Fall 2008 21

Scoring Strings with a Profile Scoring Strings with a Profile (cont’(cont’d)d)

Given a profile: P =

G 1/4 0 1/8 3/8 1/4 1/

T 1/8 1/8 0 0 1/4 7/

C 1/8 0 1/2 5/8 3/8 0

A 1/2 7/8 3/8 0 1/8 0

Prob ( aaacct | P ) = ???

The probability of the consensus string:

Scoring Strings with a Profile Scoring Strings with a Profile (cont’(cont’d)d)

Given a profile: P =

G 1/4 0 1/8 3/8 1/4 1/

T 1/8 1/8 0 0 1/4 7/

C 1/8 0 1/2 5/8 3/8 0

A 1/2 7/8 3/8 0 1/8 0

Prob ( aaacct | P ) = 1/2 x 7/8 x 3/8 x 5/8 x 3/8 x 7/8 =.

The probability of the consensus string:

11/20/2008 Comp 590/Comp 790-90 Fall 2008 25

Third try: c t a t a a a c c t t a c a t c

Second try: c t a t a a a c c t t a c a t c

First try: c t a t a a a c c t t a c a t c

P- P-Most ProbableMost Probable ll -mer-mer (cont’(cont’d)d)

G 1/4 0 1/8 3/8 1/4 1/

T 1/8 1/8 0 0 1/4 7/

C 1/8 0 1/2 5/8 3/8 0

A 1/2 7/8 3/8 0 1/8 0

Find the Prob ( a | P ) of every possible 6-mer:

-Continue this process to evaluate every possible 6-mer

P- P-Most ProbableMost Probable ll -mer-mer (cont’(cont’d)d)

ctataaaccttacat 1/8 x 1/8 x 3/8 x 5/8 x 1/8 x 7/8.

ctataaaccttacat 1/8 x 1/8 x 0 x 0 x 3/8 x 0 0

ctataaaccttacat 1/8 x 0 x 0 x 0 x 0 x 1/8 x 0 0

ctataaaccttacat 1/2 x 0 x 1/2 x 0 1/4 x 0 0

ctataaaccttacat 1/2 x 7/8 x 1/2 x 5/8 x 1/4 x 7/8.

ctataaaccttacat 1/2 x 7/8 x 3/8 x 5/8 x 3/8 x 7/8.

ctataaaccttacat 1/8 x 7/8 x 3/8 x 0 x 3/8 x 0 0

ctataaaccttacat 1/2 x 1/8 x 3/8 x 0 x 1/8 x 0 0

ctataaaccttacat 1/2 x 7/8 x 0 x 0 x 1/8 x 0 0

ctataaaccttacat 1/8 x 1/8 x 3/8 x 0 x 1/8 x 0 0

String, Highlighted in Red Calculations prob ( a | P )

Compute prob ( a | P ) for every possible 6-mer:

11/20/2008 Comp 590/Comp 790-90 Fall 2008 27

P- P-Most ProbableMost Probable ll -mer-mer (cont’(cont’d)d)

ctataaaccttacat 1/8 x 1/8 x 3/8 x 5/8 x 1/8 x 7/8.

ctataaaccttacat 1/8 x 1/8 x 0 x 0 x 3/8 x 0 0

ctataaaccttacat 1/8 x 0 x 0 x 0 x 0 x 1/8 x 0 0

ctataaaccttacat 1/2 x 0 x 1/2 x 0 1/4 x 0 0

ctataaaccttacat 1/2 x 7/8 x 1/2 x 5/8 x 1/4 x 7/8.

ctataaaccttacat 1/2 x 7/8 x 3/8 x 5/8 x 3/8 x 7/8.

ctataaaccttacat 1/8 x 7/8 x 3/8 x 0 x 3/8 x 0 0

ctataaaccttacat 1/2 x 1/8 x 3/8 x 0 x 1/8 x 0 0

ctataaaccttacat 1/2 x 7/8 x 0 x 0 x 1/8 x 0 0

ctataaaccttacat 1/8 x 1/8 x 3/8 x 0 x 1/8 x 0 0

String, Highlighted in Red Calculations Prob ( a | P )

P-Most Probable 6-mer in the sequence is aaacct:

P- P-Most ProbableMost Probable ll -mer-mer (cont’(cont’d)d)

ctataaaccttacatc

because Prob ( aaacct | P ) = .0336 is greater

than the Prob ( a | P ) of any other 6-mer in the

sequence.

aaacct is the P -most probable 6-mer in:

11/20/2008 Comp 590/Comp 790-90 Fall 2008 31

P- P-Most ProbableMost Probable ll -mers-mers in Manyin Many

SequencesSequences (^) (cont(cont’’d)d)

ctataaacgttacatc

atagcgattcgactg

cagcccagaaccct

cggtgaaccttacatc

tgcattcaatagctta

tgtcctgtccactcac

ctccaaatcctttaca

ggtctacctttatcct

P -Most Probable l -mers form a new profile

G 2/8 0 0 2/8 1/8 2/
T 1/8 3/8 0 0 3/8 6/
C 0 0 4/8 6/8 4/8 0
A 5/8 5/8 4/8 0 0 0

8 t a c c t t

7 a t c c t t

6 g a c c t g

5 a t a g c t

4 g a a c c t

3 a a c c c t

2 a t a g c g

1 a a a c g t

Comparing New and Old Profiles Comparing New and Old Profiles

Red – frequency increased, Blue – frequency decreased

G 2/8 0 0 2/8 1/8 2/
T 1/8 3/8 0 0 3/8 6/
C 0 0 4/8 6/8 4/8 0
A 5/8 5/8 4/8 0 0 0

8 t a c c t t

7 a t c c t t

6 g a c c t g

5 a t a g c t

4 g a a c c t

3 a a c c c t

2 a t a g c g

1 a a a c g t

G 1/4^0 1/8 3/8 1/4^ 1/
T 1/8 1/8^00 1/4 7/
C 1/8^0 1/2 5/8 3/8^0
A 1/2 7/8 3/8^0 1/8^0

11/20/2008 Comp 590/Comp 790-90 Fall 2008 33

Greedy Profile Motif Search Greedy Profile Motif Search

Use P-Most probable l- mers to adjust start positions until

we reach a “best” profile; this is the motif.

1) Select random starting positions.

2) Create a profile P from the substrings at these starting

positions.

3) Find the P -most probable l -mer a in each sequence and

change the starting position to the starting position of a.

4) Compute a new profile based on the new starting

positions after each iteration and proceed until we

cannot increase the score anymore.

GreedyProfileMotifSearch Algorithm GreedyProfileMotifSearchAlgorithm

  1. GreedyProfileMotifSearch(DNA,t, n, l )
  2. Randomly select starting positions s=(s 1 ,…,s (^) t) fromDNA
  3. bestScore Å 0
  4. while Score(s,DNA)> bestScore
  5. Form profile P from s
  6. bestScore Å Score(s,DNA)
  7. for i Å 1 to t
  8. Find a P-most probablel-mer a from thei th^ sequence
  9. s (^) i Å starting position of a
  10. returnbestScore

11/20/2008 Comp 590/Comp 790-90 Fall 2008 37

How Gibbs Sampling Works How Gibbs Sampling Works

1) Randomly choose starting positions

s = ( s 1 ,..., s t) and form the set of l -mers associated

with these starting positions.

2) Randomly choose one of the t sequences.

3) Create a profile P from the other t -1 sequences.

4) For each position in the removed sequence,

calculate the probability that the l -mer starting at

that position was generated by P.

5) Choose a new starting position for the removed

sequence at random based on the probabilities

calculated in step 4.

6) Repeat steps 2-5 until there is no improvement

Gibbs Sampling: an Example Gibbs Sampling: an Example

Input :

t = 5 sequences, motif length l = 8

1. GTAAACAATATTTATAGC

2. AAAATTTACCTCGCAAGG

3. CCGTACTGTCAAGCGTGG

4. TGAGTAAACGACGTCCCA

5. TACTTAACACCCTGTCAA

11/20/2008 Comp 590/Comp 790-90 Fall 2008 39

Gibbs Sampling: an Example Gibbs Sampling: an Example

  1. Randomly choose starting positions,

s =(s 1 ,s 2 ,s 3 ,s 4 ,s 5 ) in the 5 sequences:

s 1 =7 GTAAACAATATTTATAGC

s 2 =11 AAAATTTACCTTAGAAGG

s 3 =9 CCGTACTGTCAAGCGTGG

s 4 =4 TGAGTAAACGACGTCCCA

s 5 =1 TACTTAACACCCTGTCAA

Gibbs Sampling: an Example Gibbs Sampling: an Example

  1. Choose one of the sequences at random:

Sequence 2: AAAATTTACCTTAGAAGG

s 1 =7 GTAAACAATATTTATAGC

s 2 =11 AAAATTTACCTTAGAAGG

s 3 =9 CCGTACTGTCAAGCGTGG

s 4 =4 TGAGTAAACGACGTCCCA

s 5 =1 TACTTAACACCCTGTCAA