Example #1
0
        /// <summary>
        /// Add relations of tables to an undirected graph representing the relations between tables
        /// </summary>
        /// <param name="relationGraph">Relationship graph</param>
        public void AddRelationsToGraph(UnDirectedGraph <Table> relationGraph)
        {
            Dictionary <Table, GenericNode <Table> > nodes = new Dictionary <Table, GenericNode <Table> >();

            // Create graph nodes for each table
            foreach (var table in Tables)
            {
                nodes[table] = new GenericNode <Table>(table);
            }

            // Add vertices to graph
            relationGraph.AddVertexRange(nodes.Values);

            // Add edges to graph
            foreach (var node in nodes.Values)
            {
                foreach (var relation in node.Data.Relations)
                {
                    var anchorNode = nodes[relation.AnchorTable];
                    // If current node is anchor, then don't add an edge. Prevents duplicate edges
                    if (node.Data.Equals(anchorNode.Data))
                    {
                        continue;
                    }

                    relationGraph.AddEdge(new GenericEdge <Table>(node, anchorNode));
                }
            }
        }
Example #2
0
        public static bool IsCycle(UnDirectedGraph g)
        {
            DisJointSets dj = new DisJointSets();

            Subset[] subsets = new Subset[g.Vertices.Length];

            dj.MakeSet(subsets);
            for (int i = 0; i < g.Vertices.Length; i++)
            {
                int u = i;
                foreach (var edge in g.Vertices[i].Edges)
                {
                    int v = edge.Destination.Index;

                    int x = dj.FindSet(subsets, u);
                    int y = dj.FindSet(subsets, v);

                    if (x == y)
                    {
                        return(true);
                    }

                    dj.Union(subsets, x, y);
                }
            }
            return(false);
        }
Example #3
0
        public override void Run()
        {
            var g = new UnDirectedGraph(5);

            g.AddEdge(1, 0);
            g.AddEdge(0, 2);
            g.AddEdge(2, 0);
            g.AddEdge(0, 3);
            g.AddEdge(3, 4);

            if (g.isCyclic())
            {
                Console.WriteLine("Graph contains cycle");
            }
            else
            {
                Console.WriteLine("Graph doesn't contains cycle");
            }

            var g2 = new UnDirectedGraph(3);

            g2.AddEdge(0, 1);
            g2.AddEdge(1, 2);
            if (g2.isCyclic())
            {
                Console.WriteLine("Graph contains cycle");
            }
            else
            {
                Console.WriteLine("Graph doesn't contains cycle");
            }
        }
        public static void FindMinimumSpaningTree(UnDirectedGraph g)
        {
            DisJointSets dj = new DisJointSets();

            Subset[]    subsets  = new Subset[g.Vertices.Length];
            List <Edge> edgeList = new List <Edge>();

            dj.MakeSet(subsets);

            for (int i = 0; i < g.Vertices.Length; i++)
            {
                edgeList.AddRange(g.Vertices[i].Edges);
            }
            edgeList = edgeList.OrderBy(o => o.Weight).ToList();

            foreach (var edge in edgeList)
            {
                int x = dj.FindSet(subsets, edge.Source.Index);
                int y = dj.FindSet(subsets, edge.Destination.Index);

                if (x != y)
                {
                    Console.Write($"({edge.Source.Index},{edge.Destination.Index}) ");
                    dj.Union(subsets, x, y);
                }
            }
        }
        public override void Run()
        {
            var g2 = new UnDirectedGraph(3);

            g2.AddEdge(0, 1);
            g2.AddEdge(1, 2);
            g2.AddEdge(2, 0);

            var result = g2.isCyclic();
        }
Example #6
0
        private static UnDirectedGraph <string> GetDirectedGraph(IList <Tuple <string, string> > uniqueKeys)
        {
            var levDistance = new LevenhteinDistance();
            IList <Tuple <string, string, int> > graph = uniqueKeys.Select(x =>
                                                                           new Tuple <string, string, int>(x.Item1, x.Item2, levDistance.Calculate(x.Item1, x.Item2))).ToList();

            var directedGraph = new UnDirectedGraph <string>();

            foreach (var node in graph)
            {
                directedGraph.AddEdge(node.Item1, node.Item2, node.Item3);
            }

            return(directedGraph);
        }
Example #7
0
        private static UnDirectedGraph GetGraph1(DirectedGraph graph = null)
        {
            var    graph1 = new UnDirectedGraph();
            Vertex v0     = new Vertex(0, "A");
            Vertex v1     = new Vertex(1, "B");
            Vertex v2     = new Vertex(2, "C");
            Vertex v3     = new Vertex(3, "D");

            Vertex[] vertices = new Vertex[] { v0, v1, v2, v3 };
            graph1.AddVertices(vertices);
            graph1.AddEdge(v0, v1, 5);
            graph1.AddEdge(v1, v2, 6);
            graph1.AddEdge(v2, v3, 1);
            //graph1.AddEdge(v3, v0,2);

            return(graph1);
        }
Example #8
0
        /// <summary>
        /// Using relations split or merge tables together in common models
        /// </summary>
        /// <param name="commonModels">List of common models</param>
        /// <returns>Grouped list of common models</returns>
        public List <CommonModel> GroupCommonModels(List <CommonModel> commonModels)
        {
            List <CommonModel> groupedCommonModels = new List <CommonModel>();

            // Construct graph
            UnDirectedGraph <Table> graph = new UnDirectedGraph <Table>();

            commonModels.ForEach(c => c.AddRelationsToGraph(graph));

            // List of all not visited vertices
            HashSet <GenericNode <Table> > notVisited = new HashSet <GenericNode <Table> >(graph.Vertices);
            // List the current DFS has found
            List <Table> foundTables = new List <Table>();

            // Create an depthfirstsearch algorithm, which for each discovered vertex adds it to the found list
            var algorithm = new UndirectedDepthFirstSearchAlgorithm <GenericNode <Table>, GenericEdge <Table> >(graph);

            algorithm.DiscoverVertex += vertex =>
            {
                notVisited.Remove(vertex);
                foundTables.Add(vertex.Data);
            };

            // Perform depthfirstsearch on graph and group all related databases together in one common model
            do
            {
                // Get current root and run DFS
                var currRoot = notVisited.First();
                algorithm.Compute(currRoot);

                groupedCommonModels.Add(new CommonModel(foundTables));

                // Clean for next run
                foundTables.Clear();
            } while (notVisited.Count > 0);


            return(groupedCommonModels);
        }
 public static bool IsCycle(UnDirectedGraph g)
 {
     return(DFS(g.Vertices[0], null));
 }