algorithms and data structures ece250- fall 2017 ...ece250/assignments/a3-solution.pdf · ece250...

5
Algorithms and Data Structures ECE250- Fall 2017 Assignment 3 Solution Set

Upload: nguyenkhanh

Post on 25-Apr-2018

217 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Algorithms and Data Structures ECE250- Fall 2017 ...ece250/assignments/A3-Solution.pdf · ECE250 Assignment 3-Solution 3 A3-Q3: Solution (a) Table ientry of table W stores the weight

Algorithms and Data Structures ECE250- Fall 2017

Assignment 3 Solution Set

Page 2: Algorithms and Data Structures ECE250- Fall 2017 ...ece250/assignments/A3-Solution.pdf · ECE250 Assignment 3-Solution 3 A3-Q3: Solution (a) Table ientry of table W stores the weight

ECE250 Assignment 3-Solution 2

A3-Q1: Solution

1) Create max-heap of size n à O(n) 2) Remove max k times à O(klogn) 3) Return the last element removes à O(1)

Total: O(n+klogn)

A3-Q2: Solution

One way to solve this problem is by a radix sort from right to left. Since the strings have varying lengths, however, we have to pad out all strings that are shorter than the longest string. The padding is on the right end of the string, and it’s with a special character that is lexicographically less than any other character (e.g., in C, the character ‘\0’ with ASCII value 0). Of course, we don’t have to actually change any string; if we want to know the jth character of a string whose length is k, then if j > k, the jth character is the pad character.

Unfortunately, this scheme does not always run in the required time bound. Suppose that there are m strings and that the longest string has d characters. In the worst case, one string has n/2 characters and, before padding, n/2 strings have one character each. As in part (a), we would have d = n/2 and m = n/2 + 1. We still have to examine the pad characters in each pass of radix sort, even if we don’t actually create them in the strings. Assuming that the range of a single character is constant, the running time of radix sort would be Θ(dm) = Θ(n2). To solve the problem in O(n) time, we use the property that, if the first letter of string x is lexicographically less than the first letter of string y, then x is lexicographically less than y, regardless of the lengths of the two strings. We take advantage of this property by sorting the strings on the first letter, using counting sort. We take an empty string as a special case and put it first. We gather together all strings with the same first letter as a group. Then we recurs, within each group, based on each string with the first letter removed. The correctness of this algorithm is straightforward. Analyzing the running time is a bit trickier. Let us count the number of times that each string is sorted by a call of counting sort. Suppose that the ith string, si, has length li. Then si is sorted by at most li + 1 counting sorts. (The “+1” is because it may have to be sorted as an empty string at some point; for example, ab and a end up in the same group in the first pass and are then ordered based on b and the empty string in the second pass. The string a is sorted its length, 1, time plus one more time.) A call of counting sort on t strings take Θ(t) time (remembering that the number of different characters on which we are sorting is a constant.)

Thus, the total time for all calls of counting sort is

O l! + 1!

!!!

= O l! +m!

!!!

= O n +m

= O(n)

where the second line follows from l! !

!!! = n, and the last line is because m ≤ n.

Page 3: Algorithms and Data Structures ECE250- Fall 2017 ...ece250/assignments/A3-Solution.pdf · ECE250 Assignment 3-Solution 3 A3-Q3: Solution (a) Table ientry of table W stores the weight

ECE250 Assignment 3-Solution 3

A3-Q3: Solution

(a) Table entry i of table W stores the weight of a maximum matching for the path v1,v2,...,vi . Entry j of table M stores a pair of pointers (sub,e) , where sub is a

pointer to another entry in M that describes a part of the solution for the path v1,v2,...,vi and e is the last edge that completes the solution. The matching

stored in M[ j] can be obtained combining edges of sub and edge e. e might be nil

if the solution for the path v1,v2,...,vi is the same as the solution pointed by sub.

(b)

W i =0 if i = 1w! if i = 2max w! +W i − 2 ,W i − 1 otherwise

M i =

(nil, nil) if i = 1nil, e! if i = 2M i − 2 , e!!! if i > 2 ∧ ( w! +W i − 2 > W(i − 1)) M i − 1 , nil otherwise

(c) W is array of numbers M is array of pairs (pointer, edge)

M 0 ≔ 0, nil W[0]:=0

M[1]:=(0, e!) W[1]:= w!

for i:=3 to n if w!+W[i-2]>W[i-1] M[i]:= (pointer_to(M[i − 2]), e!!!)

W i := w! +W i − 2

