//AOV网与拓扑排序类
#ifndef _AOV_H_
#define _AOV_H_
#include<iostream>
#include<cstdio>
#include<malloc.h>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
#endif struct point
{
int vertex;//顶点
point* next;
}; class AOV
{
private:
int* ok;
int* indegree;//入度
point* aim;//邻接表
bool n_hasvalue;
int n; public:
int* ans;
AOV(int maxn,int point_num);//构造函数原型
AOV():AOV(,){};//直接构造函数
AOV(int maxn);//容器型构造参数
void maxpoint(int p);
int readin();
int* topo_sort();
}; void AOV::maxpoint(int p)
{
if(n_hasvalue==true)
{
printf("Sorry n had value,ERROR!\n");
return;
}
n=p;
n_hasvalue=true;
return;
} AOV::AOV(int maxn)//容器型构造参数
{
ans=new int[maxn];
ok=(int*)new int[maxn];
memset(ok,,sizeof(int)*maxn);
//这里的重置不能写成sizeof(ok);
indegree=new int[maxn];
aim=new point[maxn];
n_hasvalue=false;
} AOV::AOV(int maxn,int point_num)
{//maxn描述可能使用到的最大的顶点数量
ans=new int[maxn];
ok=(int*)new int[maxn];
memset(ok,,sizeof(int)*maxn);
indegree=new int[maxn];
aim=new point[maxn];
n_hasvalue=true;
n=point_num;
} int AOV::readin()
{
if(n_hasvalue==false){printf("n do not has value!\n");return ;}
memset(indegree,,sizeof(int)*(n+));
for(int i=;i<n;i++)
{
aim[i].next=NULL;
aim[i].vertex=i;
}
//初始化
int a,b;
//printf("Please input pairs of numbers, which from 0 to n-1, to describe the relationship.\n");
//printf("When there is a pair of numbers consists of two 0,then input will stop.\n");
while(cin>>a>>b)
{//a->b
if(a==&&b==)break;
if(a>=n||b>=n||a<||b<){printf("Data error\n");continue;}
indegree[b]++;//入度加1
point* temp=&aim[a];
while(temp->next!=NULL)temp=temp->next;
//找到存有指向结点链表的末端
temp->next=(point*)malloc(sizeof(point));
temp=temp->next;//进入新的point点
temp->vertex=b;//a->b
temp->next=NULL;
}//完成邻接表的构建
return ;
} int* AOV::topo_sort()
{
// for(int i=0;i<n;i++)
// {
// printf("vertex %d indegree %d points to:",aim[i].vertex,indegree[i]);
// point* temp=&aim[i];
// while(temp->next!=NULL)
// {
// temp=temp->next;
// printf("%d ",temp->vertex);
// }
// printf("\n");
// }
queue<int> psd;
int cur=;
int num=n;
while()
{
if(num)
{
for(int i=;i<n;i++)
{
if(ok[i])continue;
if(indegree[i]==)
{
psd.push(i);
ok[i]=;
num--;
}
}//检查所有入度0的顶点并入队,留下入队标记
}
if(psd.empty())break;//队列为空则排序结束
int p=psd.front();psd.pop();
point* temp=&aim[p];
ans[cur++]=p;//也可以写成ans[cur++]=aim[i].vertex;
//printf("%d ",p);
//提出结点并排序
while(temp->next!=NULL)
{
temp=temp->next;
indegree[temp->vertex]--;
}//去掉相关有向边
}
//printf("\n\ncur->%d\n",cur);
return ans;
}

