Run Prim's algorithm on a dense graph.

@param data Data set
@param adapter Adapter instance
@param collector Edge collector
187217
public static <T> void processDense(T data, Adapter<T> adapter, Collector collector) {
    // Number of nodes
    final int n = adapter.size(data);
    // Best distance for each node
    double[] best = new double[n];
    Arrays.fill(best, Double.POSITIVE_INFINITY);
    // Best previous node
    int[] src = new int[n];
    // Nodes already handled
    // byte[] uses more memory, but it will be faster.
    byte[] connected = new byte[n];

    // We always start at "random" node 0
    // Note: we use this below in the "j" loop!
    int current = 0;
    connected[current] = 1;
    best[current] = 0;

    // Search
    for(int i = n - 2; i >= 0; i--) {
      // Update best and src from current:
      int newbesti = -1;
      double newbestd = Double.POSITIVE_INFINITY;
      // Note: we assume we started with 0, and can thus skip it
      for(int j = 0; j < n; ++j) {
        if(connected[j] == 1) {
          continue;
        }
        final double dist = adapter.distance(data, current, j);
        if(dist < best[j]) {
          best[j] = dist;
          src[j] = current;
        }
        if(best[j] < newbestd || newbesti == -1) {
          newbestd = best[j];
          newbesti = j;
        }
      }
      assert (newbesti >= 0);
      // Flag
      connected[newbesti] = 1;
      // Store edge
      collector.addEdge(newbestd, src[newbesti], newbesti);
      // Continue
      current = newbesti;
    }
  }
187430
@Override
    public void registerAdapterDataObserver(RecyclerView.AdapterDataObserver observer) {
        super.registerAdapterDataObserver(observer);
        if (mAdapter != null) {
            mAdapter.registerAdapterDataObserver(observer);
        }
    }
1011605
protected static <A> double[] ranks(final NumberArrayAdapter<?, A> adapter, final A data, int[] idx) {
    final int len = idx.length;
    double[] ret = new double[len];
    for(int i = 0; i < len;) {
      final int start = i++;
      final double val = adapter.getDouble(data, idx[start]);
      // Include ties:
      while(i < len && adapter.getDouble(data, idx[i]) <= val) {
        i++;
      }
      final double score = (start + i - 1) * .5 + 1;
      for(int j = start; j < i; j++) {
        ret[idx[j]] = score;
      }
    }
    return ret;
  }
156167
@Override
    public void putGraph(Model data) {

        if (data == null)
            return;

        // Use HTTP protocol if possible
        if (this.sparqlServiceEndpoint != null) {
            datasetAccessor.putModel(data);
        } else {
            this.putGraphSparqlQuery(data);
        }

    }
329017
private <A> ArrayList<int[]> buildPartitions(NumberArrayAdapter<?, A> adapter1, A data1, int len, int depth) {
    final int[] idx = new int[len];
    final double[] tmp = new double[len];
    for(int i = 0; i < len; ++i) {
      idx[i] = i;
      tmp[i] = adapter1.getDouble(data1, i);
    }
    // Sort indexes:
    IntegerArrayQuickSort.sort(idx, (x, y) -> Double.compare(tmp[x], tmp[y]));
    Arrays.sort(tmp); // Should yield the same ordering

    ArrayList<int[]> ret = new ArrayList<>(1 << depth);
    divide(idx, tmp, ret, 0, tmp.length, depth);
    return ret;
  }
158366
public static int nvgraphSetEdgeData(
        nvgraphHandle handle, 
        nvgraphGraphDescr descrG, 
        Pointer edgeData, 
        long setnum)
    {
        return checkResult(nvgraphSetEdgeDataNative(handle, descrG, edgeData, setnum));
    }
