示例#1
0
        private List <MyGraphEdge> UpdatePath(List <Dijktra_node> path, int from, int to)
        {
            List <MyGraphEdge> result = new List <MyGraphEdge>();
            int current_node          = to;

            do
            {
                for (int i = 0; i < path.Count; i++)
                {
                    if (path[i].check == false)
                    {
                        if (path[i].index == current_node)
                        {
                            MyGraphEdge edge = new MyGraphEdge(path[i].preNode, path[i].index, path[i].lenght);
                            result.Add(edge);
                            current_node = path[i].preNode;

                            if (current_node == from)
                            {
                                break;
                            }
                        }
                    }
                }
            }while (current_node != from);


            result.Reverse();
            //result.RemoveAt(0);
            return(result);
        }
示例#2
0
        public MyGraphEdge GetEdge(int from, int to)
        {
            // Nếu id < số lượng node có nghĩa là node đó ko tồn tại
            if (!isPresent(from) || !isPresent(to))
            {
                return(null);
            }

            for (int i = 0; i < m_DListNode[from].Count; i++)
            {
                if (m_DListNode[from][i].GetTo() == to)
                {
                    MyGraphEdge result = new MyGraphEdge(from, m_DListNode[from][i].GetTo(), m_DListNode[from][i].GetCost());
                    return(result);
                }
            }

            return(null);
        }
示例#3
0
        private MyGraphEdge PrimFindEdge(List <int> p_fNodes, List <MyGraphEdge> p_fEdges)
        {
            MyGraphEdge result = null;
            float       m_fmin = float.MaxValue;

            for (int i = 0; i < p_fNodes.Count; i++)
            {
                // Nếu node có chứa cạnh thì tìm ra cạnh có trọng số nhỏ nhất
                if (this.m_DListNode.ContainsKey(p_fNodes[i]))
                {
                    for (int j = 0; j < m_DListNode[p_fNodes[i]].Count; j++)
                    {
                        // Nếu node đến ko chứa trong mảng đã có thì so sánh
                        MyListGraphEdge temp = m_DListNode[p_fNodes[i]][j];

                        if (p_fNodes.Contains(temp.GetTo()))
                        {
                            continue;
                        }

                        //if (!this.m_bDigraph)
                        //{
                        //    if (p_fNodes.Contains(p_fNodes[i]))
                        //        continue;
                        //}
                        if (m_fmin > temp.GetCost())
                        {
                            m_fmin = temp.GetCost();
                            result = new MyGraphEdge(p_fNodes[i], temp.GetTo(), temp.GetCost());
                        }
                    }
                }
            }


            return(result);
        }
示例#4
0
        public bool AddEdge(MyGraphEdge edge)
        {
            if (!isPresent(edge.GetFrom()) || !isPresent(edge.GetTo()))
            {
                return(false);
            }

            int   from = edge.GetFrom();
            int   to   = edge.GetTo();
            float cost = edge.GetCost();

            if (m_DListNode.ContainsKey(from))
            {
                m_DListNode[edge.GetFrom()].Add(new MyListGraphEdge(to, cost));
            }
            else
            {
                m_DListNode.Add(from, new List <MyListGraphEdge>());
                m_DListNode[from].Add(new MyListGraphEdge(to, cost));
            }

            //// Nếu là đồ thị vô hướng thì thêm 1 cạnh ngược lại
            if (!this.m_bDigraph)
            {
                if (m_DListNode.ContainsKey(to))
                {
                    m_DListNode[to].Add(new MyListGraphEdge(from, cost));
                }
                else
                {
                    m_DListNode.Add(to, new List <MyListGraphEdge>());
                    m_DListNode[to].Add(new MyListGraphEdge(from, cost));
                }
            }
            m_iNumEdge++;
            return(true);
        }
示例#5
0
        public bool AddEdge(MyGraphEdge edge)
        {
            if (!isPresent(edge.GetFrom()) || !isPresent(edge.GetTo()))
            {
                return false;
            }

            int from = edge.GetFrom();
            int to = edge.GetTo();
            float cost = edge.GetCost();

            if (m_DListNode.ContainsKey(from))
            {
                m_DListNode[edge.GetFrom()].Add(new MyListGraphEdge(to, cost));
            }
            else
            {
                m_DListNode.Add(from, new List<MyListGraphEdge>());
                m_DListNode[from].Add(new MyListGraphEdge(to, cost));
            }

            //// Nếu là đồ thị vô hướng thì thêm 1 cạnh ngược lại
            if (!this.m_bDigraph)
            {
                if (m_DListNode.ContainsKey(to))
                {
                    m_DListNode[to].Add(new MyListGraphEdge(from, cost));
                }
                else
                {
                    m_DListNode.Add(to, new List<MyListGraphEdge>());
                    m_DListNode[to].Add(new MyListGraphEdge(from, cost));
                }
            }
            m_iNumEdge++;
            return true;
        }
