Různé algoritmy mají různou složitost

Transkript

Různé algoritmy mají různou složitost
X36DSA 2005
The complexity of different algorithms varies: O(n), Ω(n2), Θ(n·log2(n)), …
DSA
Různé algoritmy
mají
různou složitost
Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …
1 / 12a
X36DSA 2005
The complexity of different algorithms varies: O(n), Ω(n2), Θ(n·log2(n)), …
DSA
The complexity
of different algorithms
varies
Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …
2 / 12a
X36DSA 2005
The complexity of different algorithms varies: O(n), Ω(n2), Θ(n·log2(n)), …
String matching
Σ – alphabet , e.g. Σ = {a, b, c, d .... }
string ∈ Σ*
b f l m ...
...
... p s v z
suffix
prefix
prefix
suffix
h k r d
...
h k r d
border
Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …
3 / 12a
The complexity of different algorithms varies: O(n), Ω(n2), Θ(n·log2(n)), …
X36DSA 2005
4 / 12a
Naive algorithm
worst case
a a a a a a a a a a a a a a
match
a a a a c
mismatch
a a a a a a a a a a a a a a
a a a a c
...
a a a a a a a a a a a a a a
a a a a c
Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …
The complexity of different algorithms varies: O(n), Ω(n2), Θ(n·log2(n)), …
X36DSA 2005
5 / 12a
Naive algorithm
best case
a a a a a a a a a a a a a a
match
c a a a a
mismatch
a a a a a a a a a a a a a a
c a a a a
...
a a a a a a a a a a a a a a
c a a a a
Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …
The complexity of different algorithms varies: O(n), Ω(n2), Θ(n·log2(n)), …
X36DSA 2005
Karp-Rabin Algorithm
t5:
t6:
T:
5 3 2
3 2 3
φ (t5) = 532
φ (t6) = 323
1 2 3 4 5 3 2 3 5 6 3 1
1
2
3
4
5
6
7
8
9 10 11 12
φ (t5) = t7+10(t6+10(t5)) = 2 + 10(3+10(5)) = 2 + 10(3+50) =
= 2+10·53 = 532
φ (t6) = 10(φ (t5) – 102 · t5) + t5+3 = 10(532 – 100·5) + 3 =
= 10(532 – 500) + 3 = 10·32 + 3 = 323
Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …
6 / 12a
X36DSA 2005
The complexity of different algorithms varies: O(n), Ω(n2), Θ(n·log2(n)), …
Knuth–Morris–Pratt algorithm
... x y a b c d b a b c d a b g h a g a b ...
a b c d b a b c f g h
... x y a b c d b a b c d a b g h a g a b ...
a b c d b a b c f g h
longest border (=prefix&suffix)
... x y a b c d b a b c d a b g h a g a b ...
shift = 5
a b c d b a b c f g h
Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …
7 / 12a
The complexity of different algorithms varies: O(n), Ω(n2), Θ(n·log2(n)), …
X36DSA 2005
8 / 12a
Knuth - Morris - Pratt algorithm
j
1
2
3
4
5
6
7
8
9
Longest border size
in substr (p1,...,pj)
10 11
pj
a b c d b a b c f g h
φ’(pj) 0 0 0 0 0 1 2 3 0 0 0
pj+1 = j – φ’(pj)
shift after mismatch ti
...w a b c d b a b w d a b g h a g a b ...
j=7
shift = 7 – φ’(p7) = 7 – 2 = 5
a b c d b a b c f g h
1 2
3
4
5
6
7
8
9
10 11
a b c d b a b c f g h
1 2
3
4
5
6
7
8
9
10 11
Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …
The complexity of different algorithms varies: O(n), Ω(n2), Θ(n·log2(n)), …
X36DSA 2005
Knuth - Morris - Pratt algorithm
computing φ’(pj) from φ’(pj–1 )
1 2
...
k k+1
j–1 j
a b... x z ... a b... x z
k
k
k = φ’(j–1) = size of longest border in (p1,p2, ... pj–1)
if pk+1= pj then φ’(pj) = k+1
1 2
...
k k+1
j–1 j
a b... x z ... a b... x z
k+1
k+1
Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …
9 / 12a
The complexity of different algorithms varies: O(n), Ω(n2), Θ(n·log2(n)), …
X36DSA 2005
10 / 12a
Knuth - Morris - Pratt algorithm
computing φ’(pj) from φ’(pj–1 )
if pk+1 ≠ pj then
longest border of (p1,...,pj)
might be the longest border of (p1,...,pk) ...
y?
1 2
...
k k+1
j–1 j
a b… s ? … a b… s z ... a b… s ? … a b… s y
k
k
φ’(k)
... and if it is not then continue search for the longest border
in (p1,...,pφ’(k)) recursively
Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …
X36DSA 2005
The complexity of different algorithms varies: O(n), Ω(n2), Θ(n·log2(n)), …
Knuth - Morris - Pratt algorithm
Compute shift function φ’
1: φ’(1) ← 0
2: k ← 0
3: for j ← 2..length(P) do
4: while k > 0 and pk+1 ≠ pj do
5:
k ← φ’[k]
6: end while
7: if pk+1 = pj then k ← k+1 end if
8: φ’(j) ← k
9: end for
Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …
11 / 12a
The complexity of different algorithms varies: O(n), Ω(n2), Θ(n·log2(n)), …
X36DSA 2005
Boyer - Moore algorithm
a b c d b a b c f g h
p
BCS[p]
a b c d f g h other
5 4 3 7 3 1 0
11
T:
... x y a b c d b a b c d a c g h a g a b ...
P:
a b c d b a b c f g h BCS[b] = 4
4
a b c d b a b c f g h BCS[c] = 3
a b c d b a b c f g h BCS[a] = 5
3
a b c d b a b c f g h
5
Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …
12 / 12a
The complexity of different algorithms varies: O(n), Ω(n2), Θ(n·log2(n)), …
X36DSA 2005
13 / 12a
Finite automaton
0..9
0
+, –
e ls e
29 . 74
0..9
1 e
lse
0..9
0..9
.
2
else
3
se
l
e
0..9
els
4
e
5
all
0..9
29 . 74
0
+, –
e ls e
0..9
1 e
lse
0..9
0..9
2
.
else
3
se
l
e
5
all
Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …
0..9
els
4
e
The complexity of different algorithms varies: O(n), Ω(n2), Θ(n·log2(n)), …
X36DSA 2005
14 / 12a
Finite automaton
0..9
0
+, –
e ls e
29 . 74
0..9
0..9
0..9
1 e
lse
.
2
else
3
se
l
e
0..9
els
4
e
5
all
0..9
29 . 74
0
+, –
e ls e
0..9
1 e
lse
0..9
0..9
2
.
else
3
se
l
e
5
all
Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …
0..9
els
4
e
The complexity of different algorithms varies: O(n), Ω(n2), Θ(n·log2(n)), …
X36DSA 2005
15 / 12a
Finite automaton
0..9
0..9
0
+, –
e ls e
29 . 74
0..9
1 e
lse
0..9
.
2
3
se
l
e
else
0..9
4
els
e
5
all
0..9
29 . 74
0
4 is an accepting state
—
29.74 is accepted
+, –
e ls e
0..9
1 e
lse
0..9
0..9
2
.
else
3
se
l
e
5
all
Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …
0..9
els
4
e
The complexity of different algorithms varies: O(n), Ω(n2), Θ(n·log2(n)), …
X36DSA 2005
16 / 12a
Finite automaton
29 . 74
29 . 74
0
1
2
3
4
5
0 1 2
...
9
.
+ –
2
2
2
4
4
5
...
...
...
...
...
...
2
2
2
4
4
5
5
5
3
5
5
5
1
5
5
5
5
5
2
2
2
4
4
5
2
2
2
4
4
5
1
5
5 F
5
5 F
5
0
1
2
3
4
5
0 1 2
...
9
.
+ –
2
2
2
4
4
5
...
...
...
...
...
...
2
2
2
4
4
5
5
5
3
5
5
5
1
5
5
5
5
5
2
2
2
4
4
5
2
2
2
4
4
5
Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …
1
5
5 F
5
5 F
5
The complexity of different algorithms varies: O(n), Ω(n2), Θ(n·log2(n)), …
X36DSA 2005
17 / 12a
Finite automaton
29 . 74
29 . 74
0
1
2
3
4
5
0 1 2
...
9
.
+ –
2
2
2
4
4
5
...
...
...
...
...
...
2
2
2
4
4
5
5
5
3
5
5
5
1
5
5
5
5
5
2
2
2
4
4
5
2
2
2
4
4
5
1
5
5 F
5
5 F
5
0
1
2
3
4
5
0 1 2
...
9
.
+ –
2
2
2
4
4
5
...
...
...
...
...
...
2
2
2
4
4
5
5
5
3
5
5
5
1
5
5
5
5
5
2
2
2
4
4
5
2
2
2
4
4
5
Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …
1
5
5 F
5
5 F
5
The complexity of different algorithms varies: O(n), Ω(n2), Θ(n·log2(n)), …
X36DSA 2005
18 / 12a
Finite automaton
29 . 74
29 . 74
0
1
2
3
4
5
0 1 2
...7 9
.
+ –
2
2
2
4
4
5
...
...
...
...4
...
...
5
5
3
5
5
5
1
5
5
5
5
5
2
2
2
4
4
5
2
2
2
4
4
5
2
2
2
4
4
5
1
5
5 F
5
5 F
5
0
1
2
3
4
5
0 1 2 4...
9
.
+ –
2
2
2
4
4
5
2
2
2
4
4
5
5
5
3
5
5
5
1
5
5
5
5
5
2
2
2
4
4
5
2 ...
2 ...
2 ...
4 ...
4 4...
5 ...
Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …
1
5
5 F
5
5 F
5
The complexity of different algorithms varies: O(n), Ω(n2), Θ(n·log2(n)), …
X36DSA 2005
19 / 12a
Finite automaton
0..9
0..9
82+–
0
+, –
e ls e
0..9
1 e
lse
0..9
.
2
else
3
se
l
e
0..9
els
4
e
5
all
0..9
0..9
82+–
0
+, –
e ls e
0..9
1 e
lse
2
0..9
.
else
3
se
l
e
5
all
Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …
0..9
els
4
e
The complexity of different algorithms varies: O(n), Ω(n2), Θ(n·log2(n)), …
X36DSA 2005
20 / 12a
Finite automaton
0..9
0..9
0
+, –
e ls e
82+–
0..9
1 e
lse
0..9
.
2
else
3
se
l
e
0..9
els
4
e
5
all
0..9
0..9
82+–
0
+, –
e ls e
0..9
1 e
lse
2
0..9
.
else
3
se
l
e
5
all
Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …
0..9
els
4
e
The complexity of different algorithms varies: O(n), Ω(n2), Θ(n·log2(n)), …
X36DSA 2005
21 / 12a
Finite automaton
0..9
0..9
0
82+–
+, –
e ls e
0..9
1 e
lse
2
0..9
.
else
3
se
l
e
0..9
els
5
all
5 is not accepting state —
“82+–”
is not accepted
Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …
4
e
The complexity of different algorithms varies: O(n), Ω(n2), Θ(n·log2(n)), …
X36DSA 2005
22 / 12a
Finite automaton
0..9
0
+, –
e ls e
0..9
1 e
lse
0..9
0..9
2
.
else
3
se
l
e
0
0..9
els
0 1 2
2
1 2
2
2
3 4
4
4
5 5
4
e
5
all
Q: finite set of states
2
2
2
4
4
5
2
2
2
4
4
5
...
9
.
+ –
...
...
...
...
...
...
2
2
2
4
4
5
5
5
3
5
5
5
1
5
5
5
5
5
0 1 2 3 4 5
.
Σ: finite alphabet {0,1,2,...,9, ,+, –}
δ: mapping Q× Σ → Q, all labelled arows or the whole table
q0: initial state
0
F: set of final (accepting) states
2
4
Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …
1
5
5 F
5
5 F
5
X36DSA 2005
The complexity of different algorithms varies: O(n), Ω(n2), Θ(n·log2(n)), …
Construction of String matching automaton
b a n c d ...
0
b 1
a 2
n 3
a 4
n 5
a 6
0 0 0 0 0 ...
...
...
...
...
...
...
F
Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …
23 / 12a
The complexity of different algorithms varies: O(n), Ω(n2), Θ(n·log2(n)), …
X36DSA 2005
Construction of String matching automaton
b a n c d ...
0
b 1
a 2
n 3
a 4
n 5
a 6
11 0 0 0 0 ...
1 0 0 0 0 ...
...
...
...
...
... F
i=1
r = δ(0,b) = 0
Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …
24 / 12a
X36DSA 2005
The complexity of different algorithms varies: O(n), Ω(n2), Θ(n·log2(n)), …
Construction of String matching automaton
b a n c d ...
0
b 1
a 2
n 3
a 4
n 5
a 6
11 0 0 0 0 ...
1 2 0 0 0 ...
1 0 0 0 0 ...
...
...
...
...
i=2
r = δ(1,a) = 0
F
Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …
25 / 12a
X36DSA 2005
The complexity of different algorithms varies: O(n), Ω(n2), Θ(n·log2(n)), …
Construction of String matching automaton
b a n c d ...
0
b 1
a 2
n 3
a 4
n 5
a 6
11
1
1
1
0
2
0
0
0
0
3
0
0
0
0
0
0
0
0
0
...
...
...
...
...
...
...
i=3
r = δ(2,n) = 0
F
Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …
26 / 12a
X36DSA 2005
The complexity of different algorithms varies: O(n), Ω(n2), Θ(n·log2(n)), …
Construction of String matching automaton
b a n c d ...
0
b 1
a 2
n 3
a 4
n 5
a 6
11
1
1
1
1
1
1
0
2
0
4
0
6
0
0
0
3
0
5
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...
...
...
...
...
...
...
i=6
F
r = δ(5,a) = 0
Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …
27 / 12a
X36DSA 2005
The complexity of different algorithms varies: O(n), Ω(n2), Θ(n·log2(n)), …
DSA
Různé algoritmy
mají
různou složitost
Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …
28 / 12a
X36DSA 2005
The complexity of different algorithms varies: O(n), Ω(n2), Θ(n·log2(n)), …
DSA
The complexity
of different algorithms
varies
Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …
29 / 12a

