简体   繁体   English

Java / CGAL验证是否已连接图形(描述中的一些约束)

[英]Java/CGAL verify if a graph is connected (some constraints in description)

it's my first time with CGAL, some of you may argue why do I have to learn CGAL from something like that, but it's a new project that I must do (and... yes, I must use CGAL and Java combined) :/ Long story short... I only have: 这是我第一次使用CGAL,其中有些人可能会争论为什么我必须从类似的东西中学习CGAL,但这是我必须要做的一个新项目(而且,是的,我必须结合使用CGAL和Java):/长话短说...我只有:

  • Two double arrays, representing x and y coordinates of my vertices. 两个双精度数组,分别表示我的顶点的x和y坐标。 Let's call them double[] x, y; 我们称它们为double[] x, y; .
  • Both arrays have S random values. 两个数组都有S随机值。
  • Two vertices, u and w are connected if distance(x[u], y[u], x[w], y[w]) < CONSTANT (ofc. I do distanceSquared(x[u], y[u], x[w], y[w]) < CONSTANT_SQUARED , so I avoid to call sqrt()). 两个顶点, uw连接如果distance(x[u], y[u], x[w], y[w]) < CONSTANT (OFC。我做distanceSquared(x[u], y[u], x[w], y[w]) < CONSTANT_SQUARED ,所以我避免调用sqrt())。
  • x and y are filled randomly with values from 0 to UPPER_LIMIT , no other infos are given. xy0UPPER_LIMIT值随机填充,未提供其他信息。

Question, do x and y describes a connected graph? 问题, xy描述了连通图?

Right now I have two algoritms: 现在我有两个算法:

  • Algorithm 1: 算法1:

    1. Build adjacency list ( Arraylist<Integer>[] adjLists; ) for each vertex (only upper triangular matrix explored). 为每个顶点建立邻接表( Arraylist<Integer>[] adjLists; )(仅探索上三角矩阵)。 Complexity O(|V|^2) (V = vertices set). 复杂度O(|V|^2) (V =顶点集)。
    2. Recursive graph exploration, vertex marking and counting, if visited vertex equals S my graph have only one connected component, my graph is connected. 递归图探索,顶点标记和计数,如果访问的顶点等于S ,则我的图只有一个连接的组件,则我的图已连接。 Complexity O(|E|) (E = edges set). 复杂度O(|E|) (E =设置边)。
  • Algorithm 2: 算法2:

     private static boolean algorithmGraph(double[] x, double[] y) { int unchecked, inside = 0, current = 0; double switchVar; while (current <= inside && inside != S - 1) { unchecked = inside + 1; while (unchecked < S) { if ((x[current] - x[unchecked]) * (x[current] - x[unchecked]) + (y[current] - y[unchecked]) * (y[current] - y[unchecked]) <= CONSTANT_SQUARED) { inside++; // switch x coordinates | unchecked <-> inside switchVar = x[unchecked]; x[unchecked] = x[inside]; x[inside] = switchVar; // switch y coordinates | unchecked <-> inside switchVar = y[unchecked]; y[unchecked] = y[inside]; y[inside] = switchVar; } unchecked++; } current++; } return inside == S - 1; } 

Funny thing the second one is slower, I do not use data structures, the code is iterative and in-place but the heavy use of switch makes it slow as hell. 有趣的是第二个比较慢,我不使用数据结构,代码是迭代的和就地的,但是大量使用switch使其变慢。

The problem spec changed and now I must do it with CGAL and Java, I'll read the whole " https://github.com/CGAL/cgal-swig-bindings " to learn how to use CGAL within Java.... but I'd like some help about this specific instance of CGAL code... Are there faster algorithms already implemented in CGAL? 问题规范已更改,现在我必须使用CGAL和Java进行操作,我将阅读全文“ https://github.com/CGAL/cgal-swig-bindings ”,以了解如何在Java中使用CGAL。但我希望获得有关此CGAL代码特定实例的帮助... CGAL中是否已经实现了更快的算法?

Thank you for your times guys! 谢谢你们给我们的时间! Happy coding! 编码愉快!

I believe that, without a method of spatial indexing , the best performance you are going to achieve in the worst-case-scenario (all connected) is going to be O(n*(n-1)/2) . 我相信,如果没有空间索引方法,则在最坏情况(所有连接)下要实现的最佳性能将是O(n*(n-1)/2)

If you can afford to build a spatial index (have enough memory to pay for the boost in speed), you may consider R-tree and variants - insertion is O(n) searching is O(log2(n)) : this will get your "outlier detection by examining distances" approach for a cost of of O(n*log2(n)) in the worst-case-scenario. 如果您有能力建立空间索引(有足够的内存来支付速度的提高),则可以考虑使用R树和变量 -插入为O(n)搜索为O(log2(n)) :您的“通过检查距离进行异常检测”方法在最坏的情况下的成本为O(n*log2(n))

A notable result 明显的结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM