• 邻接表实现图的深度遍历


    邻接表实现图的深度遍历,纠结的问题啊。。。。。

    View Code
    #include <stdio.h>
    #include
    <malloc.h>
    #define MAX 100
    typedef
    struct arcnode/*表节点*/
    {
    int adjvex; /*邻接点*/
    struct arcnode *nextarc;/*指向下一条弧的指针*/
    }arcnode;
    typedef
    struct vnode /*头结点*/
    {
    int data; /*定点信息*/
    arcnode
    * firstarc; /*指向第一个依附该顶点的弧的指针*/
    }vnode, adjlist[MAX];
    typedef
    struct /**/
    {
    adjlist vertices;
    /**/
    int vexnum, arcnum; /*图的顶点数和弧数*/
    }algraph;

    int visited[MAX];
    int n = 1;

    int locateVex(algraph * g, int v) /*寻找节点V的位置*/
    {
    int k, n;
    for(k = 1; k <= g->vexnum; k++)
    {
    if(g->vertices[k].data == v)
    {
    n
    = k;
    break;
    }
    }
    return n;
    }

    void Insertadj(algraph * g, int i, int j) /*插入邻接点的下标*/
    {
    arcnode
    *a1, *a2;
    a1
    = (arcnode *)malloc(sizeof(arcnode));
    a1
    ->adjvex = j; a1->nextarc = NULL;
    if(g->vertices[i].firstarc == NULL)
    {
    g
    ->vertices[i].firstarc = a1;
    }
    else
    {
    a2
    = g->vertices[i].firstarc;
    while(a2->nextarc)
    {
    a2
    = a2->nextarc;
    }
    a2
    ->nextarc = a1;
    }
    }
    void Print_algraph(algraph * g) /*打印邻接表*/
    {
    int i;
    arcnode
    *a3;
    for(i = 1; i<= g->vexnum; i++)//打印无向图邻接表
    {
    printf(
    " %d V%d |", i, g->vertices[i].data);
    if(g->vertices[i].firstarc != NULL)
    {
    a3
    = g->vertices[i].firstarc;
    while(a3)
    {
    printf(
    " --> %d ", a3->adjvex);
    a3
    = a3->nextarc;
    }
    }
    printf(
    "\n");
    }
    }

    void Init_algraph(algraph * g) /*初始化图并建图*/
    {
    int v, v1, v2, i, j;
    scanf(
    "%d %d",&g->vexnum, &g->arcnum);//输入(点,边)
    for(v = 1; v <= g->vexnum; v++)
    {
    scanf(
    "%d", &g->vertices[v].data);//输入点 V
    g->vertices[v].firstarc = NULL;
    }
    for(v = 1; v <= g->arcnum; v++)
    {
    scanf(
    "%d %d", &v1, &v2);//输入(点,点),即边
    i = locateVex(g,v1); /*v1的位置*/
    j
    = locateVex(g,v2); /*v2的位置*/
    Insertadj(g,i,j);
    Insertadj(g,j,i);
    }
    Print_algraph(g);
    }
    void DFS(algraph * g, int v)
    {
    arcnode
    * p;
    p
    = g->vertices[v].firstarc;
    if(n < g->vexnum)
    {
    printf(
    " V%d -->",g->vertices[v].data);
    n
    ++;
    }
    else
    printf(
    " V%d\n", g->vertices[v].data);
    visited[v]
    = 1;
    while(p)
    {
    if(!visited[p->adjvex])
    DFS(g,p
    ->adjvex);
    p
    = p->nextarc;
    }
    }
    void DFSvisit(algraph *g)//深度优先遍历点顺序
    {
    int v;
    printf(
    "\nDepth_first search:\n");
    for(v = 1; v <= g->vexnum; v++)
    visited[v]
    = 0;
    for(v = 1; v <= g->vexnum; v++)
    if(!visited[v])
    DFS(g,v);
    }
    int main()
    {
    algraph g;
    Init_algraph(
    &g);
    DFSvisit(
    &g);
    return 0;
    }
  • 相关阅读:
    An impassioned circulation of affection(尺取+预处理)
    旅游(CSUST省赛选拔赛2+状压dp+最短路)
    Islands and Bridges(POJ2288+状压dp+Hamilton 回路)
    Travelling(HDU3001+状压dp+三进制+最短路)
    Hie with the Pie(POJ3311+floyd+状压dp+TSP问题dp解法)
    hash(2018年CSUST省赛选拔赛第一场B题+hash+字典树)
    Everything Has Changed(HDU6354+圆交+求周长)
    [iOS Animation]-CALayer 图层几何学
    [iOS Animation]-CALayer 显示方式
    [iOS Animation]-CALayer 图层树
  • 原文地址:https://www.cnblogs.com/vongang/p/2121396.html
Copyright © 2020-2023  润新知