private void FindTrianglesDFS(VertexTriangleList triangles, Vertex startVertex, Vertex currentVertex, Stack stack) { if (stack.Count < 3) { stack.Push(currentVertex); foreach (Vertex v in currentVertex.GetEdges()) { FindTrianglesDFS(triangles, startVertex, v, stack); } stack.Pop(); } else if (stack.Count == 3 && currentVertex == startVertex) { //Znaleziono nowy trójkąt Array array = stack.ToArray(); Vertex v1 = (Vertex)array.GetValue(0); Vertex v2 = (Vertex)array.GetValue(1); Vertex v3 = (Vertex)array.GetValue(2); if (!triangles.Contains(v1, v2, v3)) { triangles.Add(v3, v2, v1); } } }
public VertexTriangleList FindTriangles() { VertexTriangleList triangles = new VertexTriangleList(); foreach (Vertex v in vertexes) { Stack stack = new Stack(); FindTrianglesDFS(triangles, v, v, stack); } return(triangles); }
private void button2_Click(object sender, EventArgs e) { Vertex[] vertexArray = vertex.ToArray(); Graph graph = new Graph(vertexArray); VertexTriangleList triangles = graph.FindTriangles(); foreach (VertexTriangle t in triangles) { foundTriLabel.Text += t.ToString() + "\n"; } foundTriLabel.Visible = true; //foundTriLabel.Text = ""; }