博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

数据结构课程设计第二击-----用C#实现关键路径

Posted on 2006-03-15 21:53  OOspurs  阅读(2208)  评论(3编辑  收藏  举报
        前几天写了一篇数据结构课程设计-----用C#实现双向链表,现在这篇是它的姐妹篇,那个课程设计做两道题目,第一道就是用双向链表来实现仓库存储.第二题就是实现关键路径,这两个作业现在已经基本上完成了.为什么我用C#来实现呢?因为我希望这两个作业有一个良好的操作界面,而用.NET来编WINDOWS程序的效率很高帮我节省大量的时间.而且,我想用面向对象的方法来实现这些数据结构,提高自己面向对象编程的能力.我先用类封装这些数据结构,之后把它编译成装配件,最后在winform程序上引用.
        下面是我实现的关键路径类
using System;
using System.Collections.Generic;
using System.Text;

namespace LJH.AOE
{
    
//项目过程
    public class ProjectItem
    
{
        
//起始项目
        private int begin;
        
//终止项目
        private int end;
        
//所用的时间
        private int buttom;
        
//构造函数
        public ProjectItem(int b, int e, int but)
        
{
            begin 
= b;
            end 
= e;
            buttom 
= but;
        }

        
//属性
        public int Begin
        
{
            
get
            
{
                
return begin;
            }

            
set
            
{
                begin 
= value;
            }

        }

        
public int End
        
{
            
get
            
{
                
return end;
            }

            
set
            
{
                end 
= value;
            }

        }

        
public int Buttom
        
{
            
get
            
{
                
return buttom;
            }

            
set
            
{
                buttom 
= value;
            }

        }

    }

    
//结点类
    public class EdgeNode
    
{
        
private int adjvex;  /* 邻接点域  */
        
private int period;  /* 时间域 */
        
private EdgeNode next;
        
public int Adjvex
        
{
            
get
            
{
                
return adjvex;
            }

            
set
            
{
                adjvex 
= value;
            }

        }

        
public int Period
        
{
            
get
            
{
                
return period;
            }

            
set
            
{
                period 
= value;
            }

        }

        
public EdgeNode Next
        
{
            
get
            
{
                
return next;
            }

            
set
            
{
                next 
= value;
            }

        }

    }


    
//定义链接图的类型
    public class Vexnode
    
{
        
private string vertex;
        
private int id;
        
private EdgeNode link;
        
//属性
        public string Vertex
        
{
            
get
            
{
                
return vertex;
            }

            
set
            
{
                vertex 
= value;
            }

        }

        
public int ID
        
{
            
get
            
{
                
return id;
            }

            
set
            
{
                id 
= value;
            }

        }

        
public EdgeNode Link
        
{
            
get
            
{
                
return link;
            }

            
set
            
{
                link 
= value;
            }

        }


    }

    
//AOE网
    public class AlGraph
    
{
        
private Vexnode[] graphic;
        
private int projectnum;
        
private int activenum;
        
private int totaltime;
        
int[] vl ;
        
int[] ve ;
        
int[] l ;
        
int[] e ;

        
//属性
        public Vexnode[] Graphic
        
{
            
get
            
{
                
return graphic;
            }

            
set
            
{
                graphic 
= value;
            }

        }

        
public int TotalTime
        
{
            
get
            
{
                
return totaltime;
            }

        }

        
public int[] Vl
        
{
            
get
            
{
                
return vl;
            }

        }

        
public int[] Ve
        
{
            
get
            
{
                
return ve;
            }

        }

        
public int[] L
        
{
            
get
            
{
                
return l;
            }

        }

        
public int[] E
        
{
            
get
            
{
                
return e;
            }

        }

        
//构造函数
        public AlGraph(int pnum, int anum)
        
{
            totaltime 
= 0;
            projectnum 
= pnum;
            activenum 
= anum;
            graphic 
= new Vexnode[pnum];
            
for (int i = 0; i < projectnum; i++)
            
{
                graphic[i] 
= new Vexnode();
            }

        }

        
//建立AOE网
        private void CreateGraph(ProjectItem[] item)
        
{

            
for (int i = 0; i < projectnum; i++)
            
{
                graphic[i].Vertex 
= i.ToString();
                graphic[i].ID 
= 0;
                graphic[i].Link 
= null;
            }

            
for (int k = 0; k < activenum; k++)
            
{
                EdgeNode p 
= new EdgeNode();
                p.Adjvex 
= item[k].End - 1;
                p.Period 
= item[k].Buttom;
                graphic[item[k].End 
- 1].ID++;
                p.Next 
= graphic[item[k].Begin - 1].Link;
                graphic[item[k].Begin 
- 1].Link = p;
            }

        }


        
//搜索关键路径
        public bool SearchMap(ProjectItem[] item)
        
{
            
int rear = -1, m = 0, front = -1, j, k;

            EdgeNode p;
            CreateGraph(item);
            
int[] topstack = new int[projectnum];

            l 
= new int[activenum*2];
            e 
= new int[activenum*2];
            vl 
= new int[projectnum];
            ve 
= new int[projectnum];
            
for (int i = 0; i < projectnum; i++)
            
{
                ve[i] 
= 0;
            }

            
for (int i = 0; i < projectnum; i++)
            
{
                
if (graphic[i].ID == 0)
                
{
                    topstack[
++rear] = i;
                    m
++;
                }

            }


            
while (front != rear)
            
{
                front
++;
                j 
= topstack[front];
                m
++;
                p 
= graphic[j].Link;
                
while (p != null)
                
{
                    k 
= p.Adjvex;
                    graphic[k].ID
--;
                    
if (ve[j] + p.Period > ve[k])
                    
{
                        ve[k] 
= ve[j] + p.Period;
                    }

                    
if (graphic[k].ID == 0)
                    
{
                        topstack[
++rear] = k;
                    }

                    p 
= p.Next;
                }

            }

            
if (m < projectnum)
            
{
                
return false;
            }


            totaltime 
= ve[projectnum - 1];
            
for (int i = 0; i < projectnum; i++)
            
{
                vl[i] 
= totaltime;
            }

            
for (int i = projectnum - 2; i >= 0; i--)
            
{
                j 
= topstack[i];
                p 
= graphic[j].Link;
                
while (p != null)
                
{
                    k 
= p.Adjvex;
                    
if ((vl[k] - p.Period) < vl[j])
                        vl[j] 
= vl[k] - p.Period;
                    p 
= p.Next;
                }

            }

            
return true;
        }



    }

}