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

#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);
	}

}

时间: 2024-08-24 20:46:35

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

leetcode 深搜广搜

遍历整个grid数组,当发现有1的时候,就把和这个1连成片的1都置为0,并增加一个计数.最后返回这个计数. 广搜,但这个代码通不过测试,栈溢出. class Solution { public: void bfs(vector<vector<char>>& grid,int i,int j){ if(i<0||j<0||i>=grid.size()||j>=grid[0].size()) return; if(grid[i][j]=='0') ret

HDU 4771 Stealing Harry Potter&#39;s Precious (深搜+广搜)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4771 题面: 欢迎参加--BestCoder周年纪念赛(高质量题目+多重奖励) Stealing Harry Potter's Precious Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2207    Accepted Submis

迷宫问题(深搜 广搜)

题目描述: 给出一个m*n的迷宫图和一个入口,一个出口. 编一个程序,打印一条从迷宫入口到出口的路径. -1表示走不通,0表示能走,只能上下左右走: 无路可走输出"no way": 样例输入: 8 5-1 -1 -1 -1 -1 0 0 0 0 -1-1 -1 -1 0 -1-1 0 0 0 -1-1 0 0 1 -1-1 0 0 0 -1-1 -1 -1 0 -1-1 0 0 0 -12 18 4 8 5-1 -1 -1 -1 -10 0 0 0 -1-1 -1 -1 0 -1-1

7.23 深搜广搜

深搜(DFS)  关键词:回溯 栈实现,(递归本质和栈一样)一直走到底再回溯,时间复杂度高,空间低 #include<iostream> #include<cstring> using namespace std; int R,C; char maps[40][40]; int dp[40][40]; int dir[2][4]={{1,0,0,-1},{0,1,-1,0}}; int ans=1<<30; void DFS(int x,int y,int step){

DFS-BFS(深搜广搜)原理及C++代码实现

深搜和广搜是图很多算法的基础,很多图的算法都是从这两个算法中启发而来. 深搜简单地说就是直接一搜到底,然后再回溯,再一搜到底,一直如此循环到没有新的结点. 广搜简单地说就是一层一层的搜,像水的波纹一样往外面扩散,扩散到最外层搜索也就完成了. prim最小生成树.Dijkstra单源最短路径算法都使用了类似广度优先搜索的思想. 拓扑排序就可以用深搜来实现,分解强连通分量也可以用深搜来实现(转置图加两次深搜) 我们实现广搜时需要用队列来辅助我们进行.实现深搜时使用栈来辅助我们进行,所以显而易见的用递

深搜,广搜

//实验要求: //用邻接表存储一个无向图, //深度优先,广度优先遍历 //拓扑排序 #include<stdio.h> #include<stdlib.h> #include<string.h> typedef int status ; struct ljno //邻接表数据类型 { int x; //存储数据 ljno* next; }ss; struct ALGraph { ljno *data ; int vexnum,exnum;//顶点个数和边的个数 };

965. 单值二叉树(深搜/广搜)

1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode(int x) { val = x; } 8 * } 9 */ 10 class Solution { 11 private int val; 12 private boolean flag=true; 13 public b

迷宫广搜

上学期学了C,这学期学C++.感觉最难的还是算法,上周作业的一道广搜题是我第一次接触广搜,由于第一学期刚学编程就接触的太多的算法难题,不禁对代码产生畏惧,不过还好没有放弃,虽然算法很难,但我慢慢找到了一点学数学时的乐趣.先介绍一下这道未来的我看过来会觉得很简单一道题吧 You are provided a maze(迷宫), and you need to program to find the least steps to walk from the start to the end.And

算法学习笔记 二叉树和图遍历—深搜 DFS 与广搜 BFS

图的深搜与广搜 马上又要秋招了,赶紧复习下基础知识.这里复习下二叉树.图的深搜与广搜.从图的遍历说起,图的遍历方法有两种:深度优先遍历(Depth First Search), 广度优先遍历(Breadth First Search),其经典应用走迷宫.N皇后.二叉树遍历等.遍历即按某种顺序访问"图"中所有的节点,顺序分为: 深度优先(优先往深处走),用的数据结构是栈, 主要是递归实现: 广度优先(优先走最近的),用的数据结构是队列,主要是迭代实现: 对于深搜,由于递归往往可以方便的利