Top Banner
םםםםםםםםםם םםםםםםםם םםםםם םםםםם םםםםם םםםםםםםםםם םםםם םםםםם: ם"ם םםם םםםם םםםם ם"ם: םםםם םםםם)םםםםם( םםםםםם םםםםםם םםםםםם2001
92

אלגוריתמים רנדומיים

Jan 03, 2016

Download

Documents

אלגוריתמים רנדומיים. עבודה בקורס ניתוח אלגוריתמים עבור המרצה: ד"ר אלי פורת מוגש ע"י: רוני שטרן ורונית אנקורי (לוריא) אוגוסט 2001. מה פירוש המונח "אלגוריתם רנדומי"?. אלגוריתם רנדומי הינו אלגוריתם המבצע בחירות אקראיות במהלך ביצועו. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: אלגוריתמים רנדומיים

אלגוריתמים רנדומיים

עבודה בקורס ניתוח אלגוריתמים

עבור המרצה: ד"ר אלי פורת

מוגש ע"י: רוני שטרן ורונית אנקורי )לוריא(

2001אוגוסט

Page 2: אלגוריתמים רנדומיים

2

מה פירוש המונח "אלגוריתם ?"רנדומי

אלגוריתם רנדומי הינו אלגוריתם המבצע בחירות אקראיות .במהלך ביצועו

ניקח לדוגמא הטלת מטבע. במטבע תקין, קיים סיכוי של שהמטבע יפול על "עץ", וסיכוי דומה שהמטבע 50%

יפול על "פלי". האלגוריתם שלנו יבצע הטלת מטבע, ועל סמך ההטלה יבצע משהו מסויים. אם קבלנו "עץ" יבצע

.משהו אחד, ואם קבלנו "פלי" יבצע משהו אחרסה"כ נקבל בחירה אקראית כלשהיא שעל פיה נמשיך

.בבצוע

Page 3: אלגוריתמים רנדומיים

3

?איך נראה אלגוריתם רנדומי

אלגוריתם רנדומי יכיל בדרך כלל פקודה :מהסוג

x := outcome of a fair coin toss

Page 4: אלגוריתמים רנדומיים

4

?למה צריך אלגוריתמים רנדומיים

אלגוריתמים מורכבים דורשים בדרך כלל עיבוד איטי על מנת לפתור את הבעיה. אלגוריתמים רנדומיים חוסכים זמן על ידי בחירה אקראית של נקודת ההתחלה, במקום חישוב המקום הטוב ביותר להתחיל בו. כתוצאה מכך,

לאלגוריתמים האלה תהיה הסתברות גדולה יותר להחזיר .תשובה נכונה

בנוסף לכך, חישוב אקראי של נקודת ההתחלה חוסך בזמן .הביצוע של התוכנית

Page 5: אלגוריתמים רנדומיים

5

?מה אם בחרנו לא טוב

אלגוריתם המתוכנן היטב יתן את התשובה הנכונה בהסתברות גבוהה. אם לא, כל שנדרש הוא להריץ את

.האלגוריתם בשנית

יתכן ותדרשנה מספר איטרציות על מנת להבטיח את נכונות התוצאה, אך בדרך כלל זה הרבה יותר מהיר

.מאשר ביצוע חישוב מסובך באלגוריתם הדטרמיניסטי

יתרה מכך, לאחר שלוש או ארבע איטרציות של האלגוריתם הרנדומי, ההסתברות להצלחה היא בהרבה

100%מקרים כמעט .

Page 6: אלגוריתמים רנדומיים

6

סוגים של אלגוריתמים רנדומיים

אלגוריתם לאס וגאסאלגוריתם לאס וגאס הוא אלגוריתם רנדומי שמחזיר תשובה רק

כאשר הוא מוצא את התשובה הנכונה. הוא לעולם לא מחזיר .תשובה שגויה

אלגוריתם מונטה קרלואלגוריתם מונטה קרלו הוא אלגוריתם רנדומי שתמיד יחזיר

תשובה כלשהיא לבעיה הנתונה. לעיתים, התוצאה עלולה להיות .שגויה

Page 7: אלגוריתמים רנדומיים

7

דוגמא לאלגוריתם לאס וגאס

לשם המחשת אלגוריתם רנדומי מסוג "לאס וגאס" ננתח את האלגוריתם

.למציאת חציון

Page 8: אלגוריתמים רנדומיים

8

האלגוריתם למציאת חציון

חציון הינו האיבר האמצעי בגודלו, כלומר האיבר שנמצא במקום -ה

n/2 .במערך

נרחיב את האלגוריתם שלנו עבור איבר הנמצא K.כלשהוא במקום

,O)nlogn( את המערך בזמן Quick-Sort ניתן למיין על ידי

ואז נקבל שסה"כ הזמן הוא )O)n, אולם זה יכול להיות מכפלה

של קבוע כלשהוא, גם גדול מאד. נרצה לצמצם אותו עד כמה .שאפשר

Page 9: אלגוריתמים רנדומיים

9

(1) K האלגוריתם למציאת-האיבר ה

Sנתון המערך

בגודלוKאנחנו רוצים למצא את האיבר ה-

SKנסמן איבר זה

.n3/4 שגודלו Rניקח איברים באקראי לתוך מערך

לוקחRמיון המערך

הוא בערך החציון., הממוין נמצא בערך באמצעRהאיבר שבמערך

הממוין.Rב- הוא בערך האיבר ה - S בגודלו ב-Kהאיבר ה-

הממוין.Rב- יהיה האיבר ה- Sב- למשל, האיבר ה-

41

n

k

n2

14

32

1

2

14

1 nn

n

()log4

3log 4

34

34

3nnnnn

Page 10: אלגוריתמים רנדומיים

10

(2) K האלגוריתם למציאת-האיבר ה

R בגודלו. ניקח שני איברים מ- Kדי קרוב לאיבר ה- האיבר הממוין:

( Rlנסמנו ( ( והאיבר ה- Rn )נסמנו - האיבר הבגודלם.

בגודלו יהיה בין האיברים הנ"לKבהסתברות טובה, האיבר ה-

Rl Sk Rn

לא בקצה.k , כלומר n1/4 < k < n-n1/4 כך ש- kנסתכל על

. נסתכל על:Rn ו- Rlידועים לנו

43

n

k

nn

k

43n

n

k

43

}|{ nl RxRSxP

Page 11: אלגוריתמים רנדומיים

11

(3) K האלגוריתם למציאת-האיבר ה

פעולות.2n => לוקח Pאם מניחים שהאיבר נמצא ביניהם, הוא נמצא ב-

)P. Si = min)P הוא המינימום ב- Si פעולות ניתן למצוא איזה nבעוד

זה פשוט )ע"י קידום מונה בעת בניית Pלמצוא כמה איברים יש מתחת ל-P.)

