图的邻接表和邻接矩阵 没写完放着。。

#include <iostream>
#include <memory.h>
#include <stack>
#include <queue>
using namespace std;

class GraphList; 

//边结点的结构体
struct Edge
{
    friend class GraphList;
    int VerAdj; //邻接顶点序号,从0开始编号
    int cost; //边的权值
    Edge*Link; //指向下一边节点的指针
    Edge(int aVerAdj,int acost,Edge*aLink=NULL){ VerAdj=aVerAdj;cost=acost;Link=aLink;}
    Edge(Edge*aLink=NULL){Link=aLink;}
}; 

//顶点表中结点的结构体
struct Vertex
{
    friend class GraphList;
    int VerName;//顶点名称
    Edge*Adjacent;//边链表的头指针
    Vertex(int name,Edge*adjacent=NULL){VerName=name;Adjacent=adjacent;}
    Vertex(Edge*adjacent=NULL){Adjacent=adjacent;}
};

 //采用邻接表储存的Graph_List类定义 其中所有边均为正权
class GraphList
{
private:
    Vertex*Head; //顶点表头指针
    int GraphSize;         //图中当前顶点的个数
    int MaxGraphSize;   //图中最大顶点个数
    int e;           //图中当前边的个数
public:
    GraphList(int MaxGraphSize);
    virtual ~GraphList();

    int IsEmpty() const{return !GraphSize;}       //判断图是否为空
    int IsFull()const{return GraphSize==MaxGraphSize;} //判断顶点数是否已满 

    int NumberOfVertices()const{return GraphSize;}//返回图中顶点的个数
    int NumberOfEdges()const{return e;}                //返回图中边的个数

    int    GetWeight(const int v1,const int v2)const;//返回指定边的权值 

    int GetFirstNeighbor(const int v)const;//返回序号为v的第一个邻接顶点的序号
    int GetNextNeighbor(const int v1,const int v2)const;//返回序号为v1的顶点相对于序号为v2的点的下一个邻接顶点的序号

    int GetNeighbors(const int v,int A[])const;//得到顶点v的邻接顶点表 返回邻接顶点个数 

                    //    bool InsertVertex(const int v);//向图中插入一个顶点
    bool InsertEdge(const int v1,const int v2,int weight);//向图中插入一条边 

                    //    bool DeleteVertex(const int v);//在图中删除一个顶点
    bool DeleteEdge(const int v1,const int v2);//在图中删除一条边

    void DepthFirstSearch();//从顶点表的第一个顶点开始进行图的深度优先搜索
    void DFS(const int v,int visited[]);//从顶点V开始进行图的深度优先搜索
    void NDFS(const int v);//从顶点V开始进行图的深度优先搜索的迭代算法
    void BFS(const int v);//从顶点V开始进行图的广度优先搜索

    void Output();//输出邻接表 

                                //void TopoOrder();//输出图的拓扑排序
                                //void CriticalPath();//输出图的关键路径

                                //void ShortestPath(const int v);//在非权图中求指定顶点到其他所有顶点的最短路径
                                //void DShortestPath(const int v);//在正权图中求指定定点到其他所有定点的最短路径
                                //void AllLength();//在正权图中求每对顶点间的最短路径

                                //void Prim();//构造图的最小支撑树的普里姆算法
                                //void Krustral();
};

//=============================================================================
//构造函数
GraphList::GraphList(int maxgraphsize):MaxGraphSize(maxgraphsize)
{
    int from,to,weight;
    //用数组实现顶点表,Head指向数组的第一个元素
    Head=new Vertex[MaxGraphSize];

    cout<<"请输入顶点数和边数"<<endl;
    cin>>GraphSize;
    cin>>e;

    //初始化:
    //顶点名字从0 -> GraphSize-1
    for(int i=0;i<GraphSize;i++)
    {
        Head[i].VerName=i;
        Head[i].Adjacent=NULL;
    }
    //空结点初始化
    for(int i=GraphSize;i<MaxGraphSize;i++)
    {
        Head[i].VerName=-1;
        Head[i].Adjacent=NULL;
    }

    for(int i=0;i<e;i++)
    {
        cout<<"输入始点,终点和权值:(这是第"<<i+1<<"条边) 空格隔开"<<endl;
        cin>>from>>to>>weight;//输入新边的始点终点和权值 

        Edge*p=new Edge(to,weight,NULL);
        Edge*q=Head[from].Adjacent;

        if(q==NULL){
            Head[from].Adjacent=p;  //如果是第一条边
        }else {
            while(q->Link!=NULL)    //如果不是第一条边,插到边结点最后
            {
                q=q->Link;
            }
            q->Link=p;
        }
    }
    cout<<"创建成功!"<<endl;
}

