<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    where the amazing happens

    算法2 : 動態(tài)規(guī)劃

    動態(tài)規(guī)劃最優(yōu)化原理中的一種重要的方法。

    動態(tài)規(guī)劃在查找有很多重疊子問題的情況的最優(yōu)解時有效。它將問題重新組合成子問題。為了避免多次解決這些子問題,它們的結果都逐漸被計算并被保存,從簡單的問題直到整個問題都被解決。因此,動態(tài)規(guī)劃保存遞歸時的結果,因而不會在解決同樣的問題時花費時間。

    動態(tài)規(guī)劃只能應用于有最優(yōu)子結構的問題。最優(yōu)子結構的意思是局部最優(yōu)解能決定全局最優(yōu)解。簡單地說,問題能夠分解成子問題來解決。

    總的來說,動態(tài)規(guī)劃的優(yōu)勢在于:

    • 重疊子問題
    • 最優(yōu)子結構
    • 記憶化


    問題描述:
    動態(tài)規(guī)劃舉例
    圖10-3(a)示出了一個數(shù)字三角形,請編一個程序,
    計算從頂至底的某處的一條路勁,
    使該路勁所經(jīng)過的數(shù)字的總和最大。
    (1) 每一步可沿左斜線向下或右斜線向下;
    (2) 1<三角形行數(shù)≤100;
    (3) 三角形中的數(shù)字為0,1,……99。
    輸入數(shù)據(jù):
    由INPUT.TXT文件中首先讀到的是三角形的行數(shù),
    在例子中INPUT.TXT表示如圖13-3(b).?
    ???????????????????????????????
    ?????????????????????????????? 7?
    ????????????????????????????? 3 8?
    ???????????????????????????? 8 1 0?
    ????????????????????????????2 7 4 4?
    ???????????????????????????4 5 2 6 5
    ????????????????????????? 5 6 9 5 9 8



    從別人blog里看到這個題目,手上癢癢,就寫了一個.用的是逆向遞推法從頂部往下.
    分2個文件,一個主程序和一個用于讀取文件的輔助類.


    思路:
    ? 1.算出每個節(jié)點的規(guī)劃值(也就是待比較的最大值),并記錄搜索路徑
    ??2.取三角形底邊所有規(guī)劃值中的最大值
    ? 3.輸出路徑

    主程序

    package ?test;
    import ?java.util. * ;

    /**
    ?*??用動態(tài)規(guī)劃法求出最優(yōu)路徑
    ?*??
    @author ?zqc
    ?*?
    */

    public ? class ?DynamicSolveTrianglePath? {
    ????
    ????
    private ?String[][]?str_triangle? = ? null ;
    ????
    private ?Node[][]?triangle_nodes? = ? null ;
    ????
    private ?List?nodes;
    ????
    private ?List?paths;
    ????
    ????
    // 節(jié)點
    ???? static ? class ?Node {
    ????????
    private ? int ?value;
    ????????
    private ?List?path? = ? new ?Vector();
    ????????
    public ?List?getPath()? {
    ????????????
    return ?path;
    ????????}

    ????????
    public ? void ?setPath(List?p) {
    ????????????path?
    = ?p;
    ????????}

    ????????
    // n=(0,0)?or?(0,1)?or?(2,2)
    ???????? public ? void ?addPath(String?n) {
    ????????????path.add(n);
    ????????}

    ????????
    public ? void ?addPath(List?pa) {
    ????????????path.addAll(pa);
    ????????}

    ????????
    public ? int ?getValue()? {
    ????????????
    return ?value;
    ????????}

    ????????
    public ? void ?setValue( int ?value)? {
    ????????????
    this .value? = ?value;
    ????????}

    ????}

    ????
    ????
    public ?DynamicSolveTrianglePath() {
    ????????initNodes();
    ????????findPath();
    ????}

    ????
    ????
    //根節(jié)點開始,逆向推解出到達所有節(jié)點的最佳路徑
    ????private?void?initNodes(){
    ????????
    this.str_triangle?=?ReadTriangle.getTriangle();
    ????????triangle_nodes?
    =?new?Node[str_triangle.length][];
    ????????nodes?
    =?new?Vector();
    ????????
    for(int?i?=?0?;?i?<?str_triangle.length;?i++){
    ????????????triangle_nodes[i]?
    =?new?Node[str_triangle[i].length];
    ????????????
    for(int?j?=?0?;?j<str_triangle[i].length;j++){
    ????????????????String?currentPath?
    =?"("+i+","+j+")";
    ????????????????Node?n?
    =?new?Node();
    ???????????????
    if(i==0&&j==0){
    ???????????????????n.setValue(
    ??????????????????????????????c(str_triangle[
    0][0])????????
    ????????????????????????????);
    ???????????????????n.addPath(currentPath);
    ???????????????????triangle_nodes[i][j]?
    =?n;
    ???????????????????
    continue;
    ???????????????}

    ???????????????
    //左右邊界節(jié)點
    ???????????????if((j==0||j==str_triangle[i].length-1)){
    ???????????????????
    if(i==1){//第2行的節(jié)點
    ???????????????????????int?value?=?c(str_triangle[0][0])+c(str_triangle[i][j]);
    ???????????????????????List?ph?
    =?triangle_nodes[0][0].getPath();
    ???????????????????????n.addPath(ph);
    ???????????????????????n.addPath(currentPath);
    ???????????????????????n.setValue(value);
    ???????????????????????ph?
    =?null;
    ???????????????????}
    else{//0,1行以下的其他邊界節(jié)點
    ?????????????????????int?value?=?j==0?c(str_triangle[i][j])+triangle_nodes[i-1][j].getValue():
    ?????????????????????????c(str_triangle[i][j])
    +triangle_nodes[i-1][j-1].getValue();
    ?????????????????????List?ph?
    =?j==0?triangle_nodes[i-1][j].getPath():
    ?????????????????????????triangle_nodes[i
    -1][j-1].getPath();
    ?????????????????????n.addPath(ph);
    ?????????????????????n.addPath(currentPath);
    ?????????????????????n.setValue(value);
    ???????????????????}

    ???????????????}
    else{//除邊界節(jié)點外其他節(jié)點
    ???????????????????????Node?node1?=?triangle_nodes[i-1][j-1];
    ???????????????????????Node?node2?
    =?triangle_nodes[i-1][j];
    ???????????????????????Node?bigger?
    =?max(node1,node2);
    ???????????????????????List?ph?
    =?bigger.getPath();
    ???????????????????????n.addPath(ph);
    ???????????????????????n.addPath(currentPath);
    ???????????????????????
    int?val?=?bigger.getValue()+c(str_triangle[i][j]);
    ???????????????????????n.setValue(val);
    ???????????????}

    ??????????????triangle_nodes[i][j]?
    =?n;?
    ??????????????n?
    =?null;
    ????????????}
    //end?of?for?j
    ????????}
    //end?of?for?i
    ????}

    ????
    ????
    private?Node?max(Node?node1,Node?node2){
    ????????
    int?i1?=?node1.getValue();
    ????????
    int?i2?=?node2.getValue();
    ????????
    return?i1>i2?node1:node2;
    ????}

    ????
    ????
    private?int?c(String?s){
    ????????
    return?Integer.parseInt(s);
    ????}

    ????
    ????
    //找出最佳路徑
    ????private?void?findPath(){
    ????????
    if(this.triangle_nodes==null)return;
    ????????
    int?max?=?0;
    ????????
    int?mi?=?0;
    ????????
    int?mj?=?0;
    ????????
    for(int?i?=?0?;?i?<?triangle_nodes.length;?i++){
    ????????????
    for(int?j?=?0?;?j<triangle_nodes[i].length;j++){
    ????????????????
    int?t?=?triangle_nodes[i][j].getValue();
    ????????????????
    if(t>max){
    ????????????????????max?
    =?t;
    ????????????????????mi?
    =?i;
    ????????????????????mj?
    =?j;
    ????????????????}

    ????????????}

    ????????}

    ????????System.out.println(
    "Max?Path:"+triangle_nodes[mi][mj].getPath());
    ????????System.out.println(
    "Max?Value:"+max);
    ????}

    ????
    ????
    public?static?void?main(String[]?args)throws?Exception{
    ????????DynamicSolveTrianglePath?dsp?
    =?new
    ???????????DynamicSolveTrianglePath();
    ????}

    ????
    }

    用于讀取文件的輔助類

    package ?test;
    import ?java.io. * ;
    import ?java.util. * ;

    /**
    ?*??輸入文本格式為
    ?*??類似這樣一個數(shù)字三角形
    ?*??
    ?*??????????x
    ?*?????????x?x
    ?*????????x?x?x
    ?*???????x?x?x?x
    ?*??????x?x?x?x?x?
    ?*??????
    ?*??
    @author ?zqc
    ?*?
    */

    public ? class ?ReadTriangle? {

    ????
    public ? static ?String?TRIANGLE_FILE? = ? " d:/triangle.txt " ;
    ????
    ????
    public ? static ?String[][]?getTriangle() {
    ????????String[][]?rtn;
    ????????
    try ? {
    ????????????File?f?
    = ? new
    ???????????????????File(ReadTriangle.TRIANGLE_FILE);
    ????????????BufferedReader?br?
    = ? new ?
    ????????????????BufferedReader(
    ???????????????
    new ?FileReader(f)????????
    ????????????);
    ????????????List?l?
    = ? new ?Vector();
    ????????????String?line?
    = ?br.readLine();
    ????????????
    while (line != null ) {
    ????????????????l.add(line.trim());
    ????????????????line?
    = ?br.readLine();
    ????????????}

    ????????????
    int ?heigth? = ?l.size();
    ????????????rtn?
    = ? new ?String[heigth][];
    ????????????
    for ( int ?i? = ? 0 ?;?i? < ?heigth?;?i ++ ) {
    ????????????????String?s?
    = ?(String)l.get(i);
    ????????????????String[]?tk?
    = ?s.split( " ? " );
    ????????????????
    int ?tklen? = ?tk.length;
    ????????????????rtn[i]?
    = ? new ?String[tklen];
    ????????????????
    for ( int ?k? = ? 0 ?;?k? < ?tklen?;?k ++ ) {
    ????????????????????rtn[i][k]?
    = ?tk[k];
    ????????????????}

    ????????????}

    ????????????
    return ?rtn;
    ????????}
    ? catch ?(FileNotFoundException?e)? {
    ????????????e.printStackTrace();
    ????????}
    ? catch ?(IOException?e)? {
    ????????????e.printStackTrace();
    ????????}

    ????????
    return ? null ;
    ????}

    ????
    }


    結果輸出如下:

    Max Path:[(0,0), (1,0), (2,0), (3,1), (4,1), (5,2)]
    Max Value:39


    同樣的方法可以解決很多類似的問題,比如,游戲中的最短路徑;最優(yōu)化投資問題;背包問題等等.

    posted on 2006-04-23 20:47 where the amazing happens 閱讀(1813) 評論(5)  編輯  收藏 所屬分類: 算法&數(shù)據(jù)結構

    評論

    # re: 算法2 : 動態(tài)規(guī)劃 2006-05-09 08:27 T.Sing

    排列三角形 你居然用節(jié)點寫,服了...  回復  更多評論   

    # re: 算法2 : 動態(tài)規(guī)劃 2006-05-10 15:46 鳥不生蛋蛋的地方

    只要思路清晰,代碼自然而然也就寫出來了,不過肯定不是最好的方法:)  回復  更多評論   

    # re: 算法2 : 動態(tài)規(guī)劃 2006-05-21 10:49 123

    、用關系“<”和“=”將3個數(shù)A、B和C依次排列時,有13種不同的序關系:
    A=B=C,A=B<C,A<B=C,A<B<C,A<C<B,A=C<B,B<A=C,
    B<A<C,B<C<A,B=C<A,C<A=B,C<A<B,C<A<B。
    若要將n個數(shù)依序進行排列,試設計一個動態(tài)規(guī)劃算法,計算出有多少鐘不同的序關系?



    求解,謝謝  回復  更多評論   

    # re: 算法2 : 動態(tài)規(guī)劃 2006-05-22 16:40 鳥不生蛋蛋的地方

    樓上的兄弟,最近事情比較多沒法靜下來好好想.如果不急的話請留下email地址.  回復  更多評論   

    # re: 算法2 : 動態(tài)規(guī)劃 2006-07-11 17:45 iris

    可否把那個動態(tài)規(guī)劃排序的程序發(fā)我一份?我看了你5.22的留言,不好意思不知道還在不在?謝謝
    junmei0825@sina.com  回復  更多評論   


    只有注冊用戶登錄后才能發(fā)表評論。


    網(wǎng)站導航:
     

    公告

    點擊這里給我發(fā)消息

    導航

    <2025年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    統(tǒng)計

    常用鏈接

    留言簿(3)

    隨筆分類(18)

    隨筆檔案(17)

    文章分類

    相冊

    其他我的blog

    技術Blog

    最新隨筆

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 日产久久强奸免费的看| 亚洲香蕉在线观看| a级毛片免费高清视频| 亚洲AV无码乱码在线观看性色扶| 亚洲Av高清一区二区三区| 噼里啪啦免费观看高清动漫4| 亚洲国产一区二区a毛片| 一级毛片在线观看免费| 久久久无码精品亚洲日韩蜜臀浪潮 | 亚洲精品无码成人片在线观看 | 国产精品亚洲精品久久精品| 成人性生交大片免费看午夜a| 亚洲精品无码成人片久久不卡| 日韩精品无码人妻免费视频| 久久久久亚洲国产AV麻豆| 全部免费国产潢色一级| 国产成人自产拍免费视频| 亚洲国产精品无码久久一线| 99re免费99re在线视频手机版| 亚洲人成高清在线播放| 成人黄18免费视频| 老司机午夜精品视频在线观看免费 | 国产va在线观看免费| 亚洲欧洲第一a在线观看| 和日本免费不卡在线v| 国产精品亚洲专区无码不卡| 亚洲一区二区三区自拍公司| 久久99青青精品免费观看| 最新国产成人亚洲精品影院| 国产成人免费福利网站| 中国内地毛片免费高清| 亚洲成无码人在线观看| 四虎影视永久免费观看地址| 最近免费字幕中文大全| 亚洲情A成黄在线观看动漫软件| 免费h成人黄漫画嘿咻破解版| 免费成人高清在线视频| 亚洲欧美第一成人网站7777| 亚洲午夜久久久影院伊人 | 日本二区免费一片黄2019| 巨胸喷奶水www永久免费|