ב- k-i את האיבר ה- Pבהנתן מספר האיברים שמתחת, נשאר למצוא ב-P

(Sk = Pk-i ).

)3n3/4logn = o)nאזי ניתן למיין אותו ב- ,P| 4n3/4|אם

.Rn ל- Rl( האיבר בין 1 הנחות: 2=> מוצאים את האיבר ב-

2 )|P| 4n3/4

Page 12: אלגוריתמים רנדומיים

12

:אי שוויונים חשובים לצורך ניתוח

אי שוויון מרקוב:

y משתנה מקרי חיובי, אזי

אי שוויון צ'בישב:

t

yty

()(Pr)

2

1(|Pr)|

ttx xx

Page 13: אלגוריתמים רנדומיים

13

k ניתוח האלגוריתם למציאת האיבר -ה

(Rn < Sk או P = ) Pr ) Rl > Sk לא מכיל אתPr ) Sk

שני הביטויים די שקולים, ולכן די אם נביט על אחד מהם.

:)Pr)Rl > Skנסתכל על

אני אנחנו דוגמים איברים. בהסתברות של R ל- Sלצורך המעבר בין .)איברים kאבחר את האיבר. )בממוצע יהיו מתחת ל-

X -מספר האיברים שדגמתי שקטנים מ = Sk.

Rl > Sk -דגמנו מהחלק שמתחת ל <= k הרבה יותר איברים

41

1

n

41

n

k

41()

n

kx

(Pr)(Pr)4

1 nn

kxSR kl

Page 14: אלגוריתמים רנדומיים

14

k ניתוח האלגוריתם למציאת האיבר -ה

.Sk התמפה איבר שגדול מ- Rl אזי לחלק של Rl > Skאם

Rl

x

(|Pr)|(Pr)4

14

1 nxn

kn

n

kx

Page 15: אלגוריתמים רנדומיים

15

k ניתוח האלגוריתם למציאת האיבר -ה

X1 -להיות קטן מ- 1– הסיכוי של האיבר ה Rl

Xi -הסיכוי של האיבר ה – i -להיות קטן מ Rl

קטן 1מ-

43

41

41

1 41

(1

1)1

()

()4

3

nnn

nxV

n

kxix

n

ii

Page 16: אלגוריתמים רנדומיים

16

k ניתוח האלגוריתם למציאת האיבר -ה

קבלנו:

Rn<Sk )או Pr)Rl>Sk

:Pההסתברות לחסום את

או

nnn

n

n

kxn

n

kx

1(

1)(|Pr)|(Pr) 2

41

41

43

41

41

n

2

43

43