//析构函数
GraphList::~GraphList()
{
    for(int i=0;i<MaxGraphSize;i++)
    {
        Edge*p=Head[i].Adjacent;
        while(p!=NULL)
        {
            Edge*q=p->Link;
            delete p;
            p=q;
        }
    }
    delete []Head;
}

int GraphList::GetWeight(const int v1,const int v2)const //返回v1->v2的边的权
{
    //如果其中一个顶点不存在
    if(v1==-1||v2==-1) return 0;

    Edge*p=Head[v1].Adjacent;
    while(p!=NULL)
    {
        if(p->VerAdj==v2)
            return p->cost;
        p=p->Link;
    }
    return 0;
}

int GraphList::GetFirstNeighbor(const int v)const //返回第一个邻接点 若找不到则返回-1
{
    if(v==-1||v>=GraphSize) return -1;    

    Edge*p=Head[v].Adjacent;
    if(p!=NULL)
        return p->VerAdj;
    else return -1;
}

int GraphList::GetNextNeighbor(const int v1,const int v2)const //返回v1除了v2以外的下一个邻接点
{
    if(v1==-1||v1>=GraphSize||v2==-1||v2>=GraphSize)
    {
        //cout<<"找不到"<<endl;
        return -1;
    }
    Edge*p=Head[v1].Adjacent;
    while(p!=NULL&&p->VerAdj!=v2)//找到v2
        p=p->Link;

    if(p==NULL||p->Link==NULL)
    {
        //cout<<"找不到"<<endl;
        return -1;
    }
    p=p->Link;
    return p->VerAdj;
}

int GraphList::GetNeighbors(const int v,int A[])const  //得到顶点v的邻接顶点表 返回v的邻接顶点个数
{
    if(v==-1||v>=GraphSize) return 0; 

    memset(A,0,sizeof(A));//初始化

    int i=0;
    Edge*p=Head[v].Adjacent;
    while(p!=NULL)
    {
        A[i++]=p->VerAdj;
        p=p->Link;
    }
    return i;
}

/*
bool GraphList::InsertVertex(const int v)//插入一个顶点
{
    if(v<MaxGraphSize&&v>=0)
    {
        if(Head[v].VerName==-1)
        {
            Head[v].VerName=v;
            GraphSize++;
            return 1;
        }
        cout<<"顶点已存在";
        return 0;
    }
    cout<<"越界,插入失败"<<endl;
    return 0;
}
*/

bool GraphList::InsertEdge(const int v1,const int v2,int weight) //插入指定边
{
    if(v1==-1||v2==-1||v1>=GraphSize||v2>=GraphSize) return 0;
    Edge*q=new Edge(v2,weight,NULL);
    Edge*p=Head[v1].Adjacent;

    if(p==NULL){
        Head[v1].Adjacent=q;
    }else {
        while(p->Link!=NULL&&p->VerAdj!=v2)
            p=p->Link;
        if(p->VerAdj==v2) return 0;//如果已经有这条边则插入失败
        p->Link=q;
    }
    e++;
    return 1;
}

bool GraphList::DeleteEdge(const int v1,const int v2)//删除指定边
{
    if(v1==-1||v2==-1||v1>=GraphSize||v2>=GraphSize) return 0;
    Edge*p=Head[v1].Adjacent;
    Edge*q=Head[v1].Adjacent;
    if(p!=NULL)
    {

        if(p->VerAdj==v2) //如果V2是第一个节点
        {
            Head[v1].Adjacent=p->Link;
            delete p;
            e--;
            return 1;
        }

        //如果V2不是第一个节点
        while(p!=NULL&&p->VerAdj!=v2)
        {
            q=p;
            p=p->Link;
        }
        if(p==NULL)
        {
            //cout<<"无该边"<<endl;
            return 0;
        }
        q->Link=p->Link;
        delete p;
        e--;
        return 1;
    }
    //cout<<"无该边"<<endl;
    return 0;

}

