AOV拓扑排序实验总结-1
 
实验数据:
1、实验输入数据在input.txt文件中
2、对于n是指有顶点n个,数据的结束标志是一行0 0。
 
实验目的:获取优秀的AOV排序算法模板
 
数据结构安排:
1、队列:负责记录入度为0且没有排序的AOV顶点
2、邻接表结点:邻接表结点采用自定义的复合结构,保存顶点信息、边表头指针。
3、邻接表边表:采取链表的形式存储数据
4、邻接表的数据类型是相同的,只是在概念上使得结点独特的保存了当前起始顶点
5、按照vertex的编号独立的使用一个数组indegree保存入度,一定程度的节省了空间
 
实验内容:1、算法模板的设计 2、算法类的设计或头文件封装的尝试
 
AOV算法模板要求:
1、假定AOV排序成立
2、vertex本身可以按照编号进行命名
 
构建的邻接表展现:
代码如下:
 for(int i=;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");
}
展示情况:
vertex 0 indegree 0 points to:8 2
vertex 1 indegree 0 points to:2 4
vertex 2 indegree 2 points to:3
vertex 3 indegree 2 points to:5 7 9 10
vertex 4 indegree 1 points to:6
vertex 5 indegree 1 points to:
vertex 6 indegree 1 points to:10
vertex 7 indegree 1 points to:
vertex 8 indegree 1 points to:9 3
vertex 9 indegree 2 points to:
vertex 10 indegree 2 points to:
总体模板运行结果:
  0  1  8  2  4  3  6  5  7  9 10
运行正常
 
实验代码:
 //算法模板的设计
#include<iostream>
#include<cstdio>
#include<malloc.h>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn = ;
struct point
{
int vertex;//顶点
point* next;
}; int indegree[maxn];
point aim[maxn];
int n; int readin()
{
scanf("%d",&n);
memset(indegree,,sizeof(int)*n);
for(int i=;i<n;i++)
{
aim[i].next=NULL;
aim[i].vertex=i;
}
//初始化
int a,b;
while(scanf("%d%d",&a,&b))
{//a->b
if(a==&&b==)break;
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 ;
} queue<int> psd;
int topo_sort(int* ans)
{
bool ok[maxn];
memset(ok,false,sizeof(ok));
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]=true;
num--;
}
}//检查所有入度0的顶点并入队,留下入队标记
}
if(psd.empty())break;//队列为空则排序结束
int p=psd.front();psd.pop();
point* temp=&aim[p];
ans[cur++]=p;//也可以写成ans[cur++]=aim[i].vertex;
//提出结点并排序
while(temp->next!=NULL)
{
temp=temp->next;
indegree[temp->vertex]--;
}//去掉相关有向边
}
return ;
} int ans[maxn];
int main()
{
//freopen("input.txt","r",stdin);
//freopen("ans.txt","w",stdout);
readin();
topo_sort(ans);
for(int i=;i<n;i++)
{
printf("%3d",ans[i]);
}
printf("\n");
return ;
}

AOV拓扑排序实验总结-1的更多相关文章

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

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

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

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

  3. 有向无环图的应用—AOV网 和 拓扑排序

    有向无环图:无环的有向图,简称 DAG (Directed Acycline Graph) 图. 一个有向图的生成树是一个有向树,一个非连通有向图的若干强连通分量生成若干有向树,这些有向数形成生成森林 ...

  4. AOV网络和Kahn算法拓扑排序

    1.AOV与DAG 活动网络可以用来描述生产计划.施工过程.生产流程.程序流程等工程中各子工程的安排问题.   一般一个工程可以分成若干个子工程,这些子工程称为活动(Activity).完成了这些活动 ...

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

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

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

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

  7. AOV网与拓扑排序

    在一个表示工程的有向图中,用顶点表示活动,用弧表示活动之间的优先关系,这样的有向图为顶点表示活动的网,我们称之为AOV网(Activity on Vextex Network).AOV网中的弧表示活动 ...

  8. 拓扑排序---AOV图

    对一个有向无环图(Directed Acyclic Graph简称DAG)G进行拓扑排序,是将G中全部顶点排成一个线性序列, 使得图中随意一对顶点u和v,若边(u,v)∈E(G),则u在线性序列中出如 ...

  9. 【algo&ds】9.拓扑排序、AOV&AOE、关键路径问题

    对一个有向无环图(Directed Acyclic Graph简称DAG)G进行拓扑排序,是将G中所有顶点排成一个线性序列,使得图中任意一对顶点u和v,若边<u,v>∈E(G),则u在线性 ...

随机推荐

  1. c++中的动态内存分配

    使用new和delete动态的分配和释放内存 使用new来分配新的内存块,通常情况下,如果成功,new将返回一个指针,指向分配的内存,否则将引发异常,使用new时,需要指定要为那种数据类型分配内存: ...

  2. 用javascript修改html元素的class

    document.getElementById("collins_contentWrp").className="content-wrp dict-container c ...

  3. 闲谈一下,ES3、ES4、ES5、ES6 分别是什么

    上图按照时间顺序说明了JavaScript.JScript和ECMAScript的发展. 显示在网景工作的Brendan Eich临危受命,用十天时间设计出LiveScript的第一个版本.临时发布前 ...

  4. Go语言实现:【剑指offer】数组中重复的数字

    该题目来源于牛客网<剑指offer>专题. 在一个长度为n的数组里的所有数字都在0到n-1的范围内.数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组 ...

  5. mysql添加远程权限

    mysql -u root -p grant all privileges on *.* to root@'%' identified by "password"; flush p ...

  6. 关于开源,Git,Github

    在Github和Git上fork之简单指南 https://linux.cn/article-4292-1.html,中文翻译 https://www.dataschool.io/simple-gui ...

  7. Angular常用命令

    一. Angular常用命令 1. ng new 文件夹名 (新建项目,选择y使用路由) 2. ng serve --open (默认浏览器运行项目) 3. ng serve --port 6060  ...

  8. zabbix-server配置文件详解

    zabbix官方文档:https://www.zabbix.com/documentation/4.0/zh/manual zabbix-server端的配置文件在/etc/zabbix/zabbix ...

  9. 面试官:你说你熟悉jvm?那你讲一下并发的可达性分析

    这是why技术的第35篇原创文章 上面这张图是我还是北漂的时候,在鼓楼附近的胡同里面拍的. 那天刚刚下完雨,路过这个地方的时候,一瞬间就被这五颜六色的门板和自行车给吸引了,于是拍下了这张图片.看到这张 ...

  10. vue中子组件触发父组件的方法

    网上找了几种方法,下面这两种最实用,最明了 方法一:父组件方法返回是字符串或数组时用这种方法 子组件: <template> <button @click="submit& ...