public void FindMinimumSpanningTree() { SortedSet <EdgeWeighted> priority = new SortedSet <EdgeWeighted>(); bool[] usedEdges = new bool[this.n + 1]; List <EdgeWeighted> mpdEdges = new List <EdgeWeighted>(); // adding edges that connect the node 1 with all the others - 2, 3, 4 ... foreach (EdgeWeighted edge in this.edges) { if (edge.StartNode == this.edges[0].StartNode) { priority.Add(edge); } } usedEdges[this.edges[0].StartNode] = true; while (priority.Count > 0) { EdgeWeighted edge = priority.Min; priority.Remove(edge); if (!usedEdges[edge.EndNode]) { usedEdges[edge.EndNode] = true; // we "visit" this node mpdEdges.Add(edge); this.AddEdges(edge, this.edges, mpdEdges, priority, usedEdges); } } this.PrintMinimumSpanningTree(mpdEdges); }
private void AddEdges(EdgeWeighted edge, List <EdgeWeighted> edges, List <EdgeWeighted> mpd, SortedSet <EdgeWeighted> priority, bool[] used) { for (int i = 0; i < edges.Count; i++) { if (!mpd.Contains(edges[i])) { if (edge.EndNode == edges[i].StartNode && !used[edges[i].EndNode]) { priority.Add(edges[i]); } } } }