图论(建图和遍历)

· · 个人记录

图论基础

图的储存

链式前向星

链式前向星存图一般需要这些数组:

/*边的信息*/

int tot;//这条边的编号
int value[i];//第i条边的权值
int to[i];//第i条边指向的终点
int nxt[i];//第i条边上一条边的编号,不建议叫next(可能会挂)

/*记录边的相对关系*/
int head[j];//以j为起点的最后一条边的编号;

/*初始化*/
tot = 0;
memset(head, -1, sizeof(-1));

当然,这么多数组看起脑壳痛,结构体大法走起:

struct theedge{
    int to;//这条边指向的点
    int value;//边权
    int nxt;//与这个边起点相同的上一条边的编号(上一条边的数组下标)
};
struct theedge edge[maxn];

int head[maxn]//head[i],表示以i为起点的第一条边的编号;

/*初始化*/
void init()
{
    tot = 0;
    memset(head, -1, sizeof(head));
}

用链式前向星增加一条边的操作如下:

void add(int u/*起点*/, int v/*终点*/, int w/*边权*/)
{
    /*建立这条边*/
    to[tot] = v;//指向终点
    value[tot] = w;//存储边权
    nxt[tot] = head[u];//把当前以u为起点上一条边的编号记录
    /*更新数值准备建下一条边*/
    head[u] = cnt;//更新以u为起点上一条边的编号
    cnt ++;
    return;
}

当然也有结构体版本的

void add(int u, int v, int w)
{
    /*建立这条边*/
    edge[tot].to = v;
    edge[tot].value = w;
    edge[tot].nxt = head[u];
    /*更新数值准备建下一条边*/
    head[u] = cnt;//更新以u为起点上一条边的编号
    cnt ++;
    return;
}

这样就建好了一条由点u指向点v,并且边权为w的有向边了。如果想要建立无向边,只需要在再建一条反向的边即可。

vector

vector建图的好处就是好记好写,然而会比前向星慢一些。

vector储存从每个点出发的每一条边,如果存在边权,自然可以开一个结构体数组来存储终点和边权。要建立无向图时同样也是把起点终点倒过来在建立一次即可。

struct theedge{
    int to,value;//终点和边权
};
std::vector<theedge> G[maxn]//G[i]中的i表示出发点

for(int i = 0; i < n; ++ i){
    G[i].clear();//初始化
}

建边操作如下:

void add(int u, int v, int w)
{
    theedge egde;
    egde.to = v;
    egde.value = w;
    G[u].push_back(egde);
    return;
}

然而,既然学会了链式前向星,我当然是选择拒绝vector了!!!!重点是慢!

拒绝一切卡常元素。

所以之后的内容统统使用链式前向星实现

图的遍历

深度优先遍历( DFS)

图的DFS遍历是从一个节点出发,顺着一条道往下溜,一边走一边标记,走到走无可走的时候就跳到上一个点去找和他相连却没有被访问过的点,直到所有节点都被访问。

由于链式前向星存储了相对位置,遍历时就可以方便的访问下一条边。

bool vis[maxn];//标记访问情况
memset(vis, 0, sizeof(vis));//初始化,清空标记

//x为当前节点
void dfs(int x)
{
    if(! vis[x]){
        vis[x] = 1;
        printf("%d\n",x);
        //如果这个点没有走过,那么标记并输出
    }
    for(int i = head[x]; i != -1; i = edge[i].nxt){
        if(! vis[edge[i].to){
            dfs(edge[i].to);
            //如果这条边指向的终点没有走过,就继续走下一条边;
        }
    }
    return;
}

广度优先遍历 (BFS)

广度优先搜索可以方便的用队列实现,只需要不停的把下一条边压入队尾即可。

//#include <queue>

bool vis[maxn];//标记访问情况
memset(vis, 0, sizeof(vis));//初始化,清空标记

std::queue<int> que;//队列里存储的是边的下标
void bfs(i)
{
    que.push(1);//把第一条边压入队,此处可以替换为任意一条开始的边
    while(! que.empty()){
        if(vis[que.front()]){
            que.pop();
            continue;
            //如果当前的边已经走过了,弹出进入下一轮
        }
        printf("%d\n", que.front());
        vis[que.front()] = 1;//把当前节点标记为走过,并且输出
        for(int i = head[que.front()]; i != -1; i = edge[i].next){
            if(! vis[edge[i].to]){
                que.push(edge[i].to);
                //如果下一条边没有走过,把它压入队列
            }
        }
        que.pop(); //弹出当前节点
    }
    return;
}

完整代码

链式前向星建图 + DFS

//graph-fs-dfs.cpp
#include <cstdio>
#include <cstring>

const int maxn = 10000;
const int maxm = 10000;

struct Edge{
    int nxt,to,value;
};

int head[maxn] = {0,};
struct Edge edge[maxn];
// int n = 0;
/*point number*/
int m = 0;
/*edge number*/
int cnt = 1;
int u = 0,v = 0, w = 0;
bool vis[maxm];

void add(int u, int v, int w);
void dfs(int x);

int main()
{
    memset(head, -1, sizeof(head));
    memset(vis, 0, sizeof(vis));

    // scanf("%d",&n);
    scanf("%d",&m);
    for(int i = 0; i < m; ++ i){
        scanf("%d%d%d",&u,&v,&w);
        add(u, v, w);
        add(v, u, w);
    }

    dfs(1);
    return 0;
}

void add(int u, int v, int w)
{
    edge[cnt].to = v;
    edge[cnt].value = w;
    edge[cnt].nxt = head[u];

    head[u] = cnt;
    ++ cnt;
    return;
}

void dfs(int x)
{
    if(! vis[x]){
        vis[x] = 1;
        printf("%d\n",x);
    }
    for(int i = head[x]; i != -1; i = edge[i].nxt){
        if(! vis[edge[i].to]){
            dfs(edge[i].to);
        }
    }
    return;
}

链式前向星建图 + BFS

#include <queue>
#include <cstdio>
#include <cstring>

const int maxn = 10000;
const int maxm = 10000;

struct Edge{
    int nxt, to;
    int value;
};

struct Edge edge[maxm];
int head[maxm];
bool vis[maxm];
int n = 0, m = 0, cnt = 0;
int u = 0, v = 0, w = 0;

std::queue<int> que;

void init();
void add(int u, int v, int w);
void bfs();

int main()
{
    init();
    scanf("%d",&m);
    for(int i = 0; i < m; ++ i){
        scanf("%d%d%d",&u, &v, &w);
        add(u, v, w);
        add(v, u, w);
    }

    bfs();
    return 0;
}

void init()
{
    cnt = 1;
    memset(vis, 0, sizeof(vis));
    memset(head, -1, sizeof(head));
    return;
}

void add(int u, int v, int w)
{
    edge[cnt].to = v;
    edge[cnt].value = w;
    edge[cnt].nxt = head[u];
    head[u] = cnt;
    ++ cnt;

    return;
}

void bfs()
{
    que.push(1);

    while(! que.empty()){
        if(vis[que.front()]){
            que.pop();
            continue;
        }
        vis[que.front()] = 1;
        printf("%d\n",que.front());

        for(int i = head[que.front()]; ~ i; i = edge[i].nxt){
            if(! vis[edge[i].to]){
                que.push(edge[i].to);
            }
        }

        que.pop();
    }
    return;
}