有向图邻接表出度入度
来源:互联网 发布:linux下perl开发工具 编辑:程序博客网 时间:2024/05/29 18:08
出边、入边使用同一顶点数组
注意输入顶点序号与数组下标的关系#include <iostream>
#include <stdlib.h>
#define MAX 100
using namespace std;
//邻接表表示有向图
typedef struct ArcNode{//边节点
int index;
ArcNode *nextarc;
};
typedef struct VNode{//顶点节点
int index;
ArcNode *head1;//出边表头节点
ArcNode *head2;//入边表头节点
};
//图
typedef struct Graph{
VNode vertexs[MAX];
int vernum,edgenum;
};
Graph g;
//创建邻接表
void createGraph(){
cin>>g.vernum>>g.edgenum;
for(int i=0; i<g.vernum; i++){
g.vertexs[i].index = i;
g.vertexs[i].head1 = g.vertexs[i].head1 = NULL;
}
int invertex, outvertex;
ArcNode *arcnode;
while(cin>>invertex>>outvertex,invertex,outvertex){
//出边
arcnode = (ArcNode*)malloc(sizeof(ArcNode));
arcnode->index = outvertex;
arcnode->nextarc = g.vertexs[invertex-1].head1;
g.vertexs[invertex-1].head1 = arcnode;
//入边
arcnode = (ArcNode*)malloc(sizeof(ArcNode));
arcnode->index = invertex;
arcnode->nextarc = g.vertexs[outvertex-1].head2;
g.vertexs[outvertex-1].head2 = arcnode;
}
}
void indegree(){
int num;
for(int i=0; i<g.vernum; i++){
num=0;
ArcNode *arcnode = g.vertexs[i].head1;
while(arcnode){
num++;
arcnode = arcnode->nextarc;
}
if(i==0){
cout<<num;
}else{
cout<<" "<<num;
}
num=0;
}
cout<<endl;
}
void outdegree(){
int num;
for(int i=0; i<g.vernum; i++){
num=0;
ArcNode *arcnode = g.vertexs[i].head2;
while(arcnode){
num++;
arcnode = arcnode->nextarc;
}
if(i==0){
cout<<num;
}else{
cout<<" "<<num;
}
num=0;
}
cout<<endl;
}
/**
4 7
1 4
2 1
2 2
2 3
2 3
4 2
4 3
0 0
**/
int main()
{
createGraph();
indegree();
outdegree();
return 0;
}
1 4 0 2
1 2 3 1
0 0
- 有向图邻接表出度入度
- 有向图-邻接表
- 有向图的邻接表类
- 用邻接表存储有向图
- 有向图的邻接表表示
- 有向图的邻接表实现
- 有向图的邻接表存储
- 邻接表有向图DFS
- 邻接表、构造有向图
- C++邻接表与有向图
- 有向图的邻接表储存
- 有向图的邻接链表
- C++邻接表实现无向图、有向图
- 邻接表-建立无向图、无向网、有向图、有向网
- 有向图邻接表表示及反向图构造
- 有向图邻接表表示及反向图构造
- 有向图邻接表表示及反向图构造
- 图结构(五)- 邻接表有向图
- C# 枚举
- 文件缓存类
- Codeforces Round #388 (Div. 2) (A+B)
- 摸爬滚打DirectX11_day08——深度测试与Z缓存(深度缓存)
- 简单文件系统的实现
- 有向图邻接表出度入度
- Zero Copy(零拷贝)
- Eclipse迁移项目到AndroidStudio上的报错。
- 近十年量化交易领域最重要的十本参考书推荐!重要!
- RIP2
- Canvas实现鼠标在网页上涂画
- Ufldl Exercise:Softmax Regression Softmax回归练习
- 是否为可构成图的序列-青蛙的邻居
- 数据库操作类