public List <BidirectionalGraph <string, Edge <string> > > GetDatabaseConnectedComponents(BidirectionalGraph <string, Edge <string> > graph) { IncrementalConnectedComponentsAlgorithm <string, Edge <string> > a = new IncrementalConnectedComponentsAlgorithm <string, Edge <string> >(graph as IMutableVertexAndEdgeSet <string, Edge <string> >); a.Compute(); KeyValuePair <int, IDictionary <string, int> > components = a.GetComponents(); List <BidirectionalGraph <string, Edge <string> > > connectedComponents = new List <BidirectionalGraph <string, Edge <string> > >(components.Key); var grouped = components.Value.GroupBy(t => t.Value); foreach (var group in grouped) { BidirectionalGraph <string, Edge <string> > g = new BidirectionalGraph <string, Edge <string> >(true, group.Count()); foreach (var item in group) { g.AddVertex(item.Key); } foreach (var item in g.Vertices) { g.AddEdgeRange(graph.OutEdges(item)); } connectedComponents.Add(g); } return(connectedComponents); }
public static KeyValuePair <int, IDictionary <int, int> > Get(UndirectedGraph <int, Edge <int> > g) { var algorithm = new IncrementalConnectedComponentsAlgorithm <int, Edge <int> >(g); algorithm.Compute(); return(algorithm.GetComponents()); }
public void InvalidUse() { var graph = new AdjacencyGraph <int, Edge <int> >(); var algorithm = new IncrementalConnectedComponentsAlgorithm <int, Edge <int> >(graph); Assert.Throws <InvalidOperationException>(() => { var _ = algorithm.ComponentCount; }); Assert.Throws <InvalidOperationException>(() => algorithm.GetComponents()); }
public void Constructor() { var graph = new AdjacencyGraph <int, Edge <int> >(); var algorithm = new IncrementalConnectedComponentsAlgorithm <int, Edge <int> >(graph); AssertAlgorithmState(algorithm, graph); algorithm = new IncrementalConnectedComponentsAlgorithm <int, Edge <int> >(null, graph); AssertAlgorithmState(algorithm, graph); }
public void Dispose() { var graph = new AdjacencyGraph <int, Edge <int> >(); var algorithm = new IncrementalConnectedComponentsAlgorithm <int, Edge <int> >(graph); Assert.DoesNotThrow(() => algorithm.Dispose()); algorithm = new IncrementalConnectedComponentsAlgorithm <int, Edge <int> >(graph); algorithm.Compute(); Assert.DoesNotThrow(() => algorithm.Dispose()); }
/// <summary> /// Computes the incremental connected components for a growing graph (edge added only). /// Each call to the delegate re-computes the component dictionary. The returned dictionary /// is shared accross multiple calls of the method. /// </summary> /// <typeparam name="TVertex">type of the vertices</typeparam> /// <typeparam name="TEdge">type of the edges</typeparam> /// <param name="g"></param> /// <returns></returns> public static Func <KeyValuePair <int, IDictionary <TVertex, int> > > IncrementalConnectedComponents <TVertex, TEdge>( IMutableVertexAndEdgeSet <TVertex, TEdge> g) where TEdge : IEdge <TVertex> { Contract.Requires(g != null); var incrementalComponents = new IncrementalConnectedComponentsAlgorithm <TVertex, TEdge>(g); incrementalComponents.Compute(); return(() => incrementalComponents.GetComponents()); }
public void IncrementalConnectedComponent() { var graph = new AdjacencyGraph <int, Edge <int> >(); graph.AddVertexRange(new[] { 0, 1, 2, 3 }); var algorithm = new IncrementalConnectedComponentsAlgorithm <int, Edge <int> >(graph); algorithm.Compute(); Assert.AreEqual(4, algorithm.ComponentCount); Assert.AreEqual(4, algorithm.GetComponents().Key); CollectionAssert.AreEquivalent( new Dictionary <int, int> { [0] = 0, [1] = 1, [2] = 2, [3] = 3 }, algorithm.GetComponents().Value); graph.AddEdge(new Edge <int>(0, 1)); Assert.AreEqual(3, algorithm.ComponentCount); Assert.AreEqual(3, algorithm.GetComponents().Key); CollectionAssert.AreEquivalent( new Dictionary <int, int> { [0] = 0, [1] = 0, [2] = 1, [3] = 2 }, algorithm.GetComponents().Value); graph.AddEdge(new Edge <int>(2, 3)); Assert.AreEqual(2, algorithm.ComponentCount); Assert.AreEqual(2, algorithm.GetComponents().Key); CollectionAssert.AreEquivalent( new Dictionary <int, int> { [0] = 0, [1] = 0, [2] = 1, [3] = 1 }, algorithm.GetComponents().Value); graph.AddEdge(new Edge <int>(1, 3)); Assert.AreEqual(1, algorithm.ComponentCount); Assert.AreEqual(1, algorithm.GetComponents().Key); CollectionAssert.AreEquivalent( new Dictionary <int, int> { [0] = 0, [1] = 0, [2] = 0, [3] = 0 }, algorithm.GetComponents().Value); graph.AddVerticesAndEdge(new Edge <int>(4, 5)); Assert.AreEqual(2, algorithm.ComponentCount); Assert.AreEqual(2, algorithm.GetComponents().Key); CollectionAssert.AreEquivalent( new Dictionary <int, int> { [0] = 0, [1] = 0, [2] = 0, [3] = 0, [4] = 1, [5] = 1 }, algorithm.GetComponents().Value); graph.AddVertex(6); Assert.AreEqual(3, algorithm.ComponentCount); Assert.AreEqual(3, algorithm.GetComponents().Key); CollectionAssert.AreEquivalent( new Dictionary <int, int> { [0] = 0, [1] = 0, [2] = 0, [3] = 0, [4] = 1, [5] = 1, [6] = 2 }, algorithm.GetComponents().Value); }
public static QuickGraph.UndirectedGraph <string, Edge <string> > DatabaseToGraph(string dbFileName) { Dictionary <string, string> irpan = new Dictionary <string, string>(); if (false) { string[] lines = System.IO.File.ReadAllLines(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "new_CnUy2007.txt")); foreach (var item in lines) { string c = item.Split('\t')[0]; string u = item.Split('\t')[1]; if (irpan.ContainsKey(c)) { continue; } irpan.Add(c, u); } } char[] spliter = new char[] { ',' }; DictBase dictBase = new DictBase(); Dictionary <string, int> cWordDict = new Dictionary <string, int>(); Dictionary <string, int> uWordDict = new Dictionary <string, int>(); Dictionary <string, int> kWordDict = new Dictionary <string, int>(); Dictionary <int, string> ccWordDict = new Dictionary <int, string>(); Dictionary <int, string> uuWordDict = new Dictionary <int, string>(); Dictionary <int, string> kkWordDict = new Dictionary <int, string>(); QuickGraph.UndirectedGraph <string, Edge <string> > graph = new UndirectedGraph <string, Edge <string> >(false); zuk_dbSQLDataSet.zuk_fixedDataTable zukTable = new zuk_dbSQLDataSet.zuk_fixedDataTable(); if (dbFileName.IndexOf("\\") > -1) { zukTable.ReadXml(dbFileName); } else { zukTable.ReadXml(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, dbFileName)); } foreach (zuk_dbSQLDataSet.zuk_fixedRow row in zukTable) { string strChinese = row.Zh.Trim(); string[] strUyghurs;// = row.Ug.Split(spliter, StringSplitOptions.RemoveEmptyEntries); if (irpan.ContainsKey(strChinese)) { strUyghurs = irpan[strChinese].Split(spliter, StringSplitOptions.RemoveEmptyEntries); } else { strUyghurs = row.Ug.Split(spliter, StringSplitOptions.RemoveEmptyEntries); } string[] strKazaks = row.Kz.Split(spliter, StringSplitOptions.RemoveEmptyEntries); //trim for (int i = 0; i < strUyghurs.Length; i++) { strUyghurs[i] = strUyghurs[i].Trim(); } for (int i = 0; i < strKazaks.Length; i++) { strKazaks[i] = strKazaks[i].Trim(); } //add to db int cID = cWordDict.Count; cWordDict.Add(strChinese, cID); ccWordDict.Add(cID, strChinese); graph.AddVertex("c" + cID); //u for (int i = 0; i < strUyghurs.Length; i++) { if (!uWordDict.ContainsKey(strUyghurs[i])) { int uID = uWordDict.Count; uWordDict.Add(strUyghurs[i], uID); uuWordDict.Add(uID, strUyghurs[i]); graph.AddVertex("u" + uID); graph.AddEdge(new Edge <string>("c" + cID.ToString(), "u" + uID.ToString())); } else { graph.AddEdge(new Edge <string>("c" + cID, "u" + uWordDict[strUyghurs[i]])); } } //k for (int i = 0; i < strKazaks.Length; i++) { if (!kWordDict.ContainsKey(strKazaks[i])) { int kID = kWordDict.Count; kWordDict.Add(strKazaks[i], kID); kkWordDict.Add(kID, strKazaks[i]); graph.AddVertex("k" + kID); graph.AddEdge(new Edge <string>("c" + cID.ToString(), "k" + kID.ToString())); } graph.AddEdge(new Edge <string>("c" + cID, "k" + kWordDict[strKazaks[i]])); } } var maxU = graph.Vertices.Where <string>(t => t.StartsWith("u")).OrderByDescending(t => graph.AdjacentEdges(t).Count()); var maxU2 = maxU.ToDictionary(t => uuWordDict[int.Parse(t.TrimStart('u'))], t => graph.AdjacentEdges(t).Count()); var maxK = graph.Vertices.Where <string>(t => t.StartsWith("k")).OrderByDescending(t => graph.AdjacentEdges(t).Count()); var maxK2 = maxK.ToDictionary(t => kkWordDict[int.Parse(t.TrimStart('k'))], t => graph.AdjacentEdges(t).Count()); //Test foreach (var item in maxU2) { if (item.Value > 1) { continue; } graph.RemoveVertex("u" + uWordDict[item.Key]); } foreach (var item in maxK2) { if (item.Value > 1) { continue; } graph.RemoveVertex("k" + kWordDict[item.Key]); } IncrementalConnectedComponentsAlgorithm <string, Edge <string> > a = new IncrementalConnectedComponentsAlgorithm <string, Edge <string> >(graph as IMutableVertexAndEdgeSet <string, Edge <string> >); a.Compute(); KeyValuePair <int, IDictionary <string, int> > components = a.GetComponents(); List <BidirectionalGraph <string, Edge <string> > > connectedComponents = new List <BidirectionalGraph <string, Edge <string> > >(components.Key); var grouped = components.Value.GroupBy(t => t.Value); foreach (var group in grouped) { BidirectionalGraph <string, Edge <string> > g = new BidirectionalGraph <string, Edge <string> >(true, group.Count()); foreach (var item in group) { g.AddVertex(item.Key); } foreach (var item in g.Vertices) { g.AddEdgeRange(graph.AdjacentEdges(item)); } connectedComponents.Add(g); } var connectedComponentsSorted = connectedComponents.OrderByDescending(t => t.VertexCount).ToList(); return(graph); }
public List <BidirectionalGraph <Word, Edge <Word> > > GetDatabaseConnectedComponents() { BidirectionalGraph <Word, Edge <Word> > graph = new BidirectionalGraph <Word, Edge <Word> >(true); //C vertexs foreach (var item in DBHelper._DictBase.CUDictbase.CtoU) { graph.AddVertex(DBHelper._DictBase.GetCWordByID(item.Key)); } //U vertexs foreach (var item in DBHelper._DictBase.CUDictbase.UtoC) { graph.AddVertex(DBHelper._DictBase.GetUWordByID(item.Key)); } //K vertexs foreach (var item in DBHelper._DictBase.CKDictbase.KtoC) { graph.AddVertex(DBHelper._DictBase.GetKWordByID(item.Key)); } //add C to U edges foreach (var item in DBHelper._DictBase.CUDictbase.CtoU) { foreach (var item2 in item.Value) { graph.AddEdge(new Edge <Word>(DBHelper._DictBase.GetCWordByID(item.Key), DBHelper._DictBase.GetUWordByID(item2))); } } //add C to K edges foreach (var item in DBHelper._DictBase.CKDictbase.CtoK) { foreach (var item2 in item.Value) { graph.AddEdge(new Edge <Word>(DBHelper._DictBase.GetCWordByID(item.Key), DBHelper._DictBase.GetKWordByID(item2))); } } if (WordRelaionGraph == null) { WordRelaionGraph = new BidirectionalGraph <Word, Edge <Word> >(true); foreach (var item in graph.Vertices) { WordRelaionGraph.AddVertex(item); } foreach (var item in graph.Edges) { WordRelaionGraph.AddEdge(item); } } IncrementalConnectedComponentsAlgorithm <Word, Edge <Word> > a = new IncrementalConnectedComponentsAlgorithm <Word, Edge <Word> >(graph as IMutableVertexAndEdgeSet <Word, Edge <Word> >); a.Compute(); KeyValuePair <int, IDictionary <Word, int> > components = a.GetComponents(); List <BidirectionalGraph <Word, Edge <Word> > > connectedComponents = new List <BidirectionalGraph <Word, Edge <Word> > >(components.Key); var grouped = components.Value.GroupBy(t => t.Value); foreach (var group in grouped) { BidirectionalGraph <Word, Edge <Word> > g = new BidirectionalGraph <Word, Edge <Word> >(true, group.Count()); foreach (var item in group) { g.AddVertex(item.Key); } foreach (var item in g.Vertices) { g.AddEdgeRange(graph.OutEdges(item)); } connectedComponents.Add(g); } return(connectedComponents); }