Exemplo n.º 1
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.º 2
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.º 3
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.º 4
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.º 5
0
 private static VFlibcs.Graph TransToVFGraph(Node node)
 {
     nodeDictionary.Clear();
     _usedEdge.Clear();
     VFlibcs.Graph result = new VFlibcs.Graph();
     nodeDictionary.Add(node, result.InsertNode(node));
     InsertGraph(result, node);
     return(result);
 }
Exemplo n.º 6
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.º 7
0
            public void TestInsertNode()
            {
                Graph gr = new Graph();

                gr.InsertNode(1);
                nNode nod = gr.FindNode(0);

                Assert.IsNotNull(nod);
                nod = gr.FindNode(1);
                Assert.IsTrue(false);
            }
Exemplo n.º 8
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.º 9
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.º 10
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.º 11
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.º 12
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.º 13
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.º 14
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.º 15
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));
            }