Browse Source

Added Graphs

Vsevolod Levitan 11 months ago
parent
commit
586ae83030

+ 31 - 0
Graphs/Graphs.sln

@@ -0,0 +1,31 @@
+
+Microsoft Visual Studio Solution File, Format Version 12.00
+# Visual Studio Version 17
+VisualStudioVersion = 17.11.34909.67
+MinimumVisualStudioVersion = 10.0.40219.1
+Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Graphs", "Graphs\Graphs.vcxproj", "{A6417E74-AE0F-4E3E-9472-AB7A9423BF90}"
+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
+		{A6417E74-AE0F-4E3E-9472-AB7A9423BF90}.Debug|x64.ActiveCfg = Debug|x64
+		{A6417E74-AE0F-4E3E-9472-AB7A9423BF90}.Debug|x64.Build.0 = Debug|x64
+		{A6417E74-AE0F-4E3E-9472-AB7A9423BF90}.Debug|x86.ActiveCfg = Debug|Win32
+		{A6417E74-AE0F-4E3E-9472-AB7A9423BF90}.Debug|x86.Build.0 = Debug|Win32
+		{A6417E74-AE0F-4E3E-9472-AB7A9423BF90}.Release|x64.ActiveCfg = Release|x64
+		{A6417E74-AE0F-4E3E-9472-AB7A9423BF90}.Release|x64.Build.0 = Release|x64
+		{A6417E74-AE0F-4E3E-9472-AB7A9423BF90}.Release|x86.ActiveCfg = Release|Win32
+		{A6417E74-AE0F-4E3E-9472-AB7A9423BF90}.Release|x86.Build.0 = Release|Win32
+	EndGlobalSection
+	GlobalSection(SolutionProperties) = preSolution
+		HideSolutionNode = FALSE
+	EndGlobalSection
+	GlobalSection(ExtensibilityGlobals) = postSolution
+		SolutionGuid = {2392C7AD-FC92-4444-AAA1-0F0FC2CC43F9}
+	EndGlobalSection
+EndGlobal

+ 175 - 0
Graphs/Graphs/Graph.cpp

