图论复习

为了准备面试,复习了图的基础知识,理论知识参考相关书籍即可。

邻接矩阵实现的图及DFS和BFS:

// GraphTheory.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
#include<queue>

using namespace std;

typedef char VertextType;
typedef int EdgeType;
#define MAXVEX 100
#define INFINITY 65535
typedef struct
{
    VertextType vexs[MAXVEX];
    EdgeType arc[MAXVEX][MAXVEX];
    int numVertexes, numEdges;
} MGraph;

void CreateMraph(MGraph *G)
{
    int i, j, k, w;
    cout << "输入顶点数和边数:\n";
    cin >> G->numVertexes, cin >> G->numEdges;

    for (i = 0; i < G->numVertexes; i++)
        cin >> G->vexs[i];
    for (i = 0; i < G->numVertexes; i++)
        cin >> G->vexs[i];

    for (i = 0; i < G->numVertexes; i++)
        for (j = 0; j < G->numVertexes; j++)
            G->arc[i][j] = INFINITY;

    for (k = 0; k < G->numEdges; k++)//输入numEdges条边
    {
        cout << "输入边(vi,vj)上的下标i,下标j和权w:\n";
        cin >> i;
        cin >> j;
        cin >> w;
        G->arc[i][j] = w;
        G->arc[j][i] = G->arc[i][j];//无向图邻接矩阵为对称阵
    }
}

typedef int Boolean;
Boolean visited[MAXVEX];

void DFS(MGraph G, int i)
{
    int j;
    visited[i] = true;
    cout << G.vexs[i] << " ";
    for (j = 0; j < G.numVertexes; j++)
        if (G.arc[i][j] == 1 && !visited[j])
            DFS(G, j);
}

void DFSTraverse(MGraph G)
{
    int i;
    for (i = 0; i < G.numVertexes; i++)
        visited[i] = false;

    for (i = 0; i < G.numVertexes; i++)
        if (!visited[i])
            DFS(G, i);
}

void BFSTraverse(MGraph G)
{
    int i, j;
    queue<VertextType> Queue;

    for (i = 0; i < G.numVertexes; i++)
        visited[i] = false;

    for (i = 0; i < G.numVertexes; i++)
    {
        if (!visited[i])
        {
            visited[i] = true;
            cout << G.vexs[i] << " ";
            Queue.push(G.vexs[i]);//将此顶点入队

            while (!Queue.empty())
            {
                Queue.pop();
                for (j = 0; j < G.numVertexes; j++)
                {
                    if (G.arc[i][j] == 1 && !visited[j])//如果其他结点与当前结点存在边且没有被访问过
                    {
                        visited[j] = true;
                        cout << G.vexs[j] << " ";
                        Queue.push(G.vexs[j]);
                    }
                }

            }
        }
    }

}

int main()
{
    return 0;
}

邻接表实现的图及BFS、DFS:

// GraphThoery2.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
#include<queue>

using namespace std;

typedef char VertexType;
typedef int EdgeType;

#define MAXVEX 100
#define INFINITY 65535

typedef struct EdgeNode//边表结点
{
    int adjvex;
    EdgeType weight;
    struct EdgeNode *next;

} EdgeNode;

typedef struct VertexNode//顶点表结点
{
    VertexType data;
    EdgeNode *firstedge;
} VertexNode,AdjList[MAXVEX];

typedef struct                         
{
    AdjList adjList;
    int numVertexes, numEdges;//图中当前顶点数和边数
} GraphAdjList;

typedef int Boolean;
Boolean visited[MAXVEX];

void CreateALGraph(GraphAdjList *G)
{
    int i, j, k;
    EdgeNode *e;
    cout << "输入顶点数和边数:\n";
    cin >> G->numVertexes, cin >> G->numEdges;

    for (i = 0; i < G->numVertexes; i++)//输入顶点信息
    {
        cin >> G->adjList[i].data;
        G->adjList[i].firstedge = NULL;//将边表置空
    }

    for (k = 0; k < G->numEdges; k++)
    {
        cout << "输入边(vi,vj)上的顶点序号:\n";
        cin >> i, cin >> j;

        //用头插法创建邻接表
        e = (EdgeNode *)malloc(sizeof(EdgeNode));

        e->adjvex = j;
        e->next = G->adjList[i].firstedge;
        G->adjList[i].firstedge = e;

        e = (EdgeNode *)malloc(sizeof(EdgeNode));

        e->adjvex = i;
        e->next = G->adjList[i].firstedge;
        G->adjList[j].firstedge = e;
    }
}
void DFS(GraphAdjList *GL, int i)
{

    EdgeNode *p;
    visited[i] = true;
    cout << GL->adjList[i].data << " ";
    p = GL->adjList[i].firstedge;

    while (p)
    {
        if (!visited[p->adjvex])
            DFS(GL, p->adjvex);
        p = p->next;
    }
}


void DFSTraverse(GraphAdjList *GL)
{
    int i;
    for (i = 0; i < GL->numVertexes; i++)
        visited[i] = false;

    for (i = 0; i < GL->numVertexes; i++)
        if (!visited[i])
            DFS(GL, i);
}

void BFSTraverse(GraphAdjList *GL)
{
    int i;
    EdgeNode *p;
    queue<VertexType> Queue;
    for (i = 0; i < GL->numVertexes; i++)
        visited[i] = true;

    for (i = 0; i < GL->numVertexes; i++)
    {
        if (!visited[i])
        {
            visited[i] = true;
            cout << GL->adjList[i].data << " ";
            Queue.push(GL->adjList[i].data);
            while (!Queue.empty())
            {
                Queue.pop();
                //找到当前顶点表链表头指针
                p = GL->adjList[i].firstedge;
                while (p)
                {
                    if (!visited[p->adjvex])
                    {
                        visited[p->adjvex] = true;
                        cout << GL->adjList[p->adjvex].data << " ";
                        //将此顶点入队列
                        Queue.push(GL->adjList[p->adjvex].data);
                    }
                    p = p->next;
                }
            }
        }
    }


}



int main()
{
    return 0;
}

参考:《大话数据结构》

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值