Podobné dokumenty

18.9.2005 – Lednice – Mistrovství ČR, mistrovství SK

18.9.2005 – Lednice – Mistrovství ČR, mistrovství SK 1.Katastrofa Lu-Kon (L.Konig, Česká Lípa) – 37,78 2.Kess Lu-Kon (A.Routnerová, Bakov n.J.) 3.J.Joplin Gandamak (Vojtekovi, Bratislava) - mistr SK 4.Fibi Gandamak (Vojtekovi, Bratislava) Afgánský ch...

Více

Černokostelecký zpravodaj 2/04

Černokostelecký zpravodaj 2/04 ! Zůstává zachována tzv. kapitační platba za tuhý domovní odpad - každý trvale přihlášený občan (kromě dětí do 4 let) platí stejnou částku - letos je to 440 Kč za rok. Dům může mít libovolný počet ...

Více

Implementace ICMP Pingu v PHP

Implementace ICMP Pingu v PHP # SOCK_RAW => poskytne možnost manuálně vytvořit libovolnou konstrukci paketu $socket = socket_create(AF_INET, SOCK_RAW, 1); # Nastaví Tmeout a připojí se k danému "hostu". socket_set_option($socke...

Více

hw-sw okruhy

hw-sw okruhy To lze vidět i v tabulce na obrázku, kde výstupy jsou kódované ke stavům (pouze jeden  sloupec u výstupů). Oproti tomu výstupní písmeno Mealyho automatu závisi na vstupním  písmenu i vnitřním stav...

Více