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

com.salesforce.jgrapht.alg.tour.TwoApproxMetricTSP Maven / Gradle / Ivy

Go to download

This project contains the apt processor that implements all the checks enumerated in @Verify. It is a self contained, and shaded jar.

The newest version!
/*
 * (C) Copyright 2017-2018, by Dimitrios Michail and Contributors.
 *
 * JGraphT : a free Java graph-theory library
 *
 * See the CONTRIBUTORS.md file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0, or the
 * GNU Lesser General Public License v2.1 or later
 * which is available at
 * http://www.gnu.org/licenses/old-licenses/lgpl-2.1-standalone.html.
 *
 * SPDX-License-Identifier: EPL-2.0 OR LGPL-2.1-or-later
 */
package com.salesforce.jgrapht.alg.tour;

import com.salesforce.jgrapht.*;
import com.salesforce.jgrapht.alg.interfaces.*;
import com.salesforce.jgrapht.alg.spanning.*;
import com.salesforce.jgrapht.graph.*;
import com.salesforce.jgrapht.traverse.*;

import java.util.*;

/**
 * A 2-approximation algorithm for the metric TSP problem.
 * 
 * 

* The travelling salesman problem (TSP) asks the following question: "Given a list of cities and * the distances between each pair of cities, what is the shortest possible route that visits each * city exactly once and returns to the origin city?". In the metric TSP, the intercity distances * satisfy the triangle inequality. * *

* This is an implementation of the folklore algorithm which returns a depth-first ordering of the * minimum spanning tree. The algorithm is a 2-approximation assuming that the instance satisfies * the triangle inequality. The implementation requires the input graph to be undirected and * complete. The running time is $O(|V|^2 \log |V|)$. * *

* See wikipedia for more * details. * * @param the graph vertex type * @param the graph edge type * * @author Dimitrios Michail */ public class TwoApproxMetricTSP implements HamiltonianCycleAlgorithm { /** * Construct a new instance */ public TwoApproxMetricTSP() { } /** * Computes a 2-approximate tour. * * @param graph the input graph * @return a tour * @throws IllegalArgumentException if the graph is not undirected * @throws IllegalArgumentException if the graph is not complete * @throws IllegalArgumentException if the graph contains no vertices */ @Override public GraphPath getTour(Graph graph) { if (!graph.getType().isUndirected()) { throw new IllegalArgumentException("Graph must be undirected"); } if (!GraphTests.isComplete(graph)) { throw new IllegalArgumentException("Graph is not complete"); } if (graph.vertexSet().isEmpty()) { throw new IllegalArgumentException("Graph contains no vertices"); } /* * Special case singleton vertex */ if (graph.vertexSet().size() == 1) { V start = graph.vertexSet().iterator().next(); return new GraphWalk<>( graph, start, start, Collections.singletonList(start), Collections.emptyList(), 0d); } /* * Create MST */ Graph mst = new SimpleGraph<>(DefaultEdge.class); for (V v : graph.vertexSet()) { mst.addVertex(v); } for (E e : new KruskalMinimumSpanningTree<>(graph).getSpanningTree().getEdges()) { mst.addEdge(graph.getEdgeSource(e), graph.getEdgeTarget(e)); } /* * Perform a depth-first-search traversal */ int n = graph.vertexSet().size(); Set found = new HashSet<>(n); List tour = new ArrayList<>(n + 1); V start = graph.vertexSet().iterator().next(); DepthFirstIterator dfsIt = new DepthFirstIterator<>(mst, start); while (dfsIt.hasNext()) { V v = dfsIt.next(); if (found.add(v)) { tour.add(v); } } // repeat the start vertex tour.add(start); /* * Explicitly build the path. */ List tourEdges = new ArrayList<>(n); double tourWeight = 0d; Iterator tourIt = tour.iterator(); V u = tourIt.next(); while (tourIt.hasNext()) { V v = tourIt.next(); E e = graph.getEdge(u, v); tourEdges.add(e); tourWeight += graph.getEdgeWeight(e); u = v; } return new GraphWalk<>(graph, start, start, tour, tourEdges, tourWeight); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy