How to separate an array of pairs into sets?

Asked

Viewed 107 times

1

I need to solve a problem in java and I’m having difficulties. I have the matrix with the following pairs:

0 - 1
1 - 2
2 - 4
2 - 5
3 - 6
4 - 5
6 - 7

I would like to group these figures as follows: --If the number 'x' is linked to 'y' and the number 'y' is linked to the number 'z', then I will group x, y, z , in the same group. The result Obtained would be:

Grupo A: 0,1,2,4,5
Grupo B: 3,6,7
Grupo C: 8

grafo que mostra as ligações dos Elementos de 0 até 8. Thank you! I await your reply!!

  • In this example you passed, who without would be the A, the B and the C?

  • 1

    What is this connection? I don’t understand the criterion. honestly it’s still quite confused.

  • @Diegof changed the explanation, did you understand? anything I put a graph explaining the links. Thank you

  • @Reginaldorigo In example A, B and C could be any numbers, it’s just an example I meant, if the number A has connection to the number B and the number B has connection to C they will be grouped in the same group.

  • really messed up your question.. I wish I could help you but it’s hard to interpret your doubt... so far I didn’t mean because "A" got the numbers 0,1,2,4,5 and B got the numbers 3,6,7

  • @James follows the modifications with a graph!

  • @Diegof Following modifications with a graph

  • In the graph I can see the link. But how can the program 'see' it? What in the matrix tells the program that there is this connection? Which rule could be applied?

  • @Reginaldorigo the program input is the matrix I put in the first line it knows that the '0' is on the '1' in the second line it knows that the '1' is on the '2' and so on. I didn’t want to use an adjacency matrix to show the links because I already have this matrix with the pairs at hand.

  • This could be so. The routine starts and creates group 1 and adds to groups. Starts the loop from line 2. Reads the line and asks if the first or second number is contained in any existing group if yes adds that line to that group. Otherwise create a new group and add that line to the new group created. And return to the beginning of the loop.

  • I just didn’t understand this: where the number came from 8 of the last group (group C), if it does not exist in the input matrix? : ) Besides, have you tried anything? Your specific difficulty is in what? The algorithm to process this matrix? If so, it seems to be a very similar problem to this: http://answall.com/questions/127070/colocando-e-comparando-domin%C3%B3s-in-order-in-c-using-list-or-n%C3%A3o-case-succeed

  • The knot 8 present alone in the graph is the representation of a pair that was omitted from the list. It would be the pair: 8 - 8.

Show 7 more comments

1 answer

2


The proposed problem can be solved with an algorithm called Flood Fill.

Important points to consider:

1) Implement a search algorithm (BFS or DFS) to traverse the graph nodes;

2) For each unidentified node that is found, you should assign a group identifier to it;

3) You should assign the same group identifier to all adjacent nodes, i.e., to all nodes that are accessible from that node;

4) When there are no more adjacent nodes that can be identified, you will start a new cycle by choosing another unnamed node;

5) Note that the fact that the new node is not identified implies that it was not accessible from our previous node and therefore is part of a different group of nodes;

6) When there are no more nodes to identify with, the number of identifiers is the same number of groups contained in the graph.

In this example graph, the identification of groups is done through distinct colors:

grafo de exemplo

Pairs:

1) Grupo Azul:

    a) 1-2
    b) 2-3
    c) 3-4
    d) 3-5
    e) 4-5
    f) 4-7
    g) 5-6
    h) 6-7

2) Grupo Verde

    i) 16-15
    j) 15-17
    k) 17-18
    l) 15-18
    m) 19-18
    n) 19-17
    o) 16-17


3) Grupo Vermelho:

    p) 10-11
    q) 14-11
    r) 10-13
    s) 12-13
    t) 10-12
    u) 12-14
    v) 12-11

4) Grupo Amarelo

    w) 8-9

This link also seemed interesting: Connected-Component Labeling

I hope I’ve helped!

  • helped a lot your explanation I’ll get this algorithm was just what I was looking for ! Thank you very much!

  • @Rafaelpinheiro Thank you :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.