All pastes #196481 Raw Edit

Something

public text v1 · immutable
·published 2006-10-10 17:04 UTC
rendered paste body
import java.io.*;
import java.util.*;
import java.util.ArrayList;

public class HamiltonPath
{
    public static final int beginPoint=0;
    public static final int size=13;
    public static int distance[][] = new int[size][size];
    public static int path[] = new int[size];

    
    public static void main(String[] args)
    {
        int a, b, c;
        
        try 
        {
            String file_name="graafhamilton.txt";
            Scanner invoer= new Scanner(new File(file_name)); 

            while (invoer.hasNext() )
            { 
                a=invoer.nextInt();
                b=invoer.nextInt();
                c=invoer.nextInt();
                distance[a-1][b-1]=1;
                distance[b-1][a-1]=1;
             }
            invoer.close(); 
        } 
        catch (Exception e) { };

        path[0] = beginPoint;
        hamiltonPath(0);
    }

    public static void hamiltonPath(int index)
    {
        if (feasible(index)) 
        {
            if (index == size - 1) 
            {
                System.out.print("A solution is: ");
                for (int i = 0; i <= index; i++)
                {
                    int k = path[i] + 1;
                    System.out.print(" " + k);
                }
                System.out.println( );
            }
            else 
            {
                for (int j = 0; j < size; j++) 
                {
                    path[index+1] = j;           //try all vertices as next one
                    hamiltonPath(index+1);
                }
            }
        }
    }

    public static boolean feasible(int index)
    {
        if (index == 0) 
            return true;
      
        if (index > 0 && distance[path[index-1]][path[index]] == 0)
            return false;
            
        int j = 0;
        while (j < index && path[j] != path[index])
            j++;
            
        return (j == index);
    }
}


----

1   2  10
2   3  6
1   4  6
1   5  4
4   5  3
3   6  7
5   6  9
4   7  5
7   8  3
5   8  7
4   8  4
7  13  6
11 13  14
8  11  4
11 10  4
12 10  3
9  12  5
6   9  7

graafhamilton.txt ^^