diff --git a/Cut Rod Problem/Cut Rod Problem.sln b/Cut Rod Problem/Cut Rod Problem.sln new file mode 100644 index 0000000..a947ecb --- /dev/null +++ b/Cut Rod Problem/Cut Rod Problem.sln @@ -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 diff --git a/Cut Rod Problem/Cut Rod Problem.v12.suo b/Cut Rod Problem/Cut Rod Problem.v12.suo new file mode 100644 index 0000000..4406c63 Binary files /dev/null and b/Cut Rod Problem/Cut Rod Problem.v12.suo differ diff --git a/Cut Rod Problem/Cut Rod Problem/App.config b/Cut Rod Problem/Cut Rod Problem/App.config new file mode 100644 index 0000000..8e15646 --- /dev/null +++ b/Cut Rod Problem/Cut Rod Problem/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Cut Rod Problem/Cut Rod Problem/Cut Rod Problem.csproj b/Cut Rod Problem/Cut Rod Problem/Cut Rod Problem.csproj new file mode 100644 index 0000000..a4f9446 --- /dev/null +++ b/Cut Rod Problem/Cut Rod Problem/Cut Rod Problem.csproj @@ -0,0 +1,61 @@ + + + + + Debug + AnyCPU + {F44B02BE-9C69-4B57-9AD8-58311CCA42B8} + Exe + Properties + Cut_Rod_Problem + Cut Rod Problem + v4.5 + 512 + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Cut Rod Problem/Cut Rod Problem/Data.txt b/Cut Rod Problem/Cut Rod Problem/Data.txt new file mode 100644 index 0000000..9d3cc64 --- /dev/null +++ b/Cut Rod Problem/Cut Rod Problem/Data.txt @@ -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 \ No newline at end of file diff --git a/Cut Rod Problem/Cut Rod Problem/Program.cs b/Cut Rod Problem/Cut Rod Problem/Program.cs new file mode 100644 index 0000000..8d2fc02 --- /dev/null +++ b/Cut Rod Problem/Cut Rod Problem/Program.cs @@ -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); + } + } +} diff --git a/Cut Rod Problem/Cut Rod Problem/Properties/AssemblyInfo.cs b/Cut Rod Problem/Cut Rod Problem/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..572a0d0 --- /dev/null +++ b/Cut Rod Problem/Cut Rod Problem/Properties/AssemblyInfo.cs @@ -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")] diff --git a/Cut Rod Problem/Cut Rod Problem/bin/Debug/Cut Rod Problem.exe b/Cut Rod Problem/Cut Rod Problem/bin/Debug/Cut Rod Problem.exe new file mode 100644 index 0000000..37e4899 Binary files /dev/null and b/Cut Rod Problem/Cut Rod Problem/bin/Debug/Cut Rod Problem.exe differ diff --git a/Cut Rod Problem/Cut Rod Problem/bin/Debug/Cut Rod Problem.exe.config b/Cut Rod Problem/Cut Rod Problem/bin/Debug/Cut Rod Problem.exe.config new file mode 100644 index 0000000..8e15646 --- /dev/null +++ b/Cut Rod Problem/Cut Rod Problem/bin/Debug/Cut Rod Problem.exe.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Cut Rod Problem/Cut Rod Problem/bin/Debug/Cut Rod Problem.pdb b/Cut Rod Problem/Cut Rod Problem/bin/Debug/Cut Rod Problem.pdb new file mode 100644 index 0000000..40f87c7 Binary files /dev/null and b/Cut Rod Problem/Cut Rod Problem/bin/Debug/Cut Rod Problem.pdb differ diff --git a/Cut Rod Problem/Cut Rod Problem/bin/Debug/Cut Rod Problem.vshost.exe b/Cut Rod Problem/Cut Rod Problem/bin/Debug/Cut Rod Problem.vshost.exe new file mode 100644 index 0000000..666c0af Binary files /dev/null and b/Cut Rod Problem/Cut Rod Problem/bin/Debug/Cut Rod Problem.vshost.exe differ diff --git a/Cut Rod Problem/Cut Rod Problem/bin/Debug/Cut Rod Problem.vshost.exe.config b/Cut Rod Problem/Cut Rod Problem/bin/Debug/Cut Rod Problem.vshost.exe.config new file mode 100644 index 0000000..8e15646 --- /dev/null +++ b/Cut Rod Problem/Cut Rod Problem/bin/Debug/Cut Rod Problem.vshost.exe.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Cut Rod Problem/Cut Rod Problem/bin/Debug/Cut Rod Problem.vshost.exe.manifest b/Cut Rod Problem/Cut Rod Problem/bin/Debug/Cut Rod Problem.vshost.exe.manifest new file mode 100644 index 0000000..061c9ca --- /dev/null +++ b/Cut Rod Problem/Cut Rod Problem/bin/Debug/Cut Rod Problem.vshost.exe.manifest @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/Cut Rod Problem/Cut Rod Problem/bin/Debug/Data.txt b/Cut Rod Problem/Cut Rod Problem/bin/Debug/Data.txt new file mode 100644 index 0000000..84870ff --- /dev/null +++ b/Cut Rod Problem/Cut Rod Problem/bin/Debug/Data.txt @@ -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 \ No newline at end of file diff --git a/Cut Rod Problem/Cut Rod Problem/obj/Debug/Cut Rod Problem.csproj.FileListAbsolute.txt b/Cut Rod Problem/Cut Rod Problem/obj/Debug/Cut Rod Problem.csproj.FileListAbsolute.txt new file mode 100644 index 0000000..ef1b962 --- /dev/null +++ b/Cut Rod Problem/Cut Rod Problem/obj/Debug/Cut Rod Problem.csproj.FileListAbsolute.txt @@ -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 diff --git a/Cut Rod Problem/Cut Rod Problem/obj/Debug/Cut Rod Problem.csprojResolveAssemblyReference.cache b/Cut Rod Problem/Cut Rod Problem/obj/Debug/Cut Rod Problem.csprojResolveAssemblyReference.cache new file mode 100644 index 0000000..43473c7 Binary files /dev/null and b/Cut Rod Problem/Cut Rod Problem/obj/Debug/Cut Rod Problem.csprojResolveAssemblyReference.cache differ diff --git a/Cut Rod Problem/Cut Rod Problem/obj/Debug/Cut Rod Problem.exe b/Cut Rod Problem/Cut Rod Problem/obj/Debug/Cut Rod Problem.exe new file mode 100644 index 0000000..37e4899 Binary files /dev/null and b/Cut Rod Problem/Cut Rod Problem/obj/Debug/Cut Rod Problem.exe differ diff --git a/Cut Rod Problem/Cut Rod Problem/obj/Debug/Cut Rod Problem.pdb b/Cut Rod Problem/Cut Rod Problem/obj/Debug/Cut Rod Problem.pdb new file mode 100644 index 0000000..40f87c7 Binary files /dev/null and b/Cut Rod Problem/Cut Rod Problem/obj/Debug/Cut Rod Problem.pdb differ diff --git a/Cut Rod Problem/Cut Rod Problem/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache b/Cut Rod Problem/Cut Rod Problem/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache new file mode 100644 index 0000000..a9bd781 Binary files /dev/null and b/Cut Rod Problem/Cut Rod Problem/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache differ diff --git a/Cut Rod Problem/Cut Rod Problem/obj/Debug/TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs b/Cut Rod Problem/Cut Rod Problem/obj/Debug/TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs new file mode 100644 index 0000000..e69de29 diff --git a/Cut Rod Problem/Cut Rod Problem/obj/Debug/TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs b/Cut Rod Problem/Cut Rod Problem/obj/Debug/TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs new file mode 100644 index 0000000..e69de29 diff --git a/Cut Rod Problem/Cut Rod Problem/obj/Debug/TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs b/Cut Rod Problem/Cut Rod Problem/obj/Debug/TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs new file mode 100644 index 0000000..e69de29