Cut Rod Problem

This commit is contained in:
Muhammad Anas Rashid 2016-03-18 21:20:46 +05:00
parent 9ce44d5faa
commit f88d8c392a
22 changed files with 253 additions and 0 deletions

View File

@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.40629.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cut Rod Problem", "Cut Rod Problem\Cut Rod Problem.csproj", "{F44B02BE-9C69-4B57-9AD8-58311CCA42B8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{F44B02BE-9C69-4B57-9AD8-58311CCA42B8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F44B02BE-9C69-4B57-9AD8-58311CCA42B8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F44B02BE-9C69-4B57-9AD8-58311CCA42B8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F44B02BE-9C69-4B57-9AD8-58311CCA42B8}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal

Binary file not shown.

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>

View File

@ -0,0 +1,61 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{F44B02BE-9C69-4B57-9AD8-58311CCA42B8}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Cut_Rod_Problem</RootNamespace>
<AssemblyName>Cut Rod Problem</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<Content Include="Data.txt" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

View File

@ -0,0 +1 @@
1 5 8 9 10 17 17 20 24 30 33 31 31 31 40 39 20 45 42 46 70 76 77 80 85 90 95 100 110 120 111 200 678

View File

@ -0,0 +1,97 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
namespace Cut_Rod_Problem
{
class Program
{
private int simpleCutRod(int [] p, int n)
{
if (n == 0)
return 0;
int q = int.MinValue;
for (int i = 1; i <= n; i++)
q = Math.Max(q, p[i] + simpleCutRod(p, n - i));
return q;
}
int memoizedCutRodAux(int [] p, int n, int [] r)
{
if (r[n] >= 0)
return r[n];
int q=0;
if (n == 0)
q = 0;
else
{
q = int.MinValue;
for (int i = 1; i <= n; i++)
q = Math.Max(q, p[i] + memoizedCutRodAux(p, n - i, r));
}
r[n] = q;
return q;
}
int memoizedCutRod(int [] p, int n)
{
int[] r = new int[n + 1];
for (int i = 0; i <= n; i++)
r[i] = int.MinValue;
return memoizedCutRodAux(p, n, r);
}
int bottomUpCutRod(int [] p, int n)
{
int[] r = new int[n + 1];
for (int j = 1; j <= n; j++)
{
int q = int.MinValue;
for (int i = 1; i <= j; i++)
q = Math.Max(q, p[i] + r[j - i]);
r[j] = q;
}
return r[n];
}
static void Main(string[] args)
{
int n = 0;
Console.WriteLine("Enter number of Inches : ");
n = int.Parse(Console.ReadLine());
string fileContent = File.ReadAllText("Data.txt");
string[] priceData = fileContent.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
int[] prices = new int[priceData.Length+1];
for (int i = 1; i < prices.Length; i++)
prices[i] = int.Parse(priceData[i-1]);
Stopwatch t = new Stopwatch();
t.Start();
Cut_Rod_Problem.Program ctp = new Program();
int bestPrice = ctp.memoizedCutRod(prices, n);
//int bestPrice = ctp.bottomUpCutRod(prices, n);
//int bestPrice = ctp.simpleCutRod(prices, n);
t.Stop();
Console.WriteLine("Best Revenue is upto : " + bestPrice);
Console.WriteLine("Time Elapsed : " + t.Elapsed);
}
}
}

View File

@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Cut Rod Problem")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Cut Rod Problem")]
[assembly: AssemblyCopyright("Copyright © 2016")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("a1eeff45-cbb5-4fca-8fc6-2a2094e1dd50")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<requestedExecutionLevel level="asInvoker" uiAccess="false"/>
</requestedPrivileges>
</security>
</trustInfo>
</assembly>

View File

@ -0,0 +1 @@
1 5 8 9 10 17 17 20 24 30 33 31 31 31 40 39 20 45 42 46 70 76 77 80 85 90 95 100 110 120 111 200 678 500 441 365 398 789 551 567 589 590 490 790 795 656 890 1111 1224

View File

@ -0,0 +1,6 @@
c:\users\muham\documents\visual studio 2013\Projects\Cut Rod Problem\Cut Rod Problem\bin\Debug\Cut Rod Problem.exe.config
c:\users\muham\documents\visual studio 2013\Projects\Cut Rod Problem\Cut Rod Problem\bin\Debug\Cut Rod Problem.exe
c:\users\muham\documents\visual studio 2013\Projects\Cut Rod Problem\Cut Rod Problem\bin\Debug\Cut Rod Problem.pdb
c:\users\muham\documents\visual studio 2013\Projects\Cut Rod Problem\Cut Rod Problem\obj\Debug\Cut Rod Problem.csprojResolveAssemblyReference.cache
c:\users\muham\documents\visual studio 2013\Projects\Cut Rod Problem\Cut Rod Problem\obj\Debug\Cut Rod Problem.exe
c:\users\muham\documents\visual studio 2013\Projects\Cut Rod Problem\Cut Rod Problem\obj\Debug\Cut Rod Problem.pdb