Matrix grouping based on relation

≡放荡痞女 提交于 2021-02-11 10:16:28

问题


I got into problem to solve matrix grouping problem based on its relation.

Problem

Consider a group of people giving books to each other. More formally, group is composed of all the people who know one another, whether directly to transitively.

Example

Consider a matrix of input M

Input

1100
1110
0110
0001

Output:

2

  • There are n = 4 people numbered related[0] through related[3].
  • There are 2 pairs who directly know each other: (related[0], related[1]) and (related[1], related[2]). Because a relation is transitive, the set of people {related[0], related[1], related[2]} is considered a single group.
  • The remaining person, related[3], does not know any other people and is a separate group: {related[3]}
  • There are a total of 2 groups.

Example

Input:

10000
01000
00100
00010
00001

Output:

5

No direct relationship are shown so there are 5 groups: {related [0], related[1], related[2], related[3], related[4]}

Example

Input

1100000
1110000
0110000
0001000
0000110
0000110
0000001

Output

4
  • There are 2 pairs who directly know each other: (related[0], related[1]) and (related[1], related[2]). Because a relation is transitive, the set of people {related[0], related[1], related[2]} is considered a single group.
  • There is 1 pair who directly know each other: (related[4], related[5]). So they are considered as single group {related[4], related[5]}
  • The remaining person, related[3], related[6] does not know any other people and is a separate group: {related[3]}, {related[6]}
  • There are total 4 group : {{related[0], related[1], related[2]}, {related[4], related[5]}, {related[3]}, {related[6]} }

I need help with how to approach with this kind of problems.

Code (I tried not completed)

def countGroup(matrix):
    people = set()
    group1 = set()
    for i in range(len(matrix)):
        people.add(i) 
        for j in range(len(matrix)):
            if i == j:
              # next
              continue
            if matrix[i][j]:
                group1.add((i,j))
                people.discard(i)
                people.discard(j)
                # group1.add(i)
                # group1.add(j)
    print(people, "people")
    print(group1, "group1")
    count = len(people)
    if group1:
        count += 1

    return count



回答1:


Your almost there, the issue is the if isn't quite right, and you need to make more use of sets. Haven't tested the code but should work with perhaps a few minor tweaks, my python is a bit rusty.

def countGroup(matrix):
    people = set()
    group1 = set()
    for i in range(len(matrix)):
        people.add(i) # Quick way to build a list of all the people
        for j in range(len(matrix)):
            if i == j: # Not useful, you should know yourself. 
              # Could avoid this by using itertools.combinations
              continue
            if matrix[i][j] == 1: # we just want to know
                group1.add(i)
                group1.add(j)
    group2 = people - group1 # Anyone not in group1 is in group2.

    return (group1, group2)


来源:https://stackoverflow.com/questions/64422591/matrix-grouping-based-on-relation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!