数据结构--图

在这里插入图片描述

1.邻接矩阵

在这里插入图片描述

2.邻接表和逆接表

在这里插入图片描述

typedef struct ArcNode
{
    int adjV;
    struct ArcNode* next;
}ArcNode;

typedef struct
{
    int data;
    ArcNode* first;
}VNode;

typedef struct
{
    VNode adjList[maxSize];
    int n,e;
}AGraph;

3.十字链表

在这里插入图片描述

4.邻接多重表

在这里插入图片描述

5.DFS(深度优先遍历)

在这里插入图片描述

//深度优先遍历补充说明:就是先确定一点开始进行访问,
//与他相连的结点符合邻接表,如果访问到,我们就用visit[v]=1用来标记一下
//如果遇到visit[v]=0的地方,则进行遍历打印。
void DFS(int v,AGraph *G)
{
    visit[v]=1;
    cout<<v<<endl;
    ArcNode *q=G->adjList[v].first;
    while(q!=NULL)
    {
        if(visit[q->adjV]==0)
            DFS(q->adjV,G);
        q=q->next;
    }
}

6.BFS(广度优先遍历)

在这里插入图片描述

//广度优先遍历补充说明:我们采取的队列存储,
//首先确定第一个结点,然后出队第一个结点,把他与其相邻的结点依次存入,
//存入之后用visit[v]=1进行标记,避免循环访问。
void BFS(AGraph *G,int v)
{
    ArcNode *p;
    int que[maxSize],front=0,rear=0;
    int j;
    cout<<v<<endl;
    rear=(rear+1)%maxSize;
    que[rear]=v;
    while(front!=rear)
    {
        front=(front+1)%maxSize;
        j=que[front];
        p=G->adjList[j].first;
        while(p!=NULL)
        {
            if(visit[p->adjV]==0)
            {
                cout<<p->adjV<<endl;
                visit[p->adjV]=1;
                rear=(rear+1)%maxSize;
                que[rear]=p->adjV;
            }
            p=p->next;
        }
    }
}

7.prim算法

在这里插入图片描述

void Prim(int n,float MGraph[][maxSize],int v0,float &sum)
{
    int lowCost[n],vSet[n];
    int v,k,min;
    for(int i=0;i<n;++i)
    {
        lowCost[i]=MGraph[v0][i];
        vSet[i]=0;
    }
    v=v0;
    vSet[v]=1;
    sum=0;
    for(int i=0;i<n-1;++i)
    {
        min=INT_FAST16_MAX;
        for(int j=0;j<n;++j)
        {
            if(vSet[j]==0&&lowCost[j]<min)
            {
                min=lowCost[j];
                k=j;
            }
            vSet[k]=1;
            v=k;
            sum+=min;
            for(int j=0;j<n;++j)
                if(vSet[j]==0&&MGraph[v][j]<lowCost[j])
                    lowCost[j]==MGraph[v][j];
        }
    }
}

8.kruskal算法

在这里插入图片描述

int v[maxSize];

int getRoot(int p)
{
    while(p!=v[p])
        p=v[p];
    return p;
}

void selectionSort1(Road arr[],int n)
{
    for(int i=0;i<n;i++)
    {
        int minIndex=i;
        for(int j=i+1;j<n;j++)
            if(arr[j]<arr[minIndex])
                minIndex=j;
        swap(arr[i],arr[minIndex]);
    }
}
void Kruskal(Road road[],int n,int e,int &sum)
{
    int a,b;
    sum=0;
    for(int i=0;i<n;++i)
        v[i]=i;
    selectionSort1(road,e);
    for(int i=0;i<e;++i)
    {
        a=getRoot(road[i].a);
        b=getRoot(road[i].b);
        if(a!=b)
        {
            v[a]=b;
            sum+=road[i].w;
        }
    }
}

9.Dijkstra算法

在这里插入图片描述

void Dijkstra(int n,float MGrph[][maxSize],int v0,int dist[],int path[])
{
    int set[maxSize];
    int min,v;
    for(int i=0;i<n;++i)
    {
        dist[i]=MGrph[v0][i];
        set[i]=0;
        if(MGrph[v0][i]<INFINITY)
            path[i]=v0;
        else    
            path[i]=-1;
        
    }
    set[v0]=1;path[v0]=-1;
    for(int i=0;i<n-1;++i)
    {
        min=INFINITY;
        for(int j=0;j<n;++j)
            if(set[j]==0&&dist[j]<min)
            {
                v=j;
                min=dist[j];
            }
        set[v]=1;
        for(int j=0;j<n;++j)
        {
            if(set[j]==0&&dist[v]+MGrph[v][j]<dist[j])
            {
                dist[j]=dist[v]+MGrph[v][j];
                path[j]=v;
            }
        }
    }
}

10.Floyd算法

在这里插入图片描述

void Floyd(int n,float MGraph[][maxSize],int path[][maxSize])
{
    int i,j,v;
    int A[n][n];
    for(i=0;i<n;++j)
        for(j=0;j<n;++j)
        {
            A[i][j]=MGraph[i][j];
            path[i][j]=-1;
        }
    for(v=0;v<n;++v)
        for(i=0;i<n;++i)
            for(j=0;j<n;++j)
                if(A[i][j]>A[i][v]+A[v][j])
                {
                    A[i][j]=A[i][v]+A[v][j];
                    path[i][j]=v;
                }
}

void printPath(int u,int v,int path[][maxSize])
{
    if(path[u][v]==-1)
        cout<<1<<endl;
    else{
        int mid =path[u][v];
        printPath(u,mid,path);
        printPath(mid,v,path);
    }
}

11.拓扑排序

在这里插入图片描述


typedef struct ArcNode
{
    int adjV;
    struct ArcNode* next;
}ArcNode;

typedef struct 
{
    int data;
    int count;
    ArcNode* first;
}VNode;

typedef struct
{
    VNode adjList[maxSize];
    int n,e;
}AGraph;


int TopSort(AGraph *G)
{
    int i,j,n=0;
    int stack[maxSize],top=-1;
    ArcNode *p;

    for(i=0;i<G->n;++i)
    {
        if(G->adjList[i].count==0)
            stack[++top]=i;
    }
    while(top!=-1)
    {
        i=stack[top--];
        ++n;
        cout<<i<<endl;

        p=G->adjList[i].first;
        while(p!=NULL)
        {
            j=p->adjV;
            --(G->adjList[j].count);
            if(G->adjList[j].count==0)
                stack[++top]=j;
            p=p->next;
        }
    }

    if(n==G->n)
        return 1;
    else   
        return 0;
}

12.逆拓扑排序

在这里插入图片描述

typedef struct ArcNode
{
    int adjV;
    struct ArcNode* next;
}ArcNode;

typedef struct 
{
    int data;
    int count;
    ArcNode* first;
}VNode;

typedef struct
{
    VNode adjList[maxSize];
    int n,e;
}AGraph;

void endSort(int v,AGraph *G)
{
    visit[v]=1;
    ArcNode* q=G->adjList[v].first;
    while(q!=NULL)
    {
        if(visit[q->adjV]==0)
            DFS(q->adjV,G);
        q=q->next;
    }
    cout<<v<<endl;
}

13.关键路径

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值