图功能的实现C++(最短路径,关键路径,拓扑排序,关节点,~~~~~~~~~~等等)

本文介绍了如何使用C++实现图的各种功能,包括创建无向网、有向网、邻接矩阵、邻接表、十字链表存储结构的图,并提供了关键路径、拓扑排序等算法的实现。通过实例化Graph模板类,调用相应的成员函数即可进行操作。
摘要由CSDN通过智能技术生成

先按照注释的提示新建三个头文件Graph,Lqueue,stack,把相应头文件的实现代码分别拷进去,最后在源程序中包含这三个头文件。

在main()函数中调用各种功能时(比如求关键路径),先实例化一个模板的对象,如Graph<int> gph;

接下来要调用创建图的函数(共有三种存储结构:邻接矩阵的,邻接表的,十字链表的---可根据单词缩写判断是哪个函数)

例如要求关键路径,需先创建一个以邻接表作存储结构的图。可调用gph.CreateAlgraph();
这时创建工作完成了,可以调用求关键路径的函数了。gph.Criticalpath();                                

这时已经可以运行求解了。但为了不浪费资源,我们还需要调用一个销毁邻接表的函数来回收空间。

最后再加一句gph.DestroyAlgraph();

这时整个程序就OK了。

关于图的其它功能函数,还请读者自己慢慢看。本人就不赘述了!

 

//**********Graph.h
#ifndef _GRAPH_H
#define _GRAPH_H
#include "Lqueue.h"
#include "stack.h"

template <class TElemType>
struct Treenode
{
 TElemType data;
 Treenode<TElemType> *lchild,*nextsibling;
};                                 //用于深度优先生成森林

template <class TElemType>
class Graph
{
 public:
  void CreateUDN();
  void DestroyUDN();
  void CreateDN();
  void DestroyDN();
  void CreateAlgraph();
  void DestroyAlgraph();
  void CreateOlgraph();
  void DestroyOlgraph();
  void DFS(int v,bool *visited);
  void DFSTraverse();
  void BFSTraverse();
  void DFSForest(Treenode<TElemType> *&T);
  void DFSTree(int v,Treenode<TElemType> *&T,bool *visited);
  void PreDFSForest(Treenode<TElemType> *T);
  void DFSOlgraph(int v,bool *visited,int *finished);  //表示顺向深度优先搜索
  void DFSOlgraph(int v,bool *visited);
  void Conponderance();                                //求有向图的强连通分量
  void Minispantree_prim();                            //PRIM算法求最小生成树
  void DFSAlgraph(int v,int *low,int *visited);
  void DFSAlgraph(int v,int *visited);                 //检验头结点是否关节点
  void FindCritic();                                   //无向图求其关节点
  void TopologicalSort();                              //有向图拓扑排序,检验环的存在与否
  float * TopologicalOrder(stack<int> &sre,int &e);
  void Criticalpath();                                 //求有向无环图关键路径
  void Shortestpath_DIJ(TElemType data1,TElemType data2); //对环不适用,如从V1到V1就不适用
  void Shortestpath_FLOYD(TElemType data1,TElemType data2);
 private:
  int count;
  template <class TElemType>
  struct Mgraph{
   int vexnum;
   int arcnum;
   TElemType *vertex;
   int **AdjMatrix;
  };
  Mgraph<TElemType> gph;        //邻接矩阵存储


   struct Arcnode       
  {
   int adjvex;
   Arcnode *nextarc;
   float weight;
  };

  template <class TElemType>
  struct Vexnode
  {
   TElemType data;
   Arcnode *firarc;
  };
  struct ALgraph
  {
   int vexnum;
   int arcnum;
   bool kind;
   Vexnode<TElemType> *vex;
  };
  ALgraph algraph;           //邻接表存储


  struct ArcBox
  {
   int headvex,tailvex;
   ArcBox *hlink,*tlink;
   float weight;
  };

  template <class TElemType>
  struct Vertex
  {
   TElemType data;
   ArcBox *firstin,*firstout;
  };

  struct Olgraph
  {
   int vexnum,arcnum;
   Vertex<TElemType> *vex;
  };
  Olgraph olgraph;               //有向图的十字链表存储结构


};


//*********Graph.cpp
template <class TElemType>
void Graph<TElemType>::CreateUDN()
{
 cout << "输入无向网的顶点数和边数:" << endl;
 cin >> gph.vexnum >> gph.arcnum;
 gph.vertex = (TElemType *)malloc(gph.vexnum * sizeof(TElemType));
 int i,j,m,n;                                   //m,n表示顶点信息对应的序号,w是权值
 int w;
 TElemType v1,v2;
 cout << "输入顶点信息:" << endl;
 for(i = 0;i < gph.vexnum;i++)
  cin >> gph.vertex[i];
 gph.AdjMatrix = (int **)malloc(gph.vexnum * sizeof(int *));
 for(i = 0;i < gph.vexnum;i++)
  gph.AdjMatrix[i] = (int *)malloc(gph.vexnum * sizeof(int));
 for(i = 0;i < gph.vexnum;i++)
  for(j = 0;j < gph.vexnum;j++)
   gph.AdjMatrix[i][j] = INT_MAX;                   //INT_MAX
 cout << "输入一条边依附的两点及权值:" << endl;
 for(int k = 0;k < gph.arcnum;k++)
 {
  cin >> v1 >> v2 >> w;
  for(i = 0;i < gph.vexnum;i++)
  {
   if(v1 == gph.vertex[i]) m = i;
   if(v2 == gph.vertex[i]) n = i;
  }
  gph.AdjMatrix[m][n] = gph.AdjMatrix[n][m] = w;
 }
}


template <class TElemType>
void Graph<TElemType>::DestroyUDN()
{
 free(gph.vertex);
 for(i = 0;i < gph.vexnum;i++)
  free(gph.AdjMatrix[i]);
 free(gph.AdjMatrix);
}


template <class TElemType>
void Graph<TElemType>::CreateDN()
{
 cout << "输入有向图的顶点数和边数:" << endl;
 cin >> gph.vexnum >> gph.arcnum;
 gph.vertex = (TElemType *)malloc(gph.vexnum * sizeof(TElemType));
 int i,j,m,n;                                   //m,n表示顶点信息对应的序号,w是权值
 int w;
 TElemType v1,v2;
 cout << "输入顶点信息:" << endl;
 for(i = 0;i < gph.vexnum;i++)
  cin >> gph.vertex[i];
 gph.AdjMatrix = (int **)malloc(gph.vexnum * sizeof(int *));
 for(i = 0;i < gph.vexnum;i++)
  gph.AdjMatrix[i] = (int *)malloc(gph.vexnum * sizeof(int));
 for(i = 0;i < gph.vexnum;i++)
  for(j = 0;j < gph.vexnum;j++)
   gph.AdjMatrix[i][j] = INT_MAX;                   //INT_MAX
 cout << "输入弧尾弧头两点及权值:" << endl;
 for(int k = 0;k < gph.arcnum;k++)
 {
  cin >> v1 >> v2 >> w;
  for(i = 0;i < gph.vexnum;i++)
  {
   if(v1 == gph.vertex[i]) m = i;
   if(v2 == gph.vertex[i]) n = i;
  }
  gph.AdjMatrix[m][n] = w;
 }
}


template <class TElemType>
void Graph<TElemType>::DestroyDN()
{
 for(int i = 0;i < gph.vexnum;i++)
  free(gph.AdjMatrix[i]);
 free(gph.AdjMatrix);
 free(gph.vertex);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值