private static UNSequence ComputeSequence(Datastructures.Graph graph, BitSet connectedComponent, CandidateStrategy candidateStrategy, int init, ref int max) { int n = graph.Size; List<int> sequence = new List<int>() { init }; BitSet left = new BitSet(0, n, new int[] { init }); BitSet right = connectedComponent - init; // Initially we store the empty set and the set with init as the representative, ie N(init) * right Set<BitSet> unLeft = new Set<BitSet>() { new BitSet(0, n), graph.OpenNeighborhood(init) * right }; int value = int.MinValue; while (!right.IsEmpty) { Set<BitSet> unChosen = new Set<BitSet>(); int chosen = Heuristics.TrivialCases(graph, left, right); if (chosen != -1) { unChosen = IncrementUn(graph, left, unLeft, chosen); } // If chosen has not been set it means that no trivial case was found // Depending on the criteria for the next vertex we call a different algorithm else { BitSet candidates = Heuristics.Candidates(graph, left, right, candidateStrategy); int min = int.MaxValue; foreach (int v in candidates) { Set<BitSet> unV = IncrementUn(graph, left, unLeft, v); if (unV.Count < min) { chosen = v; unChosen = unV; min = unV.Count; } } } // This should never happen if (chosen == -1) throw new Exception("No vertex is chosen for next step in the heuristic"); // Add/remove the next vertex in the appropiate sets sequence.Add(chosen); left += chosen; right -= chosen; unLeft = unChosen; value = Math.Max(unChosen.Count, value); if (value > max) { return new UNSequence() { Sequence = null, Value = int.MaxValue }; } } return new UNSequence() { Sequence = sequence, Value = value }; }
public void SetUp() { _messageBroker = Substitute.For <IMessageBroker>(); _node = new Node(CandidateName, _messageBroker) { Status = new CandidateStatus(CandidateTerm) }; _candidateStrategy = new CandidateStrategy(_node, 3); }
private static List<int> ComputeSequence(Graph graph, BitSet connectedComponent, CandidateStrategy candidateStrategy, int init, out int value) { int n = graph.Size; List<int> sequence = new List<int>() { init }; BitSet left = new BitSet(0, n) { init }; BitSet right = connectedComponent - init; // Initially we store the empty set and the set with init as the representative, ie N(init) * right Set<BitSet> UN_left = new Set<BitSet>() { new BitSet(0, n), graph.OpenNeighborhood(init) * right }; value = int.MinValue; while (!right.IsEmpty) { Set<BitSet> UN_chosen = new Set<BitSet>(); int chosen = Heuristics.TrivialCases(graph, left, right); if (chosen != -1) { UN_chosen = IncrementUN(graph, left, UN_left, chosen); } // If chosen has not been set it means that no trivial case was found // Depending on the criteria for the next vertex we call a different algorithm else { BitSet candidates = Heuristics.Candidates(graph, left, right, candidateStrategy); int min = int.MaxValue; foreach (int v in candidates) { Set<BitSet> UN_v = IncrementUN(graph, left, UN_left, v); if (UN_v.Count < min) { chosen = v; UN_chosen = UN_v; min = UN_v.Count; } } } // This should never happen if (chosen == -1) throw new Exception("No vertex is chosen for next step in the heuristic"); // Add/remove the next vertex in the appropiate sets sequence.Add(chosen); left.Add(chosen); right.Remove(chosen); UN_left = UN_chosen; value = Math.Max(UN_chosen.Count, value); } return sequence; }
public static LinearDecomposition Compute(Datastructures.Graph graph, CandidateStrategy candidateStrategy, InitialVertexStrategy initialVertexStrategy) { List<BitSet> connectedComponents = DepthFirstSearch.ConnectedComponents(graph); List<int> sequence = new List<int>(); foreach (BitSet connectedComponent in connectedComponents) { switch (initialVertexStrategy) { case InitialVertexStrategy.All: { UNSequence minSequence = new UNSequence() { Value = int.MaxValue}; int max = int.MaxValue; object _lock = new object(); Parallel.ForEach(connectedComponent, vertex => { UNSequence tempSequence = ComputeSequence(graph, connectedComponent, candidateStrategy, vertex, ref max); lock (_lock) { if (tempSequence.Value < minSequence.Value) { max = minSequence.Value; minSequence = tempSequence; } } }); sequence.AddRange(minSequence.Sequence); } break; case InitialVertexStrategy.Bfs: { int init = Heuristics.Bfs(graph, connectedComponent.Last()); sequence.AddRange(ComputeSequence(graph, connectedComponent, candidateStrategy, init)); } break; case InitialVertexStrategy.DoubleBfs: { int init = Heuristics.Bfs(graph, Heuristics.Bfs(graph, connectedComponent.Last())); sequence.AddRange(ComputeSequence(graph, connectedComponent, candidateStrategy, init)); } break; } } return new LinearDecomposition(graph, sequence); }
public static LinearDecomposition Compute(Graph graph, CandidateStrategy candidateStrategy, InitialVertexStrategy initialVertexStrategy) { List<BitSet> connectedComponents = DepthFirstSearch.ConnectedComponents(graph); List<int> sequence = new List<int>(); int tempValue; foreach (BitSet connectedComponent in connectedComponents) { switch (initialVertexStrategy) { case InitialVertexStrategy.All: { List<int> minList = null; int minValue = int.MaxValue; foreach (int vertex in connectedComponent) { List<int> temp = ComputeSequence(graph, connectedComponent, candidateStrategy, vertex, out tempValue); if (tempValue < minValue) { minValue = tempValue; minList = temp; } } sequence.AddRange(minList); } break; case InitialVertexStrategy.BFS: { int init = Heuristics.BFS(graph, connectedComponent.Last()); sequence.AddRange(ComputeSequence(graph, connectedComponent, candidateStrategy, init, out tempValue)); } break; case InitialVertexStrategy.DoubleBFS: { int init = Heuristics.BFS(graph, Heuristics.BFS(graph, connectedComponent.Last())); sequence.AddRange(ComputeSequence(graph, connectedComponent, candidateStrategy, init, out tempValue)); } break; } } return new LinearDecomposition(graph, sequence); }
/*************************/ // Candidate strategy /*************************/ public static BitSet Candidates(Graph graph, BitSet left, BitSet right, CandidateStrategy candidateStrategy) { BitSet nl = graph.Neighborhood(left) * right; return candidateStrategy == CandidateStrategy.All ? right.Copy() : (nl + graph.Neighborhood(nl)) * right; }
private static List<int> ComputeSequence(Datastructures.Graph graph, BitSet connectedComponent, CandidateStrategy candidateStrategy, int init) { int max = int.MaxValue; return ComputeSequence(graph, connectedComponent, candidateStrategy, init, ref max).Sequence; }