图的创建,深搜,广搜(基于临接表实现)

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <queue>
using namespace std;

#define MaxVertexNum 100			//最大顶点数
typedef enum{DG,UDG,DN,UDN} GraphKind; 	//图的种类
typedef int InfoType; 
typedef char VertexType; 
 
typedef struct ArcNode {
	 int adjvex;    				//邻接点域,存储该弧指向顶点的下标  
	 struct ArcNode *next;			//指向下一条弧的指针 
	 InfoType* info;				//该弧相关信息的指针 
}ArcNode;

typedef struct VertexNode {
	VertexType data;				//数据域 
	ArcNode *firstArc;				//指向第一条依附该顶点的弧的指针 
}VertexNode,AdjList[MaxVertexNum];

typedef struct {
	AdjList vertices;				//顶点集		
	int vexnum,arcnum; 				//图的顶点数和弧数
	GraphKind kind;
}ALGraph;		//Adjacency List
bool visited[MaxVertexNum];   //设访问标志数组,供遍历时使用

void CreateALGraph(ALGraph *G);
int LocateVex(ALGraph *G,VertexType v);
void Display(ALGraph *G);
void DFS(ALGraph *G,int v);
void DFSTraver(ALGraph *G);
void BFSTraver(ALGraph *G);

int main()
{
	ALGraph G;
	CreateALGraph(&G); 
    Display(&G);
	DFSTraver(&G);
	BFSTraver(&G);
	return 0;
}

//求顶点位置函数
int LocateVex(ALGraph *G,VertexType v)
{
    int i;
    for(i=0;i<G->vexnum;i++)
    {
        if(G->vertices[i].data == v)
            return i;
    }
    return -1;
}

/*
(a,b,1)
(a,c,1)
(b,d,1)
(c,d,1)
(d,a,1)
*/

//以又向带权图为例(DN)
void CreateALGraph(ALGraph *G)
{
    VertexType v1,v2;
    int weight;
    ArcNode *Arc;
    //1.确定顶点数和弧数
    printf("请输入顶点数和边数:");
    scanf("%d %d",&G->vexnum,&G->arcnum);

    //2.确定各个顶点的值
    printf("请输入各顶点的数据:");     
    for(int i=0;i<G->vexnum;i++){
        scanf(" %c",&(G->vertices[i].data));
        G->vertices[i].firstArc = NULL;     //顶点的边表设为空
    }

    printf("请依次输入n组边对应的两个顶点以及权值,输入格式为(a,b,c):\n");
    for(int k=0;k<G->arcnum;k++) {
        scanf(" (%c,%c,%d)",&v1,&v2,&weight);
            //printf("%c %c %d\n",v1,v2,weight);

        int i = LocateVex(G,v1);
        int j = LocateVex(G,v2);  
        Arc = (ArcNode*)malloc(sizeof(ArcNode));
        Arc->adjvex = j;
        Arc->info = (int *) malloc(sizeof(int));  
        *Arc->info = weight;
        Arc->next = G->vertices[i].firstArc;    //头插        
        G->vertices[i].firstArc = Arc;

        //若是无向图,还要再生成一个节点表示<V2,v1>
    }
}

void Display(ALGraph *G)
{
    ArcNode *p;
    printf("编号,  顶点,   相邻边的顶点\n");
    for(int i=0;i<G->vexnum;i++)
    {
        printf("%4d %4c",i,G->vertices[i].data);
        for(p=G->vertices[i].firstArc;p!=NULL;p=p->next)
            printf("%8d",p->adjvex);
        printf("\n");
    }
}

void Visit(ALGraph *G,int v)
{
    printf("%c ",G->vertices[v].data);
}
 
//深度优先遍历       
void DFS(ALGraph *G,int v)
{
    Visit(G,v);
    visited[v] = true;
    ArcNode *p = G->vertices[v].firstArc;
	while(p)
	{
		if(!visited[p->adjvex])
		{
			DFS(G,p->adjvex);			
		}p = p->next;
	}
}


