0

I have a kattis task that I'm struggling with to complete. The task is to "find a set of at least k connected entries in a matrix, such that the difference between the largest and smallest entry in the set was minimized".

The input is first the size of the matrix:

5 10

Then the values of the matrix:

0  0  3  46 0  46 0  0  12 12

0  0  13 50 49 46 11 10 10 11

0  51 51 49 99 99 89 0  0  10

0  0  48 82 70 99 0  52 13 14

51 50 50 51 70 35 70 10 14 11

After that there's the ammount of k values:

 6 

And then the actual values on k:

1

5

10

12

47

50 

The task states that: "An entry in the matrix, a(i,j), is adjacent to the entries a(i,j+1), a(i+1,j), a(i,j-1) and a(i-1,j). A set of entries are connected if for every pair of entries in the set, there is a connecting path of adjacent entries in it.".

For the values given the output should be:

0

0

3

4

89

99

I have written the code to take all the inputs:

  Scanner sc = new Scanner(System.in);
  int r = sc.nextInt();
  int c = sc.nextInt();

  // fill with values
  int[][] dimMat = new int[r][c];
  for (int i = 0; i < dimMat.length; i++) {
     for (int j = 0; j < dimMat[i].length; j++) {
        dimMat[i][j] = sc.nextInt();
     }
  }

  int n = sc.nextInt();
  int[] myK = new int[n];

  // fill k
  for(int k= 0; k< myK.length; k++){
     myK[k] = sc.nextInt();
  }

But have no idea how to traverse the matrix, get all the different paths or find the value they ask for. I've been googling dynamic programming and other stuff for days without any result.

Thanks in advance for any help.

J. doe
  • 17
  • 2
  • so you just managed to fill up the matrix, apart from that, what have you tried mate? – nafas Jun 08 '17 at 12:30
  • To start of you could transform the matrix to a graph where the height difference are the value of each node. From here you should be able to make use of a greedy approach. Loop through each node and from the set of all available paths take the cheapest one. When you've taken k steps, you perform the same process on the next node. If this times you out you can optimize it with dynamic programming if you're familiar with it (Cache the results so you don't compute the value of the same path twice) – Ted Cassirer Jun 08 '17 at 12:35
  • it's more than a question - to solve this problem. Try expressing the problem in your own words first to see whether you understand it enough to explain to someone. Once you are there some avenues should become a bit more obvious. – diginoise Jun 08 '17 at 12:36
  • Isn't the point of those puzzles for you to solve them yourself? If you have an algorithm draft we can help you debug it but as it is, it looks like you want us to solve the whole problem. I advise you to do easier problems first, skip this one and try it again later. – Konrad Höffner Jun 08 '17 at 12:45

2 Answers2

0

I might be able to get you started. A good way to approach these problems is to first come up with a naive solution and then optimize. A naive approach to this problem might look like this:

k = desired size of region;
minValue = infinity;
for every node in your graph {
  S = an empty set of nodes;
  add current node to s;
  while(S.size < k) {
    nextNode = infinity;
    for every node in S called s {
      N = the smallest connected node to s;
      if N < nextNode
        nextNode = n;
    }
    add nextNode to S;
  }
  find greatest difference between nodes in your set;
  if greatestDifference < minValue
    minValue = greatestDifference;
}

This algorithm loops over every node in your graph, builds a region of smallest distance from that node, sees if the max distance between those nodes is currently globally optimal, and if it is, stores the max distance. You can take this pseudo-code, type it up, and then optimize it. I hope that is enough of a start for you. This algorithm is pretty unoptimized.

Jayson Boubin
  • 1,504
  • 2
  • 11
  • 19
0

A first algorithm in O((RC)^3):

Considering the small size of the matrices, you might be able to go for a very naive approach that runs in time O((RC)^3), which is :

  1. Store all the values of altitudes alts in a separate array and sort them

Now for all pairs (L,H), L <= H, of values of this array alts :

  1. Consider the matrix B such that b(i,j) = 1 if L <= a(i,j) <= H, b(i,j) = 0 otherwise.
  2. Use several BFS or a union-find structure to find the size of the largest 1-valued connected component in B.

You now know for each pair (L,H), k(L,H), the size of the largest connected component of values between L and H. Now, to find the lowest altitude difference for a given k, you just have to compute the minimum of H-L for all pairs (L,H) such that k(L,H) >= k.

A variation in O((RC)^2 log(RC)) per request:

For any given k, for all possible values of L (the lowest point), do a dichotomic search over H (the value of the highest point) to find the lowest H such that k(L,H) >= k. This is possible because H1 >= H2 implies k(L,H1) >= k(L,H2) for all L.

Finally, a solution in O((RC)^2 log(RC))

For all points of the matrix, start a Dijkstra search (priority is the altitude, and you never go to points that are above your starting altitude). The assumption behind each search is that you start your search from the peak, and only go down from there, slowly increasing the altitude difference.

As before, during this computation you get connected components of given (L,H) of which you know the size k(L,H). This allows you to compute the lowest difference H-L that achieves a given k for all k, and thus answer all the questions (you can store all the minimal H-L for k from 0 to RC in a (RC+1)-sized array).

A.N.
  • 320
  • 1
  • 8
  • I hope this answer that I did while thinking about the problem myself might give you an idea of what the thought process of solving such a problem can be. Start with something simple, try to make it better, try using the algorithms that you know as building blocks ... I'm not convinced that the O((RC)^2 log(RC)) algorithm is optimal, I wouldn't be surprised there was a O((RC)^2) or a O(RC log(RC)) solution. – A.N. Jun 08 '17 at 13:07