Exercise 1

Assume that a graph G = (V, E) is represented using an array of length V, where position n contains the adjacency list for the node with node index n (n ∈ {0,…,V-1}), and the adjacency lists contain node indices.

We need to check if a given graph is reflexive, symmetric, and transitive. Checking for reflexivity and symmetry is easy:

reflexive(G):
  for n ∈ {0,…,number of nodes in G - 1} do
    if n does not occur in its own adjacency list then
      return false

  return true

symmetric(G):
  M ← adjacency matrix for G

  for m ∈ {0,…,number of nodes in G - 1} do
    for n ∈ {0,…,number of nodes in G - 1} do
      if M[m,n] ≠ M[n,m] then
        return false

  return true

(The code for symmetric can be optimised.)

A reflexive graph G = (V, E) is transitive iff all shortest paths between any two nodes contain at most one edge. Proof:

Hence we can check transitivity as follows:

// Precondition: G must be reflexive.
transitive(G):
  for every node v in G do
    p ← an array containing the lengths of the shortest paths from
          v to every node in G (using -1 to mark the absence of a
          path)

    if any element in p is ≥ 2 then
      return false

  return true

Finally the pieces above can be combined as follows:

equivalence-relation(G):
  return (reflexive(G) and symmetric(G) and transitive(G))

Time complexity

Assuming a uniform cost model.

Assume that the input is G = (V, E). The different functions have the following time complexities:

We see that the time complexity is dominated by transitive. The total time required is O(∣V∣(∣V∣ + ∣E∣)).

Exercise 2

Algorithm

Basic idea: Find a minimum spanning tree, return the weight of (one of) its most expensive edge(s).

This can be implemented by modifying Kruskal's algorithm so that the weight of the last edge added to the minimum spanning tree is returned.

Time complexity

Assuming a uniform cost model.

This algorithm has the same time complexity as Kruskal's algorithm: O(ElogV), if we assume that we can compare two weights in constant time.

Correctness

More

You may want to try to generalise the algorithm to partitions with more than two components.

This exercise is related to cluster analysis, see for instance the book Algorithm Design by Kleinberg and Tardos. Some examples found online.

Exercise 3

No solution given.