Skip to content
Open

h8 #22

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions HomeWork.sln
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "LineralAlgebra", "src\Liner
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "LineralAlgebraT", "Tests\LineralAlgebraT\LineralAlgebraT.fsproj", "{C42A6017-8A61-4469-8FCD-EEBF49198D80}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Graphs", "src\Graphs\Graphs.fsproj", "{F91F37CD-D4A5-486D-AC25-8738BEFF89EE}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "GraphsT", "Tests\GraphsT\GraphsT.fsproj", "{57E88C7C-9510-49DA-9A33-8D19CBAC6D40}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Benchmarks", "Benchmarks", "{1BC2D310-CEB6-4FDE-9A59-D2D02DDA80E9}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "SortingsB", "Benchmarks\SortingsB\SortingsB.fsproj", "{B9D2A2D8-E4CD-40A4-B96F-211CA90A8865}"
Expand Down Expand Up @@ -80,6 +84,14 @@ Global
{C42A6017-8A61-4469-8FCD-EEBF49198D80}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C42A6017-8A61-4469-8FCD-EEBF49198D80}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C42A6017-8A61-4469-8FCD-EEBF49198D80}.Release|Any CPU.Build.0 = Release|Any CPU
{F91F37CD-D4A5-486D-AC25-8738BEFF89EE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F91F37CD-D4A5-486D-AC25-8738BEFF89EE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F91F37CD-D4A5-486D-AC25-8738BEFF89EE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F91F37CD-D4A5-486D-AC25-8738BEFF89EE}.Release|Any CPU.Build.0 = Release|Any CPU
{57E88C7C-9510-49DA-9A33-8D19CBAC6D40}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{57E88C7C-9510-49DA-9A33-8D19CBAC6D40}.Debug|Any CPU.Build.0 = Debug|Any CPU
{57E88C7C-9510-49DA-9A33-8D19CBAC6D40}.Release|Any CPU.ActiveCfg = Release|Any CPU
{57E88C7C-9510-49DA-9A33-8D19CBAC6D40}.Release|Any CPU.Build.0 = Release|Any CPU
{B9D2A2D8-E4CD-40A4-B96F-211CA90A8865}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B9D2A2D8-E4CD-40A4-B96F-211CA90A8865}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B9D2A2D8-E4CD-40A4-B96F-211CA90A8865}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand All @@ -96,6 +108,8 @@ Global
{7E4032B2-27F7-4691-8474-5C57A97F5721} = {B4AC50D2-34AD-46DF-B39E-FBCC5D0653C6}
{C05FDC93-8EE3-442C-868A-031CE2A5F4E4} = {00DFC406-0A95-4C11-8BF2-4FD28C28061D}
{C42A6017-8A61-4469-8FCD-EEBF49198D80} = {B4AC50D2-34AD-46DF-B39E-FBCC5D0653C6}
{F91F37CD-D4A5-486D-AC25-8738BEFF89EE} = {00DFC406-0A95-4C11-8BF2-4FD28C28061D}
{57E88C7C-9510-49DA-9A33-8D19CBAC6D40} = {B4AC50D2-34AD-46DF-B39E-FBCC5D0653C6}
{B9D2A2D8-E4CD-40A4-B96F-211CA90A8865} = {1BC2D310-CEB6-4FDE-9A59-D2D02DDA80E9}
EndGlobalSection
EndGlobal
132 changes: 132 additions & 0 deletions Tests/GraphsT/DataAndFuncs.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
namespace DataAndFuncs

open System
open System.Collections.Generic
open Xunit
open FsCheck
open FsCheck.Xunit
open FsCheck.FSharp
open LineralAlgebra

open Graphs


module Data =
let leaf = Leaf (Some [|1|])
let gLeaf1 = { n = 1; qtree = leaf }
let gLeaf4 = { n = 4; qtree = leaf }

let leafArr = Leaf (Some [|1; 2; 3|])
let gLeafArr1 = { n = 1; qtree = leafArr }
let gLeafArr4 = { n = 4; qtree = leafArr }

let node = Node (
Node ( Leaf None, Leaf (Some [|1|]), Leaf (Some [|1; 2|]), Leaf (Some [|3|])),
Node ( Leaf (Some [|4; 5|]), Leaf (Some [|6|]), Leaf (Some [|7; 8|]), Leaf (Some [|9|])),
Leaf (Some [|10|]),
Node ( Leaf (Some [|11|]), Leaf (Some [|12|]), Leaf (Some [|13|]), Leaf (Some [|14|]))
)
let gNode = { n = 4; qtree = node }

