public void WhenNStar(int N)
        {
            UndirectedGraph <int, IEdge <int> > graph = new UndirectedGraph <int, IEdge <int> >(false);
            int root = 0;
            List <IEdge <int> > edges = new List <IEdge <int> >();

            for (int i = 1; i <= N; i++)
            {
                edges.Add(new Edge <int>(root, i));
            }
            graph.AddVerticesAndEdgeRange(edges);

            UndirectedGraph <int, IEdge <int> > g = sut.Triangulate(graph, root).nonTreeEdges;

            foreach (IEdge <int> edge in edges)
            {
                Assert.False(g.ContainsEdge(edge));
            }
            for (int i = 2; i <= N; i++)
            {
                Assert.True(g.ContainsEdge(i - 1, i));
            }
            if (N > 1)
            {
                Assert.True(g.ContainsEdge(N, 1));
                Assert.Equal(2 * N - 3, g.EdgeCount);
            }
        }
        public void SimpleBinaryTree()
        {
            UndirectedGraph <int, IEdge <int> > graph = new UndirectedGraph <int, IEdge <int> >(false);
            int root = 0;
            List <IEdge <int> > edges = new List <IEdge <int> >();

            edges.Add(new Edge <int>(0, 1));
            edges.Add(new Edge <int>(0, 2));
            edges.Add(new Edge <int>(1, 3));
            edges.Add(new Edge <int>(1, 4));
            edges.Add(new Edge <int>(2, 5));
            edges.Add(new Edge <int>(2, 6));
            graph.AddVerticesAndEdgeRange(edges);

            UndirectedGraph <int, IEdge <int> > g = sut.Triangulate(graph, root).nonTreeEdges;

            foreach (IEdge <int> edge in edges)
            {
                Assert.False(g.ContainsEdge(edge));
            }
            Assert.True(g.ContainsEdge(1, 2));
            Assert.True(g.ContainsEdge(0, 3));
            Assert.True(g.ContainsEdge(3, 4));
            Assert.True(g.ContainsEdge(4, 2));
            Assert.True(g.ContainsEdge(4, 5));
            Assert.True(g.ContainsEdge(5, 6));
            Assert.True(g.ContainsEdge(6, 0));
            Assert.True(g.ContainsEdge(0, 4));
            Assert.True(g.ContainsEdge(0, 5));
        }
Ejemplo n.º 3
0
 private static List <int> FindFourCycle3Inner(UndirectedGraph <int, Edge <int> > graph, List <int> path, int start, int depth, int goal)
 {
     path.Add(start);
     foreach (var e in graph.AdjacentEdges(start))
     {
         var n = e.GetOtherVertex(start);
         if (path.Contains(n) && depth != 0)
         {
             continue;
         }
         if (depth == 0)
         {
             if (goal == n)
             {
                 return(path);
             }
             else
             {
                 continue;
             }
         }
         var newPath = CloneList(path);
         var cycle   = FindFourCycle3Inner(graph, newPath, n, depth - 1, goal);
         if (cycle != null)
         {
             if (graph.ContainsEdge(cycle[0], cycle[2]) || graph.ContainsEdge(cycle[1], cycle[3])) //not chordless
             {
                 continue;
             }
             return(cycle);
         }
     }
     return(null);
 }
        public void WhenNPath(int N)
        {
            UndirectedGraph <int, IEdge <int> > graph = new UndirectedGraph <int, IEdge <int> >(false);
            int root = 0;
            List <IEdge <int> > edges = new List <IEdge <int> >();

            for (int i = 0; i < N; i++)
            {
                edges.Add(new Edge <int>(i, i + 1));
            }
            graph.AddVerticesAndEdgeRange(edges);

            UndirectedGraph <int, IEdge <int> > g = sut.Triangulate(graph, root).nonTreeEdges;

            foreach (IEdge <int> edge in edges)
            {
                Assert.False(g.ContainsEdge(edge));
            }
            for (int i = 2; i <= N; i++)
            {
                Assert.True(g.ContainsEdge(root, i));
            }
            for (int i = 3; i < N; i++)
            {
                Assert.True(g.ContainsEdge(1, i));
            }
        }
