Esempio n. 1
0
 /// <summary>
 /// Konstruktor klasy NewGraphPage
 /// </summary>
 public NewGraphPage()
 {
     InitializeComponent();
     graph = new BipartiteGraph();
     ConnectionsTable.AutoGenerateColumns = false;
     SizeChanged += DrawOnSizeChange;
 }
            public override void Reset()
            {
                // get number of bipartite graphs
                m = (int)Math.Ceiling(Math.Log(this.Domains.Count, 2));

                // create bipartite graphs
                this.bipartiteGraphs = new List <BipartiteGraph>(m);
                for (int i = 0; i < m; ++i)
                {
                    // create bipartie graph
                    BipartiteGraph bg = new BipartiteGraph(this.Domains.Count);
                    this.bipartiteGraphs.Add(bg);

                    // do some swapping
                    if (i > 0)
                    {
                        bg.Swap(i - 1, bg.Left.Count + i - 1);
                    }
                }


                this.bipartiteGraphIndex = -1;
                this.leftIndex           = 0;
                this.rightIndex          = 0;
                this.tuple = null;
            }
        public void TestTryBuildWithEmptyGraph()
        {
            var      graph = new BipartiteGraph(1000, 2000);
            Matching matching;

            Assert.IsFalse(builder.TryBuild(graph, out matching));
        }
Esempio n. 4
0
        public void IsBipartite(List <int[]> input, bool expected)
        {
            var sut    = new BipartiteGraph();
            var actual = sut.IsBipartite(input.ToArray());

            Assert.AreEqual(expected, actual);
        }
Esempio n. 5
0
        public GraphBasedDependencyAnalysis(IEnumerable <WorkflowComponent> components)
        {
            Components = components?.ToList() ?? throw new NullReferenceException();
            List <Data> allData = components.GetAllData();

            forwardGraph  = GraphBuilder.BipartiteFromTwoSets(components, allData, c => c.ModelDataOutputs, c => c.ModelDataInputs);
            backwardGraph = forwardGraph.Transpose();
        }
 /// <summary>
 /// Metoda LoadGraph wczytująca graf z pliku użytkownika <see cref="BipartiteGraphIO"/>
 /// </summary>
 private void LoadGraph()
 {
     graph = BipartiteGraphIO.LoadBipartiteGraph();
     if (graph != null)
     {
         UpdateVertex();
     }
 }
        public void TestSimpleTryBuild()
        {
            var graph = new BipartiteGraph(1, 2);

            graph.AddEdge(0, 1);
            Matching matching;

            Assert.IsTrue(builder.TryBuild(graph, out matching));
        }
Esempio n. 8
0
        public static EdgeList ToEdges(BipartiteGraph g, List <GraphNode> cycle)
        {
            var elist = new EdgeList();

            for (int i = 1; i < cycle.Count; i++)
            {
                Edge edg = g.Edges.FindByNodes(cycle[cycle.Count - i], cycle[cycle.Count - i - 1]);
                elist.Add(edg);
            }
            return(elist);
        }
        public void TestTryBuildWithCycleGraph()
        {
            var graph = new BipartiteGraph(2, 2);

            graph.AddEdge(0, 1);
            graph.AddEdge(1, 0);
            graph.AddEdge(0, 0);
            graph.AddEdge(1, 1);
            Matching matching;

            Assert.IsTrue(builder.TryBuild(graph, out matching));
        }
Esempio n. 10
0
 public bool TryBuild(BipartiteGraph graph, out Matching matching)
 {
     matching = new Matching(graph.SecondPartSize);
     for (var i = 0; i < graph.FirstPartSize; ++i)
     {
         var used = new bool[graph.FirstPartSize];
         if (!TryKuhn(i, used, graph, matching))
         {
             return(false);
         }
     }
     return(true);
 }
        public void TestHasAnotherMatching()
        {
            var graph = new BipartiteGraph(2, 2);

            graph.AddEdge(0, 0);
            graph.AddEdge(0, 1);
            graph.AddEdge(1, 1);
            var matching = new Matching(2);

            matching.AddPair(0, 0);
            matching.AddPair(1, 1);
            Assert.IsFalse(builder.HasAnotherMatching(graph, matching));
        }
Esempio n. 12
0
        public double Compute(BipartiteGraph <V, EA> bipartitePossibilities)
        {
            // "dijkstra" simple upper bound heuristic
            return(Math.Min(

                       bipartitePossibilities.potentialConnections
                       .Where((kvp) => kvp.Value.Count > 0)
                       .Count(),

                       bipartitePossibilities.potentialConnectionsReversed
                       .Where((kvp) => kvp.Value.Count > 0)
                       .Count()

                       ));
        }