let zeroGraph = { n = 4; qtree = Leaf None }
let fullGraph = { n = 4; qtree = Leaf (Some [|1|])}

let node1 = Node (
Leaf None,
Leaf (Some [|'a'|]),
Leaf None,
Leaf None
)
let graph1 = { n = 2; qtree = node1 }
let node2 = Node (
Node ( Leaf None, Leaf (Some [|true|]), Leaf None, Leaf None),
Node ( Leaf None, Leaf None, Leaf (Some [|true|]), Leaf None),
Leaf None,
Node ( Leaf None, Leaf (Some [|true|]), Leaf None, Leaf None)
)
let graph2 = { n = 4; qtree = node2 }


module Generator =
let GraphGen size typeOfMatr =
let LeafGen =
let someOrNone = Random().Next(0, 1)
if someOrNone = 0 then Leaf None
else
let size = Random().Next(1, 10)
let array = Array.zeroCreate size
for i in 0 .. size - 1 do
array.[i] <- Random().Next(-100, 10)

Leaf (Some array)

let rec QTreeGen size typeOfMatr =
if size = 1 then
match typeOfMatr with
| "Leaf" -> LeafGen
| "Node" -> LeafGen
| _ -> failwith "Not Implemented"
else
let nw = QTreeGen (size / 2) typeOfMatr
let ne = QTreeGen (size / 2) typeOfMatr
let sw = QTreeGen (size / 2) typeOfMatr
let se = QTreeGen (size / 2) typeOfMatr
Node (nw, ne, sw, se)

let MatrGen (size: int) =
let qtree = QTreeGen size typeOfMatr
{ n = size; qtree = QTrees.toCorrectQTree qtree }

let res = MatrGen size
res


