Java 集群中的分组/链接
我正在使用处理,到目前为止,我有一个草图,画随机球,并在连接在一定半径内时画一条线Java 集群中的分组/链接,java,processing,grouping,cluster-analysis,Java,Processing,Grouping,Cluster Analysis,我正在使用处理,到目前为止,我有一个草图,画随机球,并在连接在一定半径内时画一条线 for(int i=0;i<=people.size()-1;i++){ Person p = people.get(i); for(Person pp: people){ if(pp!=p){ if(p.isIntersecting(pp)){ // Draw the line connecting the two here
for(int i=0;i<=people.size()-1;i++){
Person p = people.get(i);
for(Person pp: people){
if(pp!=p){
if(p.isIntersecting(pp)){
// Draw the line connecting the two here
line(p.loc.x,p.loc.y,pp.loc.x,pp.loc.y);
// Store the other person in this persons "connections" list
if(!p.connections.contains(pp)){ p.connections.add(pp);}
} else {
p.connections.remove(pp);
}
}
}
}
for(int i=0;i看起来您有一个名为Person
的类,因此我会将ArrayList包含在该类中。不知道您的完整代码,它可能会如下所示:
class Person {
ArrayList<Person> connected = new ArrayList<Person>();
Person() {
// create as you already are doing
}
void checkConnections() {
connected.clear(); // delete existing connections
for (Person other : persons) {
if (dist(x,y, other.x,other.y) < 100) {
connected.add(other):
}
}
}
}
班级人员{
ArrayList connected=新建ArrayList();
人(){
//按照您已经在做的事情进行创建
}
void checkConnections(){
connected.clear();//删除现有连接
个人(其他:个人){
if(距离(x,y,其他.x,其他.y)<100){
已连接。添加(其他):
}
}
}
}
这首先清除ArrayList,然后检查其自身与所有其他对象之间的距离。如果距离足够短(在我的示例中为100px),则会将其添加到ArrayList中
根据您的评论编辑:
这本质上是一个关于图和节点的问题。请参见作为起点。我假设您正在定义一个“集群”作为一组对象,这些对象与该集群中的任何其他对象相差连接链接的人
对象。请注意,这并不是传统计算机科学意义上的“集群”,因此它可能会放弃谷歌搜索
首先,您可以考虑将集群组织成一个ArrayList
的ArrayLists
的Person
对象。我们称之为ArrayList集群
。每个内部ArrayList
代表一个“集群”连接的Person
对象。您还将有另一个ArrayList
表示您已访问的Person
对象,将其称为已访问的对象。然后,您尝试执行的基本操作是:
ArrayList<Person> visited;
ArrayList<ArrayList<Person>> clusters;
void createGroups(){
for(Person p : people){
//if we've already added this Person, skip it
if(!visited.contains(p)){
//since we haven't seen this Person, it isn't clustered yet
ArrayList<Person> newCluster = new ArrayList<Person>();
clusters.add(newCluster);
//start the recursion
addMe(p, newCluster);
}
}
}
void addMe(Person p, ArrayList<Person> cluster){
//if we've already added this Person, skip it
if(visited.contains(p)){
return;
}
//add me first
cluster.add(p);
//remember that we've already visited me
visited.add(p);
//now add my neighbors
for(Person n : p.connections){
addMe(n, cluster);
}
}
ArrayList已访问;
阵列簇;
void createGroups(){
用于(人员p:人员){
//如果我们已经添加了此人,请跳过它
如果(!visted.contains(p)){
//因为我们还没见过这个人,所以它还没有聚集在一起
ArrayList newCluster=新的ArrayList();
clusters.add(newCluster);
//开始递归
addMe(p,newCluster);
}
}
}
void addMe(Person p,ArrayList集群){
//如果我们已经添加了此人,请跳过它
如果(包含(p)){
回来
}
//先加上我
添加(p);
//记住我们已经拜访过我了
添加(p);
//现在加上我的邻居
对于(人员n:p.connections){
地址(n,簇);
}
}
请注意,我还没有测试这段代码,但基本情况是:您需要跟踪您已经访问过的用户,并跳过它们以避免无限递归。这是您的基本情况
还要注意的是,您可能希望了解“真实的”聚类算法。谷歌是你的朋友,因为聚类本身就是一个完整的领域。这已经是我在做的事情了。我在检查人脉是否相交后,将一个连接推到人脉连接数组中。这也是你上面的代码所做的。我真正的问题是。我如何从所有的孩子那里获得所有的连接d连接。例如……假设它们都连接在一条线上。6个连接。每个连接在一个链中。第一个元素如何判断它连接到最后一个元素。当它实际通过一系列连接连接时。不是直接连接。我不确定我这样说是否有意义?也许你需要一个组类…用来保存Person的连接实例?我明白了–这在您的问题中不是很清楚。请查看我的更新答案,并提供一个示例链接。