数据结构C++ 图

#include <iostream>
#include <string>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

#define TURE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define maxSize 100
typedef int Status;
typedef int ElemType;


//邻接矩阵
#define MaxInt 32767          //表示无穷大
#define MVNum 100             //最大顶点数
typedef char VerTexType;      //设顶点的数据类型为字符型
typedef int ArcType;          //假设边的权值类型为整型

typedef struct{
    VerTexType vexs[MVNum];     //顶点表
    ArcType arcs[MVNum][MVNum];  //邻接矩阵
    int vexnum,arcnum;          //图的当前点数和边数
}AMGraph;//Adjacency Matrix Graph

//用邻接矩阵创建无向网
Status CreateUDN(AMGraph &G){
    cin>>G.vexnum>>G.arcnum;
    for(int i=0;i<G.vexnum;i++){
        cin>>G.vexs[i];
    }
    for(int i=0;i<G.vexnum;++i)
        for(int j=0;j<G.vexnum;++j)
            G.arcs[i][j]=MaxInt;
    for(int k=0;k<G.arcnum;++k){
        int v1,v2,w;
        cin>>v1>>v2>>w;
        int i=LocateVex(G,v1);
        int j=LocateVex(G,v2);
        G.arcs[i][j]=w;
        G.arcs[i][j]=G.arcs[i][j];
    }
    return OK;
}
//无向图1.初始化邻接矩阵时,w均为0    2.构造邻接矩阵时,w为1
//有向网 邻接矩阵是非对称矩阵,仅为G.arcs[i][j]赋值,无需为G.arcs[j][i]赋值。

//查找顶点
int LocateVex(AMGraph G,VerTexType u){
    int i;
    for(i=0;i<G.vexnum;++i)
        if(u==G.vexs[i])   return i;
    return -1;
}

//邻接表
//顶点结点结构
typedef struct VNode{
    VerTexType data;
    ArcNode *firstarc;
}VNode,AdjList[MVNum];
//边结点结构
typedef struct ArcNode{
    int adjvex;
    struct ArcNode *nextarc;
    OtherInfo info;              //和边相关的信息
}ArcNode;
//图的结构定义
typedef struct{
    AdjList vertices;
    int vexnum,arcnum;
}ALGraph;

//举例
int juli(){
ALGraph G;ArcNode *p;
G.vexnum=5;G.arcnum=5;
G.vertices[1].data='b';
p=G.vertices[1].firstarc;
p->adjvex=4;
}

//采用邻接表表示法创建无向网
Status CreateUDG(ALGraph &G){
    cin>>G.vexnum>>G.arcnum;
    int v1,v2,i,j;

    for(int i=0;i<G.vexnum;++i){
        cin>>G.vertices[i].data;
        G.vertices[i].firstarc=NULL;
    }

    for(int k=0;k<G.arcnum;++k){
        cin>>v1>>v2;
        i=LocateVex2(G,v1);
        j=LocateVex2(G,v2);
//若为有向网逆邻接表,则省略下部
    ArcNode *p1=new ArcNode;
    p1->adjvex=j;
    p1->nextarc=G.vertices[i].firstarc;
    G.vertices[i].firstarc=p1;
//若为有向网,则省略下部
    ArcNode *p2=new ArcNode;
    p2->adjvex=i;
    p2->nextarc=G.vertices[j].firstarc;
    G.vertices[j].firstarc=p2;
    }
    return OK;
}


//图的遍历
//用邻接矩阵表示图的深度优先遍历
void DFS(AMGraph G,int v){
    cout<<v; visited[v]=true;
    for(int w=0;w<G.vexnum;w++)
        if((G.arcs[v][w]!=0)&&(!visited[w]))
            DFS(G,w);
}

//按广度优先非递归遍历连通图G
void BFS(ALGraph G,int v){
    cout<<v; visited[v]=true;
    InitQueue(Q);
    EnQueue(Q,v);
    while(!QueueEmpty(Q)){
        DeQueue(Q,u);
        for(int w=FirstAdjVex(G,u);w>=0;w=NextAdjVex(G,u,w))
        if(!visited[w]){
            cout<<w;visited[w]=true;EnQueue(Q,w);
        }
    }
}


//图的应用
//最小生成树
//普里姆(prim)算法
//克鲁斯卡尔(Kruskal)算法

//最短路径
//Dijkstra算法求单源最短路径问题
//FLoyd算法求各项点之间最短路径问题

//有向无环图的应用(DAG图)
//拓扑排序
Status TopologicalSort(Graph G){
    InitStack(S);
    int i;
    for(i=0;i<G.vexnum;i++)
        if(indegree[i]==0)
            Push(S,i);
    int count=0;
    while(!IsEmpty(S)){
        Pop(S,i);
        print[count++]=i;
        for(p=G.vertices[i].firstarc;p;p=p->nextarc){
            v=p->adjvex;
            if(!(--indegree[v]))
                Push(S,v);
        }
    }
    if(count<G.vexnum)
        return FALSE;
    else
        return OK;
}
//关键路径

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值