else M i ≔ (pointer_to(M i − 1 , nil) W i ≔ W i − 1 print W n print edges in M n

(d) Each iteration of the loop performs a constant number of operations. Printing out edges of the matching takesO(n) . Therefore the running time of the algorithm is

)(nO .

Page 4: Algorithms and Data Structures ECE250- Fall 2017 ...ece250/assignments/A3-Solution.pdf · ECE250 Assignment 3-Solution 3 A3-Q3: Solution (a) Table ientry of table W stores the weight

ECE250 Assignment 3-Solution 4

A3-Q4: Solution Create a graph G where each vertex represents a wrestler and each edge represents a rivalry. The graph will contain n vertices and r edges. Perform as many as BFS’s needed to visit all vertices. Assign all wrestlers whose distance is even to be good guys and all wrestlers whose distance is odd to be bad guys. Then, check each edge to verify that it goes between a good guy and a bad guy. This solution would take O(n + r) time for the BFS, O(n) time to designate each wrestler as a good guy or bad guy, and O(r) time to check edges, which is O(n + r) time overall. A3-Q5: Solution We can use the Bellman-Ford algorithm on a suitable weighted, directed graph G = (V, E), which we form as follows. There is one vertex in V for each currency, and for each pair of currencies ci, and cj, there are directed edges v!, v! and v!, v! . (Thus, |V| = n and |E| = !

! .) To determine edge weights, we start by observing that R[i1, i2]. R[i2, i3]… R[ik-1, ik]. R[ik, i1] > 1 If and only if

1R[i!, i!]

.1

R i!, i!…

1R i!!!, i!

.1

R i!, i!< 1

Taking logs of both sides of the inequality above, we express this condition as

lg1

R[i!, i!]+ lg

1R i!, i!

+⋯+ lg1

R i!!!, i!+ lg

1R i!, i!

< 0

Therefore, if we define the weight of edge v!, v! as

w v!, v! = lg1

R i, j

= − lgR i, j , Then, we want to find whether there exists a negative-weight cycle in G with these edge weights. We can determine whether there exists a negative-weight cycle in G by adding an extra vertex v! with 0-weight edges v!, v! for all v! ∈ V, running BELLMAN-FORD from v!, and using the boolean result of BELLMAN-FORD (which is TRUE if there are no negative-weight cycles and FALSE if there is a negative-weight cycle) to guide our answer. That is, we invert the Boolean result of BELLMAN-FORD. This method works because adding the new vertex v! with 0-weight edges form v! to all other vertices cannot introduce any new cycles, yet it ensures that all negative-weight cycles are reachable from v!. It takes Θ(n2) time to create G, which has Θ(n2) edges. Then it takes Θ(n3) time to run BELLMAN-FORD. Thus, the total time is Θ(n3).

Page 5: Algorithms and Data Structures ECE250- Fall 2017 ...ece250/assignments/A3-Solution.pdf · ECE250 Assignment 3-Solution 3 A3-Q3: Solution (a) Table ientry of table W stores the weight

ECE250 Assignment 3-Solution 5

Another way to determine whether a negative-weight cycle exist to create G and, without adding v! and its incident edges, run either of all-pairs shortest paths algorithms. If the resulting shortest-path distance matrix has any negative values on the diagonal, then there is a negative-weight cycle. A3-Q6: Solution Here are two ways to detect negative-weight cycles: 1. Check the main-diagonal entries of the result matrix for a negative value. There is a negative weight cycle if and only if d!!

(!) < 0 for some vertex i:

• d!!(!) is a path weight from i to itself; so if it is negative, there is a path from i to itself

(i.e., a cycle), with negative weight. • If there is a negative-weight cycle, consider the one with the fewest vertices.

o If it has one vertex, then some w!! < 0, so d!! starts out negative, and since d values are never increased, it is also negative when the algorithm terminates.

o If it has at least two vertices, let k be the highest-numbered vertex in the cycle, and let i be some other vertex in the cycle. d!"

(!!!) and d!"(!!!) have correct

shortest-path weights, because they are not based on negative-weight cycles. (Neither d!"

(!!!) nor d!"(!!!) can include k as an intermediate vertex, and i and k

are on the negative-weight cycle with the fewest vertices.) Since i → k → i is a negative-weight cycle, the sum of those two weights is negative, so d!!

(!) will be set to a negative value. Since d values are never increased, it is also negative when the algorithm terminates.

In fact, it suffices to check whether d!!

(!!!) < 0 for some vertex i. Here’s why. A negative-weight cycle containing vertex i either contains vertex n or it does not. If it does not, then clearly d!!

(!!!) < 0. If the negative-weight cycle contains vertex n then consider d!!

(!!!). This value must be negative, since the cycle, starting and ending at vertex n, does not include vertex n as an intermediate vertex. 2. Alternatively, one could just run the normal FLOYD-WARSHALL algorithm one extra iteration to see if any of the d values change. If there are negative cycles, then some shortest-path cost will be cheaper. If there are no such cycles, then no d values will change because the algorithm gives the correct shortest paths.