@@ -0,0 +1,175 @@
+#include "Graph.h"
+#include "GraphAlgo.h"
+
+
+void GraphNode::add_neigbor(GraphNode* neighboor)
+{
+	neighbors.insert(neighboor);
+}
+
+void GraphNode::remove_neighbor(GraphNode* neighboor)
+{
+	neighbors.erase(neighboor);
+}
+
+GraphNode::GraphNode()
+{
+}
+
+GraphNode::GraphNode(const string& aname)
+{
+	name = aname;
+}
+
+const string GraphNode::get_name() const
+{
+	return name;
+}
+
+const bool& GraphNode::operator==(const GraphNode& r)
+{
+	return name == r.name;
+}
+
+void Graph::add_node(GraphNode* GraphNode)
+{
+	if (taken_names[GraphNode->name]) {
+		throw Graph_exception();
+	}
+	else {
+		taken_names[GraphNode->name] = true;
+		nodes.insert(GraphNode);
+	}
+}
+
+void Graph::remove_node(GraphNode* GraphNode)
+{
+	nodes.erase(GraphNode);
+	for (auto it = nodes.begin(); it != nodes.end(); it++) {
+		(*it)->remove_neighbor(GraphNode);
+	}
+
+}
+
+void Graph::add_edge(GraphNode* begin, GraphNode* end)
+{
+	if (nodes.find(begin) == nodes.end())
+		return;
+	if (nodes.find(end) == nodes.end())
+		return;
+	begin->add_neigbor(end);
+	end->add_neigbor(begin);
+}
+
+void Graph::remove_edge(GraphNode* begin, GraphNode* end)
+{
+	if (nodes.find(begin) == nodes.end())
+		return;
+	if (nodes.find(end) == nodes.end())
+		return;
+	begin->remove_neighbor(end);
+	end->remove_neighbor(begin);
+}
+
+
+
+Graph Graph::read_file(const string &file_name)
+{
+	Graph graph = Graph();
+	ifstream input;
+	input.open(file_name);
+	if (input.is_open()) {
+		string line;
+		getline(input, line);
+
+		while (getline(input, line)) {
+			string fir = "", sec = "";
+			int splitter = 0;
+			while (line[splitter] != '\t')
+			{
+				fir += line[splitter];
+				splitter++;
+			}
+			splitter++;
+			while (line[splitter])
+			{
+				sec += line[splitter];
+				splitter++;
+			}
+
+			graph.saved[fir] = graph.taken_names[fir] ? graph.saved[fir] : GraphNode(fir);
+			graph.saved[sec] = graph.taken_names[sec] ? graph.saved[sec] : GraphNode(sec);
+
+			try {
+				graph.add_node(&graph.saved[fir]);
+			}
+			catch (Graph_exception ex) {}
+			try {
+				graph.add_node(&graph.saved[sec]);
+			}
+			catch (Graph_exception ex) {}
+
+			graph.add_edge(&graph.saved[fir], &graph.saved[sec]);
+		}
+	}
+	input.close();
+
+	return graph;
+}
+
+void Graph::write_graph(vector<vector<node_iterator>> ind)
+{
+	int size = ind.size();
+	for (int i = 0; i < size; i++) {
+
+		string c_name = "graph";
+		c_name += (i + 49);
+		c_name += ".txt";
+
+		ofstream out(c_name);
+		for (int j = 0; j < ind[i].size(); j++) {
+			auto& cur = *ind[i][j];
+			auto it = cur->neighbors.begin();
+			while (it != cur->neighbors.end()) {
+				auto neighb = *it;
+				out << cur->name << '\t' << neighb->name << '\n';
+				it++;
+			}
+		}
+		out.close();
+	}
+}
+
+void Graph::find_independent_graphs()
+{
+	int size = nodes.size();
+	vector <vector <node_iterator> > independent;
+	set<GraphNode*> working_nodes = nodes;
+
+
+	for (int i = 0; i < size; i++) {
+
+		set<GraphNode*>::iterator it = working_nodes.begin(), compared = working_nodes.begin();
+		vector<node_iterator> tmp_nodes;
+		tmp_nodes.push_back(nodes.find(*it));
+
+		BFS fs(this);
+		compared++;
+		while (compared != working_nodes.end()) {
+
+			if (fs.connected(*it, *compared))
+				tmp_nodes.push_back(nodes.find(*compared));
+			compared++;
+
+		}
+
+		independent.push_back(tmp_nodes);
+
+		for (int i = 0; i < tmp_nodes.size(); i++) {
+			working_nodes.erase(working_nodes.find(*tmp_nodes[i]));
+			size--;
+		}
+	}
+
+	write_graph(independent);
+}

+ 68 - 0
Graphs/Graphs/Graph.h

@@ -0,0 +1,68 @@
+#pragma once 
+#include <string>
+#include <map>
+#include <set>
+#include <fstream>
+#include <vector>
+#include <iostream>
+
+using namespace std;
+
+
+class Graph_exception {};
+
+class GraphNode {
+	typedef set<GraphNode*>::const_iterator node_iterator;
+
+private:
+	string name;
+	set <GraphNode*> neighbors;
+	void add_neigbor(GraphNode* neighboor);
+	void remove_neighbor(GraphNode* neighboor);
+
+public:
+	GraphNode();
+	GraphNode(const string& aname);
+	const string get_name() const;
+
+
+	node_iterator nb_begin() const {
+		return neighbors.begin();
+	};
+	node_iterator nb_end() const {
+		return neighbors.end();
+	};
+
+	const bool& operator == (const GraphNode& r);
+
+
+	friend class Graph;
+};
+
+class Graph
+{
+	typedef set<GraphNode*>::const_iterator node_iterator;
+private:
+	map <string, bool> taken_names;
+	map <string, GraphNode> saved;
+	set<GraphNode*> nodes;
+	void write_graph(vector <vector <node_iterator> > ind);
+public:
+	Graph() {};
+	void add_node(GraphNode* node);
+	void remove_node(GraphNode* node);
+	void add_edge(GraphNode* begin, GraphNode* end);
+	void remove_edge(GraphNode* begin, GraphNode* end);
+	static Graph read_file(const string& file_name);
+	void find_independent_graphs();
+
+
+	node_iterator begin() {
+		return nodes.begin();
+	};
+
+	node_iterator end() {
+		return nodes.end();
+	};
+	friend class Node;
+};