Pr)(4|Pr)| nSRnP kl (43

nSR kn

Page 17: אלגוריתמים רנדומיים

17

דוגמא לאלגוריתמי מונטה קרלו

לשם המחשת אלגוריתמים רנדומי מסוג "מונטה קרלו" ננתח את

ואת MinCutהאלגוריתם של .swap matchingהאלגוריתם של

Page 18: אלגוריתמים רנדומיים

18

Min-Cut problem

Definitions:

MultiGraph: A graph that may contain multiple edges between any pair of vertices.

Cut: A set of edges from a graph G,whose removal from G results in G being broken into two or more components.

Min-Cut: A cut with minimum cardinality.

Let G be an undirected multigraph with n vertices. The problem is how to find the Min-cut of G.

Page 19: אלגוריתמים רנדומיים

19

הסבר המטרה היא למצוא את מספר הקשתות

המינימלי שיש להוריד מהגרף כדי להפוך אותו ללא קשיר.

הגרף הואmultigraph ז"א יתכן יותר מקשת ,אחת בין זוג קודקודים.

Page 20: אלגוריתמים רנדומיים

20

Example

Min-Cut

regular Cut

G

Page 21: אלגוריתמים רנדומיים

21

Random Algorithm for Min-Cut

1. While number of nodes in G is more than 2

1.1. Choose a random edge.

1.2. Merge the 2 nodes connected by that edge.

2. MinCut edges that connect the 2 remaining vertices. Notice that these edges are actually all the edges connecting nodes contracted to one of the remaining nodes with nodes contracted to the other remaining node.

Page 22: אלגוריתמים רנדומיים

22

Example

G

Iteration 1: Iteration 2:

Page 23: אלגוריתמים רנדומיים

23

Example(cont.)

G

Result:

all the edges connecting 5 with 1,2,3,4

Iteration 3: Iteration 4: Result:

Page 24: אלגוריתמים רנדומיים

24

Min-Cut algorithm analysisWhen will this algorithm return the Min-Cut?

Let k be the Min-Cut size.

Let C be a particular Min-Cut with k edges.

The algorithm will return the Min-Cut if it never merges nodes connected by an edge from C.

The probability that the algorithm will return the Min-Cut is bounded from below by the probability that in each iteration it chooses an edge not from the C.

Page 25: אלגוריתמים רנדומיים

25

הסבר נניח כי ישMinCut .יחיד אזי כדי שהאלגוריתם יחזיר אותו, כל הקשתות

הזה חייבות להשאר עד השלב MinCutשב-האחרון באלגוריתם.

ז"א אסור שהאלגוריתם יבחר ל"אחד" קשת)ז"א לאחד את הקודקודים שבקצוותיה(

, כדי שהקשת הזו תשאר לשלב MinCutשב-הסופי של האלגוריתם.

Page 26: אלגוריתמים רנדומיים

26

Analysis(cont.) The probability of picking at each iteration an

edge from C is , where |E| is the number of edges still in G.

There’s no vertex with degree less than k, or else cutting it would give a cut less than k.

Since the graph is connected, then The probability of choosing an edge from C in

iteration i is bound from below by:

Ek

2

nkE

1

2

2()

inkin

k

Page 27: אלגוריתמים רנדומיים

27

הסבר-ההסתברות לבחור קשת מC היא מספר הקשתות בגרף

:Cלחלק למספר הקשתות ב--אם היה קודקוד בגרף עם דרגה קטנה מk ז"א שיוצאות –

היה כל הקשתות MinCut קשתות – אז ה-kממנו פחות מ-ליצור היוצאות מקודקוד זה – וע"י כך לנתק אותו מהגרף

גרף לא קשיר. הגרף קשיר והדרגה המינימלית היאk לכן יש לפחות ,

קשתות, כי המצב שבו יהיו הכי מעט קשתות הוא שכל הקשתות מחברות בין קודקודים שונים )בלי קשת מקודקוד

.kלעצמו( ודרגת כל קודקוד היא בדיוק "בכל שלב באלגוריתם יש קודקוד פחות )"מצטמצם

לשכנו(.

Ek

2

nk

Page 28: אלגוריתמים רנדומיים

28

Conditional Probabilities For events 1 , 2 ,.., k.

Pr[j | i] is the conditional probability, which

means the probability that j will happen given

that i happened.

1

1123121

1

|Pr|Pr|PrPrPrk

iik

k

ii

12112 |PrPrPr

Page 29: אלגוריתמים רנדומיים

29

Probability Analysis• i = the probability of not choosing an edge from C in iteration i.

• The probability of not choosing an edge from C in iteration i is:

• The probability of not choosing an edge from C at all is:

1

21|Pr

1

1

in

i

jji

(1)

2Pr

2

1

nn

n

ii

Page 30: אלגוריתמים רנדומיים

30

הסבר-ההסתברות שהאלגוריתם ימצא את הMinCut גדול

השלבים n-2מההסתברות שהאלגותרים לא יבחר ב n-2 ולכן יש 2שלו )מצמצמים את הגרף עד שנשארים

. Cשלבים( שום קודקוד מ- במילים אחרות, זה ההסתברות שהאלגורתים לא יבחר

... עד וגם , 2שלא יבחר בשלב וגם , C קשת מ-1בשלב . n-2שלב

ז"א זה ההסתברות לחיתוך של המאורעות של אי-בחירת.=i 1,2,3,..,n-2 , כאשר i בשלב Cקשת מ-

Page 31: אלגוריתמים רנדומיים

31

Improving the lower bound• The probability that the algorithm will not find the Min-Cut is:

• To improve this probability we repeat the algorithm

times, making independent random choices each time.

• Thus, the probability that the algorithm will not find the Min-Cut is reduced to 1/e:

2

2n

ennn

nn

121

(1)

21

2

2

222

(1)

21

nn

Page 32: אלגוריתמים רנדומיים

32

הסבר ההסתברות שע"י ריצה אחת של האלגוריתם לא

. קטנה מ-MinCutנמצא את ה- זהו חסם גדול מדי, כי ייתכן שבהסתברות גבוהה לא

נמצא פתרון נכון. פעמים ונקבל הסתברות לכן נריץ את האלגוריתם

.e/1לטעות קטנה מ 2

2n

(1)

21

nn

Page 33: אלגוריתמים רנדומיים

33

Pattern Matching with swaps

• The problem is finding all locations in a given text of size n, where a given pattern of length m match.

• A pattern is said to match the text at a given location l, if adjacent text characters can be swapped, if necessary, to make the substring of the text starting at location l, identical to the pattern.

• All swaps are constraint to be disjoint, i.e. each character is involved in at most one swap.

B D AD B AB C AA B AText:

B A CPattern:

Page 34: אלגוריתמים רנדומיים

34

הסבר הבעיה היא למצוא את המקומות בטקסט בהם

ניתן התבנית מתאימה, כאשר מאפשרים להחליף בין תווים צמודים בטקסט.

אסור להחליף את המיקום של אותו תו יותרמפעם אחת.

Page 35: אלגוריתמים רנדומיים

35

Problem Reduction• First we will solve the problem for |

) without loss of generality, let {a,b} (

• Then we will create a set of coding, C, such that each coding will keep:

• And, finally use this code and solve the problem for larger sizes of

()()()|,, cbaCcbacba

Page 36: אלגוריתמים רנדומיים

36

הסבר- נראה אלגוריתם המוצא את הswap matches עבור

א"ב עם רק שתי תווים. לאחר מכן ניתן ליצור מספיק קידודים שונים שמקודדים את

קידודים יש 3 תווים, כך שעבור כל 2א"ב כלשהו לא"ב עם קידודים אך שונה בקידוד 2תו המקודד באופן זהה עבור

השלישי. )יש אלגוריתם דטרמיניסטי המוצא מספיק קידודים כאלו(.

-עבור כל קידוד של הטקסט והתבנית נמצא את הswap matches.בעזרת האלגורתים הנ"ל, ונאחד את התוצאות

Page 37: אלגוריתמים רנדומיים

37

Swap Matching for |=2• Partition the text and pattern into maximal streams.

• A stream is an alternating substring, i.e., no two consecutive characters in a stream are identical.

• Notice that the character following a maximal stream is the same as the last character in that stream.

• The character preceding a maximal stream is the same as the first character in that stream.

b a aa b aa b ba b aText:

Page 38: אלגוריתמים רנדומיים

38

הסברStreamרצף , הוא רצף של תווים מתחלפים – חלוקה לרצפים באורכים מקסימליים תגרום לכך

שמתחילים רצף חדש רק כאשר תו תו מופיע פעמיים ברציפות.

Page 39: אלגוריתמים רנדומיים

39

Lemma 1The pattern matches in a particular alignment if and only if one of following holds for each stream A in the text and each stream B.

I. The overlap between A and B has even length.

II. The overlap between A and B has odd length, and the portion of B overlapping A matches exactly )without swaps(.

III. The overlap between A and B has odd length, and either B is the leftmost stream in the pattern and A extends further to the left, or B is the rightmost stream in the pattern and A extends further to the right of B.

Page 40: אלגוריתמים רנדומיים

40

הסבר.I אם רצף בטקסט חופף בתבנית באורך זוגי, אם החפיפה זהה

– אין בעיה, התבנית בחלק זה מתאימה. אחרת ניתן להחליף כל זוג תווים בחלק החופף וכך לקבל התאמה מלאה.

.II אם החפיפה באורך אי-זוגי לא ניתן להחליף כל זוג תווים)ישאר בסוף תו שלא ניתן להחליפו כי הוא בקצה רצף ולכן

החלפה לא תשנה כלום( ולכן תתכן התאמה רק אם החפיפה זהה.

.III אם החפיפה באורך אי-זוגי אך הרצף הוא בקצה ההתאמה ביןהטקסט לתבנית, והטקסט ממשיך לאחר הקצה אז ניתן

במקרה של חפיפה לא זהה להחליף כל זוג )ו"לקחת" מהטקסט שאחר החפיפה(.

Page 41: אלגוריתמים רנדומיים

41

Examples:

b aa b abA:

b a baB: a b

b aa b abA:

b a baB: a b

b aa b abA:

a b abB: b a

Page 42: אלגוריתמים רנדומיים

42

Divide and Conquer

• We will show next how to find all matches of the pattern beginning at odd location in the text. Finding the matches beginning at even location will be found similarly.

b aa b bb b a a baa ab

7 84 5 63 9 10 12 13111 20

Text:

Odd path: Will find matches in odd locations.)3,5,7(

Even path:Will find matches in even locations.)0,2,4(

b baPattern:

Page 43: אלגוריתמים רנדומיים

43

Even & Odd streams• A stream is called odd if the a’s in it occur at odd locations )indexes(.

• A stream is called even if the a’s in it occur at even locations.

b aa b bb b a a baa ab

7 84 5 63 9 10 12 13111 20

even odd evenodd even odd

Page 44: אלגוריתמים רנדומיים

44

הסבר-רצף זוגי הוא רצף שהa ים בו מופיעים בו-

באינדקסים זוגיים.-רצף אי-זוגי הוא רצף שהa ים בו מופיעים בו-

באינדקסים אי-זוגיים. יש לשם לב שרצפים מאותו סוג מתאימים בדיוק

)ללא החלפות( כאשר בודקים התאמות של התבנית באינדקסים אי-זוגיים בטקסט.

Page 45: אלגוריתמים רנדומיים

45

Even & Odd stream (cont.)

streamevenaninisitext

streamoddaninisitextitodd ][0

][1][

streamoddaninisitext

streamevenaninisitextiteven ][0

][1][

streamevenaninisipattern

streamoddaninisipatternelyalternativipodd ][

][

0

1][

streamoddaninisipattern

streamevenaninisipatternelyalternativipeven ][

][

0

1][

Definitions:

Page 46: אלגוריתמים רנדומיים

46

Example

b aa b bb b a a baa ab

7 84 5 63 9 10 12 13111 20

Text:

todd: 0 00 0 10 0 0 0 011 01

teven: 1 11 1 01 1 1 1 100 10

b ab a bab ba

7 84 5 631 20

Pattern:

0 01 -1 1-10 10

1 -10 0 00-1 01

podd:peven:

Page 47: אלגוריתמים רנדומיים

47

Dot product

pattern text

The dot product of the text and the pattern is polynomial multiplication of the text and the pattern ordered backwards.

121 tttt nn mm pppp 121

1121111 tptptptp nn 1222122 tptptptp nn

121 tptptptp mmnmnm

Page 48: אלגוריתמים רנדומיים

48

הסבר-שימוש בכפל פולינומי בpattern matching

הוא אסטרטגיה נפוצה.-יש לשם לב שהמספר במקום הm בתוצאה הוא

1למעשה "כפל" של התבנית בטקסט במקום ה-בטקסט.

-באופן כללי המספר במקום הm+i בתוצאה הוא למעשה "כפל" של התבנית בטקסט במקום

בטקסט. iה-

Page 49: אלגוריתמים רנדומיים

49

Odd-Even pair

1. 0, if either A is an even stream or B is an odd stream.

2. 0, if the overlap has even length, then j = 0.

3. if the overlap has odd length, A is an odd stream, and B is an even stream.1

For any odd placement of the pattern in the text any overlapping streams, A in the text and B in the pattern, the dot product of the todd and peven which corresponds to the overlap of the streams A and B is:

Page 50: אלגוריתמים רנדומיים

50

Even-Odd pair

1. 0, if either A is an odd stream or B is an even stream.

2. 0, if the overlap has even length, then j = 0.

3. if the overlap has odd length, A is an even stream, and B is an odd stream.1

For any odd placement of the pattern in the text any overlapping streams, A in the text and B in the pattern, the dot product of the teven and podd which corresponds to the overlap of the streams A and B is:

Page 51: אלגוריתמים רנדומיים

51

הסבר

אם החפיפה היא בין מקטעים השווים לאפס )לדוגמא 1.( ברור שהמכפלה המתאימה toddמקטעים זוגיים ב-

לכך תהיה אפס.

אם החפיפה באורך זוגי אז המכפלה המתאימה תהיה 2.- שבתבנית יצמצמו.1,1אפס מכיוון שה

אם החפיפה היא בין מקטעים השונים מאפס, 3.והחפיפה באורך אי-זוגי, אז המכפלה המתאימה תהיה

1... באורך אי-זוגי = 1-1+1-1+1+- , מכיוון ש 1 או 1-.1או

Page 52: אלגוריתמים רנדומיים

52

Corollary For any odd placement of the pattern in the text and any overlapping streams, the dot product of the corresponding overlapping portions of either teven,podd or todd,peven is if and only if the stream doesn’t match with swaps in that placement, with the exception of the leftmost and rightmost overlaps.

1

Page 53: אלגוריתמים רנדומיים

53

הסבר רק כאשר יש חפיפה באורך 1 או 1המכפלות הנ"ל נותנות -

-even או odd-evenאי-זוגי בין מקטעים מסוג שונה )odd .)

לפיLemma1 במצב שיש חפיפה לא זהה )התווים לא מתאימים בדיוק( באורך אי-זוגי אין התאמה )תיתכן התאמה

