Esempio n. 1
0
 /// <summary>
 /// Trả về các cạnh kề với đỉnh
 /// </summary>
 /// <param name="start">Đỉnh xét</param>
 /// <returns>Null nếu đỉnh không có cạnh kề</returns>
 public EdgeCollection this[Node start]
 {
     get
     {
         EdgeCollection edgeCollection = new EdgeCollection();
         foreach (Edge item in _list)
         {
             if (item.start == start)
             {
                 edgeCollection.Add(item);
             }
             else if (item.IsUndirected && item.end == start)
             {
                 edgeCollection.Add(item);
             }
         }
         if (edgeCollection.Count > 0)
         {
             return(edgeCollection);
         }
         else
         {
             return(null);
         }
     }
 }
Esempio n. 2
0
 void matrix_convert(Matrix a)
 {
     Clear();
     for (int i = 0; i < a.n; i++)
     {
         _nodeList.Add(new Node(i));
     }
     for (int i = 0; i < a.n; i++)
     {
         Node start = _nodeList[i];
         for (int j = 0; j < a.n; j++)
         {
             if (a[i, j] != 0)
             {
                 Node end  = _nodeList[j];
                 Edge edge = new Edge(start, end, a[i, j]);
                 _edgeList.Add(edge);
             }
         }
     }
     IsUndirected = true;
     for (int i = 0; i < _edgeList.Count; i++)
     {
         if (_edgeList[i].IsUndirected == false)
         {
             IsUndirected = false;
             break;
         }
     }
     xdBac_node();
 }
Esempio n. 3
0
        public EdgeCollection GetAllEdgeFromNodes(IEnumerable <Node> nodes)
        {
            EdgeCollection edgeCollection = new EdgeCollection();

            foreach (Edge edge in _edgeList)
            {
                if (nodes.Contains(edge.start) && edge.start.IsVisit && !edge.end.IsVisit)
                {
                    edgeCollection.Add(edge);
                }
                else
                {
                    if (edge.IsUndirected && edge.end.IsVisit && !edge.start.IsVisit && nodes.Contains(edge.end))
                    {
                        edgeCollection.Add(edge);
                    }
                }
            }
            return(edgeCollection);
        }
Esempio n. 4
0
        /// <summary>
        /// Tìm cây khung nhỏ nhất theo thuật toán Kruskal
        /// </summary>
        /// <param name="a">Mảng chứa danh sách kề</param>
        /// <param name="n">Số đỉnh</param>
        static void Kruskal(int[,] a, int n)
        {
            EdgeCollection list = matrix_edge_convert(a, n);

            list.Sort();
            EdgeCollection listResult = new EdgeCollection();

            //danh dau nhan i cho dinh i
            int[] label = new int[n];
            for (int i = 0; i < n; i++)
            {
                label[i] = i;
            }
            int lab1 = 0;
            int lab2 = 0;

            foreach (Edge item in list)
            {
                if (label[item.start.Index] != label[item.end.Index])
                {
                    listResult.Add(item);
                    if (label[item.start.Index] > label[item.end.Index])
                    {
                        lab1 = label[item.end.Index];
                        lab2 = label[item.start.Index];
                    }
                    else
                    {
                        lab2 = label[item.end.Index];
                        lab1 = label[item.start.Index];
                    }
                    for (int i = 0; i < n; i++)
                    {
                        if (label[i] == lab2)
                        {
                            label[i] = lab1;
                        }
                    }
                }
            }
            foreach (Edge item in listResult)
            {
                Console.WriteLine(item.ToString());
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Tìm cây khung nhỏ nhất theo thuật toán Prim
        /// </summary>
        public EdgeCollection Prim(Node startNode = null)
        {
            Reset();
            EdgeCollection listResult = new EdgeCollection();

            if (startNode == null)
            {
                startNode = _nodeList[0];
            }
            Node node = startNode;

            node.IsVisit = true;
            NodeCollection nodes = new NodeCollection();

            nodes.Add(node);
            Edge edge = null;
            int  min  = 0;
            int  n    = 0;

            while (!_nodeList.IsAllVisit)
            {
                n++;
                min = 0;
                EdgeCollection edges = GetAllEdgeFromNodes(nodes);
                foreach (Edge item in edges)
                {
                    if (min > item.weight || min == 0)
                    {
                        min  = item.weight;
                        edge = item;
                    }
                }
                if (edge != null)
                {
                    listResult.Add(edge);
                    edge.start.IsVisit = true;
                    edge.end.IsVisit   = true;
                    nodes.Add(edge.start);
                    nodes.Add(edge.end);
                }
            }
            return(listResult);
        }
Esempio n. 6
0
        /// <summary>
        /// Tìm cây khung nhỏ nhất theo thuật toán Krukal
        /// </summary>
        public EdgeCollection Kruskal()
        {
            Reset();
            EdgeCollection listResult = new EdgeCollection();

            _edgeList.Sort();
            //danh dau nhan i cho dinh i
            int[] label = new int[n];
            for (int i = 0; i < n; i++)
            {
                label[i] = i;
            }
            int lab1 = 0;
            int lab2 = 0;

            foreach (Edge item in _edgeList)
            {
                if (label[item.start.Index] != label[item.end.Index])
                {
                    listResult.Add(item);
                    if (label[item.start.Index] > label[item.end.Index])
                    {
                        lab1 = label[item.end.Index];
                        lab2 = label[item.start.Index];
                    }
                    else
                    {
                        lab2 = label[item.end.Index];
                        lab1 = label[item.start.Index];
                    }
                    for (int i = 0; i < n; i++)
                    {
                        if (label[i] == lab2)
                        {
                            label[i] = lab1;
                        }
                    }
                }
            }
            return(listResult);
        }
Esempio n. 7
0
        /// <summary>
        /// Chuyển ma trận kề về danh sách các cạnh
        /// </summary>
        /// <param name="a">ma trận kề</param>
        /// <param name="n">số đỉnh</param>
        /// <returns>Danh sách cạnh</returns>
        static EdgeCollection matrix_edge_convert(int[,] a, int n)
        {
            EdgeCollection listEdge = new EdgeCollection();
            Edge           edge;

            for (int i = 0; i < n; i++)
            {
                Node start = new Node(i);
                for (int j = i; j < n; j++)
                {
                    if (a[i, j] > 0)
                    {
                        Node end = new Node(j);
                        edge = new Edge(start, end, a[i, j]);
                        listEdge.Add(edge);
                    }
                }
            }
            // listEdge.Sort();
            return(listEdge);
        }