/*bool GraphList::DeleteVertex(const int v)
{
    if(v<0||v>=MaxGraphSize||Head[v].VerName==-1) return -1;
    //删除始点为v的边
    Edge*p=Head[v].Adjacent;
    while(p!=NULL)
    {
        Head[v].Adjacent=p->Link;
        delete p;
        e--;
        p=Head[v].Adjacent;
    }
    //删除终点为v的边
    for(int i=0;i<GraphSize;i++)
    {
        if(i==v) continue;
        Edge*p=Head[i].Adjacent;
        Edge*q;
        while(p!=NULL)
        {
            q=p->Link;
            if(q!=NULL&&q->VerAdj==v)
            {
                p->Link=q->Link;
                delete q;
                e--;
            }
            p=p->Link;
        }

    }
    GraphSize--;
    Head[v]=Vertex(-1);
    return 1;

}
*/

//==============================================================

void GraphList::DepthFirstSearch()
{
    int*visited=new int[GraphSize];
    int i=0;
    while(i<GraphSize)
    {
        visited[i++]=0;
    }
    DFS(0,visited);
    delete[]visited;
}

void GraphList::DFS(int v,int visited[])
{
    cout<<v<<" ";
    visited[v]=1;
    Edge*p=Head[v].Adjacent;
    while(p!=NULL)
    {
        if(visited[p->VerAdj]!=1)
        {
            DFS(p->VerAdj,visited);
        }
        p=p->Link;
    }
}

void GraphList::NDFS(int v)
{
    //初始化
    if(v==-1||v>=GraphSize) return;
    int*visited=new int[GraphSize];
    int i=0;
    while(i<GraphSize) {visited[i++]=0;}
    stack<int>S;

    //访问顶点v
    cout<<v<<" ";
    visited[v]=1;
    S.push(v);

    int w;

    while(!S.empty())
    {
        int v=S.top();
        S.pop();
        w=GetFirstNeighbor(v);
        while(w!=-1)
        {
            if(!visited[w])
            {
                cout<<w<<" ";
                visited[w]=1;
                S.push(w);
            }
            w=GetNextNeighbor(v,w);
        }

    }
} 

void GraphList::BFS(const int s)
{
    //初始化
    if(s==-1||s>=GraphSize) return;
    int*visited=new int[GraphSize];
    int i=0;
    while(i<GraphSize) {visited[i++]=0;}
    //访问顶点s
    cout<<s<<" ";
    visited[s]=1;
    queue<int>q;
    q.push(s);
    while(!q.empty())
    {
        int v=q.front();
        q.pop();
        int w=GetFirstNeighbor(v);
        while(w!=-1)
        {
            if(!visited[w])
            {
                cout<<w<<" ";
                visited[w]=1;
                q.push(w);
            }
            w=GetNextNeighbor(v,w);
        }

    }
    delete []visited;
}

