Exemplo n.º 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());
            }
Exemplo n.º 2
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());
            }
Exemplo n.º 3
0
        internal Graph IsomorphicShuffling(Random rnd)
        {
            Graph graph = new Graph();

            int[] ariShuffle = new int[NodeCount];

            for (int i = 0; i < NodeCount; i++)
            {
                ariShuffle[i] = i;
            }
            Shuffle <int>(ariShuffle, rnd);

            graph.InsertNodes(NodeCount);

            for (int inod = 0; inod < NodeCount; inod++)
            {
                int   inodShuffled = ariShuffle[inod];
                nNode nod          = _nodes[inodShuffled];

                foreach (eNode end in nod._edgesFrom.Values)
                {
                    graph.InsertEdge(ariShuffle[PosFromId(end._idFrom)], ariShuffle[PosFromId(end._idTo)]);
                }
            }
            return(graph);
        }
Exemplo n.º 4
0
 // DFS Insert node.
 private static void InsertGraph(VFlibcs.Graph graph, Node node)
 {
     foreach (Node child in node.Children)
     {
         if (child == null)
         {
             continue;
         }
         // If the node have not set then set it.
         if (!nodeDictionary.ContainsKey(child))
         {
             nodeDictionary.Add(child, graph.InsertNode(child));
         }
         string stringEdge = nodeDictionary[node] + "+" + nodeDictionary[child];
         if (_usedEdge.Exists(x => x == stringEdge))
         {
             continue;
         }
         // Set edge.
         graph.InsertEdge(nodeDictionary[node], nodeDictionary[child]);
         _usedEdge.Add(stringEdge);
         InsertGraph(graph, child);
     }
     foreach (var parent in node.Parents)
     {
         if (parent == null)
         {
             continue;
         }
         // If the node have not set then set it.
         if (!nodeDictionary.ContainsKey(parent))
         {
             nodeDictionary.Add(parent, graph.InsertNode(parent));
         }
         string stringEdge = nodeDictionary[parent] + "+" + nodeDictionary[node];
         if (_usedEdge.Exists(x => x == stringEdge))
         {
             continue;
         }
         // Set edge.
         graph.InsertEdge(nodeDictionary[parent], nodeDictionary[node]);
         _usedEdge.Add(stringEdge);
         InsertGraph(graph, parent);
     }
 }
Exemplo n.º 5
0
            public void TestDeleteEdge()
            {
                Graph gr = new Graph();

                Assert.AreEqual(0, gr.InsertNode());
                Assert.AreEqual(1, gr.InsertNode());
                Assert.AreEqual(2, gr.InsertNode());
                gr.InsertEdge(0, 1);
                gr.InsertEdge(1, 2);
                gr.InsertEdge(2, 0);
                gr.DeleteEdge(1, 2);
                Assert.AreEqual(1, gr.OutEdgeCount(0));
                Assert.AreEqual(0, gr.OutEdgeCount(1));
                Assert.AreEqual(1, gr.OutEdgeCount(2));

                // Trigger the exception - no edge from 1 to 0...
                gr.DeleteEdge(1, 0);
            }
Exemplo n.º 6
0
            public void TestDeleteNode()
            {
                Graph gr = new Graph();

                Assert.AreEqual(0, gr.InsertNode());
                Assert.AreEqual(1, gr.InsertNode());
                Assert.AreEqual(2, gr.InsertNode());
                gr.InsertEdge(0, 1);
                gr.InsertEdge(1, 2);
                gr.InsertEdge(2, 0);
                gr.DeleteNode(0);

                Assert.AreEqual(1, gr.OutEdgeCount(1));
                Assert.AreEqual(0, gr.OutEdgeCount(2));

                // Trigger the exception - shouldn't be a zero node any more...
                gr.FindNode(0);
            }
Exemplo n.º 7
0
            public void TestInsertEdge()
            {
                object attr;
                Graph  gr     = new Graph();
                int    idFrom = gr.InsertNode(0);
                int    idTo   = gr.InsertNode(1);

                gr.InsertEdge(idFrom, idTo, 100);
                Assert.AreEqual(gr.OutEdgeCount(idFrom), 1);
                Assert.AreEqual(gr.OutEdgeCount(idTo), 0);
                int idEdge = gr.GetOutEdge(idFrom, 0, out attr);

                Assert.AreEqual(100, (int)attr);
                Assert.AreEqual(idTo, idEdge);

                // Try inserting the same edge twice to trigger exception...
                gr.InsertEdge(0, 1, 200);
            }
