homework2w24
homework2w24
Winter, 2023
Due January 31, 11:59 PM
Graph search, shortest paths, modifying algorithms, reductions, fitting data
structures to algorithms.
Ungraded problems
Which data structure? Say you are running Dijkstra’s algorithm on the fol-
lowing types of graphs, where the edges have arbitrary non-negative weights
and directions. For each, which data structure would give the best asymp-
totic performance and what would that be in terms of n? (Choose between
array and binary heap based on |E| and |V |.)
1. A complete bipartite graph with n vertices on each side
2. An n × n grid
3. The hypercube graph on n = 2k nodes, where every node corresponds
to a k bit string, and two nodes are connected if there is exactly one
bit difference between the two strings.
Round trip times The round trip distance between s and t in a strongly con-
nected weighted directed graph with non-negative weights is the minimum
total lengths of edges of a cycle that contains both s and t. Give an equiv-
alent definition in terms of the shortest path distances between vertices.
Then use this to give as efficient an algorithm as possible to, given s and
t, compute the round trip distance between them.
Finding the largest k values in an array Give an O(n log k) time algorithm
to find the largest k values in an unsorted array.
Nearest k points Give an O(k|V |) time algorithm that given a vertex s in
a directed weighted graph with non-negative edge weights, finds the k
points that are closest to s. You can refer to other algorithms presented
in class and use properties of these algorithms proved in class to modify
them. (5 points correct algorithm , 5 points correctness argument, 10
points efficiency, 5 points time analysis, incl.data structures)
Top k elements in heap Give an efficient algorithm that, given a binary max-
heap H of size n and a number 1 ≤ k ≤ n, returns the k largest elements
of H. Analyze the time in terms of both k and n (although, for the best
algorithm I know, the time does not depend on n at all, just k.)
Increasing path Give an efficient algorithm for the following problem: You
are given a directed graph G (in adjacency list format) where every edge
has an integer weight w(e), and two vertices s and t. You want to deter-
mine whether there is a path from s to t, e1 , ..ek , with w(e1 ) < w(e2 ) <
...w(ek ).
1
Total transmission time Consider a graph representing a communications
network where every edge has two non-negative labels, bandwidth and
latency. The latency of a path, L(p) is the sum of the latencies of the
edges in p, whereas the bandwidth BW (p) is the minimum bandwidth of
an an edge in p. Given a file size F , the total transmission time for F
given a path p is L(p) + F/BW (p). Given such a graph along with source
and target vertices, provide an algorithm for finding a path of minimum
possible total transmission time that takes O(|E|(|E| + |V |) log |V |) time.
Total flight time Give an efficient algorithm for the flight scheduling problem
when we use the objective function that is at.fk − dt.f1 , the total time we
spend in transit. (5 points correct polynomial time algortithm, 5 points
correctness proof, 10 points efficiency and time analysis.)
Capped difference path You are given a directed graph where each edge e
has integer valued weight w(e), and an integer k. Give an algorithm
to decide whether there is a path from s to t, e1 , ..., e` so that |w(ei ) −
w(ei−1 )| ≤ k for all 2 ≤ i ≤ `. (5 points correct polynomial time algorithm,
5 points correctness proof, 10 points efficiency and time analysis. Give the
analysis in terms of |V |, |E| and k. ).
Path with no consecutive repeated colors Find an efficient algorithm for
the following problem. You are given a directed graph G = (V, E) where
every edge e has a color χ(G), and two vertices, s and t. The output is a
(not necessarily simple) path e1 , e2 , .., ek from s to t so that no consecutive
edges ei and ei+1 have the same color, χ(ei ) 6= χ(ei+1 ) for all 1 ≤ i ≤ k−1,
or False if no such paths exist. (10 points correct algorithm and correctness
proof, 10 points efficiency and time analysis.)
Almost sorted array Let A[1..n] be an array with n distinct integer elements
, and let A[σ(1)], A[σ(2)], ..A[σ(n)] be the same array when sorted. For
example, if the original array is A[1..3] = (3, 2, 5), then σ(1) = 2, σ(2) = 1
and σ(3) = 3, because the smallest element is in the second position, the
middle element is in the first position, and the largest element is in the last
position. Call the array k-almost sorted if for all 1 ≤ i ≤ n, σ(i) ≤ i + k.
Give an O(n log k) time algorithm to sort a k-almost sorted array. (5
points correctness, 15 points time analysis and efficiency).
Implementation problem: Implement Dijkstra’s algorithm with a priority
queue for a variety of sizes n, for complete graphs with edge weights ran-
domly chosen real numbers between 0 and 1. How does the number of
decrease-key operations actually scale with n? How does this compare to
the worst-case number of decrease-key operations? Why are the two dif-
ferent, if they are? Present your data clearly, and explain how you draw
2
your conclusions. (5 points doing implementation, 5 points clear presen-
tation of data, 5 points collecting sufficient data, 5 points clear statement
of conclusions.)