示例#6
0
        /// <summary>
        /// Tìm cây khung có trọng số nhỏ nhất
        /// </summary>
        /// <returns></returns>
        public MyListGraph PrimAlgo(MyGraphNode startNode)
        {
            MyListGraph result = new MyListGraph(this.m_bDigraph);

            List <int>         p_fNodes = new List <int>();
            List <MyGraphEdge> p_fEdges = new List <MyGraphEdge>();


            p_fNodes.Add(startNode.getIndex());

            while (p_fEdges.Count < this.m_Nodes.Count)
            {
                MyGraphEdge edge = PrimFindEdge(p_fNodes, p_fEdges);
                if (edge != null)
                {
                    p_fNodes.Add(edge.GetTo());
                    p_fEdges.Add(edge);
                }
                else
                {
                    // Đồ thị không liên thông
                    break;
                }
            }

            for (int i = 0; i < p_fNodes.Count; i++)
            {
                result.AddNode(new MyGraphNode(p_fNodes[i]));
            }
            for (int i = 0; i < p_fEdges.Count; i++)
            {
                result.AddEdge(p_fEdges[i]);
            }

            return(result);
        }
示例#7
0
文件: MyMap.cs 项目: hieuntp2/MyGraph
 private void AddEdge(int i, int j, int x, int y, MyMapNode[,] temp_map)
 {
     if (temp_map[x, y].cost > 0)
     {
         MyGraphEdge edge = new MyGraphEdge(temp_map[i, j].index, temp_map[x, y].index, temp_map[i, j].cost);
         m_cGraph.AddEdge(edge);
     }
 }
示例#8
0
 public bool AddEdge(MyGraphEdge edge)
 {
     m_Edges.Add(edge);
     return true;
 }
示例#9
0
        private List<MyGraphEdge> UpdatePath(List<Dijktra_node> path, int from, int to)
        {
            List<MyGraphEdge> result = new List<MyGraphEdge>();
            int current_node = to;

            do
            {
                for(int i = 0; i < path.Count; i++)
                {
                    if(path[i].check == false)
                    {
                        if (path[i].index == current_node)
                        {
                            MyGraphEdge edge = new MyGraphEdge(path[i].preNode, path[i].index, path[i].lenght);
                            result.Add(edge);
                            current_node = path[i].preNode;

                            if(current_node == from)
                            {
                                break;
                            }
                        }
                    }
                }
            }
            while (current_node != from);

            result.Reverse();
            //result.RemoveAt(0);
            return result;
        }
示例#10
0
        private MyGraphEdge PrimFindEdge(List<int> p_fNodes, List<MyGraphEdge> p_fEdges)
        {
            MyGraphEdge result = null;
            float m_fmin = float.MaxValue;

            for (int i = 0; i < p_fNodes.Count; i++)
            {

                // Nếu node có chứa cạnh thì tìm ra cạnh có trọng số nhỏ nhất
                if (this.m_DListNode.ContainsKey(p_fNodes[i]))
                {
                    for (int j = 0; j < m_DListNode[p_fNodes[i]].Count; j++)
                    {
                        // Nếu node đến ko chứa trong mảng đã có thì so sánh
                        MyListGraphEdge temp = m_DListNode[p_fNodes[i]][j];

                        if (p_fNodes.Contains(temp.GetTo()))
                        {
                            continue;
                        }

                        //if (!this.m_bDigraph)
                        //{
                        //    if (p_fNodes.Contains(p_fNodes[i]))
                        //        continue;
                        //}
                        if (m_fmin > temp.GetCost())
                        {
                            m_fmin = temp.GetCost();
                            result = new MyGraphEdge(p_fNodes[i], temp.GetTo(), temp.GetCost());
                        }

                    }
                }
            }

            return result;
        }
示例#11
0
        public MyGraphEdge GetEdge(int from, int to)
        {
            // Nếu id < số lượng node có nghĩa là node đó ko tồn tại
            if (!isPresent(from) || !isPresent(to))
            {
                return null;
            }

            for (int i = 0; i < m_DListNode[from].Count; i++)
            {
                if (m_DListNode[from][i].GetTo() == to)
                {
                    MyGraphEdge result = new MyGraphEdge(from, m_DListNode[from][i].GetTo(), m_DListNode[from][i].GetCost());
                    return result;
                }
            }

            return null;
        }
示例#12
0
 public bool AddEdge(MyGraphEdge edge)
 {
     m_Edges.Add(edge);
     return(true);
 }