-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Add: kmp(Knuth-Morris-Pratt) algorithm #586
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f0a8643
remove: deprecated types
uh-zz 9f20bdc
remove: comment-out block of code
uh-zz eaec086
add: kmp algorithm
uh-zz 6ab45d0
update: reference code - kmp algorithm
uh-zz 945adb4
Merge branch 'master' into add-strings-kmp
uh-zz 78ac44c
fix: not to export: Table -> table
uh-zz 7f59345
Merge branch 'master' into add-strings-kmp
uh-zz f782e8e
Merge branch 'master' into add-strings-kmp
uh-zz 403ca60
Merge branch 'master' into add-strings-kmp
uh-zz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,117 +1,49 @@ | ||
package kmp | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
// User defined. | ||
// Set to true to read input from two command line arguments | ||
// Set to false to read input from two files "pattern.txt" and "text.txt" | ||
|
||
// const isTakingInputFromCommandLine bool = true | ||
|
||
const notFoundPosition int = -1 | ||
|
||
type Result struct { | ||
resultPosition int | ||
numberOfComparison int | ||
} | ||
|
||
// Implementation of Knuth-Morris-Pratt algorithm (Prefix based approach). | ||
// Requires either a two command line arguments separated by a single space, | ||
// or two files in the same folder: "pattern.txt" containing the string to | ||
// be searched for, "text.txt" containing the text to be searched in. | ||
// func main() { | ||
// var text string | ||
// var word string | ||
|
||
// if isTakingInputFromCommandLine { // case of command line input | ||
// args := os.Args | ||
// if len(args) <= 2 { | ||
// log.Fatal("Not enough arguments. Two string arguments separated by spaces are required!") | ||
// } | ||
// word = args[1] | ||
// text = args[2] | ||
// for i := 3; i < len(args); i++ { | ||
// text = text + " " + args[i] | ||
// } | ||
// } else { // case of file input | ||
// patFile, err := ioutil.ReadFile("../pattern.txt") | ||
// if err != nil { | ||
// log.Fatal(err) | ||
// } | ||
// textFile, err := ioutil.ReadFile("../text.txt") | ||
// if err != nil { | ||
// log.Fatal(err) | ||
// } | ||
// text = string(textFile) | ||
// word = string(patFile) | ||
// } | ||
// Kmp Function kmp performing the Knuth-Morris-Pratt algorithm. | ||
func Kmp(word, text string, patternTable []int) []int { | ||
if len(word) > len(text) { | ||
return nil | ||
} | ||
|
||
// if len(word) > len(text) { | ||
// log.Fatal("Pattern is longer than text!") | ||
// } | ||
// fmt.Printf("\nRunning: Knuth-Morris-Pratt algorithm.\n\n") | ||
// fmt.Printf("Search word (%d chars long): %q.\n", len(word), word) | ||
// fmt.Printf("Text (%d chars long): %q.\n\n", len(text), text) | ||
var ( | ||
i, j int | ||
matches []int | ||
) | ||
for i+j < len(text) { | ||
|
||
// r := kmp(text, word) | ||
// if r.resultPosition == notFoundPosition { | ||
// fmt.Printf("\n\nWord was not found.\n%d comparisons were done.", r.numberOfComparison) | ||
// } else { | ||
// fmt.Printf("\n\nWord %q was found at position %d in %q. \n%d comparisons were done.", word, | ||
// r.resultPosition, text, r.numberOfComparison) | ||
// } | ||
// } | ||
if word[j] == text[i+j] { | ||
j++ | ||
if j == len(word) { | ||
matches = append(matches, i) | ||
|
||
// Kmp Function kmp performing the Knuth-Morris-Pratt algorithm. | ||
// Prints whether the word/pattern was found and on what position in the text or not. | ||
// m - current match in text, i - current character in w, c - amount of comparisons. | ||
func Kmp(text string, word string) Result { | ||
m, i, c := 0, 0, 0 | ||
t := kmpTable(word) | ||
for m+i < len(text) { | ||
fmt.Printf("\n comparing characters %c %c at positions %d %d", text[m+i], word[i], m+i, i) | ||
c++ | ||
if word[i] == text[m+i] { | ||
fmt.Printf(" - match") | ||
if i == len(word)-1 { | ||
return Result{ | ||
m, c, | ||
} | ||
i = i + j | ||
j = 0 | ||
} | ||
i++ | ||
} else { | ||
m = m + i - t[i] | ||
if t[i] > -1 { | ||
i = t[i] | ||
i = i + j - patternTable[j] | ||
if patternTable[j] > -1 { | ||
j = patternTable[j] | ||
} else { | ||
i = 0 | ||
j = 0 | ||
} | ||
} | ||
} | ||
return Result{notFoundPosition, | ||
c, | ||
} | ||
return matches | ||
} | ||
|
||
// Table building algorithm. | ||
// Takes word to be analyzed and table to be filled. | ||
func kmpTable(word string) (t []int) { | ||
t = make([]int, len(word)) | ||
pos, cnd := 2, 0 | ||
t[0], t[1] = -1, 0 | ||
for pos < len(word) { | ||
if word[pos-1] == word[cnd] { | ||
cnd++ | ||
t[pos] = cnd | ||
pos++ | ||
} else if cnd > 0 { | ||
cnd = t[cnd] | ||
} else { | ||
t[pos] = 0 | ||
pos++ | ||
// Table building for kmp algorithm. | ||
func Table(w string) []int { | ||
var ( | ||
t []int = []int{-1} | ||
k int | ||
) | ||
for j := 1; j < len(w); j++ { | ||
k = j - 1 | ||
for w[0:k] != w[j-k:j] && k > 0 { | ||
raklaptudirm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
k-- | ||
} | ||
t = append(t, k) | ||
} | ||
return t | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.