Algorithm 如何从具有链接信息的成对数据列表中查找唯一名称的数量?

Algorithm 如何从具有链接信息的成对数据列表中查找唯一名称的数量?,algorithm,graph-theory,Algorithm,Graph Theory,我有n行数据集,每个数据集有两个由空格分隔的组件。第一个是卡号,第二个是名字。如果一个人有相同的卡号或姓名,那么他就是相同的。如何从数据集中找到唯一人员的总数 例如: 1A 1b 2 B 3 C 此数据集有两个唯一的个人。这是因为第一行和第二行卡号相同,第二行和第三行名称相同 什么样的算法可以用来解决这类问题?我提出的解决方案是使用某种分区:大多数操作都是在O(1)或O(logn)中完成的,并且由用户完成一次,因此时间复杂度大约是O(n),O(logn),这取决于字典的实现方式 int Coun

我有n行数据集,每个数据集有两个由空格分隔的组件。第一个是卡号,第二个是名字。如果一个人有相同的卡号或姓名,那么他就是相同的。如何从数据集中找到唯一人员的总数

例如:

1A

1b

2 B

3 C

此数据集有两个唯一的个人。这是因为第一行和第二行卡号相同,第二行和第三行名称相同


什么样的算法可以用来解决这类问题?

我提出的解决方案是使用某种分区:大多数操作都是在O(1)或O(logn)中完成的,并且由用户完成一次,因此时间复杂度大约是O(n),O(logn),这取决于字典的实现方式

int CountUnique(Person persons)
{
        Dictionary<string, int> phones = new Dictionary<string, int>(); //Keep a dictionary where each phone number is mapped to a partition
        Dictionary<string, int> email = new Dictionary<string, int>();  //Keep a dictionary where each email is mapped to a partition
        bool[,] linked = new bool[n, n]; //Lookup table used to tell if 2 partitions are linked (represents the same person)
        int count = 0;
        int max = 0;
        for (int i = 0; i < n; i++)
        {
            Person p = persons[i];
            int pA = -1, pB = -1; // Partition found using the phone number, Partition found using email
            if (phones.ContainsKey(p.Phone))
            {
                pA = phones[p.Phone];
            }
            if (emails.ContainsKey(p.Email))
            {
                pB = emails[p.Email];
            }

            if (pA == -1 && pB == -1) // First case, not found: Add both phones and email and create a new partition. Number of unique persons is also incremented.
            {
                phones.Add(p.Phone.Trim(), max);
                emails.Add(p.Email.Trim().ToLower(), max);
                max++;
                count++;
            }
            else
            {
                if (pA != -1 && pB != -1 && pA != pB) // Found using both parameters on different partitions
                {
                    if (!linked[pA, pB] && !linked[pB, pA]) // If the partition are not linked, link them
                    {
                        count--; // We'lost one partition => one unique person less
                        linked[pA, pB] = linked[pB, pA] = true;
                    }
                }
                if (pA == -1) // We did find an existing email but no phone
                {
                    phones.Add(p.Phone.Trim(), pB); // Add the phone number
                    max++;
                }
                if (pB == -1) // We did find an existing phone but no email
                {
                    emails.Add(p.Email.Trim().ToLower(), pA); // Add the email number
                    max++;
                }
            }

        }



return count;
}
int CountUnique(个人)
{
Dictionary phones=new Dictionary();//保留一个字典,其中每个电话号码都映射到一个分区
Dictionary email=new Dictionary();//保留一个字典,其中每个电子邮件都映射到一个分区
bool[,]linked=newbool[n,n];//用于判断两个分区是否链接的查找表(表示同一个人)
整数计数=0;
int max=0;
对于(int i=0;i少了一个唯一的人
链接的[pA,pB]=链接的[pB,pA]=真;
}
}
if(pA=-1)//我们确实找到了一封现有的电子邮件,但没有电话
{
phones.Add(p.Phone.Trim(),pB);//添加电话号码
max++;
}
if(pB=-1)//我们确实找到了一个现有的电话,但没有电子邮件
{
emails.Add(p.Email.Trim().ToLower(),pA);//添加电子邮件号码
max++;
}
}
}
返回计数;
}

< /代码> C++类。但是把它当作sudo代码

int uniqueCount = 0;
map<string, bool> column_1;
map<string, bool> column_2;
string left, right
for(int x = 0 ;x < matrix.count; x++) {
   left = matrix[x][0]
   right = matrix[x][1];
   if(column_1.find(left) != column_1.end && column_2.find(right) != column_2.end){
  ++uniqueCount 
   column_1[left] = true;
   column_2[right] = true;
   }
  else --uniqueCount;
}
int uniqueCount=0;
图列_1;
地图栏2;
左串,右串
对于(int x=0;x

我很抱歉,如果上面没有编译。把它当作SUDO代码,我已经有一段时间没有C++了,并且没有想到Rails代码会有帮助。p> 下面是另一个使用图论和连接组件的解决方案:

              int CountUnique(Person[] persons)
                Dictionary<string, int> phones = new Dictionary<string, int>();
                Dictionary<string, int> emails = new Dictionary<string, int>();
                bool[] unique = new bool[n];
                int count = 0;
                int max = 0;
                for (int i = 0; i < n; i++)
                {
                    Person p = persons[i];
                    int pA = -1, pB = -1;
                    if (phones.ContainsKey(p.Phone))
                    {
                        pA = phones[p.Phone];
                    }
                    if (emails.ContainsKey(p.Email))
                    {
                        pB = emails[p.Email];
                    }
                    if (pA != -1)
                    {
                        persons[pA].Next.Add(p);
                        p.Next.Add(persons[pA]);
                    }
                    else
                    {
                        phones.Add(p.Phone, p.Index);
                    }

                    if (pB != -1 && pB != pA)
                    {
                        persons[pB].Next.Add(p);
                        p.Next.Add(persons[pB]);
                    }
                    if (pB == -1)
                    {
                        emails.Add(p.Email, p.Index);
                    }
                }


                int current = 0;
                Person pCurrent;
                count = 0;
                while ((pCurrent = FindUnvisited(persons, current)) != null)
                {
                    BFS(pCurrent);
                    count++;
                }

                return count;


            }

            private static void DFS(Person pCurrent)
            {
                pCurrent.Visited = true;
                foreach (Person p in pCurrent.Next)
                {
                    if (!p.Visited)
                    {
                        BFS(p);
                    }
                }
            }

            private static Person FindUnvisited(Person[] persons, int current)
            {
                for (int i = current; i < persons.Length; i++)
                {
                    if (persons[i].Visited == false) return persons[i];
                }
                return null;
            }


        }
    }
int CountUnique(个人[]个人)
字典电话=新字典();
字典电子邮件=新字典();
bool[]unique=新bool[n];
整数计数=0;
int max=0;
对于(int i=0;i
如果我添加了
3 B
,结果如何?