GroupByVertexAttributeOther ( Microsoft.Office.Interop.Excel.Workbook workbook, String attributeColumnName ) { Debug.Assert(workbook != null); Debug.Assert(!String.IsNullOrEmpty(attributeColumnName)); IGraph oGraph = ReadWorkbook(workbook); // The key is a unique attribute value from the specified column, and // the value is a list of the vertices that have that attribute value. Dictionary <String, LinkedList <IVertex> > oGroupDictionary = new Dictionary <String, LinkedList <IVertex> >(); foreach (IVertex oVertex in oGraph.Vertices) { Object oAttributeValue; if (!oVertex.TryGetValue(attributeColumnName, typeof(String), out oAttributeValue)) { continue; } String sAttributeValue = (String)oAttributeValue; LinkedList <IVertex> oVerticesInGroup; if (!oGroupDictionary.TryGetValue(sAttributeValue, out oVerticesInGroup)) { oVerticesInGroup = new LinkedList <IVertex>(); oGroupDictionary.Add(sAttributeValue, oVerticesInGroup); } oVerticesInGroup.AddLast(oVertex); } // Convert the collection of groups to an array of GraphMetricColumn // objects, then write the array to the workbook. GraphMetricColumn [] aoGraphMetricColumns = GroupsToGraphMetricColumnsConverter.Convert <LinkedList <IVertex> > (oGroupDictionary.Values, (oGroup) => oGroup); WriteGraphMetricsToWorkbook(workbook, aoGraphMetricColumns); }
TryCalculateGraphMetrics ( IGraph graph, CalculateGraphMetricsContext calculateGraphMetricsContext, out GraphMetricColumn [] graphMetricColumns ) { Debug.Assert(graph != null); Debug.Assert(calculateGraphMetricsContext != null); AssertValid(); graphMetricColumns = null; // Partition the graph into clusters using the ClusterCalculator class // in the Algorithms namespace, which knows nothing about Excel. ICollection <Community> oCommunities; Algorithms.ClusterCalculator oClusterCalculator = new Algorithms.ClusterCalculator(); oClusterCalculator.Algorithm = m_eAlgorithm; if (!oClusterCalculator.TryCalculateGraphMetrics(graph, calculateGraphMetricsContext.BackgroundWorker, out oCommunities)) { // The user cancelled. return(false); } // Convert the collection of communities to an array of // GraphMetricColumn objects. graphMetricColumns = GroupsToGraphMetricColumnsConverter.Convert <Community>( oCommunities, (oCommunity) => oCommunity.Vertices ); return(true); }
TryCalculateGraphMetrics ( IGraph graph, CalculateGraphMetricsContext calculateGraphMetricsContext, out GraphMetricColumn [] graphMetricColumns ) { Debug.Assert(graph != null); Debug.Assert(calculateGraphMetricsContext != null); AssertValid(); graphMetricColumns = null; // Partition the graph into strongly connected components using the // ConnectedComponentCalculator class in the Algorithms namespace, // which knows nothing about Excel. // // Note that ConnectedComponentCalculator does its work synchronously. Algorithms.ConnectedComponentCalculator oConnectedComponentCalculator = new Algorithms.ConnectedComponentCalculator(); IList <LinkedList <IVertex> > oComponents = oConnectedComponentCalculator.CalculateStronglyConnectedComponents( graph, false); // Convert the collection of components to an array of // GraphMetricColumn objects. graphMetricColumns = GroupsToGraphMetricColumnsConverter.Convert <LinkedList <IVertex> >( oComponents, (oComponent) => oComponent ); return(true); }
GroupByVertexAttributeGeneric <T> ( Microsoft.Office.Interop.Excel.Workbook oWorkbook, String sAttributeColumnName, IEnumerable <T> oMinimumValues, T oMinimumValueForType, StringToTypeConverter <T> oStringToTypeConverter, TypeAdjuster <T> oTypeAdjuster ) where T : IComparable <T> { Debug.Assert(oWorkbook != null); Debug.Assert(!String.IsNullOrEmpty(sAttributeColumnName)); Debug.Assert(oMinimumValues != null); IGraph oGraph = ReadWorkbook(oWorkbook); List <T> oMinimumValueList = new List <T>(oMinimumValues); // Add a group that will hold vertices whose values are less than the // first minimum value in oMinimumValueList. That way, every vertex // will end up in a group. oMinimumValueList.Insert(0, oMinimumValueForType); // For each group whose minimum value is stored in oMinimumValueList, // there is one LinkedList of vertices in oGroupList. Int32 iGroups = oMinimumValueList.Count; LinkedList <IVertex> [] oGroupList = new LinkedList <IVertex> [iGroups]; for (Int32 i = 0; i < iGroups; i++) { oMinimumValueList[i] = oTypeAdjuster(oMinimumValueList[i]); oGroupList[i] = new LinkedList <IVertex>(); } foreach (IVertex oVertex in oGraph.Vertices) { Object oAttributeValue; T tAttributeValue; if ( !oVertex.TryGetValue(sAttributeColumnName, typeof(String), out oAttributeValue) || !oStringToTypeConverter((String)oAttributeValue, out tAttributeValue) ) { continue; } tAttributeValue = oTypeAdjuster(tAttributeValue); // (This search technique is simple but slow. It should be // replaced with a binary search.) for (Int32 i = iGroups - 1; i >= 0; i--) { if (tAttributeValue.CompareTo(oMinimumValueList[i]) >= 0) { oGroupList[i].AddLast(oVertex); break; } } } // Convert the list of groups to an array of GraphMetricColumn objects, // then write the array to the workbook. GraphMetricColumn [] aoGraphMetricColumns = GroupsToGraphMetricColumnsConverter.Convert <LinkedList <IVertex> > (oGroupList, (oGroup) => oGroup); WriteGraphMetricsToWorkbook(oWorkbook, aoGraphMetricColumns); }