/// <summary> /// This resets the valves to be open or closed according to the predefined initial configuration, or a random /// state with no running fans if no preset was specified. /// To determine the logical closed state of a valve, its current physical state is read for reference. The /// logical closed state is then adjusted to match or deviate from the current physical states. /// See <see cref="Valve.IsOpen"/>. /// </summary> /// <param name="physical">the physical interface to get the physical valve states from</param> public void Initialize(IEtsInterface physical) { Log.Verbose("Initializing pipe system " + Index); // store the current physical states of each valve for reference foreach (Valve v in valves) { v.currentPhysicalState = physical.GetValveState(v.Row, v.PositionInRow); //Log.Debug("Initialized valve with physical state " + v.currentPhysicalState); } // if the set of open valves is predefined: apply the preset if (openValvesAtStart != null) { foreach (Valve v in valves) { // if the valve is not reported as broken by the physical interface, set the logical closed state as intended if (!physical.IsValveBroken(v.Row, v.PositionInRow)) { v.logicalClosedState = openValvesAtStart.Contains(v) ? !v.currentPhysicalState : v.currentPhysicalState; } // otherwise, override it such that the system can be solved without turning this valve (i.e. closed iff the valve is connected to an outlet) else { bool connectedToOutlet = false; foreach (Edge e in graph.AdjacentEdges(v)) { if (e.GetOtherVertex((Vertex)v) is Outlet) { connectedToOutlet = true; } } v.logicalClosedState = connectedToOutlet ? v.currentPhysicalState : !v.currentPhysicalState; } } RunningFansCount = findCorrectlyConnectedFans().Count; } // otherwise: set up a random configuration with no running fans else { int runningFans = 1; while (runningFans > 0) { foreach (Valve v in valves) { v.currentPhysicalState = r.Next(0, 1) == 1; } RunningFansCount = findCorrectlyConnectedFans().Count; if (RunningFansCount > 0) { Log.Debug("Discarding initial valves configuration with {0} running fans.", runningFans); } } } }
public bool IsHamiltonian() { // Using Dirac's theorem: if |vertices| >= 2 and for any vertex deg(vertex) >= (|vertices| / 2) then graph is Hamiltonian int n = graph.VertexCount; if (n == 0) { return(false); } else if (n == 1) { return(true); } else { double threshold = n / 2.0; foreach (var v in graph.Vertices) { if (graph.AdjacentEdges(v).Count() < threshold) { return(false); } } return(true); } }
private int CurveSize(TaggedUndirectedEdge <Pixel, EdgeTag> edge) { int size = 0; bool hasEdge = true; TaggedUndirectedEdge <Pixel, EdgeTag> nextEdge1 = edge; //a primeira direção do vertice TaggedUndirectedEdge <Pixel, EdgeTag> nextEdge2 = edge; //a segunda direção do vertice if (edge.Source.valence == 2 || edge.Target.valence == 2) { size++; while (hasEdge) { hasEdge = false; if (nextEdge1.Source.valence == 2 && !nextEdge1.Source.Equals(edge.Source)) { size++; foreach (var e in g.AdjacentEdges(nextEdge1.Source)) { if (!e.Equals(nextEdge1)) { nextEdge1 = e; hasEdge = true; } } } if (nextEdge2.Source.valence == 2 && !nextEdge1.Source.Equals(edge.Source)) { size++; foreach (var e in g.AdjacentEdges(nextEdge2.Source)) { if (!e.Equals(nextEdge2)) { nextEdge2 = e; hasEdge = true; } } } } } return(size); }
public static Holder Init(UndirectedGraph <int, Edge <int> > graph) { var graph1 = new Dictionary <string, List <string> >(); foreach (var v in graph.Vertices) { graph1.Add(v.ToString(), graph.AdjacentEdges(v).Select(e => e.GetOtherVertex(v).ToString()).ToList()); } var kernel = new Kernel(graph1); return(kernel.RunPhase1Then2()); }
public ComponentWithEdges checkComponentsWithEdges() { var componentsAlgo = new ConnectedComponentsAlgorithm <TVertex, UndirectedEdge <TVertex> >(this.graph); componentsAlgo.Compute(); bool[] hasEdgesInComponent = new bool[componentsAlgo.ComponentCount]; foreach (var verticeAndComponent in componentsAlgo.Components) { hasEdgesInComponent[verticeAndComponent.Value] = graph.AdjacentEdges(verticeAndComponent.Key).Count() > 0; } var t = firstAndSecondIndexOfTrue(hasEdgesInComponent); int?firstIndex = t.Item1, secondIndex = t.Item2; if (!firstIndex.HasValue) { return(ComponentWithEdges.NoComponent); } if (secondIndex.HasValue) { return(ComponentWithEdges.ManyComponents); } return(ComponentWithEdges.OneComponent); }
private static int LongestPathDepthFirstSearch(this UndirectedGraph <BoardVertex, BoardEdge> graph, Player player, BoardEdge edge) { var longestPath = 0; var visited = new HashSet <BoardEdge>(); visited.Add(edge); var stack = new Stack <Tuple <BoardEdge, int> >(); stack.Push(Tuple.Create(edge, 1)); while (stack.Count > 0) { var current = stack.Pop(); if (visited.Contains(current.Item1)) { continue; } visited.Add(current.Item1); var vertices = new[] { current.Item1.Source, current.Item1.Target }; foreach (BoardVertex vertex in vertices) { if (vertex.PlayerOwner == player || vertex.Type == VertexTypeConstants.Empty) { foreach (BoardEdge neighbor in graph.AdjacentEdges(vertex).Where(edge => !visited.Contains(edge))) { visited.Add(neighbor); stack.Push(Tuple.Create(neighbor, current.Item2 + 1)); } } } if (longestPath < current.Item2) { longestPath = current.Item2; } } return(longestPath); }