private void Run() { Forest.Clear(); PriorityQueue <Node, TCost> priorityQueue = new PriorityQueue <Node, TCost>(); HashSet <Node> processed = new HashSet <Node>(); Dictionary <Node, Arc> parentArc = new Dictionary <Node, Arc>(); // start with one point from each component var components = new ConnectedComponents(Graph, ConnectedComponents.Flags.CreateComponents); foreach (var c in components.Components) { Node root = c.First(); processed.Add(root); foreach (var arc in Graph.Arcs(root)) { Node v = Graph.Other(arc, root); parentArc[v] = arc; priorityQueue[v] = Cost(arc); } } components = null; while (priorityQueue.Count != 0) { Node n = priorityQueue.Peek(); priorityQueue.Pop(); processed.Add(n); Arc arcToAdd = parentArc[n]; Forest.Add(arcToAdd); ForestGraph.Enable(arcToAdd, true); foreach (var arc in Graph.Arcs(n)) { Node v = Graph.Other(arc, n); if (processed.Contains(v)) { continue; } TCost arcCost = Cost(arc); TCost vCost; bool vInPriorityQueue = priorityQueue.TryGetPriority(v, out vCost); if (!vInPriorityQueue || arcCost.CompareTo(vCost) < 0) { priorityQueue[v] = arcCost; parentArc[v] = arc; } } } }
private void Run() { Forest.Clear(); PriorityQueue <Node, TCost> priorityQueue = new PriorityQueue <Node, TCost>(); HashSet <Node> hashSet = new HashSet <Node>(); Dictionary <Node, Arc> dictionary = new Dictionary <Node, Arc>(); ConnectedComponents connectedComponents = new ConnectedComponents(Graph, ConnectedComponents.Flags.CreateComponents); foreach (HashSet <Node> component in connectedComponents.Components) { Node node = Enumerable.First <Node>((IEnumerable <Node>)component); hashSet.Add(node); foreach (Arc item in Graph.Arcs(node, ArcFilter.All)) { Node node2 = Graph.Other(item, node); dictionary[node2] = item; priorityQueue[node2] = Cost(item); } } connectedComponents = null; while (priorityQueue.Count != 0) { Node node3 = priorityQueue.Peek(); priorityQueue.Pop(); hashSet.Add(node3); Forest.Add(dictionary[node3]); foreach (Arc item2 in Graph.Arcs(node3, ArcFilter.All)) { Node node4 = Graph.Other(item2, node3); if (!hashSet.Contains(node4)) { TCost value = Cost(item2); if (value.CompareTo(priorityQueue[node4]) < 0) { priorityQueue[node4] = value; dictionary[node4] = item2; } } } } }
public BiEdgeConnectedComponents(IGraph graph, Flags flags = Flags.None) { Graph = graph; BridgeDfs bridgeDfs = new BridgeDfs(); bridgeDfs.Run(graph, null); Count = bridgeDfs.ComponentCount; if ((flags & Flags.CreateBridges) != 0) { Bridges = bridgeDfs.Bridges; } if ((flags & Flags.CreateComponents) != 0) { Subgraph subgraph = new Subgraph(graph); foreach (Arc bridge in bridgeDfs.Bridges) { subgraph.Enable(bridge, false); } Components = new ConnectedComponents(subgraph, ConnectedComponents.Flags.CreateComponents).Components; } }
public BiEdgeConnectedComponents(IGraph graph, Flags flags = 0) { Graph = graph; var dfs = new BridgeDfs(); dfs.Run(graph); Count = dfs.ComponentCount; if (0 != (flags & Flags.CreateBridges)) { Bridges = dfs.Bridges; } if (0 != (flags & Flags.CreateComponents)) { Subgraph withoutBridges = new Subgraph(graph); foreach (var arc in dfs.Bridges) { withoutBridges.Enable(arc, false); } Components = new ConnectedComponents(withoutBridges, ConnectedComponents.Flags.CreateComponents).Components; } }
public Isomorphism(IGraph firstGraph, IGraph secondGraph, int maxIterations = 16) { FirstGraph = firstGraph; SecondGraph = secondGraph; FirstToSecond = null; if (firstGraph.NodeCount() != secondGraph.NodeCount() || firstGraph.ArcCount() != secondGraph.ArcCount() || firstGraph.ArcCount(ArcFilter.Edge) != secondGraph.ArcCount(ArcFilter.Edge)) { Isomorphic = false; } else { ConnectedComponents firstCC = new ConnectedComponents(firstGraph, ConnectedComponents.Flags.CreateComponents); ConnectedComponents secondCC = new ConnectedComponents(secondGraph, ConnectedComponents.Flags.CreateComponents); if (firstCC.Count != secondCC.Count || !firstCC.Components.Select(s => s.Count).OrderBy(x => x).SequenceEqual( secondCC.Components.Select(s => s.Count).OrderBy(x => x))) { Isomorphic = false; } else { NodeHash firstHash = new NodeHash(firstGraph); NodeHash secondHash = new NodeHash(secondGraph); if (firstHash.ColoringHash != secondHash.ColoringHash) { // degree distribution not equal Isomorphic = false; } else if (firstHash.RegularGraph && secondHash.RegularGraph && firstHash.ColoringHash == secondHash.ColoringHash) { // TODO do something with regular graphs // maybe spectral test Isomorphic = null; } else { Isomorphic = null; for (int i = 0; i < maxIterations; ++i) { firstHash.Iterate(); secondHash.Iterate(); if (firstHash.ColoringHash != secondHash.ColoringHash) { Isomorphic = false; break; } } if (Isomorphic == null) { // graphs are very probably isomorphic (or tricky), try to find the mapping var firstColor = firstHash.GetSortedColoring(); var secondColor = secondHash.GetSortedColoring(); // is the canonical coloring the same, and does it uniquely identify nodes? Isomorphic = true; for (int i = 0; i < firstColor.Count; ++i) { if (firstColor[i].Value != secondColor[i].Value) { // unlikely because the hashes matched Isomorphic = false; break; } else if (i > 0 && firstColor[i].Value == firstColor[i - 1].Value) { // two nodes colored the same way (this may happen) // TODO handle this case. Else we won't work for graphs with symmetries. Isomorphic = null; break; } } if (Isomorphic == true) { FirstToSecond = new Dictionary <Node, Node>(firstColor.Count); for (int i = 0; i < firstColor.Count; ++i) { FirstToSecond[firstColor[i].Key] = secondColor[i].Key; } } } } } } }