Ejemplo n.º 5
0
 public bool AreConnected(int i, int j)
 {
     if (i == j)
     {
         return(false);
     }
     return(i < j?graph.ContainsEdge(i, j) : graph.ContainsEdge(j, i));
 }
Ejemplo n.º 6
0
        // More memory intensive (and faster!!) cycle finder
        public static List <int> FindFourCycle2(UndirectedGraph <int, Edge <int> > graph)
        {
            var idTable    = new int[graph.VertexCount];
            var idTableRev = new Dictionary <int, int>();
            int i          = 0;

            foreach (int v in graph.Vertices)
            {
                idTable[i]    = v;
                idTableRev[v] = i++;
            }
            var matrix = new int[graph.VertexCount, graph.VertexCount];

            matrix.Initialize();
            for (i = 0; i < graph.VertexCount; i++) //Assumes that vertex set is of format {0, 1, ... n-1}
            {
                foreach (var e1 in graph.AdjacentEdges(idTable[i]))
                {
                    var n1 = e1.GetOtherVertex(idTable[i]);
                    foreach (var e2 in graph.AdjacentEdges(idTable[i]))
                    {
                        var n2 = e2.GetOtherVertex(idTable[i]);
                        if (n1 == n2)
                        {
                            continue;
                        }
                        var temp = Math.Max(n1, n2);
                        n1 = Math.Min(n1, n2);
                        n2 = temp;
                        if (matrix[idTableRev[n1], idTableRev[n2]] == 1)
                        {
                            if (graph.ContainsEdge(n1, n2))
                            {
                                continue;
                            }
                            // find the vertice that set matrix[n1, n2] == 1
                            for (int j = 0; j < i; j++)
                            {
                                if (graph.ContainsEdge(idTable[j], n1) && graph.ContainsEdge(idTable[j], n2))
                                {
                                    if (graph.ContainsEdge(idTable[i], idTable[j]))
                                    {
                                        continue;
                                    }
                                    return(new List <int> {
                                        n1, idTable[j], idTable[i], n2
                                    });
                                }
                            }
                        }
                        matrix[idTableRev[n1], idTableRev[n2]] = 1;
                    }
                }
            }
            return(null);
        }
Ejemplo n.º 7
0
        public void TestTargetAddEdge()
        {
            UndirectedGraph target = new UndirectedGraph();

            target.AddVertex("v1");
            target.AddVertex("v2");
            Assert.IsTrue(target.ContainsVertex("v1"));
            Assert.IsTrue(target.ContainsVertex("v2"));
            target.AddEdge("v1", "v2");
            Assert.IsTrue(target.ContainsEdge("v1", "v2"));
            Assert.IsTrue(target.ContainsEdge("v2", "v1"));
        }
Ejemplo n.º 8
0
        public void removeAdjacentEdgeIfTest()
        {
            var graph = new UndirectedGraph <int, IEdge <int> >();

            graph.AddVertex(1);
            graph.AddVertex(2);
            graph.AddVertex(3);
            graph.AddEdge(new EquatableEdge <int>(1, 2));
            graph.AddEdge(new EquatableEdge <int>(1, 3));
            graph.RemoveAdjacentEdgeIf(1, (edge) => edge.Target == 2);
            Assert.IsTrue(graph.ContainsEdge(1, 3));
            Assert.IsFalse(graph.ContainsEdge(1, 2));
        }
