diff --git a/BelyakovaEA/Lab2/build/lab2/Dijkstra/Dijkstra.vcxproj b/BelyakovaEA/Lab2/build/lab2/Dijkstra/Dijkstra.vcxproj new file mode 100644 index 000000000..5e9c45a1b --- /dev/null +++ b/BelyakovaEA/Lab2/build/lab2/Dijkstra/Dijkstra.vcxproj @@ -0,0 +1,160 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + {0A941875-030C-4C0F-98FF-FCDF8EA39AF6} + Win32Proj + Dijkstra + 8.1 + + + + Application + true + v140 + Unicode + + + Application + false + v140 + true + Unicode + + + Application + true + v140 + Unicode + + + Application + false + v140 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + ..\..\..\include;$(IncludePath) + + + true + + + false + ..\..\..\include;$(IncludePath) + + + false + + + + + + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + Graph_lib.lib;%(AdditionalDependencies) + ..\Debug;%(AdditionalLibraryDirectories) + + + + + + + Level3 + Disabled + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + Graph_lib.lib;%(AdditionalDependencies) + ..\Release;%(AdditionalLibraryDirectories) + + + + + Level3 + + + MaxSpeed + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + \ No newline at end of file diff --git a/BelyakovaEA/Lab2/build/lab2/Dijkstra/Dijkstra.vcxproj.filters b/BelyakovaEA/Lab2/build/lab2/Dijkstra/Dijkstra.vcxproj.filters new file mode 100644 index 000000000..37c88ca72 --- /dev/null +++ b/BelyakovaEA/Lab2/build/lab2/Dijkstra/Dijkstra.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Файлы исходного кода + + + \ No newline at end of file diff --git a/BelyakovaEA/Lab2/build/lab2/Djikastra/Djikastra.sln b/BelyakovaEA/Lab2/build/lab2/Djikastra/Djikastra.sln new file mode 100644 index 000000000..c90e54296 --- /dev/null +++ b/BelyakovaEA/Lab2/build/lab2/Djikastra/Djikastra.sln @@ -0,0 +1,67 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 14 +VisualStudioVersion = 14.0.25420.1 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Kruskal", "..\Kruskal\Kruskal.vcxproj", "{A79AF118-E17D-458F-B464-C9E646C93C8D}" + ProjectSection(ProjectDependencies) = postProject + {DACC956A-F0C4-4098-8304-530878DCEADE} = {DACC956A-F0C4-4098-8304-530878DCEADE} + EndProjectSection +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "PySort", "..\PySort\PySort.vcxproj", "{616C8F6D-0779-455D-B628-FC61B0F35C80}" + ProjectSection(ProjectDependencies) = postProject + {DACC956A-F0C4-4098-8304-530878DCEADE} = {DACC956A-F0C4-4098-8304-530878DCEADE} + EndProjectSection +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Graph_lib", "..\Graph_lib\Graph_lib.vcxproj", "{DACC956A-F0C4-4098-8304-530878DCEADE}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Dijkstra", "..\Dijkstra\Dijkstra.vcxproj", "{0A941875-030C-4C0F-98FF-FCDF8EA39AF6}" + ProjectSection(ProjectDependencies) = postProject + {DACC956A-F0C4-4098-8304-530878DCEADE} = {DACC956A-F0C4-4098-8304-530878DCEADE} + EndProjectSection +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {A79AF118-E17D-458F-B464-C9E646C93C8D}.Debug|x64.ActiveCfg = Debug|x64 + {A79AF118-E17D-458F-B464-C9E646C93C8D}.Debug|x64.Build.0 = Debug|x64 + {A79AF118-E17D-458F-B464-C9E646C93C8D}.Debug|x86.ActiveCfg = Debug|Win32 + {A79AF118-E17D-458F-B464-C9E646C93C8D}.Debug|x86.Build.0 = Debug|Win32 + {A79AF118-E17D-458F-B464-C9E646C93C8D}.Release|x64.ActiveCfg = Release|x64 + {A79AF118-E17D-458F-B464-C9E646C93C8D}.Release|x64.Build.0 = Release|x64 + {A79AF118-E17D-458F-B464-C9E646C93C8D}.Release|x86.ActiveCfg = Release|Win32 + {A79AF118-E17D-458F-B464-C9E646C93C8D}.Release|x86.Build.0 = Release|Win32 + {616C8F6D-0779-455D-B628-FC61B0F35C80}.Debug|x64.ActiveCfg = Debug|x64 + {616C8F6D-0779-455D-B628-FC61B0F35C80}.Debug|x64.Build.0 = Debug|x64 + {616C8F6D-0779-455D-B628-FC61B0F35C80}.Debug|x86.ActiveCfg = Debug|Win32 + {616C8F6D-0779-455D-B628-FC61B0F35C80}.Debug|x86.Build.0 = Debug|Win32 + {616C8F6D-0779-455D-B628-FC61B0F35C80}.Release|x64.ActiveCfg = Release|x64 + {616C8F6D-0779-455D-B628-FC61B0F35C80}.Release|x64.Build.0 = Release|x64 + {616C8F6D-0779-455D-B628-FC61B0F35C80}.Release|x86.ActiveCfg = Release|Win32 + {616C8F6D-0779-455D-B628-FC61B0F35C80}.Release|x86.Build.0 = Release|Win32 + {DACC956A-F0C4-4098-8304-530878DCEADE}.Debug|x64.ActiveCfg = Debug|x64 + {DACC956A-F0C4-4098-8304-530878DCEADE}.Debug|x64.Build.0 = Debug|x64 + {DACC956A-F0C4-4098-8304-530878DCEADE}.Debug|x86.ActiveCfg = Debug|Win32 + {DACC956A-F0C4-4098-8304-530878DCEADE}.Debug|x86.Build.0 = Debug|Win32 + {DACC956A-F0C4-4098-8304-530878DCEADE}.Release|x64.ActiveCfg = Release|x64 + {DACC956A-F0C4-4098-8304-530878DCEADE}.Release|x64.Build.0 = Release|x64 + {DACC956A-F0C4-4098-8304-530878DCEADE}.Release|x86.ActiveCfg = Release|Win32 + {DACC956A-F0C4-4098-8304-530878DCEADE}.Release|x86.Build.0 = Release|Win32 + {0A941875-030C-4C0F-98FF-FCDF8EA39AF6}.Debug|x64.ActiveCfg = Debug|x64 + {0A941875-030C-4C0F-98FF-FCDF8EA39AF6}.Debug|x64.Build.0 = Debug|x64 + {0A941875-030C-4C0F-98FF-FCDF8EA39AF6}.Debug|x86.ActiveCfg = Debug|Win32 + {0A941875-030C-4C0F-98FF-FCDF8EA39AF6}.Debug|x86.Build.0 = Debug|Win32 + {0A941875-030C-4C0F-98FF-FCDF8EA39AF6}.Release|x64.ActiveCfg = Release|x64 + {0A941875-030C-4C0F-98FF-FCDF8EA39AF6}.Release|x64.Build.0 = Release|x64 + {0A941875-030C-4C0F-98FF-FCDF8EA39AF6}.Release|x86.ActiveCfg = Release|Win32 + {0A941875-030C-4C0F-98FF-FCDF8EA39AF6}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/BelyakovaEA/Lab2/build/lab2/Graph_lib/Graph_lib.vcxproj b/BelyakovaEA/Lab2/build/lab2/Graph_lib/Graph_lib.vcxproj new file mode 100644 index 000000000..db599c8a1 --- /dev/null +++ b/BelyakovaEA/Lab2/build/lab2/Graph_lib/Graph_lib.vcxproj @@ -0,0 +1,163 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + {DACC956A-F0C4-4098-8304-530878DCEADE} + Win32Proj + Graph_lib + 8.1 + + + + StaticLibrary + true + v140 + Unicode + + + StaticLibrary + false + v140 + true + Unicode + + + StaticLibrary + true + v140 + Unicode + + + StaticLibrary + false + v140 + true + Unicode + + + + + + + + + + + + + + + + + + + + + ..\..\..\include;$(IncludePath) + + + ..\..\..\include;$(IncludePath) + + + + + + Level3 + Disabled + WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) + true + ..\..\..\include + + + Windows + + + + + + + Level3 + Disabled + _DEBUG;_LIB;%(PreprocessorDefinitions) + true + + + Windows + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) + true + ..\..\..\include + + + Windows + true + true + + + + + Level3 + + + MaxSpeed + true + true + NDEBUG;_LIB;%(PreprocessorDefinitions) + true + + + Windows + true + true + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/BelyakovaEA/Lab2/build/lab2/Graph_lib/Graph_lib.vcxproj.filters b/BelyakovaEA/Lab2/build/lab2/Graph_lib/Graph_lib.vcxproj.filters new file mode 100644 index 000000000..7da1fe814 --- /dev/null +++ b/BelyakovaEA/Lab2/build/lab2/Graph_lib/Graph_lib.vcxproj.filters @@ -0,0 +1,63 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + + Файлы исходного кода + + + + + Заголовочные файлы + + + Заголовочные файлы + + + Заголовочные файлы + + + Заголовочные файлы + + + Заголовочные файлы + + + Заголовочные файлы + + + Заголовочные файлы + + + Заголовочные файлы + + + + + Файлы исходного кода + + + Файлы исходного кода + + + Файлы исходного кода + + + Файлы исходного кода + + + \ No newline at end of file diff --git a/BelyakovaEA/Lab2/build/lab2/Kruskal/Kruskal.vcxproj b/BelyakovaEA/Lab2/build/lab2/Kruskal/Kruskal.vcxproj new file mode 100644 index 000000000..05931032a --- /dev/null +++ b/BelyakovaEA/Lab2/build/lab2/Kruskal/Kruskal.vcxproj @@ -0,0 +1,160 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + {A79AF118-E17D-458F-B464-C9E646C93C8D} + Win32Proj + Kruskal + 8.1 + + + + Application + true + v140 + Unicode + + + Application + false + v140 + true + Unicode + + + Application + true + v140 + Unicode + + + Application + false + v140 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + ..\..\..\include;$(IncludePath) + + + true + + + false + ..\..\..\include;$(IncludePath) + + + false + + + + + + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + Graph_lib.lib;%(AdditionalDependencies) + ..\Debug + + + + + + + Level3 + Disabled + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + Graph_lib.lib;%(AdditionalDependencies) + ..\Release + + + + + Level3 + + + MaxSpeed + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + \ No newline at end of file diff --git a/BelyakovaEA/Lab2/build/lab2/Kruskal/Kruskal.vcxproj.filters b/BelyakovaEA/Lab2/build/lab2/Kruskal/Kruskal.vcxproj.filters new file mode 100644 index 000000000..0ec3b67aa --- /dev/null +++ b/BelyakovaEA/Lab2/build/lab2/Kruskal/Kruskal.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Файлы исходного кода + + + \ No newline at end of file diff --git a/BelyakovaEA/Lab2/build/lab2/PySort/PySort.vcxproj b/BelyakovaEA/Lab2/build/lab2/PySort/PySort.vcxproj new file mode 100644 index 000000000..2601f8c87 --- /dev/null +++ b/BelyakovaEA/Lab2/build/lab2/PySort/PySort.vcxproj @@ -0,0 +1,160 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + {616C8F6D-0779-455D-B628-FC61B0F35C80} + Win32Proj + PySort + 8.1 + + + + Application + true + v140 + Unicode + + + Application + false + v140 + true + Unicode + + + Application + true + v140 + Unicode + + + Application + false + v140 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + ..\..\..\include;$(IncludePath) + + + true + + + false + ..\..\..\include;$(IncludePath) + + + false + + + + + + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + Graph_lib.lib;%(AdditionalDependencies) + ..\Debug;%(AdditionalLibraryDirectories) + + + + + + + Level3 + Disabled + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + Graph_lib.lib;%(AdditionalDependencies) + ..\Release;%(AdditionalLibraryDirectories) + + + + + Level3 + + + MaxSpeed + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + \ No newline at end of file diff --git a/BelyakovaEA/Lab2/build/lab2/PySort/PySort.vcxproj.filters b/BelyakovaEA/Lab2/build/lab2/PySort/PySort.vcxproj.filters new file mode 100644 index 000000000..33ac48f08 --- /dev/null +++ b/BelyakovaEA/Lab2/build/lab2/PySort/PySort.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Файлы исходного кода + + + \ No newline at end of file diff --git a/BelyakovaEA/Lab2/doc/Belyakova.doc b/BelyakovaEA/Lab2/doc/Belyakova.doc new file mode 100644 index 000000000..d1ca91b6c Binary files /dev/null and b/BelyakovaEA/Lab2/doc/Belyakova.doc differ diff --git a/BelyakovaEA/Lab2/include/Dijkstra.h b/BelyakovaEA/Lab2/include/Dijkstra.h new file mode 100644 index 000000000..0eb8a20be --- /dev/null +++ b/BelyakovaEA/Lab2/include/Dijkstra.h @@ -0,0 +1,13 @@ +#ifndef DIJKSTRA_H +#define DIJKSTRA_H + +#include "edge.h" +#include "Graph.h" + +class Dijkstra { +public: + static VertexDist* DijkstraSearch(const Graph& graph); +}; + + +#endif diff --git a/BelyakovaEA/Lab2/include/Graph.h b/BelyakovaEA/Lab2/include/Graph.h new file mode 100644 index 000000000..6df1beb9d --- /dev/null +++ b/BelyakovaEA/Lab2/include/Graph.h @@ -0,0 +1,47 @@ +#ifndef GRAPH_H +#define GRAPH_H + +#include "edge.h" +#include +#include +#include +#include +#include + +struct Neighboor // +{ + int vertex; + double edgeWeight; +}; + +typedef std::vector NeighboorVector; + +class Graph +{ +public: + Graph(int nVertex, int nEdges, bool doGenerateEdges = true); + ~Graph() {} + + int GetVertexNum() const; + int GetEdgesNum() const; + double Graph::GetSumOfWeights() const { return sumOfWeights; } + + Edge GetEdge(int idx) const {/* printf("n: %d, k: %d\n", edges[idx].Ne, edges[idx].Ke);*/ return edges[idx]; } + + void AddEdge(int start, int finish, double weight); + + NeighboorVector GetNeigboors(int vertex) const; // + + void Print(); + +private: + double generateWeight(double minRangw = 0.01, double maxRange = 10.0); + void GenerateEdges(int nEdges); + + int nVertex; + std::vector edges; + + double sumOfWeights; +}; + +#endif \ No newline at end of file diff --git a/BelyakovaEA/Lab2/include/Kruskal.h b/BelyakovaEA/Lab2/include/Kruskal.h new file mode 100644 index 000000000..77f6c31bc --- /dev/null +++ b/BelyakovaEA/Lab2/include/Kruskal.h @@ -0,0 +1,63 @@ +#ifndef KRUSKAL_H +#define KRUSKAL_H + +#include "dheap.h" +#include "Graph.h" +#include "SeparatedSet.h" +#include "PriorituQueue.h" + +template +class Algorithms { +public: + static Graph* Kruskal(const Graph& graph, PriorityQueue* queue); +}; + +template +Graph* Algorithms::Kruskal(const Graph& graph, PriorityQueue* queue) +{ + int n = graph.GetVertexNum(); + int m = graph.GetEdgesNum(); + Graph *tree = new Graph(n, m, false); + + SeparatedSet *set = new SeparatedSet(n); + for (int i = 0; i < n; ++i) + { + set->makesets(i); + } + + for (int i = 0; i < m; ++i) + { + queue->Push(T(graph.GetEdge(i))); + } + + int treeEdgeSize = 0; + T tmp; + + while ((treeEdgeSize < n - 1) && (!queue->isEmpty())) + { + tmp = queue->Pop(); + + int u = tmp.vertex; + int v = tmp.upVertex; + double weight = tmp.dist; + + int An = set->findsets(u); + int Ak = set->findsets(v); + + if (An != Ak) + { + set->unionsets(An, Ak); + //printf("After union\n"); + tree->AddEdge(u, v, weight); + //printf("After add\n"); + ++treeEdgeSize; + } + } + + //tree->Print(); + return tree; +} + +template class Algorithms; + +#endif diff --git a/BelyakovaEA/Lab2/include/PriorituQueue.h b/BelyakovaEA/Lab2/include/PriorituQueue.h new file mode 100644 index 000000000..13960b168 --- /dev/null +++ b/BelyakovaEA/Lab2/include/PriorituQueue.h @@ -0,0 +1,40 @@ +#ifndef PRIORITYQUEUE_H +#define PRIORITYQUEUE_H + +#include "dheap.h" + +template class PriorityQueue +{ +public: + virtual void Push(T& t) = 0; + virtual T Pop() = 0; + virtual bool isEmpty() const = 0; +}; + +template class PriorityQueueDHeap : public PriorityQueue +{ +public: + PriorityQueueDHeap(T* keys, int d, int n) + { + dheap = new DHeap(keys, d, n); + } + + virtual void Push(T& t) { dheap->insert(t); } + virtual T Pop() { return dheap->getmin(); } + virtual bool isEmpty() const { return dheap->isEmpty(); } + + + int GetSize() { return dheap->GetSize(); } + T& GetElement(int idx) { return dheap->GetElement(idx); } + void RepairQueue() { dheap->hilling(); } + void SetSize(int new_size) { dheap->SetSize(new_size); } + + virtual ~PriorityQueueDHeap() { delete dheap; } + +private: + DHeap* dheap; +}; + +template class PriorityQueueDHeap; +template class PriorityQueueDHeap; +#endif \ No newline at end of file diff --git a/BelyakovaEA/Lab2/include/PySort.h b/BelyakovaEA/Lab2/include/PySort.h new file mode 100644 index 000000000..9c915cf26 --- /dev/null +++ b/BelyakovaEA/Lab2/include/PySort.h @@ -0,0 +1,41 @@ +#ifndef PYR_SORT_H +#define PYR_SORT_H + +#include "dheap.h" + +/* +template void PyrSort(DHeap* heap) +{ +heap->hilling(); +int size = heap->GetSize(); +while (size > 0) +{ +heap->transpose(0, size - 1); +heap->remove(size - 1); +size--; +heap->sinking(0); +//size--; +} +}*/ + +template void PyrSort(DHeap* heap) +{ + heap->hilling(); + int cur_size = heap->GetSize(); + int old_size = cur_size; + while (cur_size > 0) + { + heap->transpose(0, cur_size - 1); + cur_size--; + heap->SetSize(cur_size); + heap->sinking(0); + } + + heap->SetSize(old_size); +} + +template void PyrSort(DHeap*); + +#endif + + diff --git a/BelyakovaEA/Lab2/include/SeparatedSet.h b/BelyakovaEA/Lab2/include/SeparatedSet.h new file mode 100644 index 000000000..744df285f --- /dev/null +++ b/BelyakovaEA/Lab2/include/SeparatedSet.h @@ -0,0 +1,18 @@ +#ifndef SEPARATEDSET +#define SEPARATEDSET + +class SeparatedSet +{ +public: + SeparatedSet(int size); + ~SeparatedSet() { delete[] harVec; } + + void makesets(int idx); + int findsets(int idx); + void unionsets(int x, int y); + +private: + int* harVec; + int size; +}; +#endif diff --git a/BelyakovaEA/Lab2/include/dheap.h b/BelyakovaEA/Lab2/include/dheap.h new file mode 100644 index 000000000..cf5f45099 --- /dev/null +++ b/BelyakovaEA/Lab2/include/dheap.h @@ -0,0 +1,163 @@ +#ifndef DHEAP_H +#define DHEAP_H + +#include "edge.h" +#include "Dijkstra.h" + + +template class DHeap +{ +private: + int d; + int n; // + T* keys; // + int min(int a, int b); + +public: + DHeap(T* keys, int d, int n); + DHeap(const DHeap &H); // + ~DHeap(); // + + virtual bool isEmpty() const { return n == 0; } + + void transpose(int i, int j); + void surfacing(int i); + void sinking(int i); // + void hilling(); // + void remove(int i); + void insert(T k); + int minchild(int i); + + T getmin(); + + int GetSize() { return n; } + void SetSize(int new_size) { n = new_size; } + T& GetElement(int idx) { return keys[idx]; } +}; + +template +DHeap ::DHeap(T* keys, int d, int n) +{ + this->keys = keys; + this->d = d; + this->n = n; +} + +template +DHeap::DHeap(const DHeap&H) +{ + d = H.d; + n = H.n; + keys = new T[n]; + for (int i = 0; i < n; i++) + { + keys[i] = H.keys[i]; + } +} + +template +DHeap::~DHeap() { + //Do not delete keys as we do not own it; +} + +template +int DHeap::min(int a, int b) +{ + if (a <= b) return a; + else return b; +} + +template +void DHeap ::transpose(int i, int j) { + T tmp = keys[i]; + keys[i] = keys[j]; + keys[j] = tmp; +} + +template +void DHeap::surfacing(int i) { + int p = (i - 1) / d; + while (p > 0) { + if (keys[p] > keys[i]) + { + transpose(p, i); + i = p; + p = (i - 1) / d; + } + + else + { + break; + } + } +} + + +template +void DHeap::sinking(int i) { + int c = minchild(i); + while (c != -1 && keys[c] < keys[i]) { + transpose(c, i); + i = c; + c = minchild(i); + } +} + +template +int DHeap::minchild(int i) { + if (i * d + 1 > n - 1) return -1; + + int minidx = i * d + 1; + int i1 = i * d + 1; + int i2 = min(i * d + d, n - 1); + T minkey = keys[i1]; + for (int i = i1; i <= i2; i++) { + if (keys[minidx] > keys[i]) { + minidx = i; + minkey = keys[i]; + } + } + return minidx; +} + +template +T DHeap ::getmin() { + + T tmp = keys[0]; + transpose(0, n - 1); + n--; + sinking(0); + return tmp; +} + +template +void DHeap::hilling() { + for (int i = n - 1; i >= 0; i--) + sinking(i); +} + +template +void DHeap::remove(int i) { + if (i == n - 1) + { + n--; + return; + } + + keys[i] = keys[n - 1]; + n--; + sinking(i); +} + +template +void DHeap::insert(T k) { + keys[n] = k; + surfacing(n); + ++n; +} + + +template class DHeap; +template class DHeap; + +#endif \ No newline at end of file diff --git a/BelyakovaEA/Lab2/include/edge.h b/BelyakovaEA/Lab2/include/edge.h new file mode 100644 index 000000000..21995341c --- /dev/null +++ b/BelyakovaEA/Lab2/include/edge.h @@ -0,0 +1,47 @@ +#ifndef EDGE_H +#define EDGE_H + +class Edge { +public: + int Ne; // + int Ke; // + double W; // + + Edge(int N, int K, double _W) + { + Ne = N; + Ke = K; + W = _W; + } +}; + + +class VertexDist +{ +public: + VertexDist() : vertex(0), dist(0.0), upVertex(0) {} + VertexDist(const Edge& edge) : vertex(edge.Ne), upVertex(edge.Ke), dist(edge.W) {} + + + int vertex; + + double dist; + int upVertex; + + bool operator<(const VertexDist& vd) + { + return dist < vd.dist; + } + + bool operator>(const VertexDist& vd) + { + return dist > vd.dist; + } + + bool operator==(const VertexDist& vd) + { + return dist == vd.dist; + } +}; + +#endif diff --git a/BelyakovaEA/Lab2/sample/mainDjikstra.cpp b/BelyakovaEA/Lab2/sample/mainDjikstra.cpp new file mode 100644 index 000000000..2a7b89961 --- /dev/null +++ b/BelyakovaEA/Lab2/sample/mainDjikstra.cpp @@ -0,0 +1,48 @@ +#include "dheap.h" +#include "Graph.h" +#include "Dijkstra.h" + +#include + +const int SIZE = 10; + + +int main() +{ + int d = 4; + + int nVertex = 4; + + int nEdges = 5; + + if (nEdges <= 0 || nVertex <= 0) + { + printf("ERROR: Number of edges and verteces must be positive!\n"); + return 1; + } + + Graph graph(nVertex, nEdges); + + printf("Dijkstra\n"); + VertexDist *result = Dijkstra::DijkstraSearch(graph); + + printf("Result:\n"); + for (int i = 0; i < graph.GetVertexNum(); ++i) + { + printf("Vertex: %d, up: %d, ", result[i].vertex, result[i].upVertex); + + if (result[i].dist >= graph.GetSumOfWeights()) + { + printf("UNREACHABLE\n"); + } + + else + { + printf("dist: %f\n", result[i].dist); + } + } + + delete[] result; + + return 0; +} \ No newline at end of file diff --git a/BelyakovaEA/Lab2/sample/mainKruskal.cpp b/BelyakovaEA/Lab2/sample/mainKruskal.cpp new file mode 100644 index 000000000..7bb408939 --- /dev/null +++ b/BelyakovaEA/Lab2/sample/mainKruskal.cpp @@ -0,0 +1,28 @@ +#include "Kruskal.h" +#include +#include "PriorituQueue.h" +#include "Graph.h" +#include "edge.h" +#include "dheap.h" + +int main() { + + int d = 4; + int nVertex = 4; + int nEdges = 5; + if (nEdges <= 0 || nVertex <= 0) + { + printf("ERROR: Number of edges and verteces must be positive!\n"); + return 1; + } + + Graph graph(nVertex, nEdges); + VertexDist* dheapValues = new VertexDist[graph.GetVertexNum()]; + PriorityQueueDHeap* vdHeap = new PriorityQueueDHeap(dheapValues, d, 0); + printf("Kruskal\n"); + Graph* tree = Algorithms::Kruskal(graph, vdHeap); + + printf("Result:\n"); + tree->Print(); + +} \ No newline at end of file diff --git a/BelyakovaEA/Lab2/sample/mainPySort.cpp b/BelyakovaEA/Lab2/sample/mainPySort.cpp new file mode 100644 index 000000000..699587850 --- /dev/null +++ b/BelyakovaEA/Lab2/sample/mainPySort.cpp @@ -0,0 +1,48 @@ +#include "dheap.h" +#include "PySort.h" +#include + +const int SIZE = 10; + +int main() { + + + int d = 4; + int nVertex = 4; + int nEdges = 5; + + if (nEdges <= 0 || nVertex <= 0) + { + printf("ERROR: Number of edges and verteces must be positive!\n"); + return 1; + } + + //printf("Sort\n"); + int *arr = new int[SIZE]; + + for (int i = 0; i < SIZE; ++i) + { + arr[i] = i; + } + + for (int i = 0; i < SIZE; ++i) + { + printf("%d; ", arr[i]); + } + printf("\n"); + + DHeap *heap = new DHeap(arr, d, SIZE); + printf("Sort\n"); + PyrSort(heap); + + for (int i = 0; i < SIZE; ++i) + { + printf("%d; ", arr[i]); + } + + printf("\n"); + + delete heap; + delete[] arr; + return 0; +} \ No newline at end of file diff --git a/BelyakovaEA/Lab2/src/Djikstra.cpp b/BelyakovaEA/Lab2/src/Djikstra.cpp new file mode 100644 index 000000000..c4d5ea27b --- /dev/null +++ b/BelyakovaEA/Lab2/src/Djikstra.cpp @@ -0,0 +1,50 @@ +#include "Dijkstra.h" +#include "dheap.h" +#include "PriorituQueue.h" + +VertexDist* Dijkstra::DijkstraSearch(const Graph& graph /*,TODO: arnost*/) +{ + VertexDist *result = new VertexDist[graph.GetVertexNum()]; + + //Fill values for root + result[0].vertex = 0; + result[0].dist = 0.0; + result[0].upVertex = 0; + + //Fill values for other verteces + //Infinity == (Sum of all weights + 1) + for (int i = 1; i < graph.GetVertexNum(); ++i) + { + result[i].vertex = i; + result[i].dist = graph.GetSumOfWeights(); + result[i].upVertex = i; + } + + PriorityQueueDHeap dheap(result, 4 , graph.GetVertexNum()); + + while (!dheap.isEmpty()) + { + VertexDist vd = dheap.Pop(); + NeighboorVector neighboors = graph.GetNeigboors(vd.vertex); + + for (int i = 0; i < neighboors.size(); ++i) //cocedi + { + for (int j = 0; j < dheap.GetSize(); ++j) //cocedi ostavshiesya v kuche + { + if (dheap.GetElement(j).vertex == neighboors[i].vertex) + { + if (dheap.GetElement(j).dist > vd.dist + neighboors[i].edgeWeight) + { + dheap.GetElement(j).dist = vd.dist + neighboors[i].edgeWeight; + dheap.GetElement(j).upVertex = vd.vertex; + } + } + } + } + + dheap.RepairQueue(); + } + + dheap.SetSize(graph.GetVertexNum()); + return result; +} \ No newline at end of file diff --git a/BelyakovaEA/Lab2/src/Graph.cpp b/BelyakovaEA/Lab2/src/Graph.cpp new file mode 100644 index 000000000..186ac7903 --- /dev/null +++ b/BelyakovaEA/Lab2/src/Graph.cpp @@ -0,0 +1,113 @@ +#include "Graph.h" +#include +using namespace std; + +Graph::Graph(int nVertex, int nEdges, bool doGenerateEdges) +{ + this->nVertex = nVertex; + this->sumOfWeights = 1.0; + edges.reserve(nEdges); + + if (doGenerateEdges) + { + GenerateEdges(nEdges); + } +} + + +int Graph::GetVertexNum() const { return nVertex; } +int Graph::GetEdgesNum() const { return edges.size(); } + + +void Graph::AddEdge(int start, int finish, double weight) +{ + sumOfWeights += weight; + //printf("AddEdge\n"); + edges.push_back(Edge(start, finish, weight)); +} + +double Graph::generateWeight(double minRange, double maxRange) +{ + double d = minRange; + double c = (double)(maxRange - minRange) / RAND_MAX; + double result = c * rand() + d; + return result; +} + +void Graph::Print() +{ + printf("Graph:\n"); + for (int i = 0; i < edges.size(); ++i) + { + printf("Edge %d: %d -> %d with %f\n", i, edges[i].Ne, edges[i].Ke, edges[i].W); + } +} + +NeighboorVector Graph::GetNeigboors(int vertex) const +{ + NeighboorVector result; + for (int i = 0; i < edges.size(); ++i) + { + if (edges[i].Ke == vertex || edges[i].Ne == vertex) + { + Neighboor nv; + nv.edgeWeight = edges[i].W; + + if (edges[i].Ke == vertex) + { + nv.vertex = edges[i].Ne; + } + + else + { + nv.vertex = edges[i].Ke; + } + + result.push_back(nv); + } + } + + return result; +} + +void Graph::GenerateEdges(int nEdges) { + int currentEdgesNum = 0; + + while (edges.size() < nEdges) + { + bool fail = false; + int n = rand() % nVertex; + int k = rand() % nVertex; + + //The same vertex - try again + if (n == k) + { + continue; + } + + //This edge already exists + for (int i = 0; i < edges.size(); ++i) + { + if ((edges[i].Ke == n) && (edges[i].Ne == k) || + (edges[i].Ne == n) && (edges[i].Ke == k)) + { + fail = true; + break; + } + } + + if (fail) + { + continue; + } + + //This is a new edge + double W = generateWeight(); + sumOfWeights += W; + edges.push_back(Edge(n, k, W)); + } + + //print graph + Print(); + printf("\n\n"); +} \ No newline at end of file diff --git a/BelyakovaEA/Lab2/src/Kruskal.cpp b/BelyakovaEA/Lab2/src/Kruskal.cpp new file mode 100644 index 000000000..64be34e28 --- /dev/null +++ b/BelyakovaEA/Lab2/src/Kruskal.cpp @@ -0,0 +1 @@ +#include "Kruskal.h" \ No newline at end of file diff --git a/BelyakovaEA/Lab2/src/PriorituQueue.cpp b/BelyakovaEA/Lab2/src/PriorituQueue.cpp new file mode 100644 index 000000000..505aa0100 --- /dev/null +++ b/BelyakovaEA/Lab2/src/PriorituQueue.cpp @@ -0,0 +1 @@ +#include "PriorituQueue.h" \ No newline at end of file diff --git a/BelyakovaEA/Lab2/src/SeparatedSet.cpp b/BelyakovaEA/Lab2/src/SeparatedSet.cpp new file mode 100644 index 000000000..0de2bfbd0 --- /dev/null +++ b/BelyakovaEA/Lab2/src/SeparatedSet.cpp @@ -0,0 +1,48 @@ +#include "SeparatedSet.h" + +#include + +SeparatedSet::SeparatedSet(int _size) +{ + this->size = _size; + harVec = new int[size]; + + for (int i = 0; i < size; ++i) + { + harVec[i] = i; + } +} + + +void SeparatedSet::makesets(int idx) +{ + //printf("makeset %d\n", idx); + if ((idx > size - 1) || (idx < 0)) + throw ("out of range"); + + harVec[idx] = idx; +} + +int SeparatedSet::findsets(int idx) +{ + //printf("findset %d\n", idx); + if ((idx > size - 1) || (idx < 0)) + throw ("out of range"); + + return harVec[idx]; +} + +void SeparatedSet::unionsets(int x, int y) +{ + //printf("x %d y %d\n", x, y); + if ((x > size - 1) || (x < 0) || (y > size - 1) || (y < 0)) + throw ("out of range"); + + for (int i = 0; i < size; ++i) + { + if (harVec[i] == y) + { + harVec[i] = x; + } + } +} \ No newline at end of file