• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >ruby > Ruby实现的矩阵连乘算法

Ruby实现的矩阵连乘算法

作者:junjie 字体:[增加 减小] 来源:互联网

junjie 通过本文主要向大家介绍了矩阵连乘算法,矩阵连乘算法java,矩阵连乘算法代码,矩阵连乘算法c,矩阵连乘递归算法等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

动态规划解决矩阵连乘问题,随机产生矩阵序列,输出形如((A1(A2A3))(A4A5))的结果。

代码:

#encoding: utf-8
=begin
author: xu jin, 4100213
date: Oct 28, 2012
MatrixChain
to find an optimum order by using MatrixChain algorithm
example output:
The given array is:[30, 35, 15, 5, 10, 20, 25]
The optimum order is:((A1(A2A3))((A4A5)A6))
The total number of multiplications is: 15125

The random array is:[5, 8, 8, 2, 5, 9]
The optimum order is:((A1(A2A3))(A4A5))
The total number of multiplications is: 388 
=end

INFINTIY = 1 / 0.0
p = [30, 35, 15, 5, 10, 20, 25]
m, s = Array.new(p.size){Array.new(p.size)}, Array.new(p.size){Array.new(p.size)}

def matrix_chain_order(p, m, s)
   n = p.size - 1
   (1..n).each{|i| m[i][i] = 0} 
   for r in (2..n) do
     for i in (1..n - r + 1) do
       j = r + i - 1
       m[i][j] = INFINTIY
       for k in (i...j) do
         q = m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j]                  
         m[i][j], s[i][j] = q, k if(q < m[i][j]) 
       end
     end
   end
end 

def print_optimal_parens(s, i, j)
   if(i == j) then
    print "A" + i.to_s
   else 
    print "("
    print_optimal_parens(s, i, s[i][j])
    print_optimal_parens(s, s[i][j] + 1, j)
    print ")"
   end
end

def process(p, m, s)
   matrix_chain_order(p, m, s)
   print "The optimum order is:"
   print_optimal_parens(s, 1, p.size - 1)
   printf("\nThe total number of multiplications is: %d\n\n", m[1][p.size - 1])
end

puts "The given array is:" + p.to_s
process(p, m, s)

#produce a random array
p = Array.new
x = rand(10)
(0..x).each{|index| p[index] = rand(10) + 1}
puts "The random array is:" + p.to_s
m, s = Array.new(p.size){Array.new(p.size)}, Array.new(p.size){Array.new(p.size)}
process(p, m, s)
</div>


</div>

您可能想查找下面的文章:

  • Ruby实现的矩阵连乘算法

相关文章

  • Rails脚手架使用实例
  • 使用C++来编写Ruby程序扩展的教程
  • Ruby迭代器的7种技巧分享
  • 浅析Ruby的源代码布局及其编程风格
  • Ruby连接使用windows下sql server数据库代码实例
  • Ruby定义私有方法(private)的两种办法
  • Ruby 多线程的潜力和弱点分析
  • Ruby中访问SQL Server数据库的配置实例
  • 详解Ruby中的代码块及其参数传递
  • ruby和pig处理流式文件实例

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • 详细解析Ruby中的变量
    • ruby will_paginate的用法
    • ruby 小脚本搞定CVS服务器更换后checkout下来的工程迁移
    • Ruby设计模式编程中使用Builder建造者模式的实例
    • Ruby在cmd下中文显示乱码以及不支持OpenSSL的问题解决
    • ruby 一些简单的例子
    • CentOS 7下配置Ruby语言开发环境的方法教程
    • Ruby中的p和puts的使用区别浅析
    • ruby中的双等号==问题详解
    • 利用Ruby的SOAP4R编写SOAP服务器的教程

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有