/// <summary> /// Divides the supplied graph to its connected subgraphs. /// </summary> public static IGraph[] Split(IGraph initial, IGraphBuilder builder) { // A list with the nodes awaiting to be processed NodeCollection nodes = initial.Nodes.Clone() as NodeCollection; // A list with the subgraphs IGraph subgraph = null; ArrayList subgraphs = new ArrayList(); while (nodes.Count > 0) { INode node = nodes[0]; nodes.RemoveAt(0); subgraph = builder.Create(); subgraph.Nodes.Add(node); int i = 0; while (i < subgraph.Nodes.Count) { node = subgraph.Nodes[i]; foreach (ILink link in node.InLinks) { if (!subgraph.Nodes.Contains(link.Origin)) { subgraph.Nodes.Add(link.Origin); nodes.Remove(link.Origin); } if (!subgraph.Links.Contains(link)) subgraph.Links.Add(link); } foreach (ILink link in node.OutLinks) { if (!subgraph.Nodes.Contains(link.Destination)) { subgraph.Nodes.Add(link.Destination); nodes.Remove(link.Destination); } if (!subgraph.Links.Contains(link)) subgraph.Links.Add(link); } i++; } subgraphs.Add(subgraph); } IGraph[] result = new IGraph[subgraphs.Count]; for (int i = 0; i < subgraphs.Count; i++) result[i] = subgraphs[i] as IGraph; return result; }
/// <summary> /// Divides the supplied graph to its connected subgraphs. /// </summary> public static IGraph[] Split(IGraph initial, IGraphBuilder builder) { // A list with the nodes awaiting to be processed NodeCollection nodes = initial.Nodes.Clone() as NodeCollection; // A list with the subgraphs IGraph subgraph = null; ArrayList subgraphs = new ArrayList(); while (nodes.Count > 0) { INode node = nodes[0]; nodes.RemoveAt(0); subgraph = builder.Create(); subgraph.Nodes.Add(node); int i = 0; while (i < subgraph.Nodes.Count) { node = subgraph.Nodes[i]; foreach (ILink link in node.InLinks) { if (!subgraph.Nodes.Contains(link.Origin)) { subgraph.Nodes.Add(link.Origin); nodes.Remove(link.Origin); } if (!subgraph.Links.Contains(link)) { subgraph.Links.Add(link); } } foreach (ILink link in node.OutLinks) { if (!subgraph.Nodes.Contains(link.Destination)) { subgraph.Nodes.Add(link.Destination); nodes.Remove(link.Destination); } if (!subgraph.Links.Contains(link)) { subgraph.Links.Add(link); } } i++; } subgraphs.Add(subgraph); } IGraph[] result = new IGraph[subgraphs.Count]; for (int i = 0; i < subgraphs.Count; i++) { result[i] = subgraphs[i] as IGraph; } return(result); }