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

HW#4 - Spring 2021 - Solution

Uploaded by

anureddy1722
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

HW#4 - Spring 2021 - Solution

Uploaded by

anureddy1722
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

ITCS 6114/8114

Algorithm & Data Structures


Dewan Ahmed

Homework 4: Shortest Path Algorithms, Skip Lists, 2-4 Trees, Pattern Matching
Algorithms (Horspool, BM, DFA, KMP)
1. [4 points] Explain Floyd-Warshall algorithm to find all-pairs shortest path in a directed weighted graph.
See lecture slides and textbook.

2. [4 points] Consider the following graph, and trace Bellman ford algorithm. No credit will be given if all
steps are not shown in details. I am expecting 5 figures including the initialized graph.
ITCS 6114/8114
Algorithm & Data Structures
Dewan Ahmed

3. [4 points] Run Dijkstra’s algorithm on the following undirected graph, starting at vertex A. What is the
order in which vertices get removed from the priority queue? What is the resulting shortest-path tree?
No credit will be given if all steps are not shown in details.

4. [2 + 4 + 1 = 7 points] Consider the problem of searching for genes in DNA sequences using
Boyer-Moore-Horspool’s algorithm. A DNA sequence consists of a text on the alphabet {A, C, G, T}
and the gene or gene segment is the pattern.
a. Construct the shift table for the following gene segment of your chromosome 10: TCCTATTCTT
b. Apply Boyer-Moore-Horspool’s algorithm to locate the above pattern in the following DNA
sequence: TTATAGATCTCGTATTCTTTTATAGATCTCCTATTCTT
ITCS 6114/8114
Algorithm & Data Structures
Dewan Ahmed

c. Mention total comparisons required.

Solution: For the pattern TCCTATTCTT and the alphabet {A, C, G, T}, the shift table looks as
follows:

T = TTATAGATCTCGTATTCTTTTATAGATCTCCTATTCTT
P = TCCTATTCTT
A C T G/default
5 2 1 10

TTATAGATCTCGTATTCTTTTATAGATCTCCTATTCTT
TCCTATTCTT
TCCTATTCTT
TCCTATTCTT
TCCTATTCTT
TCCTATTCTT
TCCTATTCTT
TCCTATTCTT
TCCTATTCTT
TCCTATTCTT
TCCTATTCTT
TCCTATTCTT
TCCTATTCTT
TCCTATTCTT
TCCTATTCTT
Total comparisons = 2+1+2+1+8+3+3+1+2+1+1+2+1+10 = 38
ITCS 6114/8114
Algorithm & Data Structures
Dewan Ahmed

5. [2 + 4 + 1 = 7 points] Knuth-Morris-Pratt (KMP) is a linear time algorithm that solves the string
matching problem by preprocessing P (pattern) in 𝛩(𝑚) time. The main idea is to skip some
comparisons by using the previous comparison result.
a. Construct Failure Function for the given pattern.
b. Trace KMP string matching algorithm for the following text and pattern.
c. Then mention the total comparisons required.

Text: ABABABAABACABACABC
Pattern: ABACABC
Solution:
The failure table:

0 0 1 2 3 4 5 6
- A B A C A B C
-1 0 0 1 0 1 2 0
Let 𝑘 be the number of matched characters, 𝑆ℎ𝑖𝑓𝑡 𝑎𝑚𝑜𝑢𝑛𝑡 = 𝑘 – 𝜋(𝑘 − 1)

A B A B A B A A B A C A B A C A B C
A B A C A B C
A B A C A B C
A B A C A B C
A B A C A B C
A B A C A B C
A B A C A B C

Search comparisons = 23

6. [1 + 1 + 8 (1.5+1.5+4+1) = 8 points]
a) Would the Boyer-Moore algorithm work correctly with just the bad- symbol table to guide pattern
shifts?
ITCS 6114/8114
Algorithm & Data Structures
Dewan Ahmed

Yes, the Boyer-Moore algorithm can get by with just the bad-symbol shift table.

b) Would the Boyer-Moore algorithm work correctly with just the good- suffix table to guide pattern
shifts?

No: The bad-symbol table is necessary because it’s the only one used by the algorithm if the first
pair of characters does not match.

P = ABBAAB, in the following text


c) Apply Boyer-Moore algorithm to locate the pattern,
sequence: CBBAABAABBCABAAABBBABBAAB

C B B A A B A A B B C A B A A A B B B A B B A A B
A B B A A B
A B B A A B
A B B A A B
A B B A A B
A B B A A B
A B B A A B

A B C k Pattern d2
1 3 6 1 ABBAAB 3
2 ABBAAB 4
3 ABBAAB 4
ROUND d1 d2 4 ABBAAB 4
1 6-5 = 1 4
2 3-1 = 2 3 5 ABBAAB 4
3 6-2 = 4 4
4 1-3 = -2 => 1 4
5 3-2 = 1 4
Total comparison required = 6+2+3+4+3+6 = 24
ITCS 6114/8114
Algorithm & Data Structures
Dewan Ahmed

7. [5 points] Construct a top-down 2-4 tree by inserting the following list of keys in the initially empty
tree: 10, 6, 15, 31, 20, 27, 50, 44, 18. Present the 2-4 tree after each insertion.
ITCS 6114/8114
Algorithm & Data Structures
Dewan Ahmed

8. [2 + 2 + 2 = 6 points]
a. Analyze the height of a skip list.
b. Starting with an empty skip list insert the following keys, with Head(H)/Tai(T)l information
• 5, with level T
• 26, with level T
• 25, with level HHHT
• 6, with level HHT
• 21, with level T
• 3, with level HT
• 22, with level HT
After building the skip list search value 21 and 26 and mention the path.

Answer:

See lecture slides for sample.

21: 6,6,6,21

26: 25,25,25,25,26
ITCS 6114/8114
Algorithm & Data Structures
Dewan Ahmed

9. [5 points] Build DFA, both transition diagram ane transition table, from pattern P =
aabaaabb.

Transition Table:
Build from diagram.

You might also like