1
0

Compare commits

...

11 Commits

9 changed files with 262 additions and 52 deletions

View File

@ -6,67 +6,60 @@ namespace Lab1
{
class Program
{
// O(n^5)
public static double T1(int[] n, int size)
public static int T1(int[] n, int size)
{
if (size >= 1)
if (size < 1)
return 0;
int sum = 0;
for (int i = 0; i < Math.Pow(size, 5); i++)
{
int sum = 0;
for (int i = 0; i < Math.Pow(size, 5); i++)
{
sum += i;
}
return 2 * T1(n, size / 9) + sum;
sum++;
}
return 0;
return T1(n, size / 9) + T1(n, size / 9) + sum;
}
public static double T1(int[] n)
public static int T1(int[] n)
{
return T1(n, n.Length);
}
// O(n)
public static double T2(int[] n, int size)
public static int T2(int[] n, int size)
{
if (size >= 1)
if (size < 1)
return 0;
int sum = 0;
for (int i = 0; i < size; i++)
{
int sum = 0;
for (int i = 0; i < size; i++)
{
sum += i;
}
return T2(n, size / 6) + T2(n, size / 7) + sum;
sum++;
}
return 0;
return T2(n, size / 6) + T2(n, size / 7) + sum;
}
public static double T2(int[] n)
public static int T2(int[] n)
{
return T2(n, n.Length);
}
// O(n^2)
public static double T3(int[] n, int size)
public static int T3(int[] n, int size)
{
if (size >= 1)
if (size < 1)
return 0;
int sum = 0;
for (int i = 0; i < size; i++)
{
int sum = 0;
for (int i = 0; i < size; i++)
{
sum += i;
}
return T3(n, size - 8) + T3(n, size - 6) + sum;
sum++;
}
return 0;
return T3(n, size - 8) + T3(n, size - 6) + sum;
}
public static double T3(int[] n)
public static int T3(int[] n)
{
return T3(n, n.Length);
}
// O(log4(n))
public static void TrianglesRecursive(BMPImage image, int x, int y, uint width, uint height)
{
if (width <= 3 || height <= 3) return;
@ -94,13 +87,17 @@ namespace Lab1
public static void Triangles()
{
BMPImage image = new BMPImage(1000, 1000);
uint size = 40_000;
BMPImage image = new BMPImage(size, size);
image.Fill(new Color(255, 255, 255));
var before = DateTime.Now;
TrianglesRecursive(image, 0, 0, image.width, image.height);
var duration = DateTime.Now - before;
Console.WriteLine("Triangles render duration: {0}", duration.TotalSeconds);
image.Write("result.bmp");
}
public static double TestFunc(Func<int[], double> T, uint n)
public static double TestFunc(Func<int[], int> T, uint n)
{
int[] data = new int[n];
for (int i = 0; i < n; i++)
@ -116,23 +113,34 @@ namespace Lab1
static void Main()
{
// T1(10) => 0.0056552
// T1(20) => 0.0699722
// T1(40) => 2.1500858
// T1(50) => 6.4564238
/*
uint[] N1 = { 10, 15, 20, 25, 30, 35, 40 };
Console.WriteLine("T1, s");
foreach (uint n in N1)
{
Console.WriteLine("{0}, {1}", n, TestFunc(T1, n));
}
*/
// T2(100) => 0.0039419
// T2(1_000_000) => 0.0048064
// T2(250_000_000) => 0.3011757
// T2(500_000_000) => 0.6046134
// T2(1_000_000_000) => 1.1902643
/*
uint[] N2 = { 100, 1_000_000, 250_000_000, 500_000_000, 1_000_000_000, 1_500_000_000 };
Console.WriteLine("T2, s");
foreach (uint n in N2)
{
Console.WriteLine("{0}, {1}", n, TestFunc(T2, n));
}
*/
// T3(100) => 0.0041945
// T3(150) => 0.0759872
// T3(180) => 1.4428123
// T3(190) => 3.9067703
/*
uint[] N3 = { 100, 150, 160, 170, 180, 185 };
Console.WriteLine("T3, s");
foreach (uint n in N3)
{
Console.WriteLine("{0}, {1}", n, TestFunc(T3, n));
}
*/
//Triangles();
Triangles();
}
}
}

View File

@ -19,8 +19,8 @@ Kiekvienai rekurentinei lygčiai (gautai atlikus užduoties pasirinkimo testą):
ar apskaičiuotas metodo asimptotinis sudėtingumas atitinka eksperimentinius rezultatus (1 balas).
1. $T(n)=2 T(\frac{n}{9})+n^5$
2.
3.
2. $T(n)=T(\frac{n}{6}) + T(\frac{n}{7}) + n$
3. $T(n)=T(n-8) + T(n-6) + n$
## 2 užduoties dalis
Naudojant rekursiją ir nenaudojant grafinių bibliotekų sudaryti nurodytos struktūros

Binary file not shown.

BIN
Lab1/Test Results.xlsx Normal file

Binary file not shown.

8
Lab2/Lab2.csproj Normal file
View File

@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
</Project>

185
Lab2/Program.cs Normal file
View File

@ -0,0 +1,185 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography;
namespace Lab2
{
internal class Program
{
public static uint counter = 0;
public static long methodToAnalysis1(int[] arr)
{
long n = arr.Length;
long k = n;
for (int i = 0; i < n; i++)
{
counter += 1;
if (arr[i] > 0)
{
for (int j = 0; j < n * n / 2; j++)
{
k -= 2;
counter += 1;
}
for (int j = 0; j < n * n / 2; j++)
{
k += 3;
counter += 1;
}
}
}
return k;
}
public static long methodToAnalysis2(int n, int[] arr)
{
long k = 0;
for (int i = 0; i < n; i++)
{
counter += 1;
k += arr[i] + FF4(i, arr);
}
return k;
}
public static long methodToAnalysis2(int[] arr)
{
return methodToAnalysis2(arr.Length, arr);
}
public static long FF4(int n, int[] arr)
{
if (n > 0 && arr.Length > n && arr[n] < 0)
{
counter += 1;
return FF4(n / 2, arr) + FF4(n / 3, arr);
}
return n;
}
public static double TimeFunc(Action Func)
{
var before = DateTime.Now;
Func();
var duration = DateTime.Now - before;
return duration.TotalSeconds;
}
public static double TestFunc<R>(Func<int[], R> T, uint n, int value)
{
int[] data = new int[n];
for (int i = 0; i < n; i++)
{
data[i] = value;
}
counter = 0;
return TimeFunc(() => T(data));
}
static void TestMethod(Func<int[], long> F, string format, int bestValue, uint[] best, int worstValue, uint[] worst)
{
Console.WriteLine("- Best:");
foreach (var n in best)
{
double time = TestFunc(F, n, bestValue);
Console.WriteLine(format, n, time, counter);
}
Console.WriteLine("- Worst:");
foreach (var n in worst)
{
double time = TestFunc(F, n, worstValue);
Console.WriteLine(format, n, time, counter);
}
}
static uint Recursive(Span<uint> board)
{
if (board.Length <= 1)
return 0;
else if (board.Length == 2)
return board[1];
uint score1 = board[1] + Recursive(board[1..]);
uint score2 = 2 * (uint)Math.Abs((int)board[0] - (int)board[2]) + Recursive(board[2..]);
return Math.Max(score1, score2);
}
static uint DP(uint[] board)
{
uint[] cache = new uint[board.Length];
cache[0] = 0;
cache[1] = board[1];
for (int i = 2; i < board.Length; i++)
{
uint score1 = board[i] + cache[i - 1];
uint score2 = 2 * (uint)Math.Abs((int)board[i] - (int)board[i - 2]) + cache[i - 2];
cache[i] = Math.Max(score1, score2);
}
return cache[board.Length - 1];
}
static void Main(string[] args)
{
string format = " Recursive({0}) => {1}s";
foreach (var size in new uint[] { 10, 30, 35, 37 })
{
uint[] board = new uint[size];
for (uint i = 0; i < size; i++)
{
board[i] = i;
}
double time = TimeFunc(() => Recursive(board));
Console.WriteLine(format, size, time, counter);
}
format = " DP({0}) => {1}s";
foreach (var size in new uint[] { 1000, 10000000, 30000000, 60000000, 180000000 })
{
uint[] board = new uint[size];
for (uint i = 0; i < size; i++)
{
board[i] = i;
}
double time = TimeFunc(() => DP(board));
Console.WriteLine(format, size, time, counter);
}
/*
//string format = "{1} {2}";
Console.WriteLine("methodToAnalysis1:");
TestMethod(
methodToAnalysis1,
format,
-1, new uint[] { 10000, 100000000, 300000000, 600000000, 900000000 },
1, new uint[] { 500, 600, 700, 800, 1000 }
);
Console.WriteLine("methodToAnalysis2:");
TestMethod(
methodToAnalysis2,
format,
1, new uint[] { 10000, 100000000, 300000000, 600000000, 900000000 },
-1, new uint[] { 10000, 20000, 30000, 50000, 100000 }
);
*/
}
}
}

BIN
Lab2/analysis.ods Normal file

Binary file not shown.

View File

@ -5,6 +5,8 @@ VisualStudioVersion = 17.1.32328.378
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Lab1", "Lab1\Lab1.csproj", "{497767A1-722A-4F48-86E5-C2AE6E0B9C9A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lab2", "Lab2\Lab2.csproj", "{6AF5D775-08B9-4D4F-9E2E-116C551D0581}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -15,6 +17,10 @@ Global
{497767A1-722A-4F48-86E5-C2AE6E0B9C9A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{497767A1-722A-4F48-86E5-C2AE6E0B9C9A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{497767A1-722A-4F48-86E5-C2AE6E0B9C9A}.Release|Any CPU.Build.0 = Release|Any CPU
{6AF5D775-08B9-4D4F-9E2E-116C551D0581}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6AF5D775-08B9-4D4F-9E2E-116C551D0581}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6AF5D775-08B9-4D4F-9E2E-116C551D0581}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6AF5D775-08B9-4D4F-9E2E-116C551D0581}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

3
README.md Normal file
View File

@ -0,0 +1,3 @@
# Algoritmų laborai
:)