public static Graph Boruvka(Graph graph) { var result = new Graph(); result.Vertexes = graph.Vertexes; var disjointForest = new DisjointSetUnionTree(result.Vertexes); while (result.Edges.Count < result.Vertexes.Count - 1) { var safe = new Dictionary<int, Edge<int>>(); foreach (var edge in graph.Edges) { var fromRoot = disjointForest.Find(edge.From); var toRoot = disjointForest.Find(edge.To); if (fromRoot != toRoot) { if (!safe.ContainsKey(fromRoot) || edge.Weight < safe[fromRoot].Weight) { safe[fromRoot] = edge; } if (!safe.ContainsKey(toRoot) || edge.Weight < safe[toRoot].Weight) { safe[toRoot] = edge; } } } result.Edges.AddRange(safe.Values.Distinct()); foreach (var edge in safe.Values) { disjointForest.Union(edge.From, edge.To); } } return result; }
public static Graph Kruskal(Graph inputGraph) { var result = new Graph(); result.Vertexes = inputGraph.Vertexes; var edges = inputGraph.Edges.ToList(); edges.Sort(); var enumerator = edges.GetEnumerator(); var disjointSetUnion = new DisjointSetUnionTree(inputGraph.Vertexes); while (result.Edges.Count < result.Vertexes.Count - 1) { enumerator.MoveNext(); var edge = enumerator.Current; var fromRoot = disjointSetUnion.Find(edge.From); var toRoot = disjointSetUnion.Find(edge.To); if (fromRoot != toRoot) { disjointSetUnion.Union(edge.From, edge.To); result.Edges.Add(edge); } } return result; }
public static int Cluster(Graph graph, int clusterCount) { var result = new Graph { Vertexes = graph.Vertexes }; var edges = graph.Edges.ToList(); edges.Sort(); var enumerator = edges.GetEnumerator(); var disjointSetUnion = new DisjointSetUnionTree(graph.Vertexes); while (disjointSetUnion.Count != clusterCount - 1) { enumerator.MoveNext(); var edge = enumerator.Current; var fromRoot = disjointSetUnion.Find(edge.From); var toRoot = disjointSetUnion.Find(edge.To); if (fromRoot != toRoot) { disjointSetUnion.Union(fromRoot, toRoot); result.Edges.Add(edge); } } return enumerator.Current.Weight; }
public static Graph GenerateTree(int vertexes) { var result = new Graph(); for (var i = 0; i < vertexes; i++) { result.Vertexes.Add(i); } var disjointSet = new DisjointSetUnionTree(result.Vertexes); while (result.Edges.Count < result.Vertexes.Count - 1) { var from = random.Next(vertexes); var to = random.Next(vertexes); if (from == to || disjointSet.Find(from) == disjointSet.Find(to)) { continue; } result.Edges.Add(new Edge <int>(from, to, 0)); disjointSet.Union(from, to); } return(result); }
public static Graph Kruskal(Graph inputGraph) { var result = new Graph(); result.Vertexes = inputGraph.Vertexes; var edges = inputGraph.Edges.ToList(); edges.Sort(new WeightComparer <int>()); var disjointSetUnion = new DisjointSetUnionTree(inputGraph.Vertexes); foreach (var edge in edges) { var fromRoot = disjointSetUnion.Find(edge.From); var toRoot = disjointSetUnion.Find(edge.To); if (fromRoot != toRoot) { disjointSetUnion.Union(fromRoot, toRoot); result.Edges.Add(edge); } } return(result); }
public static Graph Boruvka(Graph graph) { var result = new Graph(); result.Vertexes = graph.Vertexes; var disjointForest = new DisjointSetUnionTree(result.Vertexes); while (result.Edges.Count < result.Vertexes.Count - 1) { var safe = new Dictionary <int, Edge <int> >(); foreach (var edge in graph.Edges) { var fromRoot = disjointForest.Find(edge.From); var toRoot = disjointForest.Find(edge.To); if (fromRoot != toRoot) { if (!safe.ContainsKey(fromRoot) || edge.Weight < safe[fromRoot].Weight) { safe[fromRoot] = edge; } if (!safe.ContainsKey(toRoot) || edge.Weight < safe[toRoot].Weight) { safe[toRoot] = edge; } } } result.Edges.AddRange(safe.Values.Distinct()); foreach (var edge in safe.Values) { disjointForest.Union(edge.From, edge.To); } } return(result); }
public static Graph Kruskal(Graph inputGraph) { var result = new Graph(); result.Vertexes = inputGraph.Vertexes; var edges = inputGraph.Edges.ToList(); edges.Sort(new WeightComparer<int>()); var disjointSetUnion = new DisjointSetUnionTree(inputGraph.Vertexes); foreach (var edge in edges) { var fromRoot = disjointSetUnion.Find(edge.From); var toRoot = disjointSetUnion.Find(edge.To); if (fromRoot != toRoot) { disjointSetUnion.Union(fromRoot, toRoot); result.Edges.Add(edge); } } return result; }
public static Graph GenerateTree(int vertexes) { var result = new Graph(); for (var i = 0; i < vertexes; i++) { result.Vertexes.Add(i); } var disjointSet = new DisjointSetUnionTree(result.Vertexes); while (result.Edges.Count < result.Vertexes.Count - 1) { var from = random.Next(vertexes); var to = random.Next(vertexes); if (from == to || disjointSet.Find(from) == disjointSet.Find(to)) continue; result.Edges.Add(new Edge<int>(from, to, 0)); disjointSet.Union(from, to); } return result; }
public void DisjointSetUnionTreeTest() { var disjointSetUnionArray = new DisjointSetUnionTree(new[] {4, 3, 2, 1, 0}); TestAgainstComplexInput(disjointSetUnionArray); }
public void DisjointSetUnionTreeTest() { var disjointSetUnionArray = new DisjointSetUnionTree(new[] { 4, 3, 2, 1, 0 }); TestAgainstComplexInput(disjointSetUnionArray); }