0% found this document useful (0 votes)
24 views

Chap13 HW TextProcessing

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Chap13 HW TextProcessing

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Samantha Sinnerine

CSCI-313
HW#13

R-13.7

Failure table for pattern "cgtacgttcgtac":


fail[0] = 0
fail[1] = 0
fail[2] = 0
fail[3] = 0
fail[4] = 0
fail[5] = 1
fail[6] = 2
fail[7] = 3
fail[8] = 0
fail[9] = 1
fail[10] = 2
fail[11] = 3
fail[12] = 4

-----------------------------------------------------------------------------------
---------------------------------------------
C-13.28

Here is an algorithm for constructing the compact representation of a suffix trie:

1. Start with the noncompact representation of the suffix trie.


2. Traverse the trie in a bottom-up manner, starting from the leaves and moving
towards the root.
3. For each internal node encountered during the traversal:
- Check if it has only one child.
- If it has only one child, merge the node with its child by appending the incoming
edge label to the outgoing edge label.
4. Continue this process until all internal nodes with a single child are merged.
5. The resulting structure is the compact representation of the suffix trie.

The running time of the algorithm depends on the size of the noncompact
representation of the suffix trie. In the worst case, the algorithm's time
complexity can be O(n^2), where n is the length of the input string.

-----------------------------------------------------------------------------------
---------------------------------------------
C-13.29

import java.util.*;

class TrieNode {
private Map<Character, TrieNode> children;
private boolean isEndOfWord;

public TrieNode() {
children = new HashMap<>();
isEndOfWord = false;
}

public Map<Character, TrieNode> getChildren() {


return children;
}

public boolean isEndOfWord() {


return isEndOfWord;
}

public void setEndOfWord(boolean isEndOfWord) {


this.isEndOfWord = isEndOfWord;
}
}

public class Trie {


private TrieNode root;

public Trie() {
root = new TrieNode();
}

public Trie(List<String> words) {


this();
for (String word : words) {
insert(word);
}
}

public void insert(String word) {


TrieNode current = root;
for (int i = 0; i < word.length(); i++) {
char ch = word.charAt(i);
Map<Character, TrieNode> children = current.getChildren();
TrieNode node = children.get(ch);
if (node == null) {
node = new TrieNode();
children.put(ch, node);
}
current = node;
}
current.setEndOfWord(true);
}

public boolean search(String word) {


TrieNode current = root;
for (int i = 0; i < word.length(); i++) {
char ch = word.charAt(i);
Map<Character, TrieNode> children = current.getChildren();
TrieNode node = children.get(ch);
if (node == null) {
return false; // Word not found
}
current = node;
}
return current.isEndOfWord(); // Return true only if end of word marker is
set
}
}

You might also like