Ejemplo n.º 9
0
        public void removeIsolatedVertices()
        {
            var graph = new UndirectedGraph <int, IEdge <int> >();

            graph.AddVertex(1);
            var edge = new EquatableEdge <int>(2, 3);

            graph.AddVerticesAndEdge(edge);
            graph.RemoveVertexIf(graph.IsAdjacentEdgesEmpty);
            Assert.IsTrue(graph.ContainsVertex(2));
            Assert.IsTrue(graph.ContainsEdge(edge));
            Assert.IsTrue(graph.ContainsEdge(2, 3));
            Assert.IsTrue(graph.ContainsEdge(3, 2));
            Assert.IsFalse(graph.ContainsVertex(1));
        }
        public UndirectedGraph <int, Edge <int> > ToQuickGraph()
        {
            if (this.quickGraph != null)
            {
                return(this.quickGraph);
            }

            var g = new UndirectedGraph <int, Edge <int> >();

            for (var i = 0; i < this.container.Size; ++i)
            {
                g.AddVertex(i);
            }

            foreach (var kv in this.container.GetNeighbourship())
            {
                if (!g.ContainsEdge(kv.Key, kv.Value))
                {
                    g.AddEdge(new Edge <int>(kv.Key, kv.Value));
                }
            }

            this.quickGraph = g;
            return(g);
        }
Ejemplo n.º 11
0
        public RandomGraph(int N, double P)
        {
            int v1 = 0, v2 = 0;

            count    = 0;
            MaxEdges = (int)Math.Round(P * N * (N - 1) / 2);
            for (int i = 1; i <= N; i++)
            {
                g.AddVertex(i);
            }
            for (int i = 0; i < MaxEdges; i++)
            {
                v1 = 0;
                v2 = 0;
                v1 = random.Next() % N + 1;
                v2 = random.Next() % N + 1;
                if (v1 == v2)
                {
                    continue;
                }
                if (!g.ContainsEdge(v2, v1))
                {
                    var  e1 = new UndirectedEdge <int>(v1, v2);
                    bool v  = g.AddEdge(e1);
                    if (v)
                    {
                        count++;
                    }
                }
            }
            Console.WriteLine("Max Edges: " + MaxEdges);
            Console.WriteLine("Egdes created: " + count);
        }
        public UndirectedGraph <int, Edge <int> > ToQuickGraph()
        {
            if (this.quickGraph != null)
            {
                return(this.quickGraph);
            }
            var g = new UndirectedGraph <int, Edge <int> >();

            for (var i = 0; i < this.container.Size; ++i)
            {
                g.AddVertex(i);
            }

            for (int i = 0; i < this.container.Size; ++i)
            {
                for (int j = i + 1; j < this.container.Size; ++j)
                {
                    if (this.container[i, j] == 1 && !g.ContainsEdge(i, j))
                    {
                        g.AddEdge(new Edge <int>(i, j));
                    }
                }
            }

            this.quickGraph = g;
            return(g);
        }
Ejemplo n.º 13
0
        private void Connect(AcadGeo.Point3d fp, AcadGeo.Point3d sp)
        {
            Node fn = FindNode(fp);

            if (null == fn)
            {
                fn = new Node(fp);
                routers.AddVertex(fn);
            }

            Node sn = FindNode(sp);

            if (null == sn)
            {
                sn = new Node(sp);
                routers.AddVertex(sn);
            }

            if (routers.ContainsEdge(fn, sn))
            {
                return;
            }

            Connection conn = new Connection(fn, sn);

            routers.AddEdge(conn);
            fn.Connections.Add(conn);
            sn.Connections.Add(conn);
        }
Ejemplo n.º 14
0
        public void TestContainsEdgeWithEdgeTypeSelfEdge()
        {
            var g = new UndirectedGraph <int, Edge <int> >(true);

            g.AddVertex(1);
            g.AddEdge(new Edge <int>(1, 1));
            Assert.IsTrue(g.ContainsEdge(1, 1));
        }
Ejemplo n.º 15
0
        public void TestContainsEdgeWithWithOutSourceNode()
        {
            var g = new UndirectedGraph <int, Edge <int> >(true);

            g.AddVertex(1);
            //g.AddEdge(new Edge<int>(1, 1));
            Assert.IsTrue(g.ContainsEdge(2, 1));
        }
