## Homework #7

#### Mostly MST, BFS, DFS, topological sorting

# problems
1 GT, Exercise C-15.1 on page 439
Suppose G is a weighted, connected, undirected graph and e is a smallest-weight edge in G. Show that there is a minimum spanning tree of G that contains e.
2 GT, Exercise C-15.2 on page 439
Suppose G is a weighted, connected, undirected, simple graph and e is a largest-weight edge in G. Prove or disprove the claim that there is no minimum spanning tree of G that contains e.
( You should assume that all edge weights are distinct. )
3 Kleinberg and Tardos, Exercise 3-2 on page 107
Give an algorithm to detect whether a given undirected graph contains a cycle.
If the graph contains a cycle, then your algorithm should output one. (It should not output all cycles in the graph, just one of them.)
The running time of your algorithm should be O(m + n) for a graph with n nodes and m edges.
4 Kleinberg and Tardos, Exercise 3-4 on page 107
Leo has a collection of n butterflies that allegedly are from species A and B. He would like to divide the n specimens into two groups, those that belong to A and those that belong to B, but it is very hard to directly label any one specimen.

Leo invites an expert to study pairs of specimens side by side and, for each pair, (i,j), make a judgement as to whether that pair is from the same or different species. If the expert is uncertain about a pair, he can judge that pair to be ambiguous.

The expert delivers m unambiguous judgements. Leo would like to know whether the m judgements are consistent, which is to say that it is possible to label each specimen either A or B in such a way that if any pair (i,j) was judged to be from the same species then specimens i and j jave the same label and if pair (i,j) was judged to be different then i and j have different labels. Describe an O(m+n)-time algorithm that determines whether the m judgements are consistent.

5 Kleinberg and Tardos, Exercise 3-12 on page 112
Given facts about people P1, ..., Pn of forms
(a) for some i and j, Pi died before Pj was born; or
(b) for some i and j, the life spans of Pi and Pj overlapped.

Describe an efficient algorithm that either
(1) produces proposed birth and death dates for all n people so all facts hold true, or
(2) report (correctly) that no such dates can exist.

6 Given an undirected graph G = (V,E) and special vertex vV, describe an efficient algorithm that returns the length of the shortest simple cycle that contains vertex v, or 0 if there are none.

State the worst-case time complexity of your algorithm as a function of n, the number of nodes, and m, the number of edges in graph G.

7 You have a stack of n boxes, with widths wi, heights hi, and depths di. The boxes cannot be rotated and can only be stacked on top of one another if each box in the stack is strictly larger than the box above it in width, height, and depth. Describe an efficient algorithm to build the tallest stack possible, where the height of a stack is the sum of the heights of each box in that stack.
8 Describe and analyze an efficient algorithm that, given n nodes labeled from 0 to n-1 and a list of undirected edges (each edge is a pair of nodes), determines whether these edges make up a valid tree.

# hard problems
9 A message is to be passed to all members of a certain underground organization. Each member knows some of the other members and has procedures for arranging a rendezvous with anyone he knows. Associated with each such possible rendezvous, say between member i and member j, is a certain probability, pij, that the message will fall into hostile hands. How is the message to be distributed so as to minimize the overall probability of its being compromised?

Show that the solution is given by a spanning tree of minimum length, over a certain graph with a certain distance matrix.

10 Baase Exercise 7.43 on page 383
If a graph is represented by an adjacency matrix, then almost any algorithm that operates on the graph will have worst-case complexity in Ω(n2), where n is the number of vertices. There are, however, some problems that can be solved quickly, even when the adjacency matrix is used. Here is one.
1. Let G = (V, E) be a digraph with n vertices. Let's call a vertex s a hypersink if, for every v in V such that s ≠ v, there is an edge (v,s) and there is no edge (s,v). Give an algorithm to determine whether or not G has a hypersink, assuming that G is given by its n × n adjacency matrix.
2. How many matrix entries are examined by your algorithm in the worst case? It is easy to give an algorithm that looks at θ(n2) entries, but there is a linear solution.
11 Given an undirected graph G = (V,E), consisting of n vertices and m edges, with each edge labeled from the set {0,1}. Describe and analyze the worst-case time complexity of an efficient algorithm to find any cycle consisting of edges whose labels alternate 0,1.