ZBLOG

//深度遍历邻接表 void DFSALGraph(ALGraph G,int i) { //--------补充代码--Start------ //--------补充代码--End------ } //计算图中所有边的权值之和 int GetGraphWeight(ALGraph G) { //--------补充代码--Start------ //--------补充代码--End------ } //计算图中出度大于入度的结点个数 int GetNode(ALGraph...

下面是补充代码的实现:

#include <iostream>
using namespace std;

#define MAX_VERTEX_NUM 100

// 边结点
typedef struct ArcNode {
    int adjvex;             // 邻接点在顶点数组中的位置下标
    struct ArcNode *nextarc; // 指向下一个邻接点的指针
    int weight;             // 权值
}ArcNode;

// 顶点结点
typedef struct VNode {
    int data;               // 顶点数据
    ArcNode *firstarc;      // 指向第一个邻接点的指针
}VNode, AdjList[MAX_VERTEX_NUM];

// 图
typedef struct {
    AdjList vertices;
    int vexnum, arcnum;     // 顶点数和边数
}ALGraph;

void DFS(ALGraph G, int i, bool visited[]) {
    visited[i] = true;
    cout << G.vertices[i].data << " ";
    
    ArcNode *p = G.vertices[i].firstarc;
    while (p) {
        if (!visited[p->adjvex]) {
            DFS(G, p->adjvex, visited);
        }
        p = p->nextarc;
    }
}

void DFSALGraph(ALGraph G,int i) {
    bool visited[MAX_VERTEX_NUM];
    
    for (int j = 0; j < G.vexnum; j++) {
        visited[j] = false;
    }
    
    DFS(G, i, visited);
}

int GetGraphWeight(ALGraph G) {
   int sum = 0;
   
   for (int i = 0; i < G.vexnum; i++) { 
       ArcNode *p = G.vertices[i].firstarc;
       while (p) {
           sum += p->weight;
           p = p->nextarc;
       }
   }
   
   return sum;
}

int GetNode(ALGraph G) {
    int count = 0;
    
    for (int i = 0; i < G.vexnum; i++) {
        int outDegree = 0, inDegree = 0;
        
        ArcNode *p = G.vertices[i].firstarc;
        while (p) {
            outDegree++;
            p = p->nextarc;
        }
        
        for (int j = 0; j < G.vexnum; j++) {
            if (j != i) {
                ArcNode *q = G.vertices[j].firstarc;
                while (q) {
                    if (q->adjvex == i) {
                        inDegree++;
                    }
                    q = q->nextarc;
                }
            }
        }
        
        if (outDegree > inDegree) {
            count++;
        }
    }

    return count;
}

这样就完成了对深度遍历邻接表、计算图中所有边的权值之和和计算图中出度大于入度的结点个数的补充代码实现。请注意,以上代码只是一个示例,具体实现可能因为输入数据结构等因素而有所调整。


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=9982

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?