Ejemplo n.º 16
0
        public void TestContainsEdgeWithEdgeWithNoSourceNode()
        {
            var g = new UndirectedGraph <int, Edge <int> >(true);

            g.AddVertex(1);
            g.AddVertex(2);
            g.AddEdge(new Edge <int>(1, 2));
            Assert.IsFalse(g.ContainsEdge(new Edge <int>(3, 1)));
        }
Ejemplo n.º 17
0
        /// <summary>
        /// DFS is applied to traverse graph with color for bipartite
        /// </summary>
        /// <param name="n">N.</param>
        /// <param name="subGraph" />
        /// <param name="maxHash"></param>
        /// <param name="colorDictionary"></param>
        private bool DfsSearch(UndirectedGraph <IHash, Edge <IHash> > n, UndirectedGraph <IHash, Edge <IHash> > subGraph, ref IHash maxHash, Dictionary <IHash, int> colorDictionary)
        {
            //stack
            Stack <IHash> stack = new Stack <IHash>();

            stack.Push(maxHash);
            subGraph.AddVertex(maxHash);

            int color = 1;

            colorDictionary[maxHash] = color;
            bool res = true;


            while (stack.Count > 0)
            {
                IHash cur = stack.Pop();

                maxHash = n.AdjacentDegree(maxHash) > n.AdjacentDegree(cur) ? maxHash : cur;

                //opposite color
                color = colorDictionary[cur] * -1;

                foreach (var edge in n.AdjacentEdges(cur))
                {
                    IHash nei = edge.Source == cur ? edge.Target : edge.Source;

                    //color check
                    if (colorDictionary.Keys.Contains(nei))
                    {
                        if (colorDictionary[nei] != color)
                        {
                            res = false;
                        }
                    }
                    else
                    {
                        //add vertex
                        subGraph.AddVertex(nei);
                        colorDictionary.Add(nei, color);
                        stack.Push(nei);
                    }

                    //add edge
                    if (!subGraph.ContainsEdge(edge))
                    {
                        subGraph.AddEdge(edge);
                    }
                }
            }

            return(res);
        }
Ejemplo n.º 18
0
 private static bool IsClique2(HashSet <int> clique, UndirectedGraph <int, Edge <int> > graph)
 {
     foreach (int v1 in clique)
     {
         foreach (int v2 in clique)
         {
             if (v1 == v2)
             {
                 continue;
             }
             if (!graph.ContainsEdge(v1, v2))
             {
                 return(false);
             }
         }
     }
     return(true);
 }
