1 of 96

עצים בינארים

צביקה ברגר

רקורסיות

צביקה ברגר

מערכות הפעלה

התאמת מחרוזות

צביקה ברגר

בס"ד

2 of 96

Pattern Matching

בס"ד

3 of 96

FFT (Algo 1)

בס"ד

4 of 96

Abrahamson – Kosarahu (Algo 1)

בס"ד

5 of 96

Automa

בס"ד

Pattern: abbaba

a

b

a

b

a

b

b

a

a

b

a

b

a

b

 

6 of 96

Automa

בס"ד

Pattern: abbabc

a

b

a

b

c

b

b,c

a

a

b

a

b

a

b

 

c

a

c

c

c

c

 

 

 

 

7 of 96

  • Size: O(m)
  • Time
    • O(m) building data structure
    • O(n) traversing the text

  • New improvements:
    • Realtime
    • Streaming

KMP 1972 (Knuth, Morris, Pratt)

בס"ד

a

b

a

b

c

b

T=abbaabbabb

8 of 96

KMP – Realtime

בס"ד

9 of 96

Karp-Rabin 1987

בס"ד

O(n+m)

10 of 96

Dictionary Problem

בס"ד

B

B

11 of 96

Automa

בס"ד

D={abab,baa}

a

b

b

a

b

a

a

a

b

a

a

a

b

b

b

b

 

T= a b a a b a a b a b

12 of 96

  • Generalized version of KMP
  • Composed of
    • Goto function
    • Failure function
    • Report

  • Space – O(size(D))
  • Query time – O(|T|+ occ)

Aho Corasick 1975

בס"ד

13 of 96

D={aba,aabb,babb,bbaab}

Trie

בס"ד

a

b

a

a

b

b

b

b

b

b

a

a

a

b

Space=O(size(D))

 

14 of 96

T=ababb$

All Suffixes:�ababb$�babb$�abb$�bb$�b$�$

Indexing

בס"ד

0�1

2

3

4

5

Query: ab

a

b

b

a

b

b

b

$

a

b

$

b

b

$

$

$

$

0

1

2

3

4

5

 

 

15 of 96

T=ababb$

All Suffixes:�ababb$�babb$�abb$�bb$�b$�$

Suffix Tree

בס"ד

0�1

2

3

4

5

Query: ab

 

 

a

b

b

b$�[4,5]

b$

[4,5]

abb$

[3,5]

abb$�[2,5]

$

$

Compress Trie:

0

1

2

3

4

5

16 of 96

LCA

בס"ד

 

 

?

Text

Pattern

$

#

Build suffix tree for:

Harel & Tarjan 1983 “Fast algorithms for finding nearest common ancestors”: It is possible to preprocess a n-node tree in time O(n) and answer subsequent LCA queries in time O(1).

17 of 96

Suffix Tree + LCA

בס"ד

18 of 96

Suffix Tree + LCA

בס"ד

19 of 96

  • Building suffix tree + LCA O(n+m) time
  • Foreach text position O(k) time.
  • Overall O(nk) time.

k-missmatches

בס"ד

20 of 96

k-missmatches

בס"ד

  • Create suffix tree for: s = P#T

  • Check P at each location i of T by kangrooing

Example:

P = A B A B A A B A C A B

T = A B B A C A B A B A B C A B B C A B C A …

i

21 of 96

k-missmatches

בס"ד

  • Create suffix tree for: s = P#T

  • Check P at each location i of T by kangrooing

Example:

P = A B A B A A B A C A B

T = A B B A C A B A B A B C A B B C A B C A …

i

22 of 96

k-missmatches

בס"ד

  • Create suffix tree for: s = P#T

  • Check P at each location i of T by kangrooing

Example:

P = A B A B A A B A C A B

T = A B B A C A B A B A B C A B B C A B C A …

i

23 of 96

k-missmatches

בס"ד

  • Create suffix tree for: s = P#T

  • Check P at each location i of T by kangrooing

Example:

P = A B A B A A B A C A B

T = A B B A C A B A B A B C A B B C A B C A …

i

24 of 96

k-missmatches

בס"ד

  • Create suffix tree for: s = P#T

  • Check P at each location i of T by kangrooing

Example:

P = A B A B A A B A C A B

T = A B B A C A B A B A B C A B B C A B C A …

