コード例 #1
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");
            }
        }
コード例 #2
0
        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();
        }
コード例 #3
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));
                }
            }
        }
コード例 #4
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);
        }
コード例 #5
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);
        }