Algorithms to find the number of Hamiltonian paths in a graph

后端 未结 5 1342
轮回少年
轮回少年 2020-12-19 10:05

I\'m trying to solve a slightly modified version of the Hamiltonian Path problem. It is modified in that the start and end points are given to us and instead of determining

5条回答
  •  伪装坚强ぢ
    2020-12-19 10:22

    It can be solved using DP with bitmasking for values of n upto 20 or a few more i guess. Create a 2d dp table. dp[i][j] represents the answer of case that you are on ith vertex and j stores the information about visited vertices.Here's a C++ code.

    Macros used:

    #define    oncnt    __builtin_popcount
    typedef    vector   vi;
    

    Outside Main:

    vi ad[21];
     int n,m;
    
     int dp[20][(1<<19)+1];
    
    int sol(int i,int mask)
    {
        //cout<

    Inside Main:

    cin>>n>>m;
    
    for(int i=0;i<=n-1;i++)
     {
        for(int j=0;j<=(1<<(n-1));j++)
        dp[i][j]=-1;
     }
    
    
    int a,b;
    for(int i=1;i<=m;i++)
    {
        cin>>a>>b;
        a--;
        b--;
        ad[a].pb(b);
    }
    
    
     cout<

提交回复
热议问题