+ 37 - 0
Graphs/Graphs/GraphAlgo.cpp

@@ -0,0 +1,37 @@
+#include "GraphAlgo.h"
+#include <queue>
+
+using namespace std;
+
+bool DFS::connected(GraphNode* begin, GraphNode* end) {
+	visited.clear(); return connected(begin, end, 0);
+}
+bool DFS::connected(GraphNode* begin, GraphNode* end, int depth) {
+	if (begin == end) return true;
+	visited.insert(begin);
+	for (node_iterator it = begin->nb_begin();
+		it != begin->nb_end(); it++) {
+		if (visited.find(*it) == visited.end()) {
+			if (connected(*it, end, depth + 1)) return true;
+		}
+	}
+	return false;
+}
+
+
+bool BFS::connected(GraphNode* begin, GraphNode* end) {
+	deque <GraphNode*> nodes;
+	nodes.push_back(begin);
+	set<GraphNode*> visited;
+	while (!nodes.empty()) {
+		GraphNode* next = nodes.front(); nodes.pop_front();
+		if (end == next) return true;
+		visited.insert(next);
+		for (node_iterator it = next->nb_begin();
+			it != next->nb_end(); it++)
+			if (visited.find(*it) == visited.end())
+				nodes.push_back(*it);
+	}
+	return false;
+}
+

+ 28 - 0
Graphs/Graphs/GraphAlgo.h

@@ -0,0 +1,28 @@
+#pragma once
+#include "Graph.h"
+
+class BFS
+{
+    typedef set<GraphNode*>::const_iterator node_iterator;
+    const Graph* graph;
+public:
+    BFS(const Graph* agraph) : graph(agraph) {}
+    bool connected(GraphNode* begin, GraphNode* end);
+    friend Graph;
+    friend GraphNode;
+};
+
+
+class DFS {
+    typedef set<GraphNode*>::const_iterator node_iterator;
+private:
+    const Graph* graph;
+    set<GraphNode*> visited;
+    bool connected(GraphNode* begin, GraphNode* end, int depth);
+public:
+
+    DFS(const Graph* agraph) : graph(agraph) {}
+    bool connected(GraphNode* begin, GraphNode* end);
+    friend Graph;
+    friend GraphNode;
+};

+ 16 - 0
Graphs/Graphs/Graphs.cpp

@@ -0,0 +1,16 @@
+#include <iostream>
+#include "Graph.h"
+#include "GraphAlgo.h"
+
+using namespace std;
+
+
+int main()
+{
+	string filename;
+	cout << "Введите название файла с графом: ";
+	cin >> filename;
+	Graph gr = Graph::read_file(filename);
+	gr.find_independent_graphs();
+	cout << "fin." << endl;
+}

+ 141 - 0
Graphs/Graphs/Graphs.vcxproj