Exemplo n.º 8
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());
            }
Exemplo n.º 9
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]);
            }
Exemplo n.º 10
0
            public void TestPermutations()
            {
                Graph graph = new Graph();

                Assert.AreEqual(0, graph.InsertNode());
                Assert.AreEqual(1, graph.InsertNode());
                Assert.AreEqual(2, graph.InsertNode());
                graph.InsertEdge(1, 0);
                graph.InsertEdge(1, 2);
                int[]   mpPermutation = (new CmpNodeDegrees(graph)).Permutation;
                VfGraph vfg           = new VfGraph(graph, mpPermutation);

                Assert.AreEqual(mpPermutation[1], 0);
                int[] arOut = new int[vfg._arNodes[0].OutNeighbors.Count];
                vfg._arNodes[0].OutNeighbors.CopyTo(arOut, 0);
                int inodNeighbor1 = arOut[0];
                int inodNeighbor2 = arOut[1];

                Assert.IsTrue(inodNeighbor1 == 1 && inodNeighbor2 == 2 || inodNeighbor1 == 2 && inodNeighbor2 == 1);
            }
Exemplo n.º 11
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());
            }
Exemplo n.º 12
0
            VfGraph SetupGraph()
            {
                Graph graph = new Graph();

                Assert.AreEqual(0, graph.InsertNode());
                Assert.AreEqual(1, graph.InsertNode());
                Assert.AreEqual(2, graph.InsertNode());
                Assert.AreEqual(3, graph.InsertNode());
                Assert.AreEqual(4, graph.InsertNode());
                Assert.AreEqual(5, graph.InsertNode());
                graph.InsertEdge(0, 1);
                graph.InsertEdge(1, 2);
                graph.InsertEdge(2, 3);
                graph.InsertEdge(3, 4);
                graph.InsertEdge(4, 5);
                graph.InsertEdge(5, 0);
                graph.DeleteNode(0);
                graph.DeleteNode(1);
                graph.InsertEdge(5, 2);
                graph.InsertEdge(2, 4);

                return(new VfGraph(graph, (new CmpNodeDegrees(graph)).Permutation));
            }
Exemplo n.º 13
0
            Graph VfsTestGraph2()
            {
                Graph graph = new Graph();

                Assert.AreEqual(0, graph.InsertNode());
                Assert.AreEqual(1, graph.InsertNode());
                Assert.AreEqual(2, graph.InsertNode());
                Assert.AreEqual(3, graph.InsertNode());
                Assert.AreEqual(4, graph.InsertNode());
                Assert.AreEqual(5, graph.InsertNode());
                // Same graph in reverse order with slightly offset "extra" edge at (4,1)
                graph.InsertEdge(1, 0);
                graph.InsertEdge(2, 1);
                graph.InsertEdge(3, 2);
                graph.InsertEdge(4, 3);
                graph.InsertEdge(5, 4);
                graph.InsertEdge(0, 5);
                graph.InsertEdge(1, 4);

                return(graph);
            }
Exemplo n.º 14
0
            Graph VfsTestGraph1()
            {
                Graph graph = new Graph();

                Assert.AreEqual(0, graph.InsertNode());
                Assert.AreEqual(1, graph.InsertNode());
                Assert.AreEqual(2, graph.InsertNode());
                Assert.AreEqual(3, graph.InsertNode());
                Assert.AreEqual(4, graph.InsertNode());
                Assert.AreEqual(5, graph.InsertNode());
                // Circular graph with "extra" edge at (0,3)
                graph.InsertEdge(0, 1);
                graph.InsertEdge(1, 2);
                graph.InsertEdge(2, 3);
                graph.InsertEdge(3, 4);
                graph.InsertEdge(4, 5);
                graph.InsertEdge(5, 0);
                graph.InsertEdge(0, 3);

                return(graph);
            }
