/// <summary> /// Алгоритм Крускала. Сложность 0(Е * lgV). /// </summary> public int MinimumSpanningTreeKruskal() { EdgesList.Sort((first, second) => first.Weight.CompareTo(second.Weight)); // сортировка ребер по неубыванию int totalWeight = 0; foreach (var edge in EdgesList) { int firstVertexColor = edge.First.Color; int secondVertexColor = edge.Second.Color; int weight = edge.Weight; if (firstVertexColor != secondVertexColor) { edge.InTree = true; totalWeight += weight; foreach (var vertex in VerticesList) { if (vertex.Color == secondVertexColor) { vertex.Color = firstVertexColor; } } } } return(totalWeight); }
/// <summary> Возвращает ребро с началом в v1 и концом в v2 </summary> public override DirectedWeightedEdge this[Vertex v1, Vertex v2] { get { return(EdgesList.FirstOrDefault(e => e.Vertex1.Equals(v1) && e.Vertex2.Equals(v2))); } }
public void BuildInducedGraph(List <int> verticesIndexes) { foreach (var index in verticesIndexes) { foreach (var incidentEdge in VerticesList[index - 1].AdjacencyList) { var adjacencyVertex = incidentEdge.IncidentTo.AdjacencyList; adjacencyVertex.Remove(adjacencyVertex.First(edge => edge.IncidentTo.Index == index)); } for (int i = 0; i < EdgesList.Count; i++) { if (EdgesList[i].First.Index == index || EdgesList[i].Second.Index == index) { EdgesList.RemoveAt(i); } } } foreach (var vertexToDeleteIndex in verticesIndexes) { VerticesList.RemoveAt(vertexToDeleteIndex - 1); } Size -= 1; }
public void DeleteVertex(Ellipse ellipse) { foreach (var vert in VertexesList) { if (vert.ellipse == ellipse) { foreach (var edge in vert.lines) { if (vert.ellipse == edge.StartVertex.ellipse) { helpdeleteEdge(edge.EndVertex.ellipse, edge); } if (vert.ellipse == edge.EndVertex.ellipse) { helpdeleteEdge(edge.StartVertex.ellipse, edge); } GraphPlain.Children.Remove(edge.line); GraphPlain.Children.Remove(edge.textBox); EdgesList.Remove(edge); } vert.lines.Clear(); VertexesList.Remove(vert); GraphPlain.Children.Remove(vert.ellipse); GraphPlain.Children.Remove(vert.textBox); break; } } }
public void ReadEdge(Petzold.Media2D.ArrowLine line, TextBox textBox, GraphVertex v1, GraphVertex v2) { textBox.TextChanged += new System.Windows.Controls.TextChangedEventHandler(ChangeEdgeWeight); textBox.KeyUp += new KeyEventHandler(KeyEnterClickTextBox); textBox.MouseEnter += new MouseEventHandler(IsMouseOnTextBox); textBox.MouseDoubleClick += new MouseButtonEventHandler(TextBoxVertexDoubleClick); textBox.MouseMove += new MouseEventHandler(MouseMoveOnTextBox); GraphEdge tempEdge = new GraphEdge(line, textBox, Convert.ToDouble(textBox.Text), v1, v2); foreach (var t in VertexesList) { if (t.ellipse == v1.ellipse) { t.lines.Add(tempEdge); } if (t.ellipse == v2.ellipse) { t.lines.Add(tempEdge); } } tempEdge.line.MouseLeftButtonUp += new MouseButtonEventHandler(LineEdgeMouseClick); GraphPlain.Children.Add(tempEdge.line); GraphPlain.Children.Add(textBox); EdgesList.Add(tempEdge); }
/// <summary> Возвращает ребро между вершинамиv v1 и v2. Если ребра нет, то null. </summary> public override UndirectedEdge this[Vertex v1, Vertex v2] { get { var edgeToFind = new UndirectedEdge(v1, v2); return(EdgesList.FirstOrDefault(e => e.Equals(edgeToFind))); } }
/// <summary>Labels are not needed on edges that connect a part of speech to its token</summary> private void RemoveEdgeLabelsFromPartsOfSpeech() { IEnumerable <EdgeControl> partOfSpeechToTokenEdges = EdgesList .Where(kvp => kvp.Key is PartOfSpeechToContentEdge) .Select(kvp => kvp.Value); foreach (EdgeControl eachPartOfSpeechToContentEdge in partOfSpeechToTokenEdges) { eachPartOfSpeechToContentEdge.GetLabelControls().First().ShowLabel = false; } }
public void ConnectVertex(GraphVertex e1, GraphVertex e2) { if (e1 == e2) { return; } if (EdgeAlreadyExist(e1, e2)) { Xceed.Wpf.Toolkit.MessageBox.Show("Ребро уже было добавлено!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Exclamation); return; } TextBox textBox = new System.Windows.Controls.TextBox(); GraphEdge tempEdge_ = new GraphEdge(e1, e2, IsOriented, textBox, 0); RewriteTextBoxEdge(tempEdge_, textBox); textBox.TextChanged += new System.Windows.Controls.TextChangedEventHandler(ChangeEdgeWeight); textBox.KeyUp += new KeyEventHandler(KeyEnterClickTextBox); textBox.CaretBrush = new SolidColorBrush(Color.FromArgb(0, 0, 0, 0)); textBox.Width = 30; textBox.Height = 25; textBox.FontSize = 16; textBox.FontWeight = FontWeights.Bold; var color = new SolidColorBrush(Color.FromArgb(0, 0, 0, 0)); textBox.Background = Brushes.White; textBox.BorderThickness = new Thickness(0, 0, 0, 0); textBox.TextAlignment = TextAlignment.Center; textBox.Text = "1"; textBox.Tag = tempEdge_.line.Tag.ToString(); textBox.MouseEnter += new MouseEventHandler(IsMouseOnTextBox); textBox.MouseDoubleClick += new MouseButtonEventHandler(TextBoxVertexDoubleClick); textBox.MouseMove += new MouseEventHandler(MouseMoveOnTextBox); //textBox.MouseLeftButtonUp += new MouseButtonEventHandler(MouseLeftDown); GraphEdge tempEdge = new GraphEdge(e1, e2, IsOriented, textBox, Convert.ToDouble(textBox.Text)); foreach (var t in VertexesList) { if (t.ellipse == e1.ellipse) { t.lines.Add(tempEdge); } if (t.ellipse == e2.ellipse) { t.lines.Add(tempEdge); } } tempEdge.line.MouseLeftButtonUp += new MouseButtonEventHandler(LineEdgeMouseClick); GraphPlain.Children.Add(tempEdge.line); GraphPlain.Children.Add(textBox); EdgesList.Add(tempEdge); }
public void RemoveNode(NodeP rem) { NodeR rel; List <Edge> remove; remove = new List <Edge>(); foreach (NodeP a in this) { rel = a.relations.Find(delegate(NodeR np) { if (np.Up.Name == rem.Name) { return(true); } else { return(false); } }); if (rel != null) { a.relations.Remove(rel); a.Degree--; a.DegreeEx--; if (!edgeIsDirected || edgeIsDirected) { a.DegreeIn--; } } } remove = EdgesList.FindAll(delegate(Edge ar) { if (ar.Source.Name == rem.Name || ar.Destiny.Name == rem.Name) { return(true); } else { return(false); } }); if (remove != null) { foreach (Edge re in remove) { EdgesList.Remove(re); } } this.Remove(rem); }
public void RemoveEdge(Edge ar) { NodeR rel; rel = ar.Source.relations.Find(delegate(NodeR np) { if (np.Up.Name == ar.Destiny.Name) { return(true); } else { return(false); } }); if (rel != null) { ar.Source.relations.Remove(rel); ar.Source.Degree--; ar.Destiny.Degree--; ar.Source.DegreeEx--; ar.Destiny.DegreeIn--; } if (!edgeIsDirected) { rel = ar.Destiny.relations.Find(delegate(NodeR np) { if (np.Up.Name == ar.Source.Name) { return(true); } else { return(false); } }); if (rel != null) { ar.Destiny.relations.Remove(rel); ar.Destiny.DegreeEx--; ar.Source.DegreeIn--; } } EdgesList.Remove(ar); }
private void RemoveEdge(Petzold.Media2D.ArrowLine line) { foreach (var edge in EdgesList) { if (edge.line == line) { GraphPlain.Children.Remove(line); GraphPlain.Children.Remove(edge.textBox); EdgesList.Remove(edge); foreach (var vert in VertexesList) { foreach (var edges in vert.lines) { if (edges.line == line) { vert.lines.Remove(edges); break; } } } break; } } }
public void AddEdge(Edge A) { EdgesList.Add(A); }
public static (bool connected, Graph tree) Boruvka(this Graph g) { if (g.Directed) { throw new WrongGraphException("Graph must be an undirected graph"); } int numberOfTrees = g.VerticesCount; Graph tree = g.IsolatedGraph(false); UnionFind unionFind = new UnionFind(g.VerticesCount); PairingHeap <Edge>[] heaps = new PairingHeap <Edge> [g.VerticesCount]; HashSet <int> activeHeaps = new HashSet <int>(); for (int i = 0; i < g.VerticesCount; i++) { heaps[i] = new PairingHeap <Edge>((x, y) => x.Weight > y.Weight); activeHeaps.Add(i); foreach (Edge e in g.GetEdgesFrom(i)) { heaps[i].Insert(e); } } OrdinaryList <(int, int)> heapsToMerge = new OrdinaryList <(int, int)>(); EdgesList edges = new EdgesList(); while (true) { foreach (int heapId in activeHeaps) { var result = heaps[heapId].ExtractMinimum(); if (result.HasValue) { edges.Push(result.value); } } if (edges.IsEmpty() && numberOfTrees > 1) { return(false, tree); } while (!edges.IsEmpty()) { Edge e = edges.Pop(); if (null == tree.GetEdge(e.From, e.To)) { int heapOneId = unionFind.FindParent(e.From); int heapTwoId = unionFind.FindParent(e.To); if (heapOneId != heapTwoId) { tree.AddEdge(e); unionFind.Union(e.From, e.To); heapsToMerge.Push((heapOneId, heapTwoId)); if (unionFind.FindParent(e.From) == heapOneId) { heaps[heapOneId].Concatenate(heaps[heapTwoId]); heaps[heapTwoId] = null; activeHeaps.Remove(heapTwoId); } else { heaps[heapTwoId].Concatenate(heaps[heapOneId]); heaps[heapOneId] = null; activeHeaps.Remove(heapOneId); } numberOfTrees--; } } } if (numberOfTrees == 1) { return(true, tree); } } }