/*
void GraphList::TopoOrder()
{
    //初始化
    int n=GraphSize;
    int *count=new int[n];
    for(int i=0;i<n;i++) count[i]=0;
    for(int i=0;i<n;i++)
    {
        Edge*p=Head[i].Adjacent;
        while(p!=NULL){
            count[p->VerAdj]++;
            p=p->Link;
        }
    }
    int top=-1;

    for(int i=0;i<n;i++)
        if(count[i]==0)
        {
            count[i]=top;top=i; //入栈
        }
    for(int i=0;i<n;i++)
    {
        if(top==-1)
        {
            cout<<"There is a cycle in network!"<<endl;
        }
        else
            {
                int j=top;
                top=count[top];
                cout<<j<<" ";//出栈
                Edge*p=Head[j].Adjacent;
                while(p!=NULL)
                {
                    int k=p->VerAdj;
                    if(--count[k]==0)
                    {
                        count[k]=top;top=k;
                    }
                    p=p->Link;
                }
            }
    }
    delete []count;
}
/*
void critical_path()
{
    int ve[n];
    for(int i=0;i<n;i++)
    {
        ve[i]=0;
    }
    for(int i=0;i<n-1;i++)
    {
        p=Head[i]->adjacent(Head[i]);
        while(p!=NULL)
        {
            k=p->VerAdj;
            if(ve[i]+p->cost>ve[k])
                ve[k]=ve[i]+p->cost;
            p=p->Link;
        }
    }
    for(int i=0;i<n;i++)
    {
        vl[i]=ve[n];
     }
    for(i=n-1;i>0;i++)
    {
        p=Head[i].adjacent;
        while(p!=NULL)
        {
            k=p->VerAdj;
            if(vl[k]-p->cost<v[i])
            {
                vl[i]=vl[k]-p->cost;
            }
            p=p->Link;
        }
    }
    for(int i=0;i<n;i++)
    {
        p=Head[i]->adjacent;
        while(p!=NULL)
        {
            k=p->VerAdj;
            e=ve[i];
            l=vl[k]-p->cost;

        }
        if(l=e)
            printf("<%d,%d> is Critical Activity!");
        p=p->Link;
    }
} 

*/

