//Пошук в глибину(для 5 умови) private void DFScycleForFifth(int u, int endV, List<DataEdge> E, List<DataVertex> V, int[] color, int unavailableEdge, List<int> cycle, Graph graph) { //если u == endV, то эту вершину перекрашивать не нужно, иначе мы в нее не вернемся, а вернуться необходимо if (u != endV) color[u] = 2; else if (cycle.Count >= 2) { cycle.Reverse(); string s = cycle[0].ToString(); for (int i = 1; i < cycle.Count; i++) s += "-" + cycle[i].ToString(); /////////////////////// List<DataVertex> list = new List<DataVertex>();//Додаю вершину в цикл list.Add(V.ElementAt(cycle[0])); for (int i = 1; i < cycle.Count - 1; i++) list.Add(V.ElementAt(cycle[i])); /////////////////////// bool flag = false; //есть ли палиндром для этого цикла графа в List<string> catalogCycles? for (int i = 0; i < catalogCycles.Count; i++) if (catalogCycles[i].ToString() == s) { flag = true; break; } if (!flag) { cycle.Reverse(); list = new List<DataVertex>(); list.Add(V.ElementAt(cycle[0])); for (int i = 1; i < cycle.Count - 1; i++) list.Add(V.ElementAt(cycle[i])); catalogCycles.Add(list); } return; } for (int w = 0; w < E.Count; w++) { var q = graph.OutEdges(V[V.FindIndex(0, V.Count, x => x.Text == E[w].Target.Text)]); if (w == unavailableEdge || (graph.InEdges(V[V.FindIndex(0, V.Count, x => x.Text == E[w].Target.Text)]).Count() > 1 || graph.OutEdges(V[V.FindIndex(0, V.Count, x => x.Text == E[w].Target.Text)]).Count() > 1) && V.FindIndex(x => x.Text == E[w].Target.Text) != endV) continue; if (color[V.FindIndex(0, V.Count, x => x.Text == E[w].Target.Text)] == 1 && V.FindIndex(0, V.Count, x => x.Text == E[w].Source.Text) == u) { List<int> cycleNEW = new List<int>(cycle); cycleNEW.Add(V.FindIndex(0, V.Count, x => x.Text == E[w].Target.Text)); DFScycleForFifth(V.FindIndex(0, V.Count, x => x.Text == E[w].Target.Text), endV, E, V, color, w, cycleNEW, graph); color[V.FindIndex(0, V.Count, x => x.Text == E[w].Target.Text)] = 1; } } }
private int createEdgesAndFindMinFeedBack(List<List<string>> list) { int feedback = 0; /////////// totalGraph.RemoveEdgeIf(x => x.Text != ""); totalGraph.RemoveVertexIf(x => x.Text != ""); /////////////// for (int i = list.Count - 1; i >= 0; i--) { string str = ""; foreach (string name in list[i]) { str += name; } totalGraph.AddVertex(new DataVertex(str)); } /////////// foreach(HashSet<string> row in calc.mas) { for (int i = 0; i < row.Count-1; i++) { string nameV1 = nameVertexAndM.First(x => x.Value.Contains(row.ElementAt(i))).Key; string nameV2 = nameVertexAndM.First(x => x.Value.Contains(row.ElementAt(i+1))).Key; int indexV1 = list.FindIndex(x => x.Contains(row.ElementAt(i))); int indexV2 = list.FindIndex(x => x.Contains(row.ElementAt(i + 1))); if(nameV1 != nameV2) { var Edge = new DataEdge(totalGraph.Vertices.First(x => x.Text == nameV1), totalGraph.Vertices.First(x => x.Text == nameV2)) { Text = string.Format("{0} -> {1}", totalGraph.Vertices.First(x => x.Text == nameV1), totalGraph.Vertices.First(x => x.Text == nameV2)) }; //Перевіряю чи є вже таке ребро DataEdge temp = new DataEdge(); if (totalGraph.TryGetEdge(Edge.Source, Edge.Target, out temp) == false) { totalGraph.AddEdge(Edge); if (indexV1 > indexV2) { feedback++; } } } } } return feedback; }