Esempio n. 13
0
        public static void Enum_Maximum_Matching_Iter(BipartiteGraph G, EdgeList M)
        {
            //Step-1: If G has no edge, STOP
            if (G.Edges.Count == 0)
            {
                return;
            }

            var obj = new CycleInDirectedGraph(G);

            if (!obj.HasCycle)
            {
                return;
            }
            //Step-2: Choose an edge e.
            Edge e = chooseEdge(M, ToEdges(G, obj.Cycle));
        }
            private void CreateTuple()
            {
                // get bipartite graph
                BipartiteGraph bg = (BipartiteGraph)this.bipartiteGraphs[this.bipartiteGraphIndex];

                this.tuple = new Tuple();
                for (int i = 0; i < this.Domains.Count; ++i)
                {
                    if (bg.Left.ContainsKey(i))
                    {
                        this.tuple.Add(this.Domains[i][leftIndex]);
                    }
                    else
                    {
                        this.tuple.Add(this.Domains[i][rightIndex]);
                    }
                }
            }
Esempio n. 15
0
        public void Bipartite_Test()
        {
            var BG = new Graph(7);

            BG.AddEdge(0, 1);
            BG.AddEdge(0, 2);
            BG.AddEdge(0, 5);
            BG.AddEdge(0, 6);
            BG.AddEdge(2, 3);
            BG.AddEdge(2, 4);
            BG.AddEdge(1, 3);
            BG.AddEdge(4, 6);
            BG.AddEdge(4, 5);
            IEnumerable <int> G1;
            IEnumerable <int> G2;
            bool actual = BipartiteGraph.Depart(BG, out G1, out G2);

            Assert.IsTrue(actual);
        }
        /// <summary>
        /// Partitions a complex polygon (with chords and holes) into a group of chordless polygons.
        /// </summary>
        /// <param name="allIsoPerims">Array of lists of Vector2s, each describing a perimeter of a tile group.</param>
        /// <returns>A list of lists of Vector2s, each list describing a chordless polygon.</returns>
        // ReSharper disable once ReturnTypeCanBeEnumerable.Local
        private static (List <Chord>, List <ChordlessPolygon>) _ComplexToChordlessPolygons(List <Vector2>[] allIsoPerims)
        {
            HashSet <ConcaveVertex> concaveVertices = ConcaveVertexFinder.GetConcaveVertices(allIsoPerims);
            List <Chord>            chords          = _FindChords(concaveVertices, allIsoPerims);
            Dictionary <BipartiteGraphNode, Chord> bipartiteNodeToChords = _ConvertChordsToNodes(chords);
            var bipartiteGraph = new BipartiteGraph(bipartiteNodeToChords.Keys);
            HashSet <BipartiteGraphNode>     maxIndependentSet     = bipartiteGraph.GetMaxIndependentSet();
            List <PolygonSplittingGraphNode> polygonSplittingNodes = _ConstructPolygonSplittingNodes(allIsoPerims, bipartiteNodeToChords, maxIndependentSet);
            var holes = new List <List <Vector2> >();

            for (int i = 1; i < allIsoPerims.Length; i++)
            {
                holes.Add(allIsoPerims[i]);
            }
            var polygonSplittingGraph         = new PolygonSplittingGraph(polygonSplittingNodes, holes);
            List <ChordlessPolygon> minCycles = polygonSplittingGraph.GetMinCycles();

            return(chords, minCycles);
        }
Esempio n. 17
0
        private static int[] CanUseParameters(MethodBase info, Type[] parameterTypes)
        {
            ParameterInfo[] parameters = info.GetParameters();
            if (parameters.Length < parameterTypes.Length)
            {
                return(null);
            }
            var permutation = new int[parameters.Length];
            var graph       = new BipartiteGraph(parameterTypes.Length, parameters.Length);

            for (int index = 0; index < parameters.Length; index++)
            {
                permutation[index] = -1;
                ParameterInfo parameter = parameters[index];
                if (parameter.IsOut)
                {
                    return(null);
                }
                for (int i = 0; i < parameterTypes.Length; i++)
                {
                    if (parameter.ParameterType.IsAssignableFrom(parameterTypes[i]))
                    {
                        graph.AddEdge(i, index);
                    }
                }
            }
            Matching matching;

            if (!matchingBuilder.TryBuild(graph, out matching) || matchingBuilder.HasAnotherMatching(graph, matching))
            {
                return(null);
            }

            for (int i = 0; i < parameters.Length; i++)
            {
                if (matching.HasPair(i))
                {
                    permutation[i] = matching.GetPair(i);
                }
            }
            return(permutation);
        }
        public void ComplexTest()
        {
            var graph = new BipartiteGraph(5, 5);

            graph.AddEdge(0, 0);
            graph.AddEdge(0, 3);
            graph.AddEdge(4, 0);
            graph.AddEdge(1, 1);
            graph.AddEdge(1, 2);
            graph.AddEdge(2, 1);
            graph.AddEdge(4, 2);
            graph.AddEdge(3, 3);
            graph.AddEdge(4, 4);
            Matching matching;

            Assert.IsTrue(builder.TryBuild(graph, out matching));

            Assert.IsFalse(builder.HasAnotherMatching(graph, matching));
            graph.AddEdge(1, 4);
            Assert.IsTrue(builder.HasAnotherMatching(graph, matching));
        }
Esempio n. 19
0
 private bool TryKuhn(int v, bool[] used, BipartiteGraph bipartiteGraph, Matching matching)
 {
     if (used[v])
     {
         return(false);
     }
     used[v] = true;
     for (var i = 0; i < bipartiteGraph.SecondPartSize; ++i)
     {
         if (bipartiteGraph.IsConnected(v, i))
         {
             var to = i;
             if (!matching.HasPair(to) || TryKuhn(matching.GetPair(to), used, bipartiteGraph, matching))
             {
                 matching.AddPair(to, v);
                 return(true);
             }
         }
     }
     return(false);
 }
Esempio n. 20
0
 public bool HasAnotherMatching(BipartiteGraph graph, Matching matching)
 {
     for (var i = 0; i < graph.SecondPartSize; i++)
     {
         if (matching.HasPair(i))
         {
             var pair = matching.GetPair(i);
             matching.RemovePair(i);
             graph.RemoveEdge(pair, i);
             var used = new bool[graph.FirstPartSize];
             if (TryKuhn(pair, used, graph, matching))
             {
                 matching.AddPair(i, pair);
                 graph.AddEdge(pair, i);
                 return(true);
             }
             matching.AddPair(i, pair);
             graph.AddEdge(pair, i);
         }
     }
     return(false);
 }
Esempio n. 21
0
        public double Compute(BipartiteGraph <V, EA> bipartitePossibilities)
        {
            if (bipartitePossibilities.potentialConnections.Count == 0 || bipartitePossibilities.potentialConnectionsReversed.Count == 0)
            {
                return(0);
            }

            var setA = new List <V>();
            var setB = new List <V>();

            var edges = new List <Edge <V> >();

            foreach (var left in bipartitePossibilities.potentialConnections)
            {
                foreach (var right in left.Value)
                {
                    edges.Add(new Edge <V>(left.Key, right));
                }
            }

            setA.AddRange(bipartitePossibilities.potentialConnections.Keys);
            setB.AddRange(bipartitePossibilities.potentialConnectionsReversed.Keys);

            var graph = new AdjacencyGraph <V, Edge <V> >(true);

            graph.AddVerticesAndEdgeRange(edges);

            VertexFactory <V>          vertexFactory = () => vertexGenerator();
            EdgeFactory <V, Edge <V> > edgeFactory   = (source, target) => new Edge <V>(source, target);
            var maxMatch = new MaximumBipartiteMatchingAlgorithm <V, Edge <V> >(graph,
                                                                                setA, setB, vertexFactory, edgeFactory);

            maxMatch.Compute();

            return(maxMatch.MatchedEdges.Count);
        }
Esempio n. 22
0
        // Alternative way of calculating the boolean width of a decomposition by counting the number of maximal independent sets in bipartite graphs,
        // constructed for each cut of the decomposition
        private long CalculateBooleanWidthBiPartite()
        {
            BitSet left = new BitSet(0, Graph.Size);
            BitSet right = Graph.Vertices;
            long max = int.MinValue;

            foreach (int v in Sequence)
            {
                left += v;
                right -= v;
                // Construct the bipartite graph
                BipartiteGraph bg = new BipartiteGraph(Graph, left, right);

                // Count the number of maximal independent sets in this bipartite graph
                max = Math.Max(max, CcMis.Compute(bg));
            }
            return max;
        }