KusanoNEU的博客

No day but today.

RNA secondary structure(Dynamic Programming)
//
//  main.cpp
//  RNA
//
//  Created by Longxiang Lyu on 6/1/16.
//  Copyright (c) 2016 Longxiang Lyu. All rights reserved.
//

#include <iostream>
#include <string>
#include <vector>

using namespace std;

bool complement(char c1, char c2)
{
    return  (c1 == 'A' && c2 == 'U') ||
            (c1 == 'U' && c2 == 'A') ||
            (c1 == 'C' && c2 == 'G') ||
            (c1 == 'G' && c2 == 'C');
}

void findSolution(const vector<vector<int>> &M, int i, int j, vector<pair<int, int>> &pairs)
{
    for (int t = i; t < j - 4; ++t)
    {
        if (M[i][j] == 1 + M[i][t - 1] + M[t + 1][j - 1])
        {
            pairs.push_back({t, j});
            findSolution(M, i, t - 1, pairs);
            findSolution(M, t + 1, j - 1, pairs);
        }
    }
}

int RNA(string seq, vector<pair<int, int>> &pairs)
{
    for (auto &c : seq)
        c = toupper(c);
    int n = static_cast<int>(seq.length() - 1);
    vector<vector<int>> M(n + 1, vector<int>(n + 1, 0));
    
    for (int interval = 5; interval <= n -1; ++interval)
    {
        for (int i = 1; i <= n - interval; ++i)
        {
            // compute M[i][j]
            int j = i + interval;
            int opt = M[i][j - 1];      // case 1: bj is not involved in pair
            
            
            for (int t = i; t < j - 4; ++t)
            {
                if (complement(seq[t], seq[j]) && opt < 1 + M[i][t - 1] + M[t + 1][j - 1])
                {
                    opt = 1 + M[i][t - 1] + M[t + 1][j - 1];        // case 2: choose the largest pair sum
                }
            }
            M[i][j] = opt;
        }
    }
    findSolution(M, 1, n, pairs);
    return M[1].back();
}

int main(int argc, const char * argv[])
{
    string seq = "*AUGUGGGCAU";
    vector<pair<int, int>> ret;
    cout << "Max number of pairs: " << RNA(seq, ret) << endl;
    for (auto a : ret)
    {
        cout << a.first << " - " << a.second << endl;
    }

    return 0;
}

Reference:

http://www.cs.princeton.edu/~wayne/kleinberg-tardos/pdf/06DynamicProgrammingI.pdf

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/KusanoNEU/article/details/51563308
文章标签: Dynamic Programming
个人分类: 算法
想对作者说点什么? 我来说一句

RNA Secondary Structure Prediction7.rar

2011年12月25日 12.18MB 下载

RNAstructure

2010年04月21日 4.13MB 下载

dynamic programming by Bellman

2009年07月17日 3.16MB 下载

Dynamic Programming and optimal control

2009年08月01日 7.46MB 下载

没有更多推荐了,返回首页

不良信息举报

RNA secondary structure(Dynamic Programming)

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