Docsity

CMPS 214 Problem Set 5: Graph Algorithms and Data Structures, Exercises of Algorithms and Programming

Toggle sidebar
bg1
CMPS 214 Problem Set 5
Style guide and expectations:
Please see the ๎˜Homework๎˜‘ part of the syllabus for guidance on what we are
looking for in homework solutions. We will grade according to these standards. You should cite all sources you
used outside of the course material.
What we expect:
Make sure to look at the ๎˜
We are expecting
๎˜‘ blocks below each problem to see what we
will be grading for in each problem!
Exercises
We suggest you do these on your own. As with any homework problem, though, you may ask the course sta๎˜›
for help.
1. [OPTIONAL]
Suppose that
h
:
U ! f
0
;:::;n
๎˜€
1
g
is a uniformly random function. That is, for each
x
2 U
,
h
(
x
)
is distributed uniformly at random in the set
f
0
;:::;n
๎˜€
1
g
and the values
f
h
(
x
) :
x
2 Ug
are independent. Prove that for any
x
6
=
y
2 U
,
P
h
f
h
(
x
) =
h
(
y
)
g
=1
n:
Above, notice that
x
and
y
are ๎˜œxed and the probability is over the choice of
h
.
[
We are expecting:
A short but rigorous proof.]
Solution:
1
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download CMPS 214 Problem Set 5: Graph Algorithms and Data Structures and more Exercises Algorithms and Programming in PDF only on Docsity!

CMPS 214 Problem Set 5

Style guide and expectations: Please see the Homework part of the syllabus for guidance on what we are looking for in homework solutions. We will grade according to these standards. You should cite all sources you used outside of the course material.

What we expect: Make sure to look at the We are expecting blocks below each problem to see what we will be grading for in each problem!

Exercises

We suggest you do these on your own. As with any homework problem, though, you may ask the course sta for help.

  1. [OPTIONAL] Suppose that h : U! f 0 ; : : : ; n 1 g is a uniformly random function. That is, for each x 2 U, h(x) is distributed uniformly at random in the set f 0 ; : : : ; n 1 g and the values fh(x) : x 2 Ug are independent. Prove that for any x 6 = y 2 U,

Phfh(x) = h(y )g =

n

Above, notice that x and y are xed and the probability is over the choice of h. [We are expecting: A short but rigorous proof.]

Solution:

  1. [OPTIONAL] Let U = f 000 ; 001 ; 002 ; : : : ; 999 g (aka, all of the numbers between 0 and 999 , padded so that they are three digits long) and let n = 10. For each of the following hash families H consisting of functions h : U! f 0 ; : : : ; n 1 g, decide whether H is universal or not, and justify your result with a formal proof.

(a) For i = 1; 2 ; 3 , let hi (x) be the i'th least-signicant digit of x. (For example, h 3 (456) = 4). Dene H = fh 1 ; h 2 ; h 3 g. Is H a universal hash family?

(b) For a 2 f 1 ; : : : ; 9 g, let ha(x) be the least-signicant digit of ax. (For example, h 2 (123) is the least- signicant digit of 2  123 = 246, which is 6 ). Dene H = fhi : i = 1; : : : ; 9 g. Is H a universal hash family? Hint:To show that something is not a universal hash family, you could nd two distinct elements x; y 2 U so that the probability that h(x) = h(y ) is larger than it's supposed to be.

[We are expecting: For each part, a yes/no answer and a rigorous proof using the denition of a universal hash family.]

Solution:

  1. Consider the following directed acyclic graph (DAG):

Problems

ยˆ Try the problems on your own before asking questions. ยˆ Write up your answers yourself, in your own words. You should never share your typed-up solutions.

  1. Badger badger badger. A family of badgers lives in a network of tunnels; the network is modeled by a connected, undirected graph G with n vertices and m edges (see below). Each of the tunnels have dierent widths, and a badger of width x can only pass through tunnels of width  x.

For example, in the graph below, a badger with width x = 2 could get from v 0 to v 4 (either by v 0! v 1! v 4 or by v 0! v 3! v 4 ). However, a badger of width 3 could not get from v 0 to v 4.

v 0 3.1415 v 1

v 2 v 3 v 4

x