module Funcs =
let toAdjacencyMatrix (graph: Matrix<Edjes<'t>>) =
let func (value: Edjes<'t>) =
match value with
| Some _ -> 1
| None -> 0
let res = Matrix.map func graph
res

let rec getElOfQTree (qtree: QTree<Edjes<int>>) =
let handleLeaf edjes =
match edjes with
| None ->
Unchecked.defaultof<'t>
| Some array ->
Array.min array

match qtree with
| Leaf edjes -> handleLeaf edjes
| Node(_, _, _, _) -> failwith "Not Implemented"

let rec findValue (qtree: QTree<'t>) (size: int) i j : 't =
match qtree with
| Leaf value -> value
| Node(nw, ne, sw, se) ->
if i < size / 2 && j < size / 2 then
findValue nw (size / 2) i j
elif i < size / 2 && j >= size / 2 then
findValue ne (size / 2) i (j - size / 2)
elif i >= size / 2 && j < size / 2 then
findValue sw (size / 2) (i - size / 2) j
else
findValue se (size / 2) (i - size / 2) (j - size / 2)

let transitiveClosureMatrices (matrix: int array2d) =
if matrix = null then
Array2D.create 0 0 0
else
let rows = Array2D.length1 matrix
let res = Array2D.copy matrix

for k in 0 .. rows - 1 do
for i in 0 .. rows - 1 do
for j in 0 .. rows - 1 do
if res.[i,k] = 1 && res.[k,j] = 1 then
res.[i,j] <- 1
else
res.[i,j] <- max res.[i,j] res.[i,k] * res.[k,j]
res
32 changes: 32 additions & 0 deletions Tests/GraphsT/GraphsT.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>

<IsPackable>false</IsPackable>
<GenerateProgramFile>false</GenerateProgramFile>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<Compile Include="DataAndFuncs.fs" />
<Compile Include="PropertyGraphs.fs" />
<Compile Include="UnitGraphs.fs" />
<Compile Include="Program.fs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="fscheck" Version="3.3.0" />
<PackageReference Include="fscheck.xunit" Version="3.3.0" />
<PackageReference Include="LineralAlgebra" Version="1.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.5.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Graphs\Graphs.fsproj" />
</ItemGroup>

</Project>
1 change: 1 addition & 0 deletions Tests/GraphsT/Program.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module Program = let [<EntryPoint>] main _ = 0
70 changes: 70 additions & 0 deletions Tests/GraphsT/PropertyGraphs.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
namespace PropertyGraphs

open System
open Xunit
open FsCheck
open FsCheck.Xunit
open FsCheck.FSharp
open LineralAlgebra

open Graphs
open Graphs.Graphs
open DataAndFuncs.Generator
open DataAndFuncs.Funcs


[<Properties(MaxTest = 100)>]
type ShortestWay() =

let power = Random().Next(1, 3)
let size = pown 2 power

[<Property>]
member _.leaf1 () =
let graph = GraphGen 1 "Leaf"
let res = shortestWay graph 0 0 Array.min
let ex = getElOfQTree graph.qtree
Assert.Equal(ex, res)

[<Property>]
member _.leafSize () =
let graph = GraphGen size "Leaf"
let res = shortestWay graph 0 1 Array.min
let ex = getElOfQTree graph.qtree
Assert.Equal(ex, res)

[<Property>]
member _.node () =
let graph = GraphGen size "Node"
let res = Array2D.zeroCreate<int> size size
for i in 0 .. size - 1 do
for j in 0 .. size - 1 do
res.[i, j] <- shortestWay graph i j Array.min

let graphArray2D = QTrees.qtreeToArray2D graph.qtree graph.n

let ex = Array2D.zeroCreate size size
for i in 0 .. size - 1 do
for j in 0 .. size - 1 do
match graphArray2D.[i, j] with
| Some array -> ex.[i, j] <- Array.min array
| None -> ex.[i, j] <- 0
Assert.Equal(ex, res)


[<Properties(MaxTest = 100)>]
type TransitiveClosure() =
let power = Random().Next(1, 3)
let size = pown 2 power

[<Property>]
member _.trClosure () =
let generatedGraph = GraphGen size "Node"
let generatedGraphTrCl = transitiveClosure generatedGraph
let resArray = QTrees.qtreeToArray2D generatedGraphTrCl.qtree generatedGraph.n

let generatedGraphToAdjM = toAdjacencyMatrix generatedGraph
let adjMto2Darray = QTrees.qtreeToArray2D generatedGraphToAdjM.qtree generatedGraphToAdjM.n

let arrayTrCl = transitiveClosureMatrices adjMto2Darray
Assert.Equal(arrayTrCl, resArray)
101 changes: 101 additions & 0 deletions Tests/GraphsT/UnitGraphs.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
namespace UnitGraphs

open System
open Xunit
open FsCheck
open FsCheck.Xunit
open FsCheck.FSharp
open LineralAlgebra

open Graphs.Graphs
open DataAndFuncs.Data


module ShortestWay =

[<Fact>]
let leaf () =
let res = shortestWay gLeaf1 0 0 Array.min
let ex = 1
Assert.Equal(ex, res)

[<Fact>]
let leaf4 () =
let res = shortestWay gLeaf4 0 1 Array.min
let ex = 1
Assert.Equal(ex, res)

[<Fact>]
let leafArr () =
let res = shortestWay gLeafArr1 0 0 Array.min
let ex = 1
Assert.Equal(ex, res)

[<Fact>]
let leafArr4 () =
let res = shortestWay gLeafArr4 1 0 Array.min
let ex = 1
Assert.Equal(ex, res)

[<Fact>]
let node () =
let res = Array2D.zeroCreate<int> 4 4
for i in 0 .. 3 do
for j in 0 .. 3 do
res.[i, j] <- shortestWay gNode i j Array.min

let arrayArray =
[|
[|0; 1; 4; 6|];
[|1; 3; 7; 9|];
[|10; 10; 11; 12|];
[|10; 10; 13; 14|]
|]

let ex = Array2D.init 4 4 (fun i j ->
if j < arrayArray.[i].Length then
arrayArray.[i].[j]
else
0
)

Assert.Equal(ex, res)


module TransitiveClosure =

[<Fact>]
let zeroGraph () =
let res = transitiveClosure zeroGraph
let ex = { n = 4; qtree = Leaf 0 }
Assert.Equal(ex, res)

[<Fact>]
let fullGraph () =
let res = transitiveClosure fullGraph
let ex = { n = 4; qtree = Leaf 1 }
Assert.Equal(ex, res)

[<Fact>]
let graph2x2 () =
let res = transitiveClosure graph1
let node = Node (
Leaf 0,
Leaf 1,
Leaf 0,
Leaf 0
)
let ex = { n = 2; qtree = node }
Assert.Equal(ex, res)

[<Fact>]
let graph4x4 () =
let res = transitiveClosure graph2
let node = Node (
Node ( Leaf 0, Leaf 1, Leaf 0, Leaf 0),
Leaf 1,
Leaf 0,
Node ( Leaf 0, Leaf 1, Leaf 0, Leaf 0)
)
let ex = { n = 4; qtree = node }
Assert.Equal(ex, res)
Loading