רק אם מקטעים אלו בקצוות ההתאמה והטקסט ממשיך הלאה(.

,כאשר מנסים להתאים את התבנית במקומות אי-זוגיים( אינה Odd-Evenחפיפה בין רצפים מסוגים שונים )

חפיפה זהה.

Page 54: אלגוריתמים רנדומיים

54

Proof

Assume, without the loss of generality that the dot product of portions in todd and peven corresponding an overlap of streams, A in the text and B in the pattern, is 1 or –1.

Then A must be an odd stream, B an even stream, and the overlap is of odd size.)Odd-Even pair(

A and B are different )since the pattern is placed in odd locations on the text(, and the overlap is of odd size.

According to Lemma 1, the pattern doesn’t match in that location, unless B is the leftmost )rightmost( stream in the pattern and A extends further to the left )right(.

Page 55: אלגוריתמים רנדומיים

55

Proof (cont.)

If the pattern doesn’t match with swaps at an odd location.

Then according to Lemma 1 there is an overlap of streams, A in the text and B in the pattern, that is of odd length, where A and B doesn’t match exactly)without swaps(.

If A is an odd stream than B must be an even stream and vice versa )since the pattern is placed in odd locations on the text(.

The dot product corresponding to the overlap between A and B of todd,peven or teven,podd will be 1 or –1.

Page 56: אלגוריתמים רנדומיים

56

Problem• The dot product corresponding to streams that do not match will give us 1 or –1.

• The overall dot product of the pattern at that same location may still be 0, when two or more stream don’t match and eliminate the dot products of each other )1 + -1 = 0(.

• Thus computing the dot product of todd,peven and peven,todd may not find all the miss-matches !

Page 57: אלגוריתמים רנדומיים

57

הסבר כיוון שמחברים את התוצאה של המכפלות של

הרצפים – כמו בכל כפל פולינומי – ייתכן ששתי רצפים לא מתאימים יקזזו זה את זה.

Page 58: אלגוריתמים רנדומיים

58

Illustration

todd

1 10 0 111 11

1 -1-11

peven:

-1 -10 0 -1-1-1 -1-1

1 10 0 111 11

1 10 0 111 11

-1 -10 0 -1-1-1 -1-1

0 -10 1 -10-1 0-1000

1 10 0 111 11

1 -1-11

The pattern shouldn’t match in the third position …

But according to the dot product …todd

peven:

Page 59: אלגוריתמים רנדומיים

59

Solution: Randomize Each stream will be multiplied by a random

number between . Identical stream will still give 0 in their dot

products. Even overlaps between different streams will still

give 0 in their dot product. Odd overlap between different streams will return

0 only when two different random numbers, from the different streams, will compensate each other low probability.

],1[ 2n

Page 60: אלגוריתמים רנדומיים

60

הסבר.נכפיל כל רצף בטקסט ובתבנית במספר אקראי וכך ההסתברות שמכפלות מרצפים שונים יקזזו

זה את זה תהיה קטנה מאד.

Page 61: אלגוריתמים רנדומיים

61

Examples2 20 0 244 44

3 -3-33

-6 -60 0 -6-12-12 -12-12

6 60 0 61212 1212

0 -60 6 -66--12 0012012

todd

peven:

-6 -60 0 -6-12-12 -12-12

6 60 0 61212 1212

Notice that the miss-match marked in circle wasn’t caught before, since the –12 and 6 in the circles above would have eliminated each other )-1 + 1=0( , thus the dot product at that point would have given 0 even though it isn’t a match.

Page 62: אלגוריתמים רנדומיים

62

Even locations Finding the matches of the pattern in even

locations on the text is done exactly the same as in odd locations, with one difference.

Instead of computing the dot product of todd with peven and teven with podd, compute the dot product of todd with podd and teven with peven.

Page 63: אלגוריתמים רנדומיים

63

הסבר מציאת ההתאמה במקומות זוגיים תתבצע בדיוק

אותו דבר לבד מכך שכעת יש חפיפה זהה בין רצפים מסוג שונה , וחפיפה לא זהה בין רצפים

מאותו סוג. לכן במקום לבחון את המכפלה שלtodd עם

peven ומכפלה של teven עם peven יש לבחון , עם teven ו-podd עם toddאת המכפלות של

peven.

Page 64: אלגוריתמים רנדומיים

64

Algorithm analysis When the pattern matches the algorithm always

returns 0 in the fitting position in the result. When the pattern doesn’t match the probability

that the algorithms will return 0, thus not identifying the miss-match, is at most since each stream has a random number between .

Therefore the probability that the result of the algorithm will be incorrect )over all the text( is at most

2

1

n],1[ 2n

nnn

112

Page 65: אלגוריתמים רנדומיים

65

Time complexity

1. Create todd,teven,podd,peven - O)n(.

2. Compute

Using FFT, polynomial multiplication can be done in O)n log m(, but if n>>m, the time complexity of FFT becomes O)n log n(.

evenevenoddodd

oddevenevenodd

ptpt

ptpt

,

,

nnnnn loglog()

Page 66: אלגוריתמים רנדומיים

66

Swap matching for | Construct a large enough set of codes C = {..i..}

s.t.

1. For each code

1.1. Apply the code to the original text and pattern.

1.2. Apply the algorithm for ||>2 on the coded text and pattern.

1.3 for every location on the text that doesn’t match mark that location.

2. The pattern matches with swaps only at the locations that aren’t marked

()σ()σ()σ|σ,, iiii cbacba 0,1:σi

Page 67: אלגוריתמים רנדומיים

67

הסבר ע"י הפעלת כל אחד מהקידודים על הטקסט והתבנית, נקבל תבנית

, ונוכל להפעיל את האלגוריתם הקודם.2וטקסט עם א"ב באורך אם יש במקום מסוים בטקסט)המקורי( אי התאמה בין הטקסט

לתבנית תווים שונים שלא מתאימים.2 ז"א יש .לכן קיים קידוד המקודד שתי תווים אלו לביטים שונים ימצא אי-התאמה זו כאשר יופעל עם 2 האלגוריתם לא"ב בגודל

התווים )בהכרח קיים קידוד כזה(. 2הקידוד המפריד בין אם יש במקום מסוים בטקסט)המקורי( התאמה בין הטקסט

לתבנית תווים שלא מתאימים.2 ז"א שאין וודאי שעבור כל קידוד לא תמצא אי-התאמה כי כל קידוד אינו

מקודד אותו תו לביטים שונים )הקידוד הוא פונקציה פשוטה(.

Page 68: אלגוריתמים רנדומיים

68

Sufficient codes A set of codes C={..i..} is said to be sufficient

)for this swap matching algorithm( if

We require a set of codes to have this property in order to use it for this swap matching algorithm.

()σ()σ()σ

..σ,,

mmm

m

cba

tsCcba

Page 69: אלגוריתמים רנדומיים

69

Constructing enough codes

Code each letter to different log|| bits )ascii-like(.

Take the ith bit of each letter as it’s code.

log|| different codes. Use the xor of each pair of

these codes as a code as well O)log2||( different codes.

0 0 0 a

1 0 1 b

1 1 0 c

0 1 1 d

2nd code

1th code

21As the 3rd code

Page 70: אלגוריתמים רנדומיים

70

Are these codes sufficient? We need to prove that

Since every letter has a unique code, then the first log|S| codes keeps the following property:

s.t.

()σ()σ()σ|σ,, mmmm cbacba

()σ()σ(,)σ()σ(,)σ()σ

σσσ,,

kkjjii

kj,i,

bcbaca

cba

Page 71: אלגוריתמים רנדומיים

71

Proof If i)a(= i)b( then i is sufficient.

If k)a(= k)b( then k is sufficient. Else

But in that case, will be sufficient, since

()σ()σ()σ

()σ()σ()σ

kkk

iii

abc

bac

()σ()σ()σ()σ

()σ()σ()σ()σ

kiki

kiki

ccaa

bbaa

ki

Page 72: אלגוריתמים רנדומיים

72

הסבר

i k i k

a * * 0 * 0 * * 0 *

b * * 1 * 1 * * 0 *

c * * 0 * 1 * * 1 *

אותיות 3רוצים להוכיח כי עבור כל a,b,cקיים קידוד , s בקבוצת .)c( אך שונה ))a( = )bהקידודים שנבנתה שעבורו

