public static IBidirectionalGraph<object, IEdge<object>> AdjacentyMatrixToGraph(MatrixWithHeaders matrixWithHeaders) { var graph = new BidirectionalGraph<object, IEdge<object>>(); var headers = matrixWithHeaders.Headers; var matrix = matrixWithHeaders.Matrix; foreach (var item in headers) { graph.AddVertex(item.Value); } if (headers.Count > 1) { for (int y = 0; y < matrix.GetLength(1); y++) { for (int x = 0; x < matrix.GetLength(0); x++) { if (matrix[x, y] == 1) { graph.AddEdge(new Edge<object>(headers[x], headers[y])); } } } } else { graph.AddEdge(new Edge<object>(headers[0], headers[0])); } return graph; }
private static MatrixWithHeaders ConnectElements(MatrixWithHeaders matrixWithHeaders, List <List <string> > elements) { var matrix = matrixWithHeaders.Matrix; var headers = matrixWithHeaders.Headers; for (int x = elements.Count - 1; x >= 0; x--) { if (elements[x].Count > 0) { List <int> list = new List <int>(); foreach (var item in elements[x]) { foreach (var subitem in headers.Values) { if (subitem.Contains(item) == true) { list.Add(headers.GetFirstKeyByValue(subitem)); } } } matrixWithHeaders = MergeMatrixWithHeaders(matrixWithHeaders, list); } } return(matrixWithHeaders); }
public static MatrixWithHeaders CreateRelationMatrix(Dictionary <int, string> strings, KeyValuePair <List <int>, string> group) { var words = Extensions.GetSplittedWords(group.Value); int uniqueCount = words.Distinct().Count(); MatrixWithHeaders matrixWithHeaders = new MatrixWithHeaders(new int[uniqueCount, uniqueCount], new Dictionary <int, string>()); var headers = matrixWithHeaders.Headers; var matrix = matrixWithHeaders.Matrix; foreach (var item in words) { headers.Add(headers.Count, words[headers.Count]); } if (words.Count > 1) { foreach (var item in group.Key) { var strWords = Extensions.GetSplittedWords(strings[item]); for (int x = 1; x < strWords.Count; x++) { matrix[headers.GetFirstKeyByValue(strWords[x - 1]), headers.GetFirstKeyByValue(strWords[x])] = 1; } } } else { matrix[headers.GetFirstKeyByValue(words[0]), headers.GetFirstKeyByValue(words[0])] = 1; } return(new MatrixWithHeaders(matrix, headers)); }
private static List <List <string> > GetSinks(MatrixWithHeaders matrixWithHeaders) { IVertexListGraph <object, IEdge <object> > graph = Graph.AdjacentyMatrixToGraph(matrixWithHeaders); List <List <string> > sinks = new List <List <string> >(); foreach (var item in graph.Sinks()) { sinks.Add(new List <string>() { item.ToString() }); } return(sinks); }
private static MatrixWithHeaders MergeMatrixWithHeaders(MatrixWithHeaders matrixWithHeaders, List <int> elements) { var headers = matrixWithHeaders.Headers; int priamryElement = elements[0]; elements.Sort(); elements.RemoveAt(0); int counter = 0; foreach (var item in elements) { var matrix = matrixWithHeaders.Matrix; int index = item - counter; for (int y = matrix.GetLength(1) - 1; y >= 0; y--) { if (matrix[index, y] == 1) { matrix[priamryElement, y] = 1; } } for (int x = matrix.GetLength(0) - 1; x >= 0; x--) { if (matrix[x, index] == 1) { matrix[x, priamryElement] = 1; } } headers[priamryElement] += " " + headers[index]; matrixWithHeaders = matrixWithHeaders.TrimMatrixWithHeaders(index); counter++; } for (int x = 0; x < matrixWithHeaders.Matrix.GetLength(0); x++) { matrixWithHeaders.Matrix[x, x] = 0; } return(matrixWithHeaders); }
private static List <List <string> > GetCycle(MatrixWithHeaders matrixWithHeaders) { IVertexListGraph <object, IEdge <object> > graph = Graph.AdjacentyMatrixToGraph(matrixWithHeaders); List <List <object> > candidates = new List <List <object> >(); List <List <string> > outlines = new List <List <string> >(); var stronglyConnectedDict = (IDictionary <object, int>)(new Dictionary <object, int>()); var z = graph.StronglyConnectedComponents(out stronglyConnectedDict); for (int x = 0; x < z; x++) { candidates.Add(new List <object>()); outlines.Add(new List <string>()); } foreach (var item in stronglyConnectedDict) { candidates[item.Value].Add(item.Key); } int counter = 0; foreach (var item in candidates) { if (item.Count > 1) { foreach (var subitem in item) { outlines[counter].Add(subitem.ToString()); } counter++; } } return(outlines); }
public static MatrixWithHeaders TrimMatrixWithHeaders(this MatrixWithHeaders matrixWithHeaders, int index) { matrixWithHeaders.Matrix = matrixWithHeaders.Matrix.TrimArray(index, index); var tempDictionary = new Dictionary <int, string>(); for (int x = index + 1; x < matrixWithHeaders.Headers.Count; x++) { tempDictionary.Add(x - 1, matrixWithHeaders.Headers[x]); } for (int x = matrixWithHeaders.Headers.Count - 1; x >= index; x--) { matrixWithHeaders.Headers.Remove(x); } for (int x = 0; x < tempDictionary.Count; x++) { matrixWithHeaders.Headers.Add(matrixWithHeaders.Headers.Count, tempDictionary[matrixWithHeaders.Headers.Count]); } return(matrixWithHeaders); }
public static MatrixWithHeaders CreateRelationMatrix(Dictionary<int, string> strings, KeyValuePair<List<int>, string> group) { var words = Extensions.GetSplittedWords(group.Value); int uniqueCount = words.Distinct().Count(); MatrixWithHeaders matrixWithHeaders = new MatrixWithHeaders(new int[uniqueCount, uniqueCount], new Dictionary<int, string>()); var headers = matrixWithHeaders.Headers; var matrix = matrixWithHeaders.Matrix; foreach (var item in words) { headers.Add(headers.Count, words[headers.Count]); } if (words.Count > 1) { foreach (var item in group.Key) { var strWords = Extensions.GetSplittedWords(strings[item]); for (int x = 1; x < strWords.Count; x++) { matrix[headers.GetFirstKeyByValue(strWords[x - 1]), headers.GetFirstKeyByValue(strWords[x])] = 1; } } } else { matrix[headers.GetFirstKeyByValue(words[0]), headers.GetFirstKeyByValue(words[0])] = 1; } return new MatrixWithHeaders(matrix, headers); }
private static MatrixWithHeaders MergeMatrixWithHeaders(MatrixWithHeaders matrixWithHeaders, List<int> elements) { var headers = matrixWithHeaders.Headers; int priamryElement = elements[0]; elements.Sort(); elements.RemoveAt(0); int counter = 0; foreach (var item in elements) { var matrix = matrixWithHeaders.Matrix; int index = item - counter; for (int y = matrix.GetLength(1) - 1; y >= 0; y--) { if (matrix[index, y] == 1) { matrix[priamryElement, y] = 1; } } for (int x = matrix.GetLength(0) - 1; x >= 0; x--) { if (matrix[x, index] == 1) { matrix[x, priamryElement] = 1; } } headers[priamryElement] += " " + headers[index]; matrixWithHeaders = matrixWithHeaders.TrimMatrixWithHeaders(index); counter++; } for (int x = 0; x < matrixWithHeaders.Matrix.GetLength(0); x++) { matrixWithHeaders.Matrix[x, x] = 0; } return matrixWithHeaders; }
private static MatrixWithHeaders ConnectElements(MatrixWithHeaders matrixWithHeaders, List<List<string>> elements) { var matrix = matrixWithHeaders.Matrix; var headers = matrixWithHeaders.Headers; for (int x = elements.Count - 1; x >= 0; x--) { if (elements[x].Count > 0) { List<int> list = new List<int>(); foreach (var item in elements[x]) { foreach (var subitem in headers.Values) { if (subitem.Contains(item) == true) { list.Add(headers.GetFirstKeyByValue(subitem)); } } } matrixWithHeaders = MergeMatrixWithHeaders(matrixWithHeaders, list); } } return matrixWithHeaders; }
private static List<List<string>> GetStraight(MatrixWithHeaders matrixWithHeaders) { Dictionary<object, IEnumerable<IEdge<object>>> vertexEdges = new Dictionary<object, IEnumerable<IEdge<object>>>(); BidirectionalGraph<object, IEdge<object>> graph = Graph.AdjacentyMatrixToGraph(matrixWithHeaders) as BidirectionalGraph<object, IEdge<object>>; List<List<string>> straight = new List<List<string>>(); EdgeList<string, Edge<string>> candidates = new EdgeList<string, Edge<string>>(); var matrix = matrixWithHeaders.Matrix; var headers = matrixWithHeaders.Headers; var vertexes = graph.Vertices; foreach (var item in vertexes) { var inEdges = graph.InEdges(item); var outEdges = graph.OutEdges(item); if (inEdges.Count() == 1 && outEdges.Count() == 1) { candidates.Add(new Edge<string>(inEdges.ElementAt(0).Source.ToString(), inEdges.ElementAt(0).Target.ToString())); candidates.Add(new Edge<string>(outEdges.ElementAt(0).Source.ToString(), outEdges.ElementAt(0).Target.ToString())); } } for (int x = candidates.Count() - 1; x > 0; x--) { if (candidates[x - 1].Source == candidates[x].Source && candidates[x - 1].Target == candidates[x].Target) { candidates.RemoveAt(x); } } for (int x = 0; x < candidates.Count; x++) { for (int y = x + 1; y < candidates.Count; y++) { IEdge<object> edge = null; graph.TryGetEdge(candidates[x].Source, candidates[y].Target, out edge); if (edge != null) { var existItems = candidates.Select(z => z.Source == edge.Source.ToString() && z.Target == edge.Target.ToString()).ToList(); bool exist = false; foreach (var item in existItems) { exist = exist || item; } if (exist == false) { List<string> tempList = new List<string>(); for (int z = x; z <= y; z++) { if (tempList.Contains(candidates[z].Source) == false) { tempList.Add(candidates[z].Source); } if (tempList.Contains(candidates[z].Target) == false) { tempList.Add(candidates[z].Target); } } straight.Add(tempList); } } } } return straight; }
private static List<List<string>> GetCycle(MatrixWithHeaders matrixWithHeaders) { IVertexListGraph<object, IEdge<object>> graph = Graph.AdjacentyMatrixToGraph(matrixWithHeaders); List<List<object>> candidates = new List<List<object>>(); List<List<string>> outlines = new List<List<string>>(); var stronglyConnectedDict = (IDictionary<object, int>)(new Dictionary<object, int>()); var z = graph.StronglyConnectedComponents(out stronglyConnectedDict); for (int x = 0; x < z; x++) { candidates.Add(new List<object>()); outlines.Add(new List<string>()); } foreach (var item in stronglyConnectedDict) { candidates[item.Value].Add(item.Key); } int counter = 0; foreach (var item in candidates) { if (item.Count > 1) { foreach (var subitem in item) { outlines[counter].Add(subitem.ToString()); } counter++; } } return outlines; }
private static List<List<string>> GetSinks(MatrixWithHeaders matrixWithHeaders) { IVertexListGraph<object, IEdge<object>> graph = Graph.AdjacentyMatrixToGraph(matrixWithHeaders); List<List<string>> sinks = new List<List<string>>(); foreach (var item in graph.Sinks()) { sinks.Add(new List<string>() { item.ToString() }); } return sinks; }
public static IBidirectionalGraph <object, IEdge <object> > AdjacentyMatrixToGraph(MatrixWithHeaders matrixWithHeaders) { var graph = new BidirectionalGraph <object, IEdge <object> >(); var headers = matrixWithHeaders.Headers; var matrix = matrixWithHeaders.Matrix; foreach (var item in headers) { graph.AddVertex(item.Value); } if (headers.Count > 1) { for (int y = 0; y < matrix.GetLength(1); y++) { for (int x = 0; x < matrix.GetLength(0); x++) { if (matrix[x, y] == 1) { graph.AddEdge(new Edge <object>(headers[x], headers[y])); } } } } else { graph.AddEdge(new Edge <object>(headers[0], headers[0])); } return(graph); }
private static List <List <string> > GetStraight(MatrixWithHeaders matrixWithHeaders) { Dictionary <object, IEnumerable <IEdge <object> > > vertexEdges = new Dictionary <object, IEnumerable <IEdge <object> > >(); BidirectionalGraph <object, IEdge <object> > graph = Graph.AdjacentyMatrixToGraph(matrixWithHeaders) as BidirectionalGraph <object, IEdge <object> >; List <List <string> > straight = new List <List <string> >(); EdgeList <string, Edge <string> > candidates = new EdgeList <string, Edge <string> >(); var matrix = matrixWithHeaders.Matrix; var headers = matrixWithHeaders.Headers; var vertexes = graph.Vertices; foreach (var item in vertexes) { var inEdges = graph.InEdges(item); var outEdges = graph.OutEdges(item); if (inEdges.Count() == 1 && outEdges.Count() == 1) { candidates.Add(new Edge <string>(inEdges.ElementAt(0).Source.ToString(), inEdges.ElementAt(0).Target.ToString())); candidates.Add(new Edge <string>(outEdges.ElementAt(0).Source.ToString(), outEdges.ElementAt(0).Target.ToString())); } } for (int x = candidates.Count() - 1; x > 0; x--) { if (candidates[x - 1].Source == candidates[x].Source && candidates[x - 1].Target == candidates[x].Target) { candidates.RemoveAt(x); } } for (int x = 0; x < candidates.Count; x++) { for (int y = x + 1; y < candidates.Count; y++) { IEdge <object> edge = null; graph.TryGetEdge(candidates[x].Source, candidates[y].Target, out edge); if (edge != null) { var existItems = candidates.Select(z => z.Source == edge.Source.ToString() && z.Target == edge.Target.ToString()).ToList(); bool exist = false; foreach (var item in existItems) { exist = exist || item; } if (exist == false) { List <string> tempList = new List <string>(); for (int z = x; z <= y; z++) { if (tempList.Contains(candidates[z].Source) == false) { tempList.Add(candidates[z].Source); } if (tempList.Contains(candidates[z].Target) == false) { tempList.Add(candidates[z].Target); } } straight.Add(tempList); } } } } return(straight); }