aov.h-1.1的更多相关文章

  1. 图的拓扑排序,AOV,完整实现,C++描述

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...

  2. 算法与数据结构(八) AOV网的关键路径

    上篇博客我们介绍了AOV网的拓扑序列,请参考<数据结构(七) AOV网的拓扑排序(Swift面向对象版)>.拓扑序列中包括项目的每个结点,沿着拓扑序列将项目进行下去是肯定可以将项目完成的, ...

  3. 算法与数据结构(七) AOV网的拓扑排序

    今天博客的内容依然与图有关,今天博客的主题是关于拓扑排序的.拓扑排序是基于AOV网的,关于AOV网的概念,我想引用下方这句话来介绍: AOV网:在现代化管理中,人们常用有向图来描述和分析一项工程的计划 ...

  4. 算法与数据结构(八) AOV网的关键路径(Swift版)

    上篇博客我们介绍了AOV网的拓扑序列,请参考<数据结构(七) AOV网的拓扑排序(Swift面向对象版)>.拓扑序列中包括项目的每个结点,沿着拓扑序列将项目进行下去是肯定可以将项目完成的, ...

  5. 算法与数据结构(七) AOV网的拓扑排序(Swift版)

    今天博客的内容依然与图有关,今天博客的主题是关于拓扑排序的.拓扑排序是基于AOV网的,关于AOV网的概念,我想引用下方这句话来介绍: AOV网:在现代化管理中,人们常用有向图来描述和分析一项工程的计划 ...

  6. AOV拓扑排序实验-2-AOV类的实现

    下面是这个类的实现代码: //这只是一个基本的框架,没有封装 #include<iostream> #include<cstdio> #include<malloc.h& ...

  7. AOV拓扑排序实验总结-1

    AOV拓扑排序实验总结-1   实验数据:1.实验输入数据在input.txt文件中2.对于n是指有顶点n个,数据的结束标志是一行0 0.   实验目的:获取优秀的AOV排序算法模板   数据结构安排 ...

  8. APUE中fcntl.h的使用及O_SYNC在Mac与Ubuntu下的测试

    此部分测试涉及到APUE V3中,第三章的图3-12到图3-14. 通过fcntl.h提供的功能,修改fd的文件属性,本处增加O_SYNC功能,并测试其效果. 本文涉及代码: tree ch3 ch3 ...

  9. 关于apue.3e中apue.h的使用

    关于apue.3e中apue.h的使用 近来要学一遍APUE第三版,并于此开博做为记录. 先下载源文件: # url: http://http//www.apuebook.com/code3e.htm ...

随机推荐

  1. how to convert wstring to string

    #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> #include <local ...

  2. python学习记录(四)

    0828--https://www.cnblogs.com/fnng/archive/2013/04/18/3029807.html 0828--https://www.cnblogs.com/fnn ...

  3. 软件质量保障初探_Chris

    关于软件质量保障的体会 首先,软件质量保障的重要性不言而喻,书中说软件质量体现在以下方面 软件开发过程的可见性 软件开发过程的风险控制 软件内部模块,项目中间阶段的交付质量,项目管理工具的因素 软件开 ...

  4. Deep Learning for Chatbots(Introduction)

    聊天机器人又被称为会话系统,已经成为一个热门话题,许多公司都在这上面的投入巨大,包括微软,Facebook,苹果(Siri),Google,微信,Slack.许多创业公司尝试通过多种方式来改变与消费者 ...

  5. HDU_5057_分块

    http://acm.hdu.edu.cn/showproblem.php?pid=5057 分块,保存每个块中每位对应数字的和,复杂的是getmum,左右下标所在的块不能直接读取block数组,要重 ...

  6. dosbox,masm的挂载

    masm文件夹下 1 asm文件夹用来存储asm文件 2 几个masm的应用程序 3 masm 生成的Obj文件和Link生成的exe文件 在dosbox目录下修改option文件加入 这样开始dos ...

  7. TensorFlow中使用GPU

    TensorFlow默认会占用设备上所有的GPU以及每个GPU的所有显存:如果指定了某块GPU,也会默认一次性占用该GPU的所有显存.可以通过以下方式解决: 1 Python代码中设置环境变量,指定G ...

  8. vue文件引入全局样式导致样式重复

    通常项目中src下的子目录都会有一个style文件夹,专门用来存放全局的样式文件. 这个style文件夹下,一般有reset.css.var.scss.mixin.scss.class.scss.in ...

  9. 【UEFI】--- 探究BIOS NvRam空间的的位置以及大小

    按照国际惯例--先上问题:1. 什么是NvRam空间,里面存储了什么数据 2. 如何找到NvRam空间在BIOS-bin文件中的位置 NvRam空间的学名为: Non-Volatile Ram即非易失 ...

  10. Visual Studio 2015 配置 Python 环境

    Visual Studio 2015可以在安装时选择安装Python环境,首次使用VS2015执行python时需要配置环境变量: 配置VS2015的环境前需要先下载Python并安装: https: ...