void GraphList::Output(){
    cout<<"当前图如下:"<<endl;
    int A[e];
    for(int i=0;i<GraphSize;i++)
    {
        cout<<"H["<<i<<"]->";
        Vertex H=Head[i];
        int n=GetNeighbors(i,A);

        for(int j=0;j<n;j++)
        {
            cout<<A[j]<<"->";
        }
        cout<<"\n";
    }
}
  1 #include <iostream>
  2 #include <memory.h>
  3 using namespace std;
  4
  5 const int MaxWeight=1000;
  6
  7 class Graph_Matrix
  8 {
  9 private:
 10     int MaxGraphSize;
 11     int **Edge;
 12     int GraphSize;//顶点数
 13     int e;//边数
 14     int alllengthflag;
 15     int**path;
 16     int**A;
 17 public:
 18     Graph_Matrix(int Max);
 19     Graph_Matrix(const Graph_Matrix&A);
 20     void Create();
 21     virtual~Graph_Matrix();
 22
 23
 24     int IsEmpty() const{return !GraphSize;};
 25     int GraphFull()const{return GraphSize==MaxGraphSize;}
 26     int NumberOfVertices()const{return GraphSize;}
 27     int NumberOfEdges()const;//返回图的边的个数
 28     int    GetWeight(const int &v1,const int&v2);//返回指定边的权值
 29     void GetNeighbors(const int &v,int A[]);//返回顶点v的邻接顶点表
 30
 31     //void InsertVertex(const int &v);//向图中插入一个顶点
 32     bool InsertEdge(const int &v1,const int &v2,int weight);//向图中插入一条边
 33     bool SetEdge(const int &v1,const int &v2,int weight);
 34
 35     bool DeleteVertex(const int &v);//在图中删除一个顶点
 36     bool DeleteEdge(const int &v1,const int &v2);//在图中删除一条边
 37
 38     int GetDiameter();
 39                             //void DepthFirstSearch();//采用递归的方法从顶点表的第一个顶点开始进行图的深度优先搜索
 40                             //void DFS(const int v);//从顶点V开始进行图的深度优先搜索
 41                             //void BFS(const int v);//从顶点V开始进行图的广度优先搜索
 42
 43     bool TopoOrder();//检测是否有环,如果无环输出图的拓扑序列
 44
 45                         //void CriticalPath();//输出图的关键路径
 46
 47                         //void ShortestPath(const int v);//在非权图中求指定顶点到其他所有顶点的最短路径
 48                           //void DShortestPath(const int v);//在正权图中求指定定点到其他所有定点的最短路径
 49
 50     void AllLength();//在正权图中求每对顶点间的最短路径
 51
 52     bool IsFreeTree(); //检测是否为自由树
 53
 54
 55
 56                          //void Prim();//构造图的最小支撑树的普里姆算法
 57                         //void Output();//用来测试
 58 };
 59
 60 Graph_Matrix::Graph_Matrix(int Max=100):MaxGraphSize(Max),alllengthflag(0)
 61 {
 62     cout<<"开始"<<endl;
 63     Edge=new int*[Max];
 64     for(int i=0;i<Max;i++)
 65         Edge[i]=new int[Max];
 66     for(int i=0;i<Max;i++)
 67         for(int j=0;j<Max;j++)
 68             Edge[i][j]=MaxWeight;
 69     Create();
 70 }
 71
 72
 73 void Graph_Matrix::Create()
 74 {
 75     int from,to,weight;
 76     int E;
 77     int i=0;
 78     cout<<"请输入顶点和边的个数"<<endl;
 79     cin>>GraphSize>>E;
 80     while(i<E)
 81     {
 82         cout<<"请输入第"<<++i<<"条边的始点 终点 权"<<endl;
 83         cin>>from>>to>>weight;
 84         InsertEdge(from,to,weight);
 85     }
 86     cout<<"创建成功!"<<endl;
 87 }
 88
 89
 90 Graph_Matrix::Graph_Matrix(const Graph_Matrix&A)
 91 {
 92     e=A.e;
 93     GraphSize=A.GraphSize;
 94     MaxGraphSize=A.MaxGraphSize;
 95     Edge=new int*[MaxGraphSize];
 96     for(int i=0;i<MaxGraphSize;i++)
 97         Edge[i]=new int[MaxGraphSize];
 98     for(int i=0;i<MaxGraphSize;i++)
 99         for(int j=0;j<MaxGraphSize;j++)
100             Edge[i][j]=A.Edge[i][j];
101
102 }
103
104
105 bool Graph_Matrix::IsFreeTree()
106 {
107     Graph_Matrix A(*this);//建立自己的副本
108     //计算每个结点的入度
109     int count[A.GraphSize];
110     memset(count,0,sizeof(count));
111     for(int j=0;j<A.GraphSize;j++)//列
112     {
113         for(int i=0;i<A.GraphSize;i++) //行
114         {
115             if(A.Edge[i][j]!=MaxWeight) count[j]++;
116         }
117         cout<<"count["<<j<<"]="<<count[j]<<endl;
118     }
119
120     int temp=0; //计算度为0的点的个数
121
122     int top=-1;//初始化栈顶指针
123     for(int i=0;i<A.GraphSize;i++)
124     {
125         if(count[i]==0)//将入度为0的点入栈
126         {
127             count[i]=top;
128             top=i;
129             temp++;
130         }
131     }
132
133     if(temp!=1) return 0;/*如果有多个度为0的点,则不可能为自由树*/
134
135
136     for(int i=0;i<A.GraphSize;i++)
137     {
138         //cout<<"top="<<top<<endl;
139
140         /*循环群体还未被执行GraphSize次,已经没有入度为0的点,说明有回路*/
141         if(top==-1)
142         {
143             //cout<<"有回路"<<endl;
144             return 0;
145         }
146         else
147         {
148             int j=top;top=count[top];/*从栈中弹出一个顶点 j*/
149             cout<<j<<" ";
150
151             /*删除与j相关的边并更新count*/
152             for(int p=0;p<A.GraphSize;p++)
153             {
154                 if(A.Edge[j][p]!=MaxWeight)
155                 {
156                     A.Edge[j][p]=MaxWeight;
157                     count[p]--;
158                     A.e--;
159                 }
160             }
161             //for(int p=0;p<A.GraphSize;p++)
162             //    cout<<"count["<<p<<"]="<<count[p]<<endl;
163
164             for(int p=0;p<A.GraphSize;p++)
165             {
166                 if(count[p]==0)/*将入度为0的点入栈*/
167                 {
168                     count[p]=top;
169                     top=p;
170                 }
171             }
172
173         }
174     }
175     return 1;
176 }
177
178
179 void Graph_Matrix::AllLength()       //在正权图中求每对顶点间的最短路径
180 {
181     alllengthflag=1;                    //方便析构函数处理,如调用此函数才析构相关数组
182
183     /*初始化*/
184     int n=GraphSize;
185     path=new int*[n]; //path记录最短路径终点前一个结点
186     A=new int*[n];    //A记录最短路径
187     for(int i=0;i<n;i++)
188     {
189         path[i]=new int[n];
190         A[i]=new int[n];
191     }
192     for(int i=0;i<n;i++)
193     {
194         for(int j=0;j<n;j++)
195         {
196             A[i][j]=Edge[i][j];
197             if(i!=j&&A[i][j]<MaxWeight)
198             {
199                 path[i][j]=i;
200             }
201             else
202             {
203                 path[i][j]=-1;
204             }
205         }
206     }
207
208
209     for(int k=0;k<n;k++)//加入k作为中间结点
210     {
211         for(int i=0;i<n;i++)
212         {
213             if(i!=k)
214             {
215                 for(int j=0;j<n;j++)
216                 {
217                     if(j!=k/*以k作为终点*/&&j!=i/*到自己*/&&A[i][j]!=MaxWeight&&A[i][k]+A[k][j]<A[i][j])
218                     {
219                         //更新
220                         A[i][j]=A[i][k]+A[k][j];
221                         path[i][j]=path[k][j];
222                     }
223                 }
224             }
225         }
226     }
227
228     cout<<"最短路径最后经过的点为:"<<endl;
229     printf("      ");
230     for(int j=0;j<n;j++)
231         printf("% 5d ",j);
232     cout<<endl;
233     for(int i=0;i<n;i++)
234     {
235         printf("% 5d ",i);
236         for(int j=0;j<n;j++)
237         {
238             printf("% 5d ",path[i][j]);
239         }
240         cout<<endl;
241     }
242     cout<<"每对顶点最短路径长度为:"<<endl;
243     printf("      ");
244     for(int j=0;j<n;j++)
245         printf("% 5d ",j);
246     printf("\n");
247     for(int i=0;i<n;i++)
248     {
249         printf("% 5d ",i);
250         for(int j=0;j<n;j++)
251         {
252             printf("% 5d ",A[i][j]);
253         }
254         cout<<endl;
255     }
256 }
257
258
259 int Graph_Matrix::GetDiameter()
260 {
261     if(IsFreeTree()==0)
262     {
263         cout<<"不是有向树"<<endl;
264         return -1;
265     }
266
267     AllLength();
268
269     int d=0;
270     for(int i=0;i<GraphSize;i++)
271     {
272         for(int j=0;j<GraphSize;j++)
273         {
274             if(A[i][j]>d&&A[i][j]!=MaxWeight) d=A[i][j];
275         }
276     }
277     cout<<"直径是"<<d<<endl;
278     return d;
279 }
280
281 int    Graph_Matrix::GetWeight(const int &v1,const int&v2){
282     if(v1==-1||v2==-1||v1>=GraphSize||v2>=GraphSize) return 0;
283
284     return Edge[v1][v2];
285 }
286
287 bool Graph_Matrix::DeleteEdge(const int&v1,const int &v2)//删除指定边
288 {
289     if(v1==-1||v2==-1||v1>=GraphSize||v2>=GraphSize) return 0;
290     if(Edge[v1][v2]==MaxWeight) return 0;
291     Edge[v1][v2]==MaxWeight;
292     e--;
293     return 1;
294 }
295
296 bool Graph_Matrix::InsertEdge(const int &v1,const int &v2,int weight)//向图中插入一条边
297 {
298     if(v1==-1||v2==-1||v1>=GraphSize||v2>=GraphSize) return 0;
299     if(v1==v2) return 0;
300     if(Edge[v1][v2]!=MaxWeight) return 0;
301     Edge[v1][v2]=weight;
302     e++;
303     return 1;
304 }
305
306 bool Graph_Matrix::SetEdge(const int &v1,const int &v2,int weight)//修改指定边的权值
307 {
308     if(v1==v2) return 0;
309     if(v1==-1||v2==-1||v1>=GraphSize||v2>=GraphSize) return 0;
310     if( InsertEdge(v1,v1,weight)==0)    //插入不成功则该边已存在
311         Edge[v1][v2]=weight;
312     return 1;
313 }
314
315 bool Graph_Matrix::DeleteVertex(const int &v)//删除顶点
316 {
317     if(v==-1||v>=GraphSize) return 0;
318     for(int i=0;i<GraphSize;i++)
319     {
320         if(Edge[i][v]!=MaxGraphSize)
321         {
322             Edge[i][v]=MaxGraphSize;
323             e--;
324         }
325     }
326
327     for(int i=0;i<GraphSize;i++)
328     {
329         if(Edge[v][i]!=MaxGraphSize)
330         {
331             Edge[v][i]=MaxGraphSize;
332             e--;
333         }
334     }
335
336 }
337
338
339
340
341
342 /*
343 void Graph_Matrix::Output()
344 {
345     for(int i=0;i<GraphSize;i++)
346     {
347         for(int j=0;j<GraphSize ;j++)
348         {
349             printf("% 5d ",Edge[i][j]);
350         }
351         cout<<endl;
352     }
353
354 }//测试函数
355 */
356
357 bool Graph_Matrix::TopoOrder()
358 {    Graph_Matrix A(*this);//建立自己的副本
359     //计算每个结点的入度
360     int count[A.GraphSize];
361     memset(count,0,sizeof(count));
362     for(int j=0;j<A.GraphSize;j++)//列
363     {
364         for(int i=0;i<A.GraphSize;i++) //行
365         {
366             if(A.Edge[i][j]!=MaxWeight) count[j]++;
367         }
368         //cout<<"count["<<j<<"]="<<count[j]<<endl;
369     }
370
371     int top=-1;//初始化栈顶指针
372     for(int i=0;i<A.GraphSize;i++)
373     {
374         if(count[i]==0)//将入度为0的点入栈
375         {
376             count[i]=top;
377             top=i;
378         }
379     }
380
381     cout<<"拓扑序列为:";
382
383
384     for(int i=0;i<A.GraphSize;i++)
385     {
386         //cout<<"top="<<top<<endl;
387         //循环群体还未被执行GraphSize次,已经没有入度为0的点,说明有回路
388         if(top==-1)
389         {
390             cout<<"有回路"<<endl;
391             return 0;
392         }
393         else
394         {
395             int j=top;top=count[top];//从栈中弹出一个顶点 j
396             cout<<j<<" ";
397             //删除与j相关的边并更新count
398
399             for(int p=0;p<A.GraphSize;p++)
400             {
401                 if(A.Edge[j][p]!=MaxWeight)
402                 {
403                     A.Edge[j][p]=MaxWeight;
404                     count[p]--;
405                     A.e--;
406                 }
407             }
408             //for(int p=0;p<A.GraphSize;p++)
409             //    cout<<"count["<<p<<"]="<<count[p]<<endl;
410
411             for(int p=0;p<A.GraphSize;p++)
412             {
413                 if(count[p]==0)//将入度为0的点入栈
414                 {
415                     count[p]=top;
416                     top=p;
417                 }
418             }
419
420         }
421     }
422 }
423 Graph_Matrix::~Graph_Matrix()
424 {
425     for(int i=0;i<GraphSize;i++)
426         delete[]Edge[i];
427     delete[]Edge;
428
429     if(alllengthflag==1)
430     {
431         for(int i=0;i<GraphSize;i++)
432             delete[]A[i];
433         delete[]A;
434         for(int i=0;i<GraphSize;i++)
435             delete[]path[i];
436         delete[]path;
437     }
438 }
439
440 int main() {
441     Graph_Matrix A(100);
442     //A.Output();
443
444     //A.TopoOrder();
445     A.GetDiameter();
446     return 0;
447 }
时间: 2024-08-01 06:45:44