i

25 of 96

k-missmatches

בס"ד

  • Create suffix tree for: s = P#T

  • Check P at each location i of T by kangrooing

Example:

P = A B A B A A B A C A B

T = A B B A C A B A B A B C A B B C A B C A …

i

26 of 96

k-missmatches

בס"ד

  • Create suffix tree for: s = P#T

  • Check P at each location i of T by kangrooing

Example:

P = A B A B A A B A C A B

T = A B B A C A B A B A B C A B B C A B C A …

i

27 of 96

k-missmatches

בס"ד

  • Create suffix tree for: s = P#T

  • Check P at each location i of T by kangrooing

Example:

P = A B A B A A B A C A B

T = A B B A C A B A B A B C A B B C A B C A …

i

28 of 96

Convulution

בס"ד

0 1 0 1 1 0 1 0 0 1 0 0 1 1 1 0 1 0 0 0 1 1 0 1

1 0 1 1 0 0 1

2

29 of 96

Convulution

בס"ד

0 1 0 1 1 0 1 0 0 1 0 0 1 1 1 0 1 0 0 0 1 1 0 1

1 0 1 1 0 0 1

2 3

30 of 96

Convulution

בס"ד

0 1 0 1 1 0 1 0 0 1 0 0 1 1 1 0 1 0 0 0 1 1 0 1

1 0 1 1 0 0 1

2 3 1

With FFT can be done in O(n log m) time

31 of 96

Counting missmatch with convulutions

בס"ד

T = a b c b a b b a c a b b c

P = a b b c a

T = 0 1 1 1 0 0 1 1 0 1 0 1 1 1

P = 1 0 0 0 0 1

a

T = 1 0 1 0 1 0 0 0 1 1 1 0 0 1

P = 0 1 0 1 0 0

b

T = 1 1 0 1 1 0 1 1 1 0 1 1 1 0

P = 0 0 0 0 1 0

c

 

Support wildcards

32 of 96

Counting missmatch with convulutions

בס"ד

T = a b c b a c b b a c a b b c

P = a b c b c a

T = 0 1 1 1 0 1 1 1 0 1 0 1 1 1

P = 1 0 0 0 0 1

a

T = 1 0 1 0 1 1 0 0 1 1 1 0 0 1

P = 0 1 0 1 0 0

b

T = 1 1 0 1 1 0 1 1 1 0 1 1 1 0

P = 0 0 1 0 1 0

c

 

Support wildcards

33 of 96

  • If there are few places that match up to k:
    • We can verify those possition using suffixtree and lca.
    • How do we find those positions?
    • What happen if we have many position which match up to k?

K-mismatch (ALP 2000) - Filter

בס"ד

34 of 96

  • If there are few places that match up to k:
    • We can verify those possition using suffixtree and lca.
    • How do we find those positions?�If we have 2k or more different symbols in the pattern

K-mismatch (ALP 2000) - Filter

בס"ד

P = a b c b d c d a

k=2

F = a b c d

If the distance of F to the text is bigger than k then the distance of the text to P bigger then k

F can match at most n/k times.

35 of 96

  • If there are few places that match up to k:
    • We can verify those possition using suffixtree and lca.
    • How do we find those positions?�If we have 2k or more different symbols in the pattern

K-mismatch (ALP 2000) - Filter

בס"ד

P = a b c b d c d a

k=2

F = a b c d

d

d

F

+1

36 of 96

  • If there are few places that match up to k:
    • We can verify those possition using suffixtree and lca.
    • How do we find those positions?�If we have 2k or more different symbols in the pattern

K-mismatch (ALP 2000) - Filter

בס"ד

P = a b c b d c d a

k=2

F = a b c d

a

a

F

+1

37 of 96

  • If there are few places that match up to k:
    • We can verify those possition using suffixtree and lca.
    • How do we find those positions?�If we have 2k or more different symbols in the pattern

K-mismatch (ALP 2000) - Filter

בס"ד

P = a b c b d c d a

k=2

F = a b c d

The total number of +1 is at most n

F can match at most n/k times.

Each position which have less then k missmatch should get at least k +1’a

38 of 96

  •  

K-mismatch (ALP 2000) - Filter

בס"ד

39 of 96

  •  

Wildcards

בס"ד

T = a b c b a b b a c b b c