void DFSTraver(ALGraph *G)
{

    for (int i = 0; i < G->vexnum; i++)
    {
        visited[i] = false;
    }
	printf("DFSTraver:\n");
	for (int i = 0; i < G->vexnum; i++)
	{
		if(!visited[i])
			DFS(G,i);
	}
		
}

//广度度优先遍历  
void BFS(ALGraph *G,int v)
{
	Visit(G,v);
	visited[v] = true;
	queue<int> q;
	q.push(v);
	while(!q.empty())
	{
		int w;
		w = q.front();
		q.pop(); //队头元素出队
        ArcNode *p = G->vertices[w].firstArc;
        while(p!=NULL)
        {
            if(!visited[p->adjvex])
            {
                Visit(G,p->adjvex);
                visited[p->adjvex] = true;
                q.push(p->adjvex);
            }
            p = p->next;    
        }

	}
}

void BFSTraver(ALGraph *G)
{
	printf("\nBFSTraver:\n");
    for (int i = 0; i < G->vexnum; i++)
    {
        visited[i] = false;
    };
	for (int i = 0; i < G->vexnum; i++)
	{
		if(!visited[i])
			BFS(G,i);
	}
		
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
邻接表是一种的存储结构,它可以用于实现深度优先搜索(DFS)和广度优先搜索(BFS)。下面是邻接表深搜广的C语言实现: 1. 邻接表深度优先搜索(DFS)C语言实现: ```c #define MAXNODE 1000 #define MAXSIDE 10000 typedef struct SIDE { int tailNode; int nextSide; int weight; } SIDE; SIDE edge[MAXSIDE]; int head[MAXNODE]; int visited[MAXNODE]; int counttEdge = 0; void CreateABoundary(int headNode, int tailNode, int weight) { edge[counttEdge].tailNode = tailNode; edge[counttEdge].weight = weight; edge[counttEdge].nextSide = head[headNode]; head[headNode] = counttEdge; counttEdge++; } void DFS(int node) { visited[node] = 1; printf("%d ", node); for (int i = head[node]; i != -1; i = edge[i].nextSide) { int nextNode = edge[i].tailNode; if (!visited[nextNode]) { DFS(nextNode); } } } int main() { // 初始化 memset(head, -1, sizeof(head)); memset(visited, 0, sizeof(visited)); counttEdge = 0; // 创建 CreateABoundary(1, 2, 0); CreateABoundary(1, 3, 0); CreateABoundary(2, 4, 0); CreateABoundary(2, 5, 0); CreateABoundary(3, 6, 0); CreateABoundary(3, 7, 0); // DFS遍历 DFS(1); return 0; } ``` 2. 邻接表广度优先搜索(BFS)C语言实现: ```c #define MAXNODE 1000 #define MAXSIDE 10000 typedef struct SIDE { int tailNode; int nextSide; int weight; } SIDE; SIDE edge[MAXSIDE]; int head[MAXNODE]; int visited[MAXNODE];int counttEdge = 0; void CreateABoundary(int headNode, int tailNode, int weight) { edge[counttEdge].tailNode = tailNode; edge[counttEdge].weight = weight; edge[counttEdge].nextSide = head[headNode]; head[headNode] = counttEdge; counttEdge++; } void BFS(int node) { int queue[MAXNODE]; int front = 0, rear = 0; visited[node] = 1; printf("%d ", node); queue[rear++] = node; while (front < rear) { int curNode = queue[front++]; for (int i = head[curNode]; i != -1; i = edge[i].nextSide) { int nextNode = edge[i].tailNode; if (!visited[nextNode]) { visited[nextNode] = 1; printf("%d ", nextNode); queue[rear++] = nextNode; } } } } int main() { // 初始化 memset(head, -1, sizeof(head)); memset(visited, 0, sizeof(visited)); counttEdge = 0; // 创建 CreateABoundary(1, 2, 0); CreateABoundary(1, 3, 0); CreateABoundary(2, 4, 0); CreateABoundary(2, 5, 0); CreateABoundary(3, 6, 0); CreateABoundary(3, 7, 0); // BFS遍历 BFS(1); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值