当前位置: 首页>>代码示例>>C#>>正文


C# Graph.RemoveVertex方法代码示例

本文整理汇总了C#中Graph.RemoveVertex方法的典型用法代码示例。如果您正苦于以下问题:C# Graph.RemoveVertex方法的具体用法?C# Graph.RemoveVertex怎么用?C# Graph.RemoveVertex使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Graph的用法示例。


在下文中一共展示了Graph.RemoveVertex方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: DirectedValue

        public void DirectedValue()
        {
            var graph = new Graph<int>(true);
            var vertex1 = new Vertex<int>(1);
            var vertex2 = new Vertex<int>(2);
            var vertex3 = new Vertex<int>(3);

            graph.AddVertex(vertex1);
            graph.AddVertex(vertex2);
            graph.AddVertex(vertex3);

            graph.AddEdge(vertex1, vertex2);
            graph.AddEdge(vertex3, vertex2);
            graph.AddEdge(vertex1, vertex3);

            Assert.AreEqual(graph.Edges.Count, 3);
            Assert.AreEqual(graph.Vertices.Count, 3);

            Assert.IsTrue(graph.RemoveVertex(1));

            Assert.AreEqual(graph.Edges.Count, 1);
            Assert.AreEqual(graph.Vertices.Count, 2);

            Assert.IsFalse(graph.RemoveVertex(4));
            Assert.AreEqual(graph.Edges.Count, 1);
            Assert.AreEqual(graph.Vertices.Count, 2);
        }
开发者ID:havok,项目名称:ngenerics,代码行数:27,代码来源:RemoveVertex.cs

示例2: Johnson

                /// <summary>
                /// Finds the shortest path between all pairs of vertices in the graph, if no negative cycles are present.
                /// </summary>
                /// <param name="lengths">The lengths of the shortest paths between all pairs of vertices.</param>
                /// <param name="parents">The parent-pointers for the paths between all pairs of vertices.</param>
                /// <returns><code>true</code> if there are no negative cycles; <code>false</code> otherwise.</returns>
                public bool Johnson(out Dictionary<uint, Dictionary<uint, int>> lengths, out Dictionary<uint, Dictionary<uint, uint>> parents)
                {
                    lengths = null;
                    parents = null;

                    uint free = 0;
                    foreach (uint v in this.Vertices.Keys)
                        if (v >= free)
                            free = v + 1;

                    Graph g = new Graph();
                    foreach (Edge e in this.Edges)
                        g.AddEdge(e.From, e.To, false, e.Weight, e.Capacity);
                    foreach (uint v in this.Vertices.Keys)
                        g.AddEdge(free, v, true, 0);

                    Dictionary<uint, int> l;
                    Dictionary<uint, uint> p;
                    if (!this.BellmanFord(free, out l, out p))
                        return false;

                    foreach (Edge e in g.Edges)
                        e.Weight += l[e.From] - l[e.To];

                    g.RemoveVertex(free);

                    lengths = new Dictionary<uint, Dictionary<uint, int>>();
                    parents = new Dictionary<uint, Dictionary<uint, uint>>();
                    foreach (uint v in g.Vertices.Keys)
                    {
                        g.Dijkstra(v, out l, out p);
                        lengths[v] = l;
                        parents[v] = p;
                    }

                    return true;
                }
开发者ID:jlvermeulen,项目名称:algorithms-and-data-structures,代码行数:43,代码来源:Johnson.cs

示例3: RemoveVertexFromValueExample

        public void RemoveVertexFromValueExample()
        {
            // Initialize a new directed graph instance
            var graph = new Graph<int>(true);

            // Add 2 vertices to the graph
            graph.AddVertex(1);
            graph.AddVertex(2);

            // The graph contains 2 vertices
            Assert.AreEqual(graph.Vertices.Count, 2);

            // Remove vertex vertex1
            var removeResult = graph.RemoveVertex(1);

            // The vertex was found in the graph
            Assert.IsTrue(removeResult);

            // The graph now contains only one vertex
            Assert.AreEqual(graph.Vertices.Count, 1);
        }
开发者ID:havok,项目名称:ngenerics,代码行数:21,代码来源:GraphExamples.cs

