Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / DataEntity / System / Data / Map / Update / Internal / UndirectedGraph.cs / 1305376 / UndirectedGraph.cs
//----------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// @owner [....]
// @backupOwner [....]
//---------------------------------------------------------------------
using System.Data.Common.Utils;
using System.Collections.Generic;
using System.Text;
namespace System.Data.Mapping.Update.Internal {
// Maintains a graph where the direction of the edges is not important
class UndirectedGraph : InternalBase {
#region Constructor
internal UndirectedGraph(IEqualityComparer comparer) {
m_graph = new Graph(comparer);
m_comparer = comparer;
}
#endregion
#region Fields
private Graph m_graph; // Directed graph where we added both edges
private IEqualityComparer m_comparer;
#endregion
#region Properties
internal IEnumerable Vertices {
get { return m_graph.Vertices; }
}
///
/// Returns the edges of the graph
///
internal IEnumerable> Edges {
get {
return m_graph.Edges;
}
}
#endregion
#region Methods
// effects: Adds a new node to the graph. Does nothing if the vertex already exists.
internal void AddVertex(TVertex vertex) {
m_graph.AddVertex(vertex);
}
// requires: first and second must exist. An edge between first and
// second must not already exist
// effects: Adds a new unidirectional edge to the graph.
internal void AddEdge(TVertex first, TVertex second) {
m_graph.AddEdge(first, second);
m_graph.AddEdge(second, first);
}
// effects: Given a graph of T, returns a map such that nodes in the
// same connected component are in the same list in the KeyToListMap
internal KeyToListMap GenerateConnectedComponents() {
int count = 0;
// Set the "component number" for each node
Dictionary componentMap = new Dictionary(m_comparer);
foreach (TVertex vertex in Vertices) {
componentMap.Add(vertex, new ComponentNum(count));
count++;
}
// Run the connected components algorithm (Page 441 of the CLR -- Cormen, Rivest, Lieserson)
foreach (KeyValuePair edge in Edges) {
if (componentMap[edge.Key].componentNum != componentMap[edge.Value].componentNum) {
// Set the component numbers of both of the nodes to be the same
int oldValue = componentMap[edge.Value].componentNum;
int newValue = componentMap[edge.Key].componentNum;
componentMap[edge.Value].componentNum = newValue;
// Since we are resetting edge.Value's component number, find all components whose value
// is oldValue and reset it to the new value
foreach (TVertex vertex in componentMap.Keys) {
if (componentMap[vertex].componentNum == oldValue) {
componentMap[vertex].componentNum = newValue;
}
}
}
}
// Now just grab the vertices which have the same set numbers
KeyToListMap result = new KeyToListMap(EqualityComparer.Default);
foreach (TVertex vertex in Vertices) {
int componentNum = componentMap[vertex].componentNum;
result.Add(componentNum, vertex);
}
return result;
}
internal override void ToCompactString(StringBuilder builder) {
builder.Append(m_graph.ToString());
}
// A class just for ensuring that we do not modify the hash table
// while iterating over it. Keeps track of the component number for a
// connected component
private class ComponentNum {
internal ComponentNum(int compNum) {
componentNum = compNum;
}
internal int componentNum;
public override string ToString() {
return StringUtil.FormatInvariant("{0}", componentNum);
}
};
#endregion
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//----------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// @owner [....]
// @backupOwner [....]
//---------------------------------------------------------------------
using System.Data.Common.Utils;
using System.Collections.Generic;
using System.Text;
namespace System.Data.Mapping.Update.Internal {
// Maintains a graph where the direction of the edges is not important
class UndirectedGraph : InternalBase {
#region Constructor
internal UndirectedGraph(IEqualityComparer comparer) {
m_graph = new Graph(comparer);
m_comparer = comparer;
}
#endregion
#region Fields
private Graph m_graph; // Directed graph where we added both edges
private IEqualityComparer m_comparer;
#endregion
#region Properties
internal IEnumerable Vertices {
get { return m_graph.Vertices; }
}
///
/// Returns the edges of the graph
///
internal IEnumerable> Edges {
get {
return m_graph.Edges;
}
}
#endregion
#region Methods
// effects: Adds a new node to the graph. Does nothing if the vertex already exists.
internal void AddVertex(TVertex vertex) {
m_graph.AddVertex(vertex);
}
// requires: first and second must exist. An edge between first and
// second must not already exist
// effects: Adds a new unidirectional edge to the graph.
internal void AddEdge(TVertex first, TVertex second) {
m_graph.AddEdge(first, second);
m_graph.AddEdge(second, first);
}
// effects: Given a graph of T, returns a map such that nodes in the
// same connected component are in the same list in the KeyToListMap
internal KeyToListMap GenerateConnectedComponents() {
int count = 0;
// Set the "component number" for each node
Dictionary componentMap = new Dictionary(m_comparer);
foreach (TVertex vertex in Vertices) {
componentMap.Add(vertex, new ComponentNum(count));
count++;
}
// Run the connected components algorithm (Page 441 of the CLR -- Cormen, Rivest, Lieserson)
foreach (KeyValuePair edge in Edges) {
if (componentMap[edge.Key].componentNum != componentMap[edge.Value].componentNum) {
// Set the component numbers of both of the nodes to be the same
int oldValue = componentMap[edge.Value].componentNum;
int newValue = componentMap[edge.Key].componentNum;
componentMap[edge.Value].componentNum = newValue;
// Since we are resetting edge.Value's component number, find all components whose value
// is oldValue and reset it to the new value
foreach (TVertex vertex in componentMap.Keys) {
if (componentMap[vertex].componentNum == oldValue) {
componentMap[vertex].componentNum = newValue;
}
}
}
}
// Now just grab the vertices which have the same set numbers
KeyToListMap result = new KeyToListMap(EqualityComparer.Default);
foreach (TVertex vertex in Vertices) {
int componentNum = componentMap[vertex].componentNum;
result.Add(componentNum, vertex);
}
return result;
}
internal override void ToCompactString(StringBuilder builder) {
builder.Append(m_graph.ToString());
}
// A class just for ensuring that we do not modify the hash table
// while iterating over it. Keeps track of the component number for a
// connected component
private class ComponentNum {
internal ComponentNum(int compNum) {
componentNum = compNum;
}
internal int componentNum;
public override string ToString() {
return StringUtil.FormatInvariant("{0}", componentNum);
}
};
#endregion
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- TransactionManager.cs
- DashStyle.cs
- CloudCollection.cs
- followingsibling.cs
- CodeDirectionExpression.cs
- NamedPermissionSet.cs
- CommunicationObject.cs
- RequestContext.cs
- XmlIterators.cs
- DataListComponentEditor.cs
- GeometryDrawing.cs
- GlyphRunDrawing.cs
- ProviderConnectionPointCollection.cs
- Mapping.cs
- DebugView.cs
- WebPartMinimizeVerb.cs
- WebAdminConfigurationHelper.cs
- ClientSettingsSection.cs
- RubberbandSelector.cs
- DataGridViewComboBoxCell.cs
- StylusPointCollection.cs
- SamlAuthorizationDecisionStatement.cs
- DesignerEditorPartChrome.cs
- WCFModelStrings.Designer.cs
- SinglePhaseEnlistment.cs
- BitmapEffectrendercontext.cs
- BooleanStorage.cs
- XmlUrlResolver.cs
- SafeSystemMetrics.cs
- MemoryMappedFileSecurity.cs
- Executor.cs
- XmlExtensionFunction.cs
- StorageAssociationSetMapping.cs
- IteratorFilter.cs
- HealthMonitoringSection.cs
- DynamicControlParameter.cs
- PublisherMembershipCondition.cs
- CodeTryCatchFinallyStatement.cs
- ImageAttributes.cs
- EntityCommandDefinition.cs
- NavigationProperty.cs
- ListViewHitTestInfo.cs
- Random.cs
- PropertyCondition.cs
- FullTextState.cs
- ApplicationException.cs
- RectangleGeometry.cs
- MaterialCollection.cs
- BrowserTree.cs
- IgnoreSection.cs
- IItemProperties.cs
- NameSpaceExtractor.cs
- FrameworkElementFactoryMarkupObject.cs
- EraserBehavior.cs
- AvTrace.cs
- IsolatedStorageFileStream.cs
- NotImplementedException.cs
- StringExpressionSet.cs
- MethodAccessException.cs
- ThrowOnMultipleAssignment.cs
- HttpCapabilitiesEvaluator.cs
- FunctionMappingTranslator.cs
- FontResourceCache.cs
- NonSerializedAttribute.cs
- SmiSettersStream.cs
- InstanceLockQueryResult.cs
- TextParagraphView.cs
- MatchNoneMessageFilter.cs
- DataGridViewRowStateChangedEventArgs.cs
- ToolStripDropDownItem.cs
- SplitContainer.cs
- sqlstateclientmanager.cs
- TreeView.cs
- RequestQueryParser.cs
- Schema.cs
- XD.cs
- XslAstAnalyzer.cs
- CapabilitiesUse.cs
- ColorConvertedBitmap.cs
- FloaterBaseParaClient.cs
- Triangle.cs
- WebServiceFaultDesigner.cs
- NegatedConstant.cs
- IPEndPoint.cs
- ComboBoxAutomationPeer.cs
- RTLAwareMessageBox.cs
- Header.cs
- DbMetaDataFactory.cs
- MenuItemBinding.cs
- Literal.cs
- ListChunk.cs
- XmlConvert.cs
- XmlKeywords.cs
- MessageSecurityProtocol.cs
- ActivityCodeGenerator.cs
- Rule.cs
- PointKeyFrameCollection.cs
- TraceContextRecord.cs
- FontEmbeddingManager.cs
- ToolStripItemEventArgs.cs