לפי בנייתlog|| הקידודים הראשונים עבור כל זוג אותיות שונות קיים קידוד מקידודים אלו המבדיל ביניהם.

עם בקידוד המפריד ביןa -ל c גם )a( = )b( .אז זהו הקידוד הרצוי .c ל-bאותו דבר גם עבור הקידוד המפריד בין

אם לא התקיימו אלה, אזי בהכרח בקידוד שהוא הxor בין הקידוד אך שונה b יהיה שווה ל- c , a ל-a לקידוד המפריד בין b ל-aהמפריד בין

.cמ-

Page 73: אלגוריתמים רנדומיים

73

Algorithm analysis For each of the constructed codes run the

swap matching algorithm. There are log|| + log2|| codes. Each run of the algorithm is The time complexity of this random swap

matching algorithm for || > 2 is

nn log

2loglog nn

Page 74: אלגוריתמים רנדומיים

74

Random construction of codes We will show how to randomly construct

O)log||( that will still keep the required property

Using these O)log||( codes we will be able to solve the swap matching for ||>2 in

instead of(loglog) nnO

()σ()σ()σ|σ,, mmmm cbacba

(loglog) 2 nnO

Page 75: אלגוריתמים רנדומיים

75

הסבר רקנראה כיצד ליצור O)log||( קידודים

|O)log2המקיימים את התכונה הרצויה במקום |( .

2כך במקום להריץ את האלגוריתם לא"ב בגודל O)log2||( פעמים , נריץ אותו רק O)log||(.

בסה"כ נקבל אלגוריתם הפותר את הבעייה ב-

. (loglog) nnO

Page 76: אלגוריתמים רנדומיים

76

Random codes The probability for a random code and

that

is . If we choose 12log)||( random codes Then the

probability that the codes are insufficient is

cba ,, cba

4

1

2

1

2

1

11

21

43

43log43log123

]|,,Pr[ cbacba

Page 77: אלגוריתמים רנדומיים

77

הסבר אותיות כלשהם3ההסתברות שעבור a,b,c קידוד אקראי

הוא ¼.כלשהו יקיים כי 12נבחרlog)|S|( .קידודים אקראיים ההסתברות שלא קיים קידוד מתאים עבור שלישיית אותיות היא

לכן ההסתברות שלא קיים קידוד מתאים עבור כל השלישיות .היא שלישיות(||3האפשרויות )יש

cba

Σ12log

411

11

21

43

43log43log123

Page 78: אלגוריתמים רנדומיים

78

Problem We would like to assure that the

random codes that have been chosen are correct.

This means running on all possible triplets (||3) and searching the codes for an appropriate code (O(log||)).

time complexity of O(||3log||).

Page 79: אלגוריתמים רנדומיים

79

Code construction in O(||) We will show how to construct the

codes in O(||). Step 1: Construct codes for

letters randomly (checking the codes as well).

Step 2: Use this code to, construct codes for all the other letters.

81

Page 80: אלגוריתמים רנדומיים

80

Construct codes for Choosing randomly enough codes and

checking them. As was shown before this can be done in

81

8

138

1log

Page 81: אלגוריתמים רנדומיים

81

Codes for k2 letters Given a set of codes {c1,c2,..,cn} that are sufficient

for k letters, we will show how to construct a set of codes that are sufficient for k2 letters.

Consider the following codes x1,..,xk of size n )1..k are the letters(:

()()

(1)(1)

1

11

kk n

n

k cc

cc

x

x

Page 82: אלגוריתמים רנדומיים

82

Codes for k2 letters(cont.) The following matrix creates k2 similar codes of

size 3n.

These k2 codes of size 3n can be reversed backwards, giving 3n codes for k2 letters (coding each letter to one bit)

1

mod(1)

3111

1

xxxk

xxxj

xxx

ki

kk

kjiji

Page 83: אלגוריתמים רנדומיים

83

Examplec1

c2

c3

a 1 0 1

b 0 1 1a b

a 101|101|011

011|101|101

b 101|011|101

011|011|011c1 c2 c3 c4 c5 c6 c7 c8 c9

(a,a)

1 0 1 1 0 1 0 1 1

(a,b)

1 0 1 0 1 1 1 0 1

(b,a)

0 1 1 1 0 1 1 0 1

(b,b)

0 1 1 0 1 1 0 1 1

Page 84: אלגוריתמים רנדומיים

84

Analysis Time complexity of creating codes for k2 letters

given codes for k letters is the time it takes to build the matrix.

Creating each element in the matrix can be done in constant time )since the code for k letters is given(.

Time complexity is O)k2(. Number of new codes is 3n )n is the number of the