图的邻接表和邻接矩阵 没写完放着。。的相关文章

邻接表和邻接矩阵手写简洁代码DFS BFS

这是通过邻接矩阵进行DFS #include<iostream> #include<string> #include<windows.h> #define Max_ver_num 20 using namespace std ; bool visit[Max_ver_num] ;//这个数组的用途是标记 struct HGraph{ string vexs [ Max_ver_num ] ; //放每个顶点的数组的名字 int arcs [Max_ver_num][Ma

看数据结构写代码(36) 图的邻接表表示与实现

图的邻接表表示法,是为每一个顶点建立一个链表,链表里存放着相同弧尾的 弧的信息,这些链表顺序存放在数组中.下面是无向图g2的邻接表 邻接表 比 邻接矩阵 节省空间,同时 也带来一些操作上的 不便,例如 看 两个顶点是否 相邻,需要 遍历 链表,在 求 无向图顶点的度时,只需 遍历 顶点的链表,而 求 有向图 顶点的度 需要 遍历 整个图 查找 弧头 为这个顶点的 个数. 如果 不想这样做,可以 建立 逆邻接表,即 链表里 存放着 相同 弧头的 弧 的信息. 下一节 要说的 十字链表 类似于这种结

算法导论--图的存储(邻接表与邻接矩阵)