394961
@SuppressWarnings({ "rawtypes", "unchecked" })
  public void close() throws IOException {
    if (!closed) {
      closeNewList();
      if (collectorType.equals(DataCollector.COLLECTOR_TYPE_LIST)) {
        // compute initial basic list
        TreeMap<String, MtasDataItem<T1, T2>> basicList = new TreeMap<>();
        for (int i = 0; i < getSize(); i++) {
          MtasDataItem<T1, T2> newItem = getItem(i);
          if (basicList.containsKey(keyList[i])) {
            newItem.add(basicList.get(keyList[i]));
          }
          basicList.put(keyList[i], newItem);
        }
        // create result based on basic list
        result = new MtasDataCollectorResult<>(collectorType, sortType,
            sortDirection, basicList, start, number);
        // reduce
        if (segmentRegistration != null) {
          if (segmentRegistration.equals(SEGMENT_SORT_ASC)
              || segmentRegistration.equals(SEGMENT_SORT_DESC)) {
            reduceToKeys(result.getComparatorList().keySet());
          } else if (segmentRegistration.equals(SEGMENT_BOUNDARY_ASC)
              || segmentRegistration.equals(SEGMENT_BOUNDARY_DESC)) {
            Map<String, MtasDataItemNumberComparator> comparatorList = result
                .getComparatorList();
            HashSet<String> filteredKeySet = new HashSet<>();
            if (segmentRegistration.equals(SEGMENT_BOUNDARY_ASC)) {
              for (Entry<String, MtasDataItemNumberComparator> entry : comparatorList
                  .entrySet()) {
                if (entry.getValue().compareTo(segmentValueBoundary) < 0) {
                  filteredKeySet.add(entry.getKey());
                }
              }
            } else {
              for (Entry<String, MtasDataItemNumberComparator> entry : comparatorList
                  .entrySet()) {
                if (entry.getValue().compareTo(segmentValueBoundary) > 0) {
                  filteredKeySet.add(entry.getKey());
                }
              }
            }
            reduceToKeys(filteredKeySet);
            basicList.keySet().retainAll(filteredKeySet);
            result = new MtasDataCollectorResult<>(collectorType, sortType,
                sortDirection, basicList, start, number);
          }
        }
      } else if (collectorType.equals(DataCollector.COLLECTOR_TYPE_DATA)) {
        if (getSize() > 0) {
          result = new MtasDataCollectorResult<>(collectorType, getItem(0));
        } else {
          result = new MtasDataCollectorResult<>(collectorType, sortType,
              sortDirection);
        }
      } else {
        throw new IOException("type " + collectorType + " not supported");
      }
      closed = true;
    }
  }
549177
public void setAdapter(Adapter adapter) {
        if (mAdapter != null) {
            mAdapter.unregisterAdapterDataObserver(mObserver);
        }
        // end all running animations
        if (mItemAnimator != null) {
            mItemAnimator.endAnimations();
        }
        // Since animations are ended, mLayout.children should be equal to recyclerView.children.
        // This may not be true if item animator's end does not work as expected. (e.g. not release
        // children instantly). It is safer to use mLayout's child count.
        if (mLayout != null) {
            mLayout.removeAndRecycleAllViews(mRecycler);
            mLayout.removeAndRecycleScrapInt(mRecycler, true);
        }

        final Adapter oldAdapter = mAdapter;
        mAdapter = adapter;
        if (adapter != null) {
            adapter.registerAdapterDataObserver(mObserver);
        }
        if (mLayout != null) {
            mLayout.onAdapterChanged(oldAdapter, mAdapter);
        }
        mRecycler.onAdapterChanged(oldAdapter, mAdapter);
        mState.mStructureChanged = true;
        markKnownViewsInvalid();
        requestLayout();
    }
423192
@Override
	public EdgeMetrics<K, VV, EV> run(Graph<K, VV, EV> input)
			throws Exception {
		super.run(input);

		// s, t, (d(s), d(t))
		DataSet<Edge<K, Tuple3<EV, LongValue, LongValue>>> edgeDegreePair = input
			.run(new EdgeDegreePair<K, VV, EV>()
				.setReduceOnTargetId(reduceOnTargetId)
				.setParallelism(parallelism));

		// s, d(s), count of (u, v) where deg(u) < deg(v) or (deg(u) == deg(v) and u < v)
		DataSet<Tuple3<K, LongValue, LongValue>> edgeStats = edgeDegreePair
			.map(new EdgeStats<>())
				.setParallelism(parallelism)
				.name("Edge stats")
			.groupBy(0)
			.reduce(new SumEdgeStats<>())
			.setCombineHint(CombineHint.HASH)
				.setParallelism(parallelism)
				.name("Sum edge stats");

		edgeMetricsHelper = new EdgeMetricsHelper<>();

		edgeStats
			.output(edgeMetricsHelper)
				.setParallelism(parallelism)
				.name("Edge metrics");

		return this;
	}
203814
@Override
    public void fit(@NonNull MultiDataSetIterator iterator) {
        Map<Integer, NormalizerStats.Builder> inputStatsBuilders = new HashMap<>();
        Map<Integer, NormalizerStats.Builder> outputStatsBuilders = new HashMap<>();

        iterator.reset();
        while (iterator.hasNext()) {
            fitPartial(iterator.next(), inputStatsBuilders, outputStatsBuilders);
        }

        inputStats = buildAllStats(inputStatsBuilders);
        outputStats = buildAllStats(outputStatsBuilders);
    }