initial codes for k letters(.

Page 85: אלגוריתמים רנדומיים

85

Are these codes still sufficient? We need to prove that these codes are sufficient

for the k2 letters. Let A be the first k letters. A X A will represent the newly coded k2 letters The new codes are sufficient for A x A if

For Each , the row and column in the matrix corresponding to a will be denoted as a1, a2.

AAa ()σ()σ()σ|σ,, cbaAAcba

Page 86: אלגוריתמים רנדומיים

86

Proof Notice that For every has 3 types of

codes: codes that code a as a1. codes that code a as a2... codes that code a as the letter whose index is

)a1+a2+1(mod k

If or then since ,there is a code for which iii cba

Acba iii ,, cba

iii cba

AAa

Page 87: אלגוריתמים רנדומיים

87

הסברז"א שיש קידוד המקודד את אם , a,b -ו c כמו

מקיים את התכונה A. מכיוון ש-Aאברים שונים ב-הרצויה עבור האברים שלו, אזי קיים קידוד מתאים גם

.c ו- a,bעבור ז“א שיש קידוד המקודד את אם , a,b לאותו

לאיבר אחר לכן וודאי קיים קידוד c ואת Aאיבר ב- לכן c לאיבר לו מקודד -aהמפריד בין האיבר לו מקודד-

)מכיוון b אך שווה ל- cשונה מ- aוודאי קיים קידוד שבו (.Aשהם יקודדו כמו אותו האיבר ב-

iii cba

iii cba

Page 88: אלגוריתמים רנדומיים

88

Proof(cont.) Obviously, since if

Then . The only remaining case is when

Assume without the loss of generality that i=1. This means that in the construction matrix

a is on the same row as c b is on the same column as c a and b are in different columns and rows.:

iii cba jjj cba

cba

jibcacba iiijjj

Page 89: אלגוריתמים רנדומיים

89

Proof(cont.) Denote x3 as the letter whose index is

x1+x2+1 mod k. Then clearly since a and c are on

the same row but on different columns )and there are only k columns(.

or .

33 ca

333 cba 333 cba

Page 90: אלגוריתמים רנדומיים

90

הסבר אםa-ו c נניח( באותה שורה במטריצה i אך בעמודה )

a בהתאמה,אזי הקידוד השלישי יקודד את jc ו-jaשונה , כמו האיבר cואת modk(i+ja+1 )כמו האיבר

(i+jc+1)modk.

- מכיוון שja-ו jc-שונים זה מזה וקטנים מ k אזי יש , . Aמקודדים לאברים שונים מ- c ו-aקידודים בהם

-וכבר הראנו אולכן מתקיים ש מקודם שבמצבים כאלו מכיוון ש-

אפשר למצוא קידוד מתאים.

333 cba 333 cba Acba 333 ,,

Page 91: אלגוריתמים רנדומיים

91

Code construction conclusion Constructing a sufficient code for Apply the method to create code for from

the initial code of , then enlarge the code using the same method to and finally

Time complexity: Building a code for : Enlarge the code to : Total: .

81

81

41

81

21

2

14

1

Page 92: אלגוריתמים רנדומיים

92

הסברכדי ליצור קבוצה מספקת של קידודים עבור כל האותיות תחילה ניצור קידוד באופן רנדומי עבור .

נקבל )log( -קידודים ב זמן-ל-נגדיל את הקידוד מ .

נקבל )log(=)3log(זמן.- קידודים ב חזרה על השלב הקודם עוד פעמיים כדי לקבל קידוד

לכל סהכקבלנו)log( -קידודים ב זמן

81

814

1

81

4

1O