转载请注明出处:勿在浮沙筑高台http://blog.csdn.net/luoshixian099/article/details/51888031 图的存储方法有邻接表.邻近矩阵.邻接多重表.十字链表等.本篇文章介绍两种简单且比较常用的两种方法:邻接表与邻接矩阵方法. 以下面的无向图为例,介绍两种存储方法.有向图的存储方法类似,只是边是单方向,无向图的边可以看做双向. 1.邻接链表法 邻接链表表示法对图中的每个顶点建立一个带头的边链表:第i条链表代表依附于顶点vi所有边信息,若为有向图,则表示

图的邻接表表示、广度优先、深度优先搜索

图,就是我们在数据结构中学到的图,它是一种存储信息的结构.图是一类在实际应用中非常常见的数据结构,当数据规模大到一定程度时,如何对其进行高效计算即成为迫切需要解决的问题.最常见的大规模图数据的例子就是互联网网页数据,网页之间通过链接指向形成规模超过500 亿节点的巨型网页图.再如,Facebook 社交网络也是规模巨大的图,仅好友关系已经形成超过10 亿节点.千亿边的巨型图,考虑到Facebook 正在将所有的实体数据节点都构建成网状结构,其最终形成的巨型网络数据规模可以想见其规模.要处理如此规

数据结构之---C语言实现图的邻接表存储表示

