[PTA] 6-3 Directed graph output vertex with degree 0 (13 points) (C language)

6-3 Directed graph outputs vertices with degree 0 (13 points)

topic:

This question requires the implementation of a function that outputs all vertices with out-degree 0 in the directed graph.

Function interface definition:

The function interface is: void PrintV(MGraph G);

G is a directed graph using an adjacency matrix as a storage structure.

Sample referee test procedure:

#include <stdio.h>
#define MVNum 100 //Maximum number of vertices typedef struct { char vexs[MVNum]; //One-dimensional array of vertices stored int arcs[MVNum][MVNum]; //Adjacency matrix int vexnum, arcnum ; //Current number of vertices and arcs in the graph}MGraph; void PrintV(MGraph G); void CreatMGraph(MGraph G);/ create graph*/ int main() { MGraph G; CreateMGraph(&G); PrintV(G) ; return 0; } void CreatMGraph(MGraph G) { int i, j, k; scanf("%d%d", &G->vexnum, &G->arcnum); getchar(); for (i = 0; i < G->vexnum; i++) scanf("%c",&G->vexs[i]); for (i = 0; i < G->vexnum; i++) for (j = 0; j < G->vexnum; j++) G->arcs[i][j] = 0; for (k = 0; k < G->arcnum; k++) {
















scanf("%d%d", &i, &j);
G->arcs[i][j] = 1;
} } /
Your code will be embedded here*/

Input sample:

e.g. directed graph
insert image description here

The first line gives the number of vertices n and the number of arcs e of the graph. The second line gives n characters, representing the value of the data element of n vertices. This is followed by line e, giving the two vertex numbers for each arc.

4 5
ABCD
1 0
2 0
2 1
3 2
3 1

Output example:
The output is two lines, the first line is the number of vertices with out-degree 0, and the second line outputs all vertex element values ​​with out-degree 0 according to the input order. The element value of the vertex is character type, and the output format is each character followed by a space. If there is no vertex with an out-degree of 0, the output is only one line, and the number is 0.

1
A

Idea:
The first step is to calculate the out-degree of each node. To calculate the out-degree of the i-th node of the directed graph, that is to calculate the sum of the number of nodes in the i-th row of the adjacency matrix, denoted by od. At the same time, count the sum of the total number of nodes whose degree is 0.
The second step is to classify the output. If the number of out-degree nodes is 0, output 0; if there are nodes with out-degree 0, output the total number and corresponding nodes.

C language code:

void PrintV(MGraph G){
    
    
    int sum=0;//出度为0的顶点个数
    
    //计算各个结点的出度
    for(int i=0;i<G.vexnum;i++){
    
    
        int od=0;
        for(int j=0;j<G.vexnum;j++){
    
    
            if(G.arcs[i][j]==1) od++;
        }
        if(od==0) sum++;
    }
    
    if(sum==0){
    
    
            printf("0");
        }
        else if(sum>0){
    
    
            printf("%d\n",sum);
            for(int i=0;i<G.vexnum;i++){
    
    
                int od=0;
                for(int j=0;j<G.vexnum;j++){
    
    
                    if(G.arcs[i][j]==1) od++;
                }
                if(od==0){
    
    printf("%c ",G.vexs[i]);}
             }
       }
}

Guess you like

Origin blog.csdn.net/qq_51669241/article/details/117157103
Recommended