数据结构C语言描述 第九章 图论算法

难点之一

若干定义

#include "MyGraph.h"

void initGraphNode(graphPtr g,int identifier)
{
    if (g->arr[identifier] == NULL)
    {
        g->arr[identifier] = new graphCoor;
        g->arr[identifier]->input = 0;
        g->arr[identifier]->coord = NULL;
    }
    g->capTop++;
    return;
}

graphPtr createGraph(int capacity)
{
    graphPtr g = new graph;
    g->arr = new graphCoorPtr[capacity];
    for (int i = 0; i < capacity; i++)
    {
        g->arr[i] = NULL;
    }
    return g;
}



void insert(graphPtr g,int identifier,int vertex)
{
    if (g->arr[identifier] == NULL)
    {
        return;
    }
    Position Next = NULL;
    if (g->arr[identifier]->coord ==NULL)
    {
        g->arr[identifier]->coord = new ListNode;
        g->arr[identifier]->coord->capacity = vertex;
        g->arr[identifier]->coord->Next = NULL;
        g->arr[vertex]->input++;
        
    }
    else
    {
        Position Next = g->arr[identifier]->coord;
        while (Next->Next!=NULL)
        {
            Next = Next->Next;
        }
        Next->Next = new ListNode;
        Next->Next->capacity = vertex;
        Next->Next->Next = NULL;
        g->arr[vertex]->input++;
    }
    return;
}



int* Topologsort(graphPtr g,int capTop)
{
    int size = 0;
    int* tmpInt = new int[capTop];
    
    //while (true)
    //{
    while (true)
    {
        bool exit = true;
        for (int i = 0; i < capTop; i++) {
            if (g->arr[i] != NULL && g->arr[i]->input == 0) {
                Position ps = g->arr[i]->coord;
                while (ps != NULL)
                {
                    g->arr[ps->capacity]->input--;
                    ps = ps->Next;
                }
                g->arr[i]->coord = NULL;
                g->arr[i]->input = -1;
                tmpInt[size++] = i;
                exit = false;

            }
            
        }
        if (exit)
        {
            break;
        }

    }
    
    return tmpInt;
        
    //}





}

void Unauthorizedpath(graphPtr g)
{
    int i;
    grpahArrayPtr gp = createGraph(g->capTop);
    int next = 0,temp=0;
    bool exit = false;
    while (true)
    {
        gp->arr[next] = g->arr[next];
        for (i = 0; i < gp->capTop; i++)
        {
            
            if (gp->arr[i] !=NULL)
            {
                int shorest;
                if (gp->arr[i]->shortestRoad[next] !=-1 && gp->arr[i]->shortestRoad[temp]!=-1)
                {
                    shorest = gp->arr[i]->shortestRoad[next] < gp->arr[i]->shortestRoad[temp] ? gp->arr[i]->shortestRoad[next] : gp->arr[i]->shortestRoad[temp];
                }
                else
                {
                    shorest = gp->arr[i]->shortestRoad[temp] > -1 ? gp->arr[i]->shortestRoad[temp] : 0;
                }

                gp->arr[i]->shortestRoad[next] = shorest +1;
            }
        
            if (g->arr[next]->coord == NULL)
            {
                exit = true;
                break;
            }
            
        }
        if (exit)
        {
            break;
        }

        temp = next;
         next = PopEndidentifier(g,next);
        if (next == -1)
        {
            return;
        }
        gp->capTop++;
    }
    Unauthorizedpath(g);

    
    
}

 

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值