@@ -0,0 +1,141 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <ItemGroup Label="ProjectConfigurations">
+    <ProjectConfiguration Include="Debug|Win32">
+      <Configuration>Debug</Configuration>
+      <Platform>Win32</Platform>
+    </ProjectConfiguration>
+    <ProjectConfiguration Include="Release|Win32">
+      <Configuration>Release</Configuration>
+      <Platform>Win32</Platform>
+    </ProjectConfiguration>
+    <ProjectConfiguration Include="Debug|x64">
+      <Configuration>Debug</Configuration>
+      <Platform>x64</Platform>
+    </ProjectConfiguration>
+    <ProjectConfiguration Include="Release|x64">
+      <Configuration>Release</Configuration>
+      <Platform>x64</Platform>
+    </ProjectConfiguration>
+  </ItemGroup>
+  <PropertyGroup Label="Globals">
+    <VCProjectVersion>17.0</VCProjectVersion>
+    <Keyword>Win32Proj</Keyword>
+    <ProjectGuid>{a6417e74-ae0f-4e3e-9472-ab7a9423bf90}</ProjectGuid>
+    <RootNamespace>Graphs</RootNamespace>
+    <WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
+  </PropertyGroup>
+  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
+    <ConfigurationType>Application</ConfigurationType>
+    <UseDebugLibraries>true</UseDebugLibraries>
+    <PlatformToolset>v143</PlatformToolset>
+    <CharacterSet>Unicode</CharacterSet>
+  </PropertyGroup>
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
+    <ConfigurationType>Application</ConfigurationType>
+    <UseDebugLibraries>false</UseDebugLibraries>
+    <PlatformToolset>v143</PlatformToolset>
+    <WholeProgramOptimization>true</WholeProgramOptimization>
+    <CharacterSet>Unicode</CharacterSet>
+  </PropertyGroup>
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
+    <ConfigurationType>Application</ConfigurationType>
+    <UseDebugLibraries>true</UseDebugLibraries>
+    <PlatformToolset>v143</PlatformToolset>
+    <CharacterSet>Unicode</CharacterSet>
+  </PropertyGroup>
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
+    <ConfigurationType>Application</ConfigurationType>
+    <UseDebugLibraries>false</UseDebugLibraries>
+    <PlatformToolset>v143</PlatformToolset>
+    <WholeProgramOptimization>true</WholeProgramOptimization>
+    <CharacterSet>Unicode</CharacterSet>
+  </PropertyGroup>
+  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
+  <ImportGroup Label="ExtensionSettings">
+  </ImportGroup>
+  <ImportGroup Label="Shared">
+  </ImportGroup>
+  <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
+    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
+  </ImportGroup>
+  <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
+    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
+  </ImportGroup>
+  <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
+    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
+  </ImportGroup>
+  <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
+    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
+  </ImportGroup>
+  <PropertyGroup Label="UserMacros" />
+  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
+    <ClCompile>
+      <WarningLevel>Level3</WarningLevel>
+      <SDLCheck>true</SDLCheck>
+      <PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+      <ConformanceMode>true</ConformanceMode>
+    </ClCompile>
+    <Link>
+      <SubSystem>Console</SubSystem>
+      <GenerateDebugInformation>true</GenerateDebugInformation>
+    </Link>
+  </ItemDefinitionGroup>
+  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
+    <ClCompile>
+      <WarningLevel>Level3</WarningLevel>
+      <FunctionLevelLinking>true</FunctionLevelLinking>
+      <IntrinsicFunctions>true</IntrinsicFunctions>
+      <SDLCheck>true</SDLCheck>
+      <PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+      <ConformanceMode>true</ConformanceMode>
+    </ClCompile>
+    <Link>
+      <SubSystem>Console</SubSystem>
+      <EnableCOMDATFolding>true</EnableCOMDATFolding>
+      <OptimizeReferences>true</OptimizeReferences>
+      <GenerateDebugInformation>true</GenerateDebugInformation>
+    </Link>
+  </ItemDefinitionGroup>
+  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
+    <ClCompile>
+      <WarningLevel>Level3</WarningLevel>
+      <SDLCheck>true</SDLCheck>
+      <PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+      <ConformanceMode>true</ConformanceMode>
+    </ClCompile>
+    <Link>
+      <SubSystem>Console</SubSystem>
+      <GenerateDebugInformation>true</GenerateDebugInformation>
+    </Link>
+  </ItemDefinitionGroup>
+  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
+    <ClCompile>
+      <WarningLevel>Level3</WarningLevel>
+      <FunctionLevelLinking>true</FunctionLevelLinking>
+      <IntrinsicFunctions>true</IntrinsicFunctions>
+      <SDLCheck>true</SDLCheck>
+      <PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+      <ConformanceMode>true</ConformanceMode>
+    </ClCompile>
+    <Link>
+      <SubSystem>Console</SubSystem>
+      <EnableCOMDATFolding>true</EnableCOMDATFolding>
+      <OptimizeReferences>true</OptimizeReferences>
+      <GenerateDebugInformation>true</GenerateDebugInformation>
+    </Link>
+  </ItemDefinitionGroup>
+  <ItemGroup>
+    <ClCompile Include="Graph.cpp" />
+    <ClCompile Include="GraphAlgo.cpp" />
+    <ClCompile Include="Graphs.cpp" />
+  </ItemGroup>
+  <ItemGroup>
+    <ClInclude Include="Graph.h" />
+    <ClInclude Include="GraphAlgo.h" />
+  </ItemGroup>
+  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
+  <ImportGroup Label="ExtensionTargets">
+  </ImportGroup>
+</Project>

