public void TestAutomorphic() { const int cRows = 10; const int cCols = 10; var graph1 = new Graph(); graph1.InsertVertices(cRows * cCols); for (var iRow = 0; iRow < cRows - 1; iRow++) { for (var iCol = 0; iCol < cCols - 1; iCol++) { var ivtx = iCol * cRows + iRow; var iVertexToCol = ivtx + 1; var iVertexToRow = ivtx + cRows; graph1.AddEdge(ivtx, iVertexToCol); graph1.AddEdge(ivtx, iVertexToRow); graph1.AddEdge(iVertexToCol, ivtx); graph1.AddEdge(iVertexToRow, ivtx); } } var 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.AddEdge(0, cRows * cCols - 1); var vfs = new VfState(graph1, graph2, true); var matches = vfs.Matches().ToArray(); Assert.AreNotEqual(0, matches.Length); }
internal Graph IsomorphicShuffling(Random rnd) { var graph = new Graph(); var ariShuffle = new int[VertexCount]; for (var i = 0; i < VertexCount; i++) { ariShuffle[i] = i; } Shuffle(ariShuffle, rnd); graph.InsertVertices(VertexCount); for (var ivtx = 0; ivtx < VertexCount; ivtx++) { var ivtxShuffled = ariShuffle[ivtx]; var vtx = VertexList[ivtxShuffled]; foreach (var end in vtx.EdgesFromList) { graph.AddEdge(ariShuffle[PosFromId(end.IDFrom)], ariShuffle[PosFromId(end.IDTo)]); } } return(graph); }
public void TestMatchSubgraph() { // Note that "Subgraph" is defined as a graph derived from // deleting vertices from another graph. Thus, the edges // between the remaining vertices must match with matches in the // original graph. Adding edges between vertices in a graph does // not constitute a supergraph under this definition. var gr1 = new Graph(); var gr2 = new Graph(); gr1.InsertVertices(4); gr2.InsertVertices(3); gr1.AddEdge(0, 1); gr1.AddEdge(2, 0); gr1.AddEdge(3, 0); gr1.AddEdge(2, 3); gr2.AddEdge(0, 1); gr2.AddEdge(2, 0); var vfs = new VfState(gr1, gr2); var matches = vfs.Matches().ToArray(); Assert.AreNotEqual(0, matches.Length); gr1 = VfsTestGraph1(); gr2 = VfsTestGraph2(); vfs = new VfState(gr1, gr2); matches = vfs.Matches().ToArray(); Assert.AreNotEqual(0, matches.Length); gr1.InsertVertex(); gr1.AddEdge(6, 3); gr1.AddEdge(6, 5); // Graph 2 is isomorphic to a subgraph of graph 1 (default check is for // subgraph isomorphism). vfs = new VfState(gr1, gr2); matches = vfs.Matches().ToArray(); Assert.AreNotEqual(0, matches.Length); // The converse is false vfs = new VfState(gr2, gr1); matches = vfs.Matches().ToArray(); Assert.AreEqual(0, matches.Length); // The two graphs are subgraph ismorphic but not ismorphic vfs = new VfState(gr1, gr2, true /* fIsomorphism */); matches = vfs.Matches().ToArray(); Assert.AreEqual(0, matches.Length); }
public void TestMatchNonConnected() { var gr1 = new Graph(); var gr2 = new Graph(); gr1.InsertVertices(4); gr2.InsertVertices(4); gr1.AddEdge(0, 1); gr1.AddEdge(2, 3); gr2.AddEdge(2, 1); gr2.AddEdge(0, 3); var vfs = new VfState(gr1, gr2); var matches = vfs.Matches().ToArray(); Assert.AreNotEqual(0, matches.Length); }
public void TestMultipleMatches() { var gr1 = new Graph(); var gr2 = new Graph(); gr1.InsertVertices(3); gr2.InsertVertices(3); gr1.AddEdge(0, 1); gr1.AddEdge(1, 2); gr1.AddEdge(2, 0); gr2.AddEdge(0, 2); gr2.AddEdge(2, 1); gr2.AddEdge(1, 0); var vfs = new VfState(gr1, gr2); var matches = vfs.Matches().ToArray(); Assert.AreNotEqual(0, matches.Length); Assert.AreEqual(3, matches.Length); vfs = new VfState(gr1, gr2, true); matches = vfs.Matches().ToArray(); Assert.AreEqual(3, matches.Length); gr2.AddEdge(0, 1); gr2.AddEdge(1, 2); gr2.AddEdge(2, 0); gr1.AddEdge(0, 2); gr1.AddEdge(2, 1); gr1.AddEdge(1, 0); vfs = new VfState(gr1, gr2); matches = vfs.Matches().ToArray(); Assert.AreEqual(6, matches.Length); vfs = new VfState(gr1, gr2, true); matches = vfs.Matches().ToArray(); Assert.AreEqual(6, matches.Length); }
public Graph GetGraph(int cnod) { var cEdgesNeeded = (int)(cnod * (cnod - 1) * _pctEdges); var cEdgesSoFar = 0; var setEdges = new HashSet <EdgeKey>(); var graph = new Graph(); graph.InsertVertices(cnod); while (cEdgesSoFar < cEdgesNeeded) { var ekey = new EdgeKey(_rnd.Next(cnod), _rnd.Next(cnod)); if (setEdges.Contains(ekey) || ekey.From == ekey.To) { continue; } graph.AddEdge(ekey.From, ekey.To); setEdges.Add(ekey); cEdgesSoFar++; } return(graph); }
public void TestMatchComplex() { var vfs = VfsTest(); var matches = vfs.Matches().ToArray(); Assert.AreEqual(1, matches.Length); var dict1 = matches[0].IsomorphismVid1ToVid2; var dict2 = matches[0].IsomorphismVid2ToVid1; Assert.AreEqual(1, dict1[0]); Assert.AreEqual(0, dict1[1]); Assert.AreEqual(5, dict1[2]); Assert.AreEqual(4, dict1[3]); Assert.AreEqual(3, dict1[4]); Assert.AreEqual(2, dict1[5]); Assert.AreEqual(1, dict2[0]); Assert.AreEqual(0, dict2[1]); Assert.AreEqual(5, dict2[2]); Assert.AreEqual(4, dict2[3]); Assert.AreEqual(3, dict2[4]); Assert.AreEqual(2, dict2[5]); var graph2 = VfsTestGraph2(); graph2.DeleteEdge(1, 4); graph2.AddEdge(2, 4); vfs = new VfState(VfsTestGraph1(), graph2); matches = vfs.Matches().ToArray(); Assert.AreEqual(0, matches.Length); var graph1 = new Graph(); graph1.InsertVertices(11); graph2 = new Graph(); graph2.InsertVertices(11); graph1.AddEdge(0, 2); graph1.AddEdge(0, 1); graph1.AddEdge(2, 4); graph1.AddEdge(2, 5); graph1.AddEdge(1, 5); graph1.AddEdge(1, 3); graph1.AddEdge(4, 7); graph1.AddEdge(5, 7); graph1.AddEdge(5, 8); graph1.AddEdge(5, 6); graph1.AddEdge(3, 6); graph1.AddEdge(7, 10); graph1.AddEdge(6, 9); graph1.AddEdge(10, 8); graph1.AddEdge(8, 9); graph2.AddEdge(0, 1); graph2.AddEdge(0, 9); graph2.AddEdge(1, 2); graph2.AddEdge(1, 10); graph2.AddEdge(9, 10); graph2.AddEdge(9, 8); graph2.AddEdge(2, 3); graph2.AddEdge(10, 3); graph2.AddEdge(10, 5); graph2.AddEdge(10, 7); graph2.AddEdge(8, 7); graph2.AddEdge(3, 4); graph2.AddEdge(7, 6); graph2.AddEdge(4, 5); graph2.AddEdge(5, 6); vfs = new VfState(graph1, graph2, true); matches = vfs.Matches().ToArray(); Assert.AreNotEqual(0, matches.Length); }