// 图的数组(邻接矩阵)存储表示 #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_NAME 3 // 顶点字符串的最大长度+1 #define MAX_VERTEX_NUM 20 typedef int InfoType; // 存放网的权值 typedef char VertexType[MAX_NAME]; // 字符串类型 typedef enum{DG, DN, AG

《数据结构》C++代码 邻接表与邻接矩阵

       上一篇"BFS与DFS"写完,突然意识到这个可能偏离了"数据结构"的主题,所以回来介绍一下图的存储:邻接表和邻接矩阵.        存图有两种方式,邻接矩阵严格说就是一个bool型的二维数组,map[i][j]表示i到j有没有单向边,邻接表则是对1~N中每个点都拉出一个链表来,链表E[i]中存的每个点j都表示i到j有一条单向边. 这两种方式各有利弊,在稀疏图中,邻接表更好,省时间而且省空间:在稠密图中,邻接矩阵更好,不浪费时间的同时省去了指针域的空间

浅谈数据结构之图的邻接表深度和广度优先遍历(九)

邻接矩阵是一种不错的图存储结构,但是我们发现,对于边数相对较少的图,这种结构是存在对存储空间的极大浪费的.我们知道,顺序存储结构存在预先分配内存可能造成空间浪费的问题,于是引出了链式存储的结构.同样的,我们也可以考虑对边或弧使用链式存储的方式来避免空间浪费的问题.因此,对于图的存储结构,我们同样引入了一种数组与链表相组合的存储方法,我们一般称之为邻接表. 邻接表的处理方法是这样的:(1).图中顶点用一个一维数组存储,当然,顶点也可以用单链表来存储,不过数组可以较容易的读取顶点的信息,更加方便:另

POJ 2387 Til the Cows Come Home dijkstra算法 用邻接表和邻接矩阵

题目如下: Til the Cows Come Home Time Limit: 1000MS        Memory Limit: 65536K Total Submissions: 27726        Accepted: 9353 Description Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wa

图(邻接表)

我们先来看一个图 我们想将这个图的信息存储到邻接表中,我们需要一个数组保存节点信息,还要有一个节点用来保存与该节点相邻的节点信息. 1 typedef struct arc_node 2 { 3 int pos; 4 int distance; 5 struct arc_node * next; 6 } Arc_node;//保存Node节点的相邻节点信息 7 8 typedef struct node 9 { 10 node_type info; 11 Arc_node * next; 12