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

org.jgrapht.alg.tour.TwoApproxMetricTSP Maven / Gradle / Ivy

The newest version!
/*
 * (C) Copyright 2017-2018, by Dimitrios Michail and Contributors.
 *
 * JGraphT : a free Java graph-theory library
 *
 * This program and the accompanying materials are dual-licensed under
 * either
 *
 * (a) the terms of the GNU Lesser General Public License version 2.1
 * as published by the Free Software Foundation, or (at your option) any
 * later version.
 *
 * or (per the licensee's choosing)
 *
 * (b) the terms of the Eclipse Public License v1.0 as published by
 * the Eclipse Foundation.
 */
package org.jgrapht.alg.tour;

import org.jgrapht.*;
import org.jgrapht.alg.interfaces.*;
import org.jgrapht.alg.spanning.*;
import org.jgrapht.graph.*;
import org.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