+ 36 - 0
Graphs/Graphs/Graphs.vcxproj.filters

@@ -0,0 +1,36 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <ItemGroup>
+    <Filter Include="Source Files">
+      <UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
+      <Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
+    </Filter>
+    <Filter Include="Header Files">
+      <UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
+      <Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
+    </Filter>
+    <Filter Include="Resource Files">
+      <UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
+      <Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
+    </Filter>
+  </ItemGroup>
+  <ItemGroup>
+    <ClCompile Include="Graphs.cpp">
+      <Filter>Source Files</Filter>
+    </ClCompile>
+    <ClCompile Include="Graph.cpp">
+      <Filter>Source Files</Filter>
+    </ClCompile>
+    <ClCompile Include="GraphAlgo.cpp">
+      <Filter>Source Files</Filter>
+    </ClCompile>
+  </ItemGroup>
+  <ItemGroup>
+    <ClInclude Include="Graph.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
+    <ClInclude Include="GraphAlgo.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
+  </ItemGroup>
+</Project>

+ 62 - 0
Graphs/Graphs/graph.txt

@@ -0,0 +1,62 @@
+Source	Target
+0	1
+0	4
+0	11
+0	38
+1	20
+1	28
+2	6
+2	14
+2	20
+2	33
+2	34
+2	38
+3	31
+3	46
+4	10
+4	21
+4	26
+4	32
+5	28
+6	9
+6	18
+6	44
+7	15
+7	25
+7	42
+7	43
+8	12
+8	13
+8	46
+9	10
+9	30
+9	37
+9	46
+11	21
+11	23
+11	48
+12	16
+12	27
+16	32
+16	48
+18	24
+18	26
+19	33
+19	40
+19	43
+20	45
+21	34
+22	35
+24	46
+26	48
+29	39
+30	42
+31	48
+33	34
+33	47
+33	48
+35	43
+36	42
+36	47
+37	46
+44	46

+ 120 - 0
Graphs/Graphs/graph1.txt

@@ -0,0 +1,120 @@
+0	1
+0	4
+0	11
+0	38
+1	0
+1	20
+1	28
+4	0
+4	26
+4	21
+4	10
+4	32
+11	0
+11	23
+11	21
+11	48
+38	0
+38	2
+20	1
+20	2
+20	45
+28	1
+28	5
+2	38
+2	20
+2	6
+2	14
+2	33
+2	34
+6	2
+6	9
+6	18
+6	44
+14	2
+33	2
+33	34
+33	19
+33	48
+33	47
+34	2
+34	33
+34	21
+3	31
+3	46
+31	3
+31	48
+46	3
+46	37
+46	9
+46	44
+46	8
+46	24
+13	8
+26	4
+26	18
+26	48
+42	30
+42	7
+42	36
+30	42
+30	9
+37	46
+37	9
+23	11
+9	6
+9	46
+9	30
+9	37
+9	10
+21	4
+21	11
+21	34
+43	7
+43	19
+43	35
+25	7
+18	6
+18	26
+18	24
+15	7
+7	42
+7	43
+7	25
+7	15
+44	6
+44	46
+5	28
+8	46
+8	13
+8	12
+10	4
+10	9
+32	4
+32	16
+12	8
+12	27
+12	16
+19	33
+19	43
+19	40
+40	19
+27	12
+36	42
+36	47
+48	11
+48	33
+48	31
+48	26
+48	16
+16	32
+16	12
+16	48
+45	20
+22	35
+24	46
+24	18
+35	43
+35	22
+47	33
+47	36

+ 2 - 0
Graphs/Graphs/graph2.txt

@@ -0,0 +1,2 @@
+39	29
+29	39