aov.h-1.1
//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的更多相关文章
- 图的拓扑排序,AOV,完整实现,C++描述
body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...
- 算法与数据结构(八) AOV网的关键路径
上篇博客我们介绍了AOV网的拓扑序列,请参考<数据结构(七) AOV网的拓扑排序(Swift面向对象版)>.拓扑序列中包括项目的每个结点,沿着拓扑序列将项目进行下去是肯定可以将项目完成的, ...
- 算法与数据结构(七) AOV网的拓扑排序
今天博客的内容依然与图有关,今天博客的主题是关于拓扑排序的.拓扑排序是基于AOV网的,关于AOV网的概念,我想引用下方这句话来介绍: AOV网:在现代化管理中,人们常用有向图来描述和分析一项工程的计划 ...
- 算法与数据结构(八) AOV网的关键路径(Swift版)
上篇博客我们介绍了AOV网的拓扑序列,请参考<数据结构(七) AOV网的拓扑排序(Swift面向对象版)>.拓扑序列中包括项目的每个结点,沿着拓扑序列将项目进行下去是肯定可以将项目完成的, ...
- 算法与数据结构(七) AOV网的拓扑排序(Swift版)
今天博客的内容依然与图有关,今天博客的主题是关于拓扑排序的.拓扑排序是基于AOV网的,关于AOV网的概念,我想引用下方这句话来介绍: AOV网:在现代化管理中,人们常用有向图来描述和分析一项工程的计划 ...
- AOV拓扑排序实验-2-AOV类的实现
下面是这个类的实现代码: //这只是一个基本的框架,没有封装 #include<iostream> #include<cstdio> #include<malloc.h& ...
- AOV拓扑排序实验总结-1
AOV拓扑排序实验总结-1 实验数据:1.实验输入数据在input.txt文件中2.对于n是指有顶点n个,数据的结束标志是一行0 0. 实验目的:获取优秀的AOV排序算法模板 数据结构安排 ...
- APUE中fcntl.h的使用及O_SYNC在Mac与Ubuntu下的测试
此部分测试涉及到APUE V3中,第三章的图3-12到图3-14. 通过fcntl.h提供的功能,修改fd的文件属性,本处增加O_SYNC功能,并测试其效果. 本文涉及代码: tree ch3 ch3 ...
- 关于apue.3e中apue.h的使用
关于apue.3e中apue.h的使用 近来要学一遍APUE第三版,并于此开博做为记录. 先下载源文件: # url: http://http//www.apuebook.com/code3e.htm ...
随机推荐
- 批量解析域名为IP地址的python脚本
脚本如下: #!/usr/bin/env python #coding:utf- import os,sys from socket import gethostbyname DOMAIN= &quo ...
- asp.net core 3.x 身份验证-2启动阶段的配置
注册服务.配置选项.添加身份验证方案 在Startup.ConfigureServices执行services.AddAuthentication() 注册如下服务(便于理解省略了部分辅助服务): s ...
- 透彻分析和解决一切javaWeb项目乱码问题
前言 乱码是我们在程序开发中经常碰到且让人头疼的一件事,尤其是我们在做javaweb开发,如果我们没有清楚乱码产生的原理,碰到乱码问题了就容易摸不着头脑,无从下手. 乱码主要出现在两部分,如下: 第一 ...
- PAT_B_PRAC_1003养兔子
题目描述 一只成熟的兔子每天能产下一胎兔子.每只小兔子的成熟期是一天. 某人领养了一只小兔子,请问第N天以后,他将会得到多少只兔子. 输入描述: 测试数据包括多组,每组一行,为整数n(1≤n≤90). ...
- Affinity Propagation Demo2学习【可视化股票市场结构】
这个例子利用几个无监督的技术从历史报价的变动中提取股票市场结构. 使用报价的日变化数据进行试验. Learning a graph structure 首先使用sparse inverse(相反) c ...
- Source Code Structure - Python 源码目录结构
Source Code Structure - Python 源码目录结构 Include 目录包含了 Python 提供的所有头文件, 如果用户需要用 C 或 C++ 编写自定义模块扩展 Pytho ...
- 15-cookie技术和session技术的联系和区别
1. 联系: *session实现依赖于Cookie 2. session问题: * 由服务器创建,存储在服务器 * 当浏览器关闭时,服务器不关闭,再次打开浏览器时, 默认获得的不是同一个sessi ...
- VFP中OCX控件注册检测及自动注册
这是原来从网上搜集.整理后编制用于自己的小程序使用的OCX是否注册及未注册控件的自动注册函数. CheckCtrlFileRegist("ctToolBar.ctToolBarCtrl.4& ...
- Linux恢复删除的文件
linux恢复删除的文件 先介绍下一些文件的基本概念: · 文件实际上是一个指向inode的链接, inode链接包含了文件的所有属性, 比如权限和所有者, 数据块地址(文件存储在磁盘 ...
- light oj 1045 - Digits of Factorial K进制下N!的位数
1045 - Digits of Factorial Factorial of an integer is defined by the following function f(0) = 1 f(n ...