P = a b b c a

T = 00 01 10 01 00 ⏀⏀ 01 01 00 10 ⏀⏀ 01 01 10

P = 00 01 ⏀⏀ 01 10 00

a = 00

b = 01

c = 10

⏀= ⏀⏀

40 of 96

  •  

L2

בס"ד

41 of 96

L2

בס"ד

42 of 96

L2

בס"ד

נשים לב שאם יש התאמה יהיה 0 אחרת מספר חיובי.

43 of 96

L2

בס"ד

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

ב-O(1) (סך הכל O(n)).

44 of 96

L2

בס"ד

קונבולוציה

O(nlogm)

45 of 96

L2

בס"ד

חישוב קבוע (פעם אחת צריך לחשב)

O(m)

46 of 96

L2 matching with wildcards (Porat 2002)

בס"ד

במקום whildcard נשים 0:

47 of 96

L2 matching with wildcards (Porat 2002)

בס"ד

כדי לתקן נצטרך לחשב כמה נצטרך להוריד בכל היסט.

החישוב יהיה בצורה הבאה:

נכתוב בכל מקום שיש wildcard 1 ובכל מקום אחר 0,

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

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

חיבור שני התוצאות הוא הסכום שצריך להוריד.

בדוגמא שלנו קיבלנו 20 במקום 2, בתיקון קיבלנו 9+9=18.

לכן 20-18=2

48 of 96

L2 matching with wildcards (Porat 2002)

בס"ד

O(nlogm)

49 of 96

One Error

בס"ד

50 of 96

  •  

סיכום

בס"ד

 

51 of 96

LCA

בס"ד

בס"ד

Question 1

52 of 96

מועד א 2022

בס"ד

53 of 96

פתרון

בס"ד

54 of 96

LCA

בס"ד

בס"ד

Question 2

55 of 96

2009 מועד ב

בס"ד

56 of 96

פתרון

בס"ד

57 of 96

LCA

בס"ד

בס"ד

Question 3

58 of 96

2007 מועד ב

בס"ד

59 of 96

פתרון

בס"ד

60 of 96

LCA

בס"ד

בס"ד

Question 4

61 of 96

שאלות לדוגמא 2022

בס"ד

62 of 96

שאלות לדוגמא 2022

בס"ד

63 of 96

LCA

בס"ד

בס"ד

Question 5

64 of 96

2009 מועד א

בס"ד

65 of 96

פתרון

בס"ד

66 of 96

LCA

בס"ד

בס"ד

Question 6

67 of 96

2009 מועד א

בס"ד

68 of 96

פתרון

בס"ד

69 of 96

LCA

בס"ד

בס"ד

Question 7

70 of 96

2004 מועד א

בס"ד

71 of 96

פתרון

בס"ד

72 of 96

LCA

בס"ד

בס"ד

Question 8

73 of 96

תרגיל בית 2

בס"ד

74 of 96

פתרון

בס"ד

75 of 96

LCA

בס"ד

בס"ד

Question 9

76 of 96

תרגיל בית 2

בס"ד

77 of 96

פתרון

בס"ד

78 of 96

LCA

בס"ד

בס"ד

Question 9

79 of 96

תרגיל בית 2011 תרגיל 9

בס"ד

80 of 96

פתרון

בס"ד

81 of 96

LCA

בס"ד

בס"ד

Question 9

82 of 96

תרגיל בית 2011 תרגיל 12

בס"ד

83 of 96

פתרון

בס"ד

84 of 96

LCA

בס"ד

בס"ד

Question 9

85 of 96

תרגיל בית 2011 תרגיל 13

בס"ד

86 of 96

פתרון

בס"ד

87 of 96

LCA

בס"ד

בס"ד

Question 9

88 of 96

תרגיל בית 2011 תרגיל 10

בס"ד

89 of 96

פתרון

בס"ד

90 of 96

LCA

בס"ד

בס"ד

Question 9

91 of 96

תרגיל בית 2011 תרגיל 10

בס"ד

92 of 96

פתרון

בס"ד

93 of 96

LCA

בס"ד

בס"ד

Question 9

94 of 96

תרגיל בית 2011 תרגיל 11

בס"ד

95 of 96

פתרון

בס"ד

96 of 96

LCA

בס"ד

בס"ד

שאלות?