Ejemplo n.º 19
0
        private bool IsPathCorrect(List <Edge <int> > path, UndirectedGraph <int, Edge <int> > g, int source, int target)
        {
            foreach (var edge in path)
            {
                if (!g.ContainsEdge(edge))
                {
                    throw new ArgumentException("Path is not part of the graph");
                }
            }

            if (path.Count == 0 && source != target)
            {
                return(false);
            }

            int currentSource = source;

            foreach (var edge in path)
            {
                try
                {
                    currentSource = EdmondsAlgorithm.GetTargetVertex(edge, currentSource);
                }
                catch (ArgumentException)
                {
                    return(false);
                }
            }

            if (currentSource == target)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Ejemplo n.º 20
0
        /// <summary>
        /// Enumeration module. NB: If either of <paramref name="allMappings"/> or <paramref name="fileName"/> is null, the other will not be.
        /// </summary>
        /// <param name="allMappings"></param>
        /// <param name="inputGraph">G</param>
        /// <param name="queryGraph">H</param>
        /// <param name="expansionTree">T_k</param>
        /// <param name="parentQueryGraph"></param>
        /// <param name="fileName"></param>
        /// <param name="parentGraphMappings">NB: This param is still used even outside this method is call. So, be careful how you set/clear its values.</param>
        private static IList <Mapping> Algorithm3(Dictionary <QueryGraph, ICollection <Mapping> > allMappings, UndirectedGraph <int> inputGraph, QueryGraph queryGraph,
                                                  AdjacencyGraph <ExpansionTreeNode> expansionTree,
                                                  QueryGraph parentQueryGraph, out string newFileName, string fileName = null)
        {
            newFileName = null;
            ICollection <Mapping> parentGraphMappings;

            if (string.IsNullOrWhiteSpace(fileName))
            {
                if (!allMappings.TryGetValue(parentQueryGraph, out parentGraphMappings))
                {
                    return(new Mapping[0]);
                }
            }
            else
            {
                parentGraphMappings = parentQueryGraph.ReadMappingsFromFile(fileName);
            }
            if (parentGraphMappings.Count == 0)
            {
                return(new Mapping[0]);
            }

            var subgraphSize          = queryGraph.VertexCount;
            var parentQueryGraphEdges = new HashSet <Edge <int> >();

            foreach (var edge in parentQueryGraph.Edges)
            {
                parentQueryGraphEdges.Add(edge);
            }
            var newEdge = GetEdgeDifference(queryGraph, parentQueryGraph, parentQueryGraphEdges);

            parentQueryGraphEdges.Clear();
            parentQueryGraphEdges = null;

            // if it's NOT a valid edge
            if (newEdge.Source == Utils.DefaultEdgeNodeVal)
            {
                return(new Mapping[0]);
            }

            var list = new List <Mapping>();
            int oldCount = parentGraphMappings.Count, id = 0, queryGraphEdgeCount = queryGraph.EdgeCount;
            var queryGraphEdges = queryGraph.Edges.ToArray();

            var groupByGNodes = parentGraphMappings.GroupBy(x => x.Function.Values.ToArray(), MappingNodesComparer); //.ToDictionary(x => x.Key, x => x.ToArray(), MappingNodesComparer);

            foreach (var set in groupByGNodes)
            {
                // function.value (= set of G nodes) are all same here. So build the subgraph here and pass it dowm
                var subgraph = Utils.GetSubgraph(inputGraph, set.Key);
                foreach (var item in set)
                {
                    item.Id = id++;
                    // Remember, f(h) = g

                    // if (f(u), f(v)) ϵ G and meets the conditions, add to list
                    if (item.SubGraphEdgeCount == queryGraphEdgeCount)
                    {
                        var isMapping = Utils.IsMappingCorrect2(item.Function, subgraph, queryGraphEdges, true);
                        if (isMapping.IsCorrectMapping)
                        {
                            list.Add(item);
                        }
                        isMapping = null;
                    }
                    else if (item.SubGraphEdgeCount > queryGraphEdgeCount)
                    {
                        var newEdgeImage = item.GetImage(inputGraph, newEdge);

                        // if it's a valid edge...
                        if (newEdgeImage.Source != Utils.DefaultEdgeNodeVal &&
                            inputGraph.ContainsEdge(newEdgeImage.Source, newEdgeImage.Target))
                        {
                            list.Add(item);
                        }
                    }
                }
                subgraph = null;
            }
            Array.Clear(queryGraphEdges, 0, queryGraphEdges.Length);
            queryGraphEdges = null;
            var threadName = System.Threading.Thread.CurrentThread.ManagedThreadId;

            // Remove mappings from the parent qGraph that are found in this qGraph
            // This is because we're only interested in induced subgraphs
            var theRest = parentGraphMappings.Except(list).ToList();

            parentQueryGraph.RemoveNonApplicableMappings(theRest, inputGraph);
            parentGraphMappings.Clear();
            foreach (var item in theRest)
            {
                parentGraphMappings.Add(item);
            }
            theRest.Clear();
            theRest = null;
            // Now, remove duplicates
            queryGraph.RemoveNonApplicableMappings(list, inputGraph);
            if (!string.IsNullOrWhiteSpace(fileName) && oldCount > parentGraphMappings.Count)
            {
                // This means that some of the mappings from parent fit the current query graph
                newFileName = parentQueryGraph.WriteMappingsToFile(parentGraphMappings);
                try
                {
                    System.IO.File.Delete(fileName);
                }
                catch { } // we can afford to let this fail
            }

            Console.WriteLine("Thread {0}:\tAlgorithm 3: All tasks completed. Number of mappings found: {1}.\n", threadName, list.Count);
            return(list);
        }