342024
void build(int nodeOffset, GraphMeshData graphData, List<int[]> connections) {
		for (int n = 0; n < connections.size(); n++) {
			int[] nodeConnections = connections.get(n);
			MeshData tile = graphData.getTile(n);
			Poly node = graphData.getNode(n);
			for (int connection : nodeConnections) {
				MeshData neighbourTile = graphData.getTile(connection - nodeOffset);
				if (neighbourTile != tile) {
					buildExternalLink(tile, node, neighbourTile);
				} else {
					Poly neighbour = graphData.getNode(connection - nodeOffset);
					buildInternalLink(tile, node, neighbourTile, neighbour);
				}
			}
		}
	}
1252977
public void run(final List<Tuple> data) {
        List<Tuple> dataCopy = new ArrayList<>(data);
        this.labels = data.parallelStream().map(x -> x.label).collect(Collectors.toSet());
        if (shuffleData) {
            Collections.shuffle(dataCopy);
        }

        int chunkSize = data.size() / nfold;

        int reminder = data.size() % chunkSize;

        for (int i = data.size() - 1; i > data.size() - 1 - reminder; i--) {
            LOG.info("Dropping the tail id: " + data.get(i).id);
        }

        for (int i = 0; i < nfold; i++) {
            System.err.println("Cross validation round " + (i + 1) + "/" + nfold);
            List<Tuple> testing = new ArrayList<>(data.subList(i, i + chunkSize));
            List<Tuple> training = new ArrayList<>(data.subList(0, i));
            training.addAll(data.subList(i + chunkSize, data.size()));

            eval(training, testing, i);
        }
    }
574297
public void setRootData(Map data) {
		for (Scope cur=this; true; cur=cur.parent) {
			if (cur.parent == null) {
				cur.data = data;
				return ;
			}
		}
	}
1105971
public <T> T output(long graphId, T value, OperandsAdapter<T> adapter) {
        return adapter.output(this.output(graphId, adapter.input(value)));
    }
840197
public static void collectStats(final StatsCollector collector) {
    collector.record("http.latency", graphlatency, "type=graph");
    collector.record("http.latency", gnuplotlatency, "type=gnuplot");
    collector.record("http.graph.requests", graphs_diskcache_hit, "cache=disk");
    collector.record("http.graph.requests", graphs_generated, "cache=miss");
  }
557491
private static <T> void insertionSort(T data, Adapter<T> adapter, int start, int end) {
    for(int i = start + 1; i < end; i++) {
      for(int j = i; j > start && adapter.compare(data, j - 1, j) > 0; j--) {
        adapter.swap(data, j, j - 1);
      }
    }
  }
627880
@Override
	public Graph<K, VV, EV> runInternal(Graph<K, VV, EV> input)
			throws Exception {
		// u, d(u)
		DataSet<Vertex<K, LongValue>> vertexDegree = input
			.run(new VertexDegree<K, VV, EV>()
				.setReduceOnTargetId(reduceOnTargetId.get())
				.setParallelism(parallelism));

		// u, d(u) if d(u) > maximumDegree
		DataSet<Tuple1<K>> highDegreeVertices = vertexDegree
			.flatMap(new DegreeFilter<>(maximumDegree))
				.setParallelism(parallelism)
				.name("Filter high-degree vertices");

		JoinHint joinHint = broadcastHighDegreeVertices.get() ? JoinHint.BROADCAST_HASH_SECOND : JoinHint.REPARTITION_HASH_SECOND;

		// Vertices
		DataSet<Vertex<K, VV>> vertices = input
			.getVertices()
			.leftOuterJoin(highDegreeVertices, joinHint)
			.where(0)
			.equalTo(0)
			.with(new ProjectVertex<>())
				.setParallelism(parallelism)
				.name("Project low-degree vertices");

		// Edges
		DataSet<Edge<K, EV>> edges = input
			.getEdges()
			.leftOuterJoin(highDegreeVertices, joinHint)
			.where(reduceOnTargetId.get() ? 1 : 0)
			.equalTo(0)
				.with(new ProjectEdge<>())
				.setParallelism(parallelism)
				.name("Project low-degree edges by " + (reduceOnTargetId.get() ? "target" : "source"))
			.leftOuterJoin(highDegreeVertices, joinHint)
			.where(reduceOnTargetId.get() ? 0 : 1)
			.equalTo(0)
			.with(new ProjectEdge<>())
				.setParallelism(parallelism)
				.name("Project low-degree edges by " + (reduceOnTargetId.get() ? "source" : "target"));

		// Graph
		return Graph.fromDataSet(vertices, edges, input.getContext());
	}