示例4: mergeVertex

        //Метод для злиття вершин(повертає true, якщо злиття відбулося)
        private bool mergeVertex(Graph graph, int indexOfGraph, params DataVertex[] list)
        {
            //Перевірка чи кількість елементів модуля <=5
            int count = 0;
            for (int n = 0; n < calc.moduls[indexOfGraph].Count; n++)
            {
                string namev = "";
                foreach (string s in calc.moduls[indexOfGraph].ElementAt(n))
                {
                    namev += s;
                }
                if (list.Any(x => x.Text == namev))
                {
                    count += calc.moduls[indexOfGraph].ElementAt(n).Count;
                }
            }
            if (count > 5 || list == null) return false;//Якщо модуль буде містити більше 5 елементів, то злиття не проводимо
            /////////////////////////////////////////////
            for (int i = 1; i < list.Length; i++)
            {
                //Видаляю спільні зв'язки
                _gArea.LogicCore.Graph.RemoveEdgeIf(x => x.Source == list[0] && x.Target == list[i]);
                graph.RemoveEdgeIf(x => x.Source == list[0] && x.Target == list[i]);
                _gArea.LogicCore.Graph.RemoveEdgeIf(x => x.Target == list[0] && x.Source == list[i]);
                graph.RemoveEdgeIf(x => x.Target == list[0] && x.Source == list[i]);
                //Зливаю елементи модулей та видаляю злиті
                for (int j = 0; j < calc.moduls[indexOfGraph].Count; j++)
                {
                    string str = "";
                    foreach (string s in calc.moduls[indexOfGraph].ElementAt(j))//Формую ім'я модуля
                    {
                        str += s;
                    }
                    if (str == list[0].Text)//Знаходжу відповідні вершині модулі
                    {
                        //calc.moduls[indexOfGraph].ElementAt(j).Add(list[i].Text);
                        calc.moduls[indexOfGraph].ElementAt(j).AddRange(nameVertexAndModuls[indexOfGraph][list[i].Text]);
                        //Міняю назву ключа(Створюю новий, а старий видаляю)
                        List<string> l = new List<string>();
                        l.AddRange(nameVertexAndModuls[indexOfGraph][list[i].Text]);

                        nameVertexAndModuls[indexOfGraph].Remove(str);
                        nameVertexAndModuls[indexOfGraph].Add(str + list[i].Text, l);
                        //////
                        nameVertexAndModuls[indexOfGraph][str + list[i]] = calc.moduls[indexOfGraph].ElementAt(j);
                    }
                    if (str == list[i].Text)//Видаляю злитий
                    {
                        calc.moduls[indexOfGraph].RemoveAt(j);
                        nameVertexAndModuls[indexOfGraph].Remove(str);
                        //nameVertexAndModuls[indexOfGraph][list[i].Text].Clear();
                        //nameVertexAndModuls[indexOfGraph][list[i].Text].AddRange(calc.moduls[indexOfGraph].ElementAt(j));
                    }
                }
                //Додаю назву вершини до загальної
                list[0].Text += list[i].Text;
                //Перенаправляю всі зв'язки
                var inE = new List<DataEdge>();
                inE.AddRange(graph.InEdges(list[i]));
                foreach (DataEdge e in inE)
                {
                    //e.Target = list[0];
                    //e.Text = string.Format("{0} -> {1}", e.Source, list[0]);

                    var Edge = new DataEdge(e.Source, list[0]) { Text = string.Format("{0} -> {1}", e.Source, list[0]) };
                    _gArea.LogicCore.Graph.AddEdge(Edge);
                    graph.AddEdge(Edge);
                }

                var outE = new List<DataEdge>();
                outE.AddRange(graph.OutEdges(list[i]));
                foreach (DataEdge e in outE)
                {
                    //e.Source = list[0];
                    //e.Text = string.Format("{0} -> {1}", list[0], e.Target);
                    var Edge = new DataEdge(list[0], e.Target) { Text = string.Format("{0} -> {1}", list[0], e.Target) };
                    _gArea.LogicCore.Graph.AddEdge(Edge);
                    graph.AddEdge(Edge);
                }

                //Видаляю вершини
                if (i == list.Count())
                {
                    _gArea.LogicCore.Graph.RemoveVertex(list[0]);
                    graph.RemoveVertex(list[0]);
                }
                _gArea.LogicCore.Graph.RemoveVertex(list[i]);
                graph.RemoveVertex(list[i]);

            }
            return true;
        }
