JavaLab2/src/lab2/algorithm/TSP.java
public int CheapestInsertion(){
        ArrayList<Integer> path = new ArrayList<Integer>(w.size() + 1);
		ArrayList<Integer> notVisited = new ArrayList<Integer>(w.size());
		for(int i = 0; i < w.size(); i++)
			notVisited.add(i, i);
        int cost = 0;

        Integer nextNode = w.getMinAdjacentVertexWeightIndex(0);
        
        path.add(0);
        notVisited.remove(Integer.valueOf(0));
        path.add(nextNode);
        notVisited.remove(nextNode);
        path.add(0);

        for(int i = 0; i < w.size() - 2; i++){
            Integer tmpNode = 0;
            int minCost = Integer.MAX_VALUE;
            for(Integer nodeK : notVisited){
                for(int j = 0; j < path.size() - 1; j++){
					int tmpCost = w.get(path.get(j), nodeK)
					+ w.get(nodeK, path.get(j + 1)) - w.get(path.get(j), path.get(j + 1));
					if(tmpCost < minCost){
						tmpNode = j;
						nextNode = nodeK;
						minCost = tmpCost;
					}
				}
            }
            path.add(tmpNode + 1, nextNode);
            notVisited.remove(nextNode);
        }

        for(int i = 0; i < path.size() - 1; i++)
            cost += w.get(path.get(i), path.get(i + 1));

        return cost;
    }
/**
	 * Finds the 2-approximate solution for TSP problem in O(n^3)
	 * @return cost of path of the solution
	*/
Finds the 2-approximate solution for TSP problem in O(n^3)
@return cost of path of the solution
Finds the 2 - approximate solution for TSP problem in O ( n^3 ) @return cost of path of the solution
public int CheapestInsertion(){
        ArrayList<Integer> path = new ArrayList<Integer>(w.size() + 1);
		ArrayList<Integer> notVisited = new ArrayList<Integer>(w.size());
		for(int i = 0; i < w.size(); i++)
			notVisited.add(i, i);
        int cost = 0;
        Integer nextNode = w.getMinAdjacentVertexWeightIndex(0);
        path.add(0);
        notVisited.remove(Integer.valueOf(0));
        path.add(nextNode);
        notVisited.remove(nextNode);
        path.add(0);
        for(int i = 0; i < w.size() - 2; i++){
            Integer tmpNode = 0;
            int minCost = Integer.MAX_VALUE;
            for(Integer nodeK : notVisited){
                for(int j = 0; j < path.size() - 1; j++){
					int tmpCost = w.get(path.get(j), nodeK)
					+ w.get(nodeK, path.get(j + 1)) - w.get(path.get(j), path.get(j + 1));
					if(tmpCost < minCost){
						tmpNode = j;
						nextNode = nodeK;
						minCost = tmpCost;
					}
				}
            }
            path.add(tmpNode + 1, nextNode);
            notVisited.remove(nextNode);
        }
        for(int i = 0; i < path.size() - 1; i++)
            cost += w.get(path.get(i), path.get(i + 1));
        return cost;
    }
public int CheapestInsertion ( ) { ArrayList < Integer > path = new ArrayList < Integer > ( w . size ( ) + 1 ) ; ArrayList < Integer > notVisited = new ArrayList < Integer > ( w . size ( ) ) ; for ( int i = 0 ; i < w . size ( ) ; i ++ ) notVisited . add ( i , i ) ; int cost = 0 ; Integer nextNode = w . getMinAdjacentVertexWeightIndex ( 0 ) ; path . add ( 0 ) ; notVisited . remove ( Integer . valueOf ( 0 ) ) ; path . add ( nextNode ) ; notVisited . remove ( nextNode ) ; path . add ( 0 ) ; for ( int i = 0 ; i < w . size ( ) - 2 ; i ++ ) { Integer tmpNode = 0 ; int minCost = Integer . MAX_VALUE ; for ( Integer nodeK : notVisited ) { for ( int j = 0 ; j < path . size ( ) - 1 ; j ++ ) { int tmpCost = w . get ( path . get ( j ) , nodeK ) + w . get ( nodeK , path . get ( j + 1 ) ) - w . get ( path . get ( j ) , path . get ( j + 1 ) ) ; if ( tmpCost < minCost ) { tmpNode = j ; nextNode = nodeK ; minCost = tmpCost ; } } } path . add ( tmpNode + 1 , nextNode ) ; notVisited . remove ( nextNode ) ; } for ( int i = 0 ; i < path . size ( ) - 1 ; i ++ ) cost += w . get ( path . get ( i ) , path . get ( i + 1 ) ) ; return cost ; }
Finds the 2-approximate solution for TSP problem in O(n^3)
 @return cost of path of the solution
Finds the 2 - approximate solution for TSP problem in O ( n^3 ) @return cost of path of the solution