commit c6522b97c6c1cf2a0af154b4e5091fa06031ee13 Author: Dvurechensky <46356631+Dvurechensky@users.noreply.github.com> Date: Sat Oct 5 09:04:42 2024 +0300 1.0 Main diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..dfe0770 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# Auto detect text files and perform LF normalization +* text=auto diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fba780e --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.vs +GrufCustom/bin +GrufCustom/obj \ No newline at end of file diff --git a/GrufCustom.sln b/GrufCustom.sln new file mode 100644 index 0000000..54e38d9 --- /dev/null +++ b/GrufCustom.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.2.32616.157 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GrafCustom", "GrufCustom\GrafCustom.csproj", "{855A04E3-8F24-4879-ABD9-CECA260A7645}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {855A04E3-8F24-4879-ABD9-CECA260A7645}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {855A04E3-8F24-4879-ABD9-CECA260A7645}.Debug|Any CPU.Build.0 = Debug|Any CPU + {855A04E3-8F24-4879-ABD9-CECA260A7645}.Release|Any CPU.ActiveCfg = Release|Any CPU + {855A04E3-8F24-4879-ABD9-CECA260A7645}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {550B1FB8-6683-4CE5-86D1-D61F88AF51B9} + EndGlobalSection +EndGlobal diff --git a/GrufCustom/Data/Edge.cs b/GrufCustom/Data/Edge.cs new file mode 100644 index 0000000..0c62c69 --- /dev/null +++ b/GrufCustom/Data/Edge.cs @@ -0,0 +1,22 @@ +namespace GraphCustomSearcher.Data; + +/// +/// Шаблог ребра +/// +public class Edge +{ + /// + /// Узел начала + /// + public Verticle VerticleStart { get; set; } + /// + /// Узел конца + /// + public Verticle VerticleStop { get; set; } + + public Edge(Verticle verticleStart, Verticle verticleStop) + { + VerticleStart = verticleStart; + VerticleStop = verticleStop; + } +} diff --git a/GrufCustom/Data/Verticle.cs b/GrufCustom/Data/Verticle.cs new file mode 100644 index 0000000..74fa5cb --- /dev/null +++ b/GrufCustom/Data/Verticle.cs @@ -0,0 +1,21 @@ +namespace GraphCustomSearcher.Data; + +/// +/// Узел +/// +public class Verticle +{ + /// + /// Имя + /// + public string Name { get; set; } + /// + /// Статус посещения узла + /// + public bool IsVisited { get; set; } + + public Verticle(string name) + { + Name = name; + } +} diff --git a/GrufCustom/GrafCustom.csproj b/GrufCustom/GrafCustom.csproj new file mode 100644 index 0000000..74abf5c --- /dev/null +++ b/GrufCustom/GrafCustom.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/GrufCustom/Graph.cs b/GrufCustom/Graph.cs new file mode 100644 index 0000000..882e09c --- /dev/null +++ b/GrufCustom/Graph.cs @@ -0,0 +1,76 @@ +using GraphCustomSearcher.Data; + +namespace GraphCustomSearcher; + +public class Graph +{ + public List Verticls; + public List Edges; + public string Path { get; set; } + public Verticle Target { get; set; } + + public Graph() + { + Verticls = new List(); + Edges = new List(); + } + + /// + /// Загрузка узлов + /// + /// узлы + public void AddVerticle(List verticles) + { + Verticls.AddRange(verticles); + } + + /// + /// Загрузка рёбер + /// + /// рёбра + public void AddEdge(List edges) + { + Edges.AddRange(edges); + } + + public void RebuildEdge() + { + Path = string.Empty; + foreach (var edge in Edges) + { + edge.VerticleStop.IsVisited = false; + edge.VerticleStart.IsVisited = false; + } + } + + /// + /// Перебор рёбер - поиск вершин в которые входит точка старта + /// + /// + /// + public void SearchPath(string source, string target) + { + Console.WriteLine($"Начало: {source} Конец: {target}"); + var sourceVerticle = new Verticle(source); + var targetVerticle = new Verticle(target); + foreach (var edge in Edges) + { + var state_1 = edge.VerticleStart.Name == sourceVerticle.Name && edge.VerticleStart.IsVisited == sourceVerticle.IsVisited; + var state_2 = !edge.VerticleStart.IsVisited; + var state_3 = !(!string.IsNullOrEmpty(Path) && Path.Contains(edge.VerticleStop.Name)); + if (state_1 && state_2 && state_3) + { + Path += edge.VerticleStart.Name + " "; + Console.WriteLine($"Ребро {edge.VerticleStart.Name} направляется в {edge.VerticleStop.Name}"); + edge.VerticleStart.IsVisited = true; + Target = edge.VerticleStop; + if (targetVerticle.Name == edge.VerticleStop.Name) + { + Path += edge.VerticleStop.Name; + return; + } + SearchPath(edge.VerticleStop.Name, targetVerticle.Name); + } + } + } +} diff --git a/GrufCustom/Program.cs b/GrufCustom/Program.cs new file mode 100644 index 0000000..fb69655 --- /dev/null +++ b/GrufCustom/Program.cs @@ -0,0 +1,66 @@ +using GraphCustomSearcher.Data; +using GraphCustomSearcher; + +/// +/// Ищет в направленном графе путь от точки до точки +/// учитывает наличие круговых двухсторонних связей +/// +public class Program +{ + public static void Main() + { + var GraphX = new Graph(); + var namesVerticles = new string[] { "Пенсильвания", "Париж", "Магеллан", "Аляска", "Аляска", "Оптимум"}; + var wayEdges = new int[,] { { 0, 1 }, { 1, 0 }, { 1, 2 }, { 2, 1 }, { 2, 5 }, { 3, 1 }, { 3, 4 }, { 4, 3 } }; + + GraphX.AddVerticle(AddVerticles(namesVerticles)); + GraphX.AddEdge(AddEdges(wayEdges, AddVerticles(namesVerticles))); + + Console.WriteLine("Пенсильвания->Париж"); + StartSeachPath(GraphX, "Пенсильвания", "Париж"); + Console.WriteLine("Пенсильвания->Оптимум"); + StartSeachPath(GraphX, "Пенсильвания", "Оптимум"); + } + + /// + /// Ищет путь от узла до узла + /// + /// Граф + /// Точка старта + /// Точка прибытия + public static void StartSeachPath(Graph graph, string start, string stop) + { + Console.WriteLine(); + graph.RebuildEdge(); + graph.SearchPath(start, stop); + if (stop.Contains(graph.Target.Name)) + Console.WriteLine($"--> PATH: {graph.Path}"); + else Console.WriteLine("--> ERROR: Путь невозможен"); + Console.WriteLine(); + } + + /// + /// Добавить узлы + /// + /// список имён узлов + /// + public static List AddVerticles(string[] namesVerticles) + { + var VerticleList = new List(); + foreach (var verticle in namesVerticles) + VerticleList.Add(new Verticle(verticle)); + return VerticleList; + } + + /// + /// Добавить рёбра (возможные пути между узлами) + /// + /// + public static List AddEdges(int[,] wayEdges, List VerticleList) + { + var EdgeList = new List(); + for(int ind = 0; ind < wayEdges.GetLength(0); ind++) + EdgeList.Add(new Edge(VerticleList[wayEdges[ind,0]], VerticleList[wayEdges[ind, 1]])); + return EdgeList; + } +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..1c3d441 --- /dev/null +++ b/README.md @@ -0,0 +1,11 @@ +

+

+ Static Badge + +

+

+ +# Кастомный консольный Graph 🐠 +- Ищет путь от точки до точки, в направленном графе +- Работает только в одном направлении сверху вниз +- Создавался для узкоспециализированной задачи и мало где может пригодиться 😵 \ No newline at end of file