开发者ID:Sh-alex,项目名称:FCS,代码行数:93,代码来源:GraphVisual.cs

示例5: createVerificationModuls

        //Уточнення модулів
        ////////////////5 Лаба
        public void createVerificationModuls()
        {
            totalGraph = allGraphs.Last();
            //Зливаю назви вершин і модулі в один словник
            nameVertexAndM = new Dictionary<string, List<string>>();

            for (int i = 0; i < nameVertexAndModuls.Count; i++)
            {
                for (int j = 0; j < nameVertexAndModuls[i].Count; j++)
                {
                    try//Ключ, який вже є в словнику не додаємо
                    {
                        nameVertexAndM.Add(nameVertexAndModuls[i].ElementAt(j).Key, nameVertexAndModuls[i].ElementAt(j).Value);
                    }
                    catch (Exception)
                    {

                    }
                }
            }
            nameVertexAndM = nameVertexAndM.OrderBy(a => a.Value.Count).ToDictionary(pair => pair.Key, pair => pair.Value);

            //Зливаю модулі для кожної групи в один List
            for (int i = 0; i < calc.moduls.Count; i++)
            {
                foreach (List<string> list in calc.moduls[i])
                {
                    calc.modulsAfterVerification.Add(list);
                }
            }

            calc.modulsAfterVerification.Sort((a, b) => a.Count - b.Count);//Сортую за кількістю елементів
            //Формую такі самі множини для перевірки підмножин
            List<HashSet<string>> setModuls = new List<HashSet<string>>();
            for (int i = 0; i < calc.modulsAfterVerification.Count; i++)
            {
                setModuls.Add(new HashSet<string>());
                for (int j = 0; j < calc.modulsAfterVerification[i].Count; j++)
                {

                    setModuls[i].Add(calc.modulsAfterVerification[i][j]);
                }
            }
            //Видаляю модулі, які є підмножиною інших модулів
            for (int i = 0; i < calc.modulsAfterVerification.Count - 1; i++)
            {
                for (int j = i + 1; j < calc.modulsAfterVerification.Count; j++)
                {

                    if (setModuls[i].IsSubsetOf(setModuls[j]))//Являється підмножиною?
                    {
                        calc.modulsAfterVerification.RemoveAt(i);
                        setModuls.RemoveAt(i);
                        //////
                        string temp = "";
                        foreach(string str in setModuls[i])
                        {
                            temp += str;
                        }
                        /////

                        totalGraph.RemoveVertex(totalGraph.Vertices.First(x => x.Text == temp));
                        nameVertexAndM.Remove(temp);
                        i--;
                        break;
                    }
                }
            }
            //Видаляю елементи, які є в інших модулях(з більшого)
            for (int i = 0; i < calc.modulsAfterVerification.Count - 1; i++)
            {
                for (int j = 0; j < calc.modulsAfterVerification[i].Count; j++)
                {
                    for (int k = 1; k < calc.modulsAfterVerification.Count; k++)
                    {
                        if (calc.modulsAfterVerification[k].Contains(calc.modulsAfterVerification[i][j]))
                        {
                            if (k == i) continue;
                            ///////////////////
                            if (calc.modulsAfterVerification[k].Count >= calc.modulsAfterVerification[i].Count)
                                calc.modulsAfterVerification[k].Remove(calc.modulsAfterVerification[i][j]);
                            else
                            {
                                calc.modulsAfterVerification[i].Remove(calc.modulsAfterVerification[i][j]);
                                j--;
                            }
                            ///////////////////
                            if (calc.modulsAfterVerification[i].Count == 0) calc.modulsAfterVerification.RemoveAt(i);
                            //////
                            string namev = "";
                            foreach (string s in calc.modulsAfterVerification[k])
                            {
                                namev += s;
                            }
                            totalGraph.Vertices.ElementAt(k).Text = namev;
                        }
                    }
                }
//.........这里部分代码省略.........
开发者ID:Sh-alex,项目名称:FCS,代码行数:101,代码来源:GraphVisual.cs

示例6: ExceptionNullVertex

 public void ExceptionNullVertex()
 {
     var graph = new Graph<int>(true);
     graph.RemoveVertex(null);
 }
开发者ID:havok,项目名称:ngenerics,代码行数:5,代码来源:RemoveVertex.cs


注:本文中的Graph.RemoveVertex方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。