The graph is stored in the adjacency-list format we discussed in class. More precisely, G has vertices v 0 ; : : : ; vn 1 and is stored as an array V of length n, so that V [i] is a pointer to the head of a linked list Ni which stores integers. An integer j 2 f 0 ; : : : ; n 1 g is in Ni if and only if there is an edge between the vertices vi and vj in G.

You have access to a function tunnelWidth which runs in time O(1) so that if fvi ; vj g is an edge in G, then tunnelWidth(i,j) returns the width of the tunnel between vi and vj. (Notice that tunnelWidth(i,j)=tunnelWidth(j,i since the graph G is undirected). If fvi ; vj g is not an edge in G, then you have no guarantee about what tunnelWidth(i,j) returns. (a) Design a deterministic algorithm which takes as input G in the format above, integers s; t 2 f 0 ; : : : ; n 1 g, and a desired badger width x > 0 ; the algorithm should return True if there is a path from vs to vt that a badger of width x could t through, or False if no such path exists.

(For example, in the example above we have s = 0 and t = 4. Your algorithm should return True if 0 < x  2 and False if x > 2 ).

Your algorithm should run in time O(n + m). You may use any algorithm we have seen in class as a subroutine.

Note: In your pseudocode, make sure you use the adjacency-list format for G described above. For example, your pseudocode should not say something like iterate over all edges in the graph. Instead it should more explicitly show how to do that with the format described. (We will not be so pedantic about this in the future, but one point of this problem is to make sure you understand how the adjacency-list format works).

[We are expecting: Pseudocode AND an English description of your algorithm, and a short jus- tication of the running time. You should make sure to use the adjacency-list representation of G described above in your pseudocode. You can use any algorithms we have seen from class as a

subroutine, but if you signicantly modify them make sure to be precise about how this interacts with the adjacency-list representation.]

Solution: One way of solving this is as follows:

Algorithm 1 pathExists(G, s, t, x)

1: numV er tices = length(G:V ) 2: for i = 0 to numVertices - 1 do 3: neighbor s = G:V [i] 4: for neighbor in neighbors do 5: if tunnelWidth(i, neighbor) < x then

. Here, you may want to write the details of iterating over the linked list + the removal operation, in a way that is similar to what you used to do in CMPS 212. The specics were omitted in the solution. 6: remove neighbor from G:V [i]. Your solution should do the removal operation in O(1) 7: end if 8: end for 9: end for 10: Run BFS starting from s and use a boolean auxiliary array to keep track of the visited vertices 11: return visited[t]

The algorithm above updates the edges of the graph to exclude all edges that are narrower than x. Once that is done, all what is left to do is run BFS; the usage of BFS gets us the visited vertices for free, so if we were able to discover t starting from s (or vice versa, since the graph is undirected), we return true. Otherwise, we return false.

The BFS we run on line 10 runs in time O(n + m). The for loop on line 2 requires a more careful analysis. If the graph did not have a single edge (that is, if neighbor s was empty for all i), we would still be doing jV j  c (for some constant c) = O(n) operations from the iterations of the outer loop and the retrieval of the linked list. We should add to that the running time of the inner for loop, which is given (across all iterations) by

โˆ‘n 1 i=0 deg(i)^ ^ k^ =^ k^

โˆ‘n 1 i=0 deg(i)^ (for some constant^ k), where deg(i) is the degree of vertex i. By the Handshaking Lemma,

โˆ‘n 1 i=0 deg(i) = 2jEj^ = 2m. We therefore end up with the desired running time of O(n + m).

Alternatively, if we do not want to modify the graph G, it is sucient to run DFS on the origi- nal graph and add an if statement that only recurses on the neighbors of the current vertex if the edge to the neighbor in question has a width that is bigger than x.

(b) Design a deterministic algorithm which takes as input G in the format above and integers s; t 2 f 0 ; : : : ; n 1 g; the algorithm should return the largest real number x so that there exists a path from vs to vt which accomodates a badger of width x. Your algorithm should run in time O((n + m) log(m)). You may use any algorithm we have seen in class as a subroutine. (Hint: use part (a)).

Note: Don't assume that you know anything about the tunnel widths ahead of time. (e.g., they are not necessarily bounded integers).

Note: The same note about pseudocode holds as in part (a). [We are expecting: Pseudocode AND and English description of your algorithm, and a short justication of the running time. You should make sure to use the adjacency-list representation of G described above in your pseudocode. You can use any algorithms we have seen from class as a

Algorithm 2 getWidestBeaver(G, s, t, x)

1: if s == t then. Don't forget this corner case! 2: return 1 3: end if 4: W = [] 5: for i = 0 to numVertices - 1 do 6: neighbor s = G:V [i] 7: for neighbor in neighbors do 8: if neighbor > i then. Avoid adding every edge width twice, given that the graph is undirected 9: W:append(tunnelW idth(i; neighbor )) 10: end if 11: end for 12: end for 13: sort(W) 14: l = 0 15: r = length(W ) 1 16: while l < r do 17: mid = d l+ 2 r e 18: if pathExists(G, s, t, W[mid]) then 19: l = mid 20: else 21: r = mid 1 22: end if 23: end while 24: return W[l]

We have already seen that the for loop on line 5 runs in O(n + m). The sorting on line 13 runs in O(m log m). As is the case with a standard binary search, the while loop on line 16 does O(log m) iterations, each of which calls on a function that runs in O(n + m), so we end up with our desired running time of O((n + m) log(m)) (Note: the details of the binary search variant should be explicitly mentioned in your solution. In other words, you cannot use binary search as a black box, as there are tangible dierences between the two implementations).

  1. [OPTIONAL] Painted Penguins. Much to Plucky's delight, a large ock of T painted penguins will be waddling past the Stanford campus next week as part of their annual migration from Monterey Bay Aquarium to the Sausalito Cetacean Institute. Painted Penguins (not to be confused with pedantic penguins) are an interesting species. They can come in a huge number of colorssay, M colorsbut each ock only has m colors represented, where m < T. The penguins will waddle by one at a time, and after they have waddled by they won't come back again. You'd like to design a randomized data structure to keep track of the penguin colors so that, after all the penguins have gone, you'll be able to answer queries about what colors of penguins appeared in the ock; you'd like your answers to these queries to probably be correct. For example, if T = 7, M = 100000 and m = 3, then a ock of T painted penguins might look like:

seabreeze, seabreeze, indigo, ultraviolet, indigo, ultraviolet, seabreeze

You'll see this sequence in order, and only once. After the penguins have gone, you'll be asked questions like How many indigo penguins were there?" (Answer: 2), or How many neon orange penguins were there?" (Answer: 0).

You know m; M and T in advance, and you have access to a universal hash family H, so that each function h 2 H maps the set of M colors into the set f 0 ; : : : ; n 1 g, for some integer n. For example, one function h 2 H might have h(seabreeze) = 5.

(a) Suppose that n = 10m. Suppose also that you only have space to store: ยˆ An array B of length n, consisting of numbers in the set f 0 ; : : : ; T g, and

ยˆ one function h from H. Use the universal hash family H to create a randomized data structure that ts in this space and that supports the following operations in time O(1) in the worst case, assuming that you can evaluate h 2 H in time O(1).

ยˆ Update(color): Update the data structure when you see a penguin with color color. ยˆ Query(color): Return the number of penguins of color color that you have seen so far. For each query, your query should be correct with probability at least 9/10. That is, for all colors color,

PfQuery(color) = the true number of penguins with color color g 

To describe your data structure: i. Describe how the array B and the function h are initialized.

ii. Give pseudocode for Query. iii. Give pseudocode for Update.

[We are expecting: A description following the outline above (including pseudocode), and a short but rigorous proof that your data structure meets the requirements. Make sure you clearly indicate where you are using the property of universal hash families.]

(b) Suppose that you now have k times the space you had in part (a). That is, you can store k arrays B 1 ; : : : ; Bk and k functions h 1 ; : : : ; hk from H. Adapt your data structure from part (a) so that all operations run in time O(k), and the Query operation is correct with probability at least 1 (^101) k. [We are expecting: As in part (a), a description following the outline above (except say how all arrays Bi and functions hi are initialized), and a short but rigorous proof that your data structure meets the requirements. Make sure you clearly indicate where you are using the property of universal hash families.]

Solution ((a) + (b)):