邻接矩阵存储结构上实现图的基本操作


//uestc 的吧,多用心写代码,别只顾着copy
bool matrix_insert_vertex(MatrixGraph *G, VertexType v){
    if(matrix_locate_vertex(G,v) != -1){
        return false;
    }
    if(G -> vexnum == MAX_VERTEX_NUM){
        return false;
    }
    G -> vertex[G->vexnum] = v;
    for (int i = 0; i <= G->vexnum; i++) {
        G->arcs[i][G -> vexnum] = 0;
        G->arcs[G -> vexnum][i] = 0;
    }
    
    G->vexnum++;
    return true;
}

bool matrix_insert_arc(MatrixGraph *G, VertexType v, VertexType w){
    if(matrix_locate_vertex(G,v) == -1 || matrix_locate_vertex(G,w) == -1){
        return false;
    }
    int i = matrix_locate_vertex(G,v);
    int j = matrix_locate_vertex(G,w);
    if(G -> type == UDG){
        if(G->arcs[i][j] == 1 || G->arcs[j][i] == 1){
            return false;
        }
        G->arcs[i][j] = 1;
        G->arcs[j][i] = 1;
    }
    if(G -> type == DG){
        if(G->arcs[i][j] == 1 ){
            return false;
        }
        G->arcs[i][j] = 1;
        
    }
    G -> arcnum++;
    return true;
}
// created by ttj
上一篇:采用邻接矩阵形式村出土,进行图的深度优先搜索并输出结果。


下一篇:数据结构(5)