深度优先遍历和广度优先遍历

#include <stdio.h>
#include <stdlib.h>
#define MaxVertexNum 50
#define false 0
#define true 1
typedef int bool;
typedef int Vertex;
typedef int WeightType;
struct VertexNode;//顶点结构
struct AdjNode;//邻接顶点结构
typedef struct AdjNode *PtrToAdjVertexNode;//指向邻接点的指针
typedef struct GraphNode *PtrToGraphNode;//指向图的指针
typedef PtrToGraphNode ListGraph;
typedef struct EdgeNode *Edge;
typedef struct VertexNode
{
    PtrToAdjVertexNode Head;//指向第一个邻接点的指针
} AdjList[MaxVertexNum];
struct AdjNode
{
    Vertex AdjVertex;//邻接点下标
    WeightType Weight;//权重
    PtrToAdjVertexNode Next;
};
struct GraphNode
{
    int VertexNum;
    int EdgeNum;
    AdjList G;
};
/*将边的信息封装,V1到V2权重为Weight的边*/
struct EdgeNode
{
    Vertex V1;
    Vertex V2;
    WeightType Weight;
};
/*队列ADT*/
typedef Vertex ElementType;
typedef struct QueueNode *Queue;
struct QueueNode
{
    ElementType *Array;
    int Capacity;
    int Front;
    int Rear;
    int Size;
};
int IsEmpty(Queue Q);
int IsFull(Queue Q);
Queue CreatQueue(int Capacity);
void EnQueue(ElementType X, Queue Q);
void DeQueue(Queue Q);
ElementType Front(Queue Q);
int Succ(int Value, Queue Q);//循环队列判断函数,判断是否循环
void Error(char *String);
/*队列结束*/
ListGraph CreatGraph(int VertexNum);
void InsertEdge(ListGraph Graph, Edge E);
Edge ReadEdge(void);//输入函数
void PrintWithDFS(ListGraph Graph);//深度优先遍历
void DFS(Vertex V, int *Visited, ListGraph Graph);
void PrintWithBFS(ListGraph Graph);//广度优先遍历
int main()
{
    int VertexNum;
    Vertex V1,V2;
    WeightType Weight;
    Edge E = NULL;
    printf("输入顶点数:\n");
    scanf("%d",&VertexNum);
    ListGraph Graph = CreatGraph(VertexNum);
    printf("输入边(-1结束),格式:顶点1 顶点2 边权重\n");
    /*
    6个顶点测试数据
    1 2 5
    1 4 7
    2 3 4
    3 1 8
    3 6 9
    4 3 5
    4 6 6
    5 4 5
    6 1 3
    6 5 1
    -1
    */
    while((E = ReadEdge()) != NULL)
    {
        InsertEdge(Graph,E);
    }
    PrintWithDFS(Graph);
    PrintWithBFS(Graph);
    return 0;
}
ListGraph CreatGraph(int VertexNum)
{
    Vertex V;
    ListGraph Graph;
    Graph = (ListGraph)malloc(sizeof(struct GraphNode));
    Graph->VertexNum = VertexNum;
    Graph->EdgeNum = 0;
    for (V = 0; V < Graph->VertexNum; V++) {
        Graph->G[V].Head = NULL;
    }
    return Graph;
}
void InsertEdge(ListGraph Graph, Edge E)
{
    PtrToAdjVertexNode NewNode;
    NewNode = (PtrToAdjVertexNode)malloc(sizeof(struct AdjNode));
    NewNode->AdjVertex = E->V2;
    NewNode->Weight = E->Weight;
    /*将新添加的邻接点插在邻接表头*/
    NewNode->Next = Graph->G[E->V1].Head;
    Graph->G[E->V1].Head = NewNode;
}
Edge ReadEdge(void)
{
    Edge E = (Edge)malloc(sizeof(struct EdgeNode));
    scanf("%d",&E->V1);
    if (E->V1 != -1)
    {
        scanf("%d %d",&E->V2,&E->Weight);
    }else
    {
        E = NULL;
    }
    return E;
}
void PrintWithDFS(ListGraph Graph)
{
    Vertex V;
    int *Visited = (int*)malloc(sizeof(int)*(Graph->VertexNum+1));
    for (V=0; V<=Graph->VertexNum; V++) {
        Visited[V] = false;
    }
    printf("输入开始深度优先遍历的节点:");
    scanf("%d",&V);
    printf("深度优先遍历结果:");
    DFS(V, Visited, Graph);
    printf("\n");
}
void DFS(Vertex V, int *Visited, ListGraph Graph)
{
    PtrToAdjVertexNode W;
    Visited[V] = true;
    printf("%d ",V);
    for (W=Graph->G[V].Head; W; W=W->Next) {
        if (!Visited[W->AdjVertex]) {
            DFS(W->AdjVertex, Visited, Graph);
        }
    }
}
void PrintWithBFS(ListGraph Graph)
{
    Vertex V;
    PtrToAdjVertexNode W;
    int *Visited = (int*)malloc(sizeof(int)*(Graph->VertexNum+1));
    for (V=0; V<=Graph->VertexNum; V++) {
        Visited[V] = false;
    }
    printf("输入开始广度优先遍历的节点:");
    scanf("%d",&V);
    printf("广度优先遍历结果:");
    Queue Q = CreatQueue(50);
    Visited[V] = true;
    EnQueue(V, Q);
    while (!IsEmpty(Q)) {
        V = Front(Q);
        printf("%d ",V);
        DeQueue(Q);
        for (W=Graph->G[V].Head; W; W=W->Next) {
            if (!Visited[W->AdjVertex]) {
                Visited[W->AdjVertex] = true;
                EnQueue(W->AdjVertex, Q);
            }
        }
    }
}
/*队列实现*/
int IsEmpty(Queue Q)
{
    return Q->Size == 0;
}
int IsFull(Queue Q)
{
    return Q->Size == Q->Capacity;
}
Queue CreatQueue(int Capacity)
{
    Queue Q = (Queue)malloc(sizeof(struct QueueNode));
    Q->Array = (ElementType*)malloc(sizeof(ElementType)*Capacity);
    Q->Capacity = Capacity;
    Q->Size = 0;
    Q->Front = 1;
    Q->Rear = 0;
    return Q;
}
void EnQueue(ElementType X, Queue Q)
{
    if (IsFull(Q)) {
        Error("Full queue!");
    }else
    {
        Q->Size++;
        Q->Rear = Succ(Q->Rear, Q);
        Q->Array[Q->Rear] = X;
    }
}
void DeQueue(Queue Q)
{
    if (IsEmpty(Q)) {
        Error("Empty queue!");
    }else
    {
        Q->Size--;
        Q->Front = Succ(Q->Front, Q);
    }
}
ElementType Front(Queue Q)
{
    return Q->Array[Q->Front];
}
int Succ(int Value, Queue Q)
{
    if ( ++Value == Q->Capacity ) {
        Value = 0;
    }
    return Value;
}
void Error(char *String)
{
    puts(String);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值