文章
159
粉丝
0
获赞
0
访问
9.7k
(1)采用邻接矩阵存储,对于每个顶点,其在邻接矩阵中所对应列中非零个数即是入度,在所对应行中非零元素个数即是出度,遍历所有顶点,统计每个顶点入度和出度,如果出度大于入度则 输出该顶点,K顶点个数+1
(2)
#include <iostream>
#include <vector>
#define MAXV 100
typedef struct { // 图的类型定义
int numVertices, numEdges; // 图中顶点数和有向边数
char VerticesList[MAXV]; // 顶点表,MAXV为已定义常量
int Edge[MAXV][MAXV]; // 邻接矩阵
} MGraph;
int printKvertices(MGraph G){
int k_vertex_count = 0; //初始化记录k顶点个数
int out_degree ,in_degree ;//初始化出度和入度计数
for(int i = 0 ; i <G.numVertices;i++){
out_degree = 0;
in_degree = 0;
//计算出度
for(int j = 0 ; j < G.numVertices ; j++){
if(G.Edge[i][j] !=0 ){ //非0表示存在边
out_degree++;
}
}
//计算入度
for(int j = 0 ; j < G.numVertices ; j++){
if(G.Edge[j][i] !=0 ){ //非0表示存在边
in_degree++;
}
}
if(out_degree > in_degree){
std::cout << G.VerticesList[i] << " ";//输出k顶点
k_vertex_count++;
}
}
std::cout << s...
登录后发布评论
暂无评论,来抢沙发