Exemplo n.º 15
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());
            }
Exemplo n.º 16
0
        public Graph GetGraph(int cnod)
        {
            int           cEdgesNeeded = (int)(cnod * (cnod - 1) * _pctEdges);
            int           cEdgesSoFar  = 0;
            Set <EdgeKey> setEdges     = new Set <EdgeKey>();
            Graph         graph        = new Graph();

            graph.InsertNodes(cnod);

            while (cEdgesSoFar < cEdgesNeeded)
            {
                EdgeKey ekey = new EdgeKey(_rnd.Next(cnod), _rnd.Next(cnod));

                if (setEdges.Contains(ekey) || ekey.From == ekey.To)
                {
                    continue;
                }
                graph.InsertEdge(ekey.From, ekey.To);
                setEdges.Add(ekey);
                cEdgesSoFar++;
            }
            return(graph);
        }
Exemplo n.º 17
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]);
            }
Exemplo n.º 18
0
            VfState VfsTest()
            {
                Graph graph1 = new Graph();

                Assert.AreEqual(0, graph1.InsertNode());
                Assert.AreEqual(1, graph1.InsertNode());
                Assert.AreEqual(2, graph1.InsertNode());
                Assert.AreEqual(3, graph1.InsertNode());
                Assert.AreEqual(4, graph1.InsertNode());
                Assert.AreEqual(5, graph1.InsertNode());
                // Circular graph with "extra" edge at (0,3)
                graph1.InsertEdge(0, 1);
                graph1.InsertEdge(1, 2);
                graph1.InsertEdge(2, 3);
                graph1.InsertEdge(3, 4);
                graph1.InsertEdge(4, 5);
                graph1.InsertEdge(5, 0);
                graph1.InsertEdge(0, 3);

                Graph graph2 = new Graph();

                Assert.AreEqual(0, graph2.InsertNode());
                Assert.AreEqual(1, graph2.InsertNode());
                Assert.AreEqual(2, graph2.InsertNode());
                Assert.AreEqual(3, graph2.InsertNode());
                Assert.AreEqual(4, graph2.InsertNode());
                Assert.AreEqual(5, graph2.InsertNode());
                // Same graph in reverse order with slightly offset "extra" edge at (4,1)
                graph2.InsertEdge(1, 0);
                graph2.InsertEdge(2, 1);
                graph2.InsertEdge(3, 2);
                graph2.InsertEdge(4, 3);
                graph2.InsertEdge(5, 4);
                graph2.InsertEdge(0, 5);
                graph2.InsertEdge(4, 1);

                return(new VfState(graph1, graph2));
            }
Exemplo n.º 19
0
            VfState VfsTest()
            {
                Graph graph1 = new Graph();

                Assert.AreEqual(0, graph1.InsertNode());
                Assert.AreEqual(1, graph1.InsertNode());
                Assert.AreEqual(2, graph1.InsertNode());
                Assert.AreEqual(3, graph1.InsertNode());
                Assert.AreEqual(4, graph1.InsertNode());
                Assert.AreEqual(5, graph1.InsertNode());
                graph1.InsertEdge(0, 1);
                graph1.InsertEdge(1, 2);
                graph1.InsertEdge(2, 3);
                graph1.InsertEdge(3, 4);
                graph1.InsertEdge(4, 5);
                graph1.InsertEdge(5, 0);
                graph1.InsertEdge(0, 3);

                Graph graph2 = new Graph();

                Assert.AreEqual(0, graph2.InsertNode());
                Assert.AreEqual(1, graph2.InsertNode());
                Assert.AreEqual(2, graph2.InsertNode());
                Assert.AreEqual(3, graph2.InsertNode());
                Assert.AreEqual(4, graph2.InsertNode());
                Assert.AreEqual(5, graph2.InsertNode());
                graph2.InsertEdge(1, 0);
                graph2.InsertEdge(2, 1);
                graph2.InsertEdge(3, 2);
                graph2.InsertEdge(4, 3);
                graph2.InsertEdge(5, 4);
                graph2.InsertEdge(0, 5);
                graph2.InsertEdge(4, 1);

                return(new VfState(graph1, graph2));
            }
Exemplo n.º 20
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);
            }