CSE 2320 Section 501/571 Fall 1999

**Homework 5 Solution**

- 1.
- TOPOLOGICAL-SORT
- 2.
- STRONGLY-CONNECTED-COMPONENTS
- 3.
- MST-PRIM using vertex
*a*as the root vertex. - 4.
- Execution of the Edmonds-Karp algorithm.
- 5.
- Compute the Knuth-Morris-Pratt prefix function
for the
following patterns when the alphabet is
.
- (a)
`abccbabbcaabcbabccbabcba`

*i*1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 *P*[*i*]a b c c b a b b c a a b c b a b c c b a b c b a 0 0 0 0 0 1 2 0 0 1 1 2 3 0 1 2 3 4 5 6 7 3 0 1

- (b)
`aaaaaaaabbbbbbbbaaaaaaaa`

*i*1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 *P*[*i*]a a a a a a a a b b b b b b b b a a a a a a a a 0 1 2 3 4 5 6 7 0 0 0 0 0 0 0 0 1 2 3 4 5 6 7 8

- 6.
- Determine a possible pattern (if any) consistent with the following
KMP prefix functions when the alphabet is
.
- (a)
*i*1 2 3 4 5 6 7 8 0 1 2 3 0 1 0 1 *P*[*i*]a a a a b a b a The general form of the pattern has characters 1-4, 6 and 8 the same, and characters 5 and 7 different from the others.

- (b)
*i*1 2 3 4 5 6 7 8 0 1 2 1 2 1 2 1 No pattern would generate this prefix function, because the first three characters must be the same to generate a prefix of 012. And means that

*P*[4]=*P*[1]=*a*. But then*P*[1..4] are the same, and therefore, would be 3, not 1.

- (c)
*i*1 2 3 4 5 6 7 8 0 0 0 1 1 0 0 0 *P*[*i*]a b b a a c c c The general form of the pattern is that characters 1, 4 and 5 must be the same. Characters 2, 3, 6, 7 and 8 must be different from

*P*[1], and characters 2 and 6 must be different from eachother.