All Downloads are FREE. Search and download functionalities are using the official Maven repository.

info.debatty.java.graphs.OnlineGraph Maven / Gradle / Ivy

Go to download

Algorithms that build k-nearest neighbors graph (k-nn graph): Brute-force, NN-Descent,...

There is a newer version: 0.41
Show newest version
/*
 * The MIT License
 *
 * Copyright 2016 Thibault Debatty.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package info.debatty.java.graphs;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.concurrent.ExecutionException;

/**
 *
 * @author Thibault Debatty
 * @param 
 */
public class OnlineGraph implements GraphInterface {
    
    protected Graph graph;
    protected int expansion_levels = 3;
    
    public OnlineGraph(Graph initial) {
        this.graph = initial;
                
    }
    
    public OnlineGraph() {
        this.graph = new Graph();
    }
    
    public OnlineGraph(int k) {
        this.graph = new Graph(k);
    }
    
    public int addNode(Node node) {
        
        NeighborList neighborlist = graph.search(node.value, graph.k);
        graph.put(node, neighborlist);
        
        // Nodes to analyze at this iteration
        LinkedList> analyze = new LinkedList>();
        
        // Nodes to analyze at next iteration
        LinkedList> next_analyze = new LinkedList>();
        
        // List of already analyzed nodes
        HashMap, Boolean> visited = new HashMap, Boolean>();
        
        // Fill the list of nodes to analyze
        for (Neighbor neighbor : graph.get(node)) {
            analyze.add(neighbor.node);
        }
        
        int similarities = (int) (graph.size() / graph.speedup);
        for (int level = 0; level < expansion_levels; level++) {
            while (!analyze.isEmpty()){
                Node other = analyze.pop();
                NeighborList other_neighborlist = graph.get(other);
                
                // Add neighbors to the list of nodes to analyze at next iteration
                for (Neighbor other_neighbor : other_neighborlist) {
                    if (!visited.containsKey(other_neighbor.node)) {
                        next_analyze.add(other_neighbor.node);
                    }
                }
                
                // Try to add the new node (if sufficiently similar)
                similarities++;
                other_neighborlist.add(new Neighbor(
                        node,
                        graph.similarity.similarity(
                                node.value,
                                (T) other.value)));
                
                visited.put(other, Boolean.TRUE);
            }
            
            analyze = next_analyze;
            next_analyze =  new LinkedList>();
        }
        
        return similarities;
    }

    public ArrayList> connectedComponents() {
        return graph.connectedComponents();
    }

    public boolean containsKey(Node node) {
        return graph.containsKey(node);
    }

    public Iterable, NeighborList>> entrySet() {
        return graph.entrySet();
    }

    public NeighborList get(Node node) {
        return graph.get(node);
    }

    public int getK() {
        return graph.getK();
    }

    public SimilarityInterface getSimilarity() {
        return graph.getSimilarity();
    }

    public double getSpeedup() {
        return graph.getSpeedup();
    }

    public void prune(double threshold) {
        graph.prune(threshold);
    }

    public NeighborList put(Node node, NeighborList neighborlist) {
        return graph.put(node, neighborlist);
    }

    public NeighborList search(T query, int K) {
        return graph.search(query, K);
    }

    public NeighborList search(T query, int K, double expansion) {
        return graph.search(query, K, expansion);
    }

    public void setK(int k) {
        graph.setK(k);
    }

    public void setSimilarity(SimilarityInterface similarity) {
        graph.setSimilarity(similarity);
    }

    public void setSpeedup(double speedup) {
        graph.setSpeedup(speedup);
    }

    public int size() {
        return graph.size();
    }

    public ArrayList> stronglyConnectedComponents() {
        return graph.stronglyConnectedComponents();
    }

    public void writeGEXF(String filename) throws FileNotFoundException, IOException {
        graph.writeGEXF(filename);
    }

    public Iterable> getNodes() {
        return graph.getNodes();
    }

    public NeighborList searchExhaustive(T query, int K) 
            throws InterruptedException,ExecutionException{
        return graph.searchExhaustive(query, K);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy