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

com.googlecode.blaisemath.graph.GraphMetrics Maven / Gradle / Ivy

/**
 * GraphMetrics.java
 * Created Mar 26, 2015
 */
package com.googlecode.blaisemath.graph;

/*
 * #%L
 * BlaiseGraphTheory
 * --
 * Copyright (C) 2009 - 2016 Elisha Peterson
 * --
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */


import com.google.common.base.Function;
import com.google.common.collect.HashMultiset;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
import com.google.common.collect.Multiset;
import java.util.Map;
import java.util.Set;

/**
 * Utility class for working with {@link GraphMetric}, {@link GraphNodeMetric},
 * and {@link GraphSubsetMetric} instances.
 * @author elisha
 */
public class GraphMetrics {
    
    // utility class
    private GraphMetrics() {
    }

    //
    // METHODS FOR COMPILING INFORMATION ABOUT METRICS IN A GRAPH (ALL NODES)
    //
    
    /**
     * Returns function computing metric value for each node in the given graph.
     * @param  node type
     * @param  value type
     * @param graph the graph
     * @param metric metric used to generate values
     * @return function computing values
     */
    public static  Function asFunction(final Graph graph, final GraphNodeMetric metric) {
        return new Function(){
            @Override
            public T apply(N node) {
                return metric.apply(graph, node);
            }
        };
    }
    
    /**
     * Returns metric values associated with ndoes in the graph.
     * @param  node type
     * @param  value type
     * @param graph the graph
     * @param metric metric used to generate values
     * @return distribution of values
     */
    public static  Map computeValues(Graph graph, GraphNodeMetric metric) {
        return Maps.asMap(graph.nodes(), asFunction(graph, metric));
    }

    /**
     * Returns distribution of the values of a particular metric
     * @param  node type
     * @param  metric result type
     * @param graph the graph
     * @param metric metric used to generate values
     * @return distribution of values
     */
    public static  Multiset computeDistribution(Graph graph, GraphNodeMetric metric) {
        return HashMultiset.create(Iterables.transform(graph.nodes(), asFunction(graph, metric)));
    }
    
    //
    // FACTORY METHODS
    //
    
    /**
     * Crete an additive subset metric based on the given node metric. The resulting metric
     * adds the computed values for each node in a subset together.
     * @param  node type
     * @param baseMetric base metric
     * @return additive metric
     */
    public static  GraphSubsetMetric additiveSubsetMetric(GraphNodeMetric baseMetric) {
        return new AdditiveSubsetMetric(baseMetric);
    }
    
    /**
     * Create a contractive subset metric based on the given node metric. The resulting metric
     * computes a subset metric by first contracting the subset to a point, and then
     * using the base metric in that new graph.
     * @param  node type
     * @param baseMetric base metric
     * @return contractive metric
     */
    public static  GraphSubsetMetric contractiveSubsetMetric(GraphNodeMetric baseMetric) {
        return new ContractiveSubsetMetric(baseMetric);
    }
    
    //
    
    private static class AdditiveSubsetMetric implements GraphSubsetMetric {

        GraphNodeMetric baseMetric;

        /**
         * Constructs with provided base metric.
         * @param baseMetric the metric to use for computations on individual nodes.
         */
        private AdditiveSubsetMetric(GraphNodeMetric baseMetric) { 
            this.baseMetric = baseMetric;
        }

        @Override
        public  N getValue(Graph graph, Set vertices) {
            Double result = 0.0;
            Number val = null;
            for (V v : vertices) {
                val = (Number) baseMetric.apply(graph, v);
                result += val.doubleValue();
            }
            if (val instanceof Integer) {
                return (N) (Integer) result.intValue();
            }
            else if (val instanceof Double) {
                return (N) result;
            }
            else if (val instanceof Float) {
                return (N) (Float) result.floatValue();
            }
            return null;
        }
    }

    
    private static class ContractiveSubsetMetric implements GraphSubsetMetric {

        GraphNodeMetric baseMetric;

        /**
         * Constructs with provided base metric.
         * @param baseMetric the metric to use for computations of contracted node
         */
        private ContractiveSubsetMetric(GraphNodeMetric baseMetric) { 
            this.baseMetric = baseMetric; 
        }

        @Override
        public  N getValue(Graph graph, Set nodes) {
            V starNode = null;
            for (V v : nodes) { starNode = v; break; }
            Graph contracted = GraphUtils.contractedGraph(graph, nodes, starNode);
            return baseMetric.apply(contracted, starNode);
        }

    }
    
    //
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy