Exemple #1
0
            public void TestAutomorphic()
            {
                int   cRows = 10, cCols = 10;
                Graph graph1 = new Graph();

                graph1.InsertNodes(cRows * cCols);
                for (int iRow = 0; iRow < cRows - 1; iRow++)
                {
                    for (int iCol = 0; iCol < cCols - 1; iCol++)
                    {
                        int iNode      = iCol * cRows + iRow;
                        int iNodeToCol = iNode + 1;
                        int iNodeToRow = iNode + cRows;

                        graph1.InsertEdge(iNode, iNodeToCol);
                        graph1.InsertEdge(iNode, iNodeToRow);
                        graph1.InsertEdge(iNodeToCol, iNode);
                        graph1.InsertEdge(iNodeToRow, iNode);
                    }
                }
                Graph graph2 = graph1.IsomorphicShuffling(new Random(102));
                // Insert this and you'll wait a LONG time for this test to finish...
                // Note - the above is no longer true now that we have Degree compatibility check
                // graph1.InsertEdge(0, cRows * cCols - 1);

                VfState vfs = new VfState(graph1, graph2, true);

                Assert.IsTrue(vfs.FMatch());
            }
Exemple #2
0
            public void TestMatchComplex()
            {
                VfState vfs = VfsTest();

                Assert.IsTrue(vfs.FMatch());
                Graph graph2 = VfsTestGraph2();

                graph2.DeleteEdge(1, 4);
                graph2.InsertEdge(2, 4);
                vfs = new VfState(VfsTestGraph1(), graph2);
                Assert.IsFalse(vfs.FMatch());

                Graph graph1 = new Graph();

                graph1.InsertNodes(11);
                graph2 = new Graph();
                graph2.InsertNodes(11);

                graph1.InsertEdge(0, 2);
                graph1.InsertEdge(0, 1);
                graph1.InsertEdge(2, 4);
                graph1.InsertEdge(2, 5);
                graph1.InsertEdge(1, 5);
                graph1.InsertEdge(1, 3);
                graph1.InsertEdge(4, 7);
                graph1.InsertEdge(5, 7);
                graph1.InsertEdge(5, 8);
                graph1.InsertEdge(5, 6);
                graph1.InsertEdge(3, 6);
                graph1.InsertEdge(7, 10);
                graph1.InsertEdge(6, 9);
                graph1.InsertEdge(10, 8);
                graph1.InsertEdge(8, 9);

                graph2.InsertEdge(0, 1);
                graph2.InsertEdge(0, 9);
                graph2.InsertEdge(1, 2);
                graph2.InsertEdge(1, 10);
                graph2.InsertEdge(9, 10);
                graph2.InsertEdge(9, 8);
                graph2.InsertEdge(2, 3);
                graph2.InsertEdge(10, 3);
                graph2.InsertEdge(10, 5);
                graph2.InsertEdge(10, 7);
                graph2.InsertEdge(8, 7);
                graph2.InsertEdge(3, 4);
                graph2.InsertEdge(7, 6);
                graph2.InsertEdge(4, 5);
                graph2.InsertEdge(5, 6);

                vfs = new VfState(graph1, graph2, true);
                Assert.IsTrue(vfs.FMatch());
            }
Exemple #3
0
            public void TestRandomlyBig()
            {
                RandomGraph rg = new RandomGraph(0.3, 4000 /* seed */);
                Graph       graph1, graph2;
                VfState     vfs;

                // Vast majority of time spent here looking for 299,000 edges
                // in Isomorphic Pair...
                rg.IsomorphicPair(1000, out graph1, out graph2);

                // The actual match took less than 3 seconds on my machine
                vfs = new VfState(graph1, graph2);
                Assert.IsTrue(vfs.FMatch());
            }
Exemple #4
0
            public void TestMatchNonConnected()
            {
                Graph gr1 = new Graph();
                Graph gr2 = new Graph();

                gr1.InsertNodes(4);
                gr2.InsertNodes(4);

                gr1.InsertEdge(0, 1);
                gr1.InsertEdge(2, 3);
                gr2.InsertEdge(2, 1);
                gr2.InsertEdge(0, 3);
                VfState vfs = new VfState(gr1, gr2);

                Assert.IsTrue(vfs.FMatch());
            }
Exemple #5
0
            public void TestRandomly()
            {
                RandomGraph rg = new RandomGraph(0.3, 4000 /* seed */);
                Graph       graph1, graph2;
                VfState     vfs;

                for (int i = 0; i < 10; i++)
                {
                    rg.IsomorphicPair(100, out graph1, out graph2);
                    vfs = new VfState(graph1, graph2);
                    Assert.IsTrue(vfs.FMatch());
                }

                graph1 = rg.GetGraph(100);
                graph2 = rg.GetGraph(100);
                vfs    = new VfState(graph1, graph2);
                Assert.IsFalse(vfs.FMatch());
            }
Exemple #6
0
            public void TestMatchSubgraph()
            {
                // Note that "Subgraph" is defined as a graph derived from
                // deleting nodes from another graph.  Thus, the edges
                // between the remaining nodes must match with matches in the
                // original graph.  Adding edges between nodes in a graph does
                // not constitute a supergraph under this definition.

                Graph gr1 = new Graph();
                Graph gr2 = new Graph();

                gr1.InsertNodes(4);
                gr2.InsertNodes(3);
                gr1.InsertEdge(0, 1);
                gr1.InsertEdge(2, 0);
                gr1.InsertEdge(3, 0);
                gr1.InsertEdge(2, 3);
                gr2.InsertEdge(0, 1);
                gr2.InsertEdge(2, 0);
                VfState vfs = new VfState(gr1, gr2);

                Assert.IsTrue(vfs.FMatch());

                gr1 = VfsTestGraph1();
                gr2 = VfsTestGraph2();
                vfs = new VfState(gr1, gr2);
                Assert.IsTrue(vfs.FMatch());
                gr1.InsertNode();
                gr1.InsertEdge(6, 3);
                gr1.InsertEdge(6, 5);

                // Graph 2 is isomorphic to a subgraph of graph 1 (default check is for
                // subgraph isomorphism).
                vfs = new VfState(gr1, gr2);
                Assert.IsTrue(vfs.FMatch());

                // The converse is false
                vfs = new VfState(gr2, gr1);
                Assert.IsFalse(vfs.FMatch());

                // The two graphs are subgraph ismorphic but not ismorphic
                vfs = new VfState(gr1, gr2, true /* fIsomorphism */);
                Assert.IsFalse(vfs.FMatch());
            }
Exemple #7
0
            public void TestContextCheck()
            {
                Graph graph1 = new Graph();
                Graph graph2 = new Graph();

                graph1.InsertNode(new NodeColor("Blue"));
                graph1.InsertNode(new NodeColor("Red"));
                graph1.InsertNode(new NodeColor("Red"));
                graph1.InsertNode(new NodeColor("Red"));
                graph1.InsertNode(new NodeColor("Red"));
                graph1.InsertEdge(0, 1);
                graph1.InsertEdge(1, 2);
                graph1.InsertEdge(2, 3);
                graph1.InsertEdge(3, 4);
                graph1.InsertEdge(4, 0);

                graph2.InsertNode(new NodeColor("Red"));
                graph2.InsertNode(new NodeColor("Red"));
                graph2.InsertNode(new NodeColor("Red"));
                graph2.InsertNode(new NodeColor("Blue"));
                graph2.InsertNode(new NodeColor("Red"));
                graph2.InsertEdge(0, 1);
                graph2.InsertEdge(1, 2);
                graph2.InsertEdge(2, 3);
                graph2.InsertEdge(3, 4);
                graph2.InsertEdge(4, 0);

                VfState vfs = new VfState(graph1, graph2, true);

                Assert.IsTrue(vfs.FMatch());
                int[] mpMatch = vfs.Mapping1To2;
                // With no context checking, vertex 0 in the first graph can match
                // vertex 0 in the second graph
                Assert.AreEqual(0, mpMatch[0]);

                vfs = new VfState(graph1, graph2, true, true);
                Assert.IsTrue(vfs.FMatch());
                mpMatch = vfs.Mapping1To2;
                // With context checking, Blue in first circular graph has to map to blue
                // in second circular graph.
                Assert.AreEqual(3, mpMatch[0]);
            }
Exemple #8
0
 // Step 1: Find matchs.
 private static Rule FindMatchs(VFlibcs.Graph graphVF)
 {
     foreach (var rule in RandomOrderByWeight())
     {
         if (rule.QuantityLimitMax == 0)
         {
             continue;
         }
         // VfState: Calculate the result of subgraph isomorphic.
         VFlibcs.VfState vfs = new VFlibcs.VfState(graphVF, _ruleVFgraphTable[rule], false, true);
         if (vfs.FMatch())
         {
             // Reduce the quantity limit.
             if (rule.QuantityLimitMin > 0)
             {
                 rule.QuantityLimitMin -= 1;
             }
             if (rule.QuantityLimitMax > 0)
             {
                 rule.QuantityLimitMax -= 1;
             }
             _relatedNodes.Clear();
             for (int i = 0; i < vfs.Mapping2To1.Length; i++)
             {
                 // Set Index.
                 Node node = graphVF.GetNodeAttr(vfs.Mapping2To1[i]) as Node;
                 // Record this one is used in this iterating.
                 node.Explored = true;
                 _exploredNodeStack.Push(node);
                 if (node.IsEdge)
                 {
                     continue;
                 }
                 node.Index = (_ruleVFgraphTable[rule].GetNodeAttr(i) as Node).Index;
                 _relatedNodes.Add(node);
             }
             return(rule);
         }
     }
     return(null);
 }
Exemple #9
0
            public void TestMultipleMatches()
            {
                Graph   gr1 = new Graph();
                Graph   gr2 = new Graph();
                VfState vfs;

                gr1.InsertNodes(3);
                gr2.InsertNodes(3);
                gr1.InsertEdge(0, 1);
                gr1.InsertEdge(1, 2);
                gr1.InsertEdge(2, 0);
                gr2.InsertEdge(0, 2);
                gr2.InsertEdge(2, 1);
                gr2.InsertEdge(1, 0);

                vfs = new VfState(gr1, gr2, false, false, true);
                Assert.IsTrue(vfs.FMatch());
                Assert.AreEqual(3, vfs.Mappings.Count);

                vfs = new VfState(gr1, gr2, true, false, true);
                Assert.IsTrue(vfs.FMatch());
                Assert.AreEqual(3, vfs.Mappings.Count);

                gr2.InsertEdge(0, 1);
                gr2.InsertEdge(1, 2);
                gr2.InsertEdge(2, 0);
                gr1.InsertEdge(0, 2);
                gr1.InsertEdge(2, 1);
                gr1.InsertEdge(1, 0);

                vfs = new VfState(gr1, gr2, false, false, true);
                Assert.IsTrue(vfs.FMatch());
                Assert.AreEqual(6, vfs.Mappings.Count);

                vfs = new VfState(gr1, gr2, true, false, true);
                Assert.IsTrue(vfs.FMatch());
                Assert.AreEqual(6, vfs.Mappings.Count);
            }
Exemple #10
0
            public void TestMatch()
            {
                Graph gr1 = new Graph();
                Graph gr2 = new Graph();

                VfState vfs = new VfState(gr1, gr2);

                Assert.IsTrue(vfs.FMatch());
                gr2.InsertNode();
                vfs = new VfState(gr1, gr2);
                Assert.IsFalse(vfs.FMatch());
                gr1.InsertNode();
                vfs = new VfState(gr1, gr2);
                Assert.IsTrue(vfs.FMatch());
                gr1.InsertNode();
                vfs = new VfState(gr1, gr2);
                Assert.IsTrue(vfs.FMatch());
                gr1.InsertEdge(0, 1);
                vfs = new VfState(gr1, gr2);
                Assert.IsTrue(vfs.FMatch());
                vfs = new VfState(gr2, gr1);
                Assert.IsFalse(vfs.FMatch());
            }
Exemple #11
0
            public void TestRandomlyBigPerf()
            {
                RandomGraph rg = new RandomGraph(0.025, 7000 /* seed */);
                Graph       graph1, graph2;
                VfState     vfs;

                rg.IsomorphicPair(4000, out graph1, out graph2);

                //Object objDummy;
                //int iTo = graph2.GetOutEdge(0, 0, out objDummy);
                //graph2.DeleteEdge(0, iTo);

                int totalTicks = 0;
                int cTests     = 20;

                for (int i = 0; i < cTests; i++)
                {
                    vfs = new VfState(graph1, graph2);
                    int starttime = System.Environment.TickCount;
                    vfs.FMatch();
                    totalTicks += System.Environment.TickCount - starttime;
                }
                Console.WriteLine("Total seconds = {0}", totalTicks / (cTests * 1000.0));
            }
Exemple #12
0
            public void TestPermutationMassage()
            {
                Graph graph1 = new Graph();

                graph1.InsertNodes(4);
                graph1.InsertEdge(2, 3);

                Graph graph2 = new Graph();

                graph2.InsertNodes(2);
                graph2.InsertEdge(1, 0);

                VfState vfs = new VfState(graph1, graph2);

                Assert.IsTrue(vfs.FMatch());
                int[] arprm1To2 = vfs.Mapping1To2;
                int[] arprm2To1 = vfs.Mapping2To1;
                Assert.AreEqual(1, arprm1To2[2]);
                Assert.AreEqual(0, arprm1To2[3]);
                Assert.AreEqual(map_Illegal, arprm1To2[0]);
                Assert.AreEqual(map_Illegal, arprm1To2[1]);
                Assert.AreEqual(3, arprm2To1[0]);
                Assert.AreEqual(2, arprm2To1[1]);
            }