kawaii

图的一些操作
#define MaxWeight 10 //图的邻接矩阵存储 typedef struct { int ...
扫描右侧二维码阅读全文
23
2021/03

图的一些操作

#define MaxWeight 10
//图的邻接矩阵存储
typedef struct {
    int vex[MaxSize];
    int Edge[MaxSize][MaxSize];
    int vexnum, arcnum;
} MGraph;

//图的邻接表表示
//边表
typedef struct ArcNode {
    int adjvex;
    ArcNode *next;
} ArcNode;
//顶点表
typedef struct VexNode {
    int data;
    ArcNode *frist;
} VNode, AdjList[MaxSize];
typedef struct {
    AdjList vertices;
    int vexnum, arcnum;
} ALGraph;

//图G中第一个邻接顶点(邻接矩阵)
int FristNeighbor(MGraph G, int x) {
    int col;
    if (x != -1) {
        for (col = 1; col <= G.vexnum; col++) {
            if (G.Edge[x][col] > 0 && G.Edge[x][col] < MaxWeight)
                return col;
        }
    }
    return -1;
}

//图G中x除y的下一个邻接点(邻接矩阵)
int NextNeighbor(MGraph G, int x, int y) {
    int col;
    if (x != -1 && y != -1) {
        for (col = y + 1; col < G.vexnum; col++)
            if (G.Edge[x][col] > 0 && G.Edge[x][col] < MaxWeight)
                return col;
    }
    return -1;
}

//图G中第一个邻接顶点(邻接表)
int FristNeighbor(ALGraph G, int x) {
    if (x != -1) {
        ArcNode *p = G.vertices[x].frist;
        return p->adjvex;
    }
    return -1;
}

//图G中x除y的下一个邻接点(邻接表)
int NextNeighbor(ALGraph G, int x, int y) {
    if (x != -1 && y != -1) {
        ArcNode *p = G.vertices[x].frist;
        while (p != NULL && p->adjvex != y)
            p = p->next;
        if (p != NULL && p->next != NULL)
            return p->next->adjvex;
    }
    return -1;
}
Last modification:March 23rd, 2021 at 12:20 am
If you think my article is useful to you, please feel free to appreciate

Leave a Comment