Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
Algorithmen und Datenstrukturen
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Skripte
Algorithmen und Datenstrukturen
Commits
349f4dc1
Commit
349f4dc1
authored
7 months ago
by
Wolfgang Mulzer
Browse files
Options
Downloads
Patches
Plain Diff
Fix a notation clash and some typos.
parent
ccd6d400
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
33-hashfunc.tex
+11
-10
11 additions, 10 deletions
33-hashfunc.tex
44-stringsearch.tex
+7
-6
7 additions, 6 deletions
44-stringsearch.tex
skript.pdf
+0
-0
0 additions, 0 deletions
skript.pdf
with
18 additions
and
16 deletions
33-hashfunc.tex
+
11
−
10
View file @
349f4dc1
...
...
@@ -87,29 +87,30 @@ for different kinds of key sets.
would be ``1100101'' or ``10101`.
To define a hash code for this case, we first pick an arbitrary,
but fixed numbering
$
|
\cdot
|
$
of the symbols in
$
\Sigma
$
with
but fixed numbering
$
\
|
\cdot
\
|
$
of the symbols in
$
\Sigma
$
with
the numbers from
$
0
$
to
$
|
\Sigma
|
-
1
$
.
For example, for the Latin alphabet, we could take
$
|A|
=
0
, |B|
=
1
, |C|
=
2
,
\dots
, |Z|
=
25
$
, or for the
binary alphabet, we could take
$
|
0
|
=
0
, |
1
|
=
1
$
. Now,
$
\
|
A
\
|
=
0
,
\
|
B
\
|
=
1
,
\
|
C
\
|
=
2
,
\dots
,
\
|
Z
\
|
=
25
$
, or for the
binary alphabet, we could take
$
\
|
0
\
|
=
0
,
\
|
1
\
|
=
1
$
. Now,
given a string
$
\tau
$
over an alphabet
$
\Sigma
$
, we write
$
\tau
$
as a sequence as symbols:
$
\tau
=
\sigma
_
0
\sigma
_
1
\dots
\sigma
_{
\ell
-
1
}$
,
where
$
\ell
$
is the total number of symbols in
$
\tau
$
.
For example, if
$
\tau
=
\text
{
HELLO
}$
,
we have
$
\ell
=
5
$
and
$
\sigma
_
0
=
H,
\sigma
_
1
=
E,
\sigma
_
2
=
L,
\sigma
_
3
=
L,
\sigma
_
4
=
O
$
.
$
\sigma
_
0
=
\text
{
H
}
,
\sigma
_
1
=
\text
{
E
}
,
\sigma
_
2
=
\text
{
L
}
,
\sigma
_
3
=
\text
{
L
}
,
\sigma
_
4
=
\text
{
O
}$
.
Then we define
\[
\text
{
hc
}
(
\tau
)
=
\sum
_{
i
=
0
}^{
\ell
-
1
}
|
\sigma
_
i|
\cdot
|
\Sigma
|
^
i.
\text
{
hc
}
(
\tau
)
=
\sum
_{
i
=
0
}^{
\ell
-
1
}
\
|\sigma
_
i
\
|
\cdot
|
\Sigma
|
^
i.
\]
In other words, we interpret
$
\tau
$
as a ``number`` to base
$
|
\Sigma
|
$
, where the
''digits'' are represented by the symbols of
$
\Sigma
$
. For example,
\begin{align*}
\text
{
hc
}
(HALLO)
&
=
|H
|
\cdot
26
^
0 +
|A
|
\cdot
26
^
1 +
|L
|
\cdot
26
^
2 +
|L
|
\cdot
26
^
3 +
|O
|
\cdot
26
^
4
\\
\text
{
hc
}
(
\text
{
HALLO
}
)
&
=
\|\text
{
H
}
\
|
\cdot
26
^
0 +
\|\text
{
A
}
\
|
\cdot
26
^
1 +
\|\text
{
L
}
\
|
\cdot
26
^
2 +
\|\text
{
L
}
\
|
\cdot
26
^
3 +
\|\text
{
O
}
\
|
\cdot
26
^
4
\\
&
= 7
\cdot
1 + 0
\cdot
26 + 11
\cdot
26
^
2 + 11
\cdot
26
^
3
+ 14
\cdot
26
^
4
\\
&
= 6.598.443.
...
...
This diff is collapsed.
Click to expand it.
44-stringsearch.tex
+
7
−
6
View file @
349f4dc1
...
...
@@ -237,15 +237,15 @@ we described the following way for computing a hash function $h'$
for a string
$
a
=
\alpha
_
0
\alpha
_
1
\dots
\alpha
_{
\ell
-
1
}$
: pick a prime number
$
p
$
and interpret the individual
symbols
$
\alpha
_
i
$
as numbers between
$
0
$
and
$
|
\Sigma
|
-
1
$
. Then, set
$
\alpha
_
i
$
as numbers
$
\|
\alpha
_
i
\|
$
between
$
0
$
and
$
|
\Sigma
|
-
1
$
. Then, set
\[
h'
(
a
)
=
\left
(
\sum
_{
j
=
0
}^{
\ell
-
1
}
\alpha
_
j
|
\Sigma
|
^{
j
}
\right
)
\bmod
p.
h'
(
a
)
=
\left
(
\sum
_{
j
=
0
}^{
\ell
-
1
}
\|
\alpha
_
j
\|
|
\Sigma
|
^{
j
}
\right
)
\bmod
p.
\]
For Rabin-Karp, it turns out to be advantageous to define the hash function slightly
differently:
\[
h
(
\sigma
_
i
\dots
\sigma
_{
i
+
\ell
-
1
}
)
=
\left
(
\sum
_{
j
=
0
}^{
\ell
-
1
}
\sigma
_{
i
+
j
}
|
\Sigma
|
^{
\ell
-
1
-
j
}
\right
)
\bmod
p.
\left
(
\sum
_{
j
=
0
}^{
\ell
-
1
}
\|
\sigma
_{
i
+
j
}
\|
|
\Sigma
|
^{
\ell
-
1
-
j
}
\right
)
\bmod
p.
\]
There are two main differences between
$
h
$
and
$
h'
$
:
\begin{enumerate}
...
...
@@ -260,8 +260,8 @@ Now, the main point is that
Indeed, we have
\[
h
(
\sigma
_{
i
+
1
}
\dots
\sigma
_{
i
+
\ell
}
)
=
\left
(
|
\Sigma
|
\cdot
h
(
\sigma
_
i
\dots
\sigma
_{
i
+
\ell
-
1
}
)-
|
\Sigma
|
^
\ell
\cdot
\sigma
_
i
+
\sigma
_{
i
+
\ell
}
\right
)
\bmod
p.
\left
(
|
\Sigma
|
\cdot
h
(
\sigma
_
i
\dots
\sigma
_{
i
+
\ell
-
1
}
)-
|
\Sigma
|
^
\ell
\cdot
\|
\sigma
_
i
\|
+
\|
\sigma
_{
i
+
\ell
}
\
|\
right
)
\bmod
p.
\]
(Note that we can precompute
$
|
\Sigma
|
^
\ell
$
in advance and can reuse it every time
we update the hash function). We call
$
h
$
a
\emph
{
rolling hash
}
.
...
...
@@ -327,7 +327,8 @@ It remains to explain how to find a random prime number between
$
2
$
and
$
\ell
^
2
\log
(
|
\Sigma
|
\ell
)
$
. For this, we simply take a random
number between
$
2
$
and
$
\ell
^
2
\log
(
|
\Sigma
|
\ell
)
$
and check if it is a prime number.
If the test fails, we repeat. By the prime number theorem, the probability that we find a
prime number of
$
\Omega
(
1
/
$
. Thus, we need
$
O
(
\log
(
\ell
\Sigma
))
$
attempts in expectation to
prime number is
$
\Omega
(
1
/
\log
(
\ell
|
\Sigma
|
))
$
.
Thus, we need
$
O
(
\log
(
\ell
|
\Sigma
|
))
$
attempts in expectation to
find such a number. There are very efficient algorithms testing whether a given number is a prime
number. Thus, the time for this step is negligible.
\end{proof}
...
...
This diff is collapsed.
Click to expand it.
skript.pdf
+
0
−
0
View file @
349f4dc1
No preview for this file type
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment