图的BFS和DFS在数据结构为邻接矩阵时的实现

#include <iostream>
#include <queue>
using namespace std;
//因为只是为了练习DFS和BFS  数据结构定义的很随意 没有优化类啦 能用就行
class Graph
{
private:
	  static const int MAX=100;
	int weight[MAX][MAX];//任意一个图最大节点数为MA
	int nodes;
	int vertexs;
	bool  *isVisited;
public:
void init(int n,int v);//init相当于构造函数的功能
	~Graph()
	{
	delete []isVisited;
	}
	void SearchBfs(int node);
	void SearchDfs(int node);
  void visit (int n)
  {
  cout<<"->"<<n;
  }
};

void Graph::init (int n,int v)//先后以节点数和边数为形参
{  vertexs=v;//节点数和边数初始化
   nodes=n;
	for(int i=0;i!=n;i++)
      for(int j=0;j!=n;j++)
	    {
		  weight[i][j]=0;
		 	    }
		 	    int i=0,j=0,weight_=0;
	  for(int h=0;h!=v;h++)
	       {
	     cout<<" vertex origin to  vertex destition  and  weight "<<endl;
	       cin>>i>>j>>weight_;
		   weight[i][j]=weight_;
		   }

	    isVisited=new bool[nodes];//记录节点是否被遍历
	    for(int w=0;w!=nodes;)
	        isVisited[w++]=false;
}
void Graph::SearchDfs(int node)
{
     visit(node);
     isVisited[node]=true;
	for(int i=0;i!=nodes;i++)
	    {
	    	if(weight[node][i]!=0&&isVisited[i]==false)
	    	        SearchDfs(i);
	    }
}
void Graph::SearchBfs(int node)
{
	queue<int>myQueue;
	myQueue.push(node);
	visit(node);
	isVisited[node]=true;
	while(!myQueue.empty())
{
    int v=myQueue.front();
    myQueue.pop();
	for(int i=0;i!=nodes;i++)
	 {
	 	if(weight[v][i]!=0&&isVisited[i]==false)
	 	{
	 		visit(i);
	        isVisited[i]=true;
	       myQueue.push(i);
	 	}

	 }
}

}
int main()
{
	Graph myGraph;
	myGraph.init(9,8);
	//myGraph.SearchBfs(0);
	myGraph.SearchDfs(0);

}
				
时间: 2024-07-31 00:47:52

图的BFS和DFS在数据结构为邻接矩阵时的实现的相关文章

基于邻接矩阵和邻接表的两种方法实现无向图的BFS和DFS

广度优先搜索(Breadth-First-Search)和深度优先搜索(Deep-First-Search)是搜索策略中最经常用到的两种方法,特别常用于图的搜索. BFS的思想: 从一个图的某一个顶点V0出发,首先访问和V0相邻的且未被访问过的顶点V1.V2.--Vn,然后依次访问与V1.V2--Vn相邻且未被访问的顶点.如此继续,找到所要找的顶点或者遍历完整个图.我们采用队列来存储访问过的节点. DFS的思想: 深度优先搜索所遵循的策略就是尽可能"深"的在图中进行搜索,对于图中某一个

图的遍历(bfs 和dfs)

BFS的思想: 从一个图的某一个顶点V0出发,首先访问和V0相邻的且未被访问过的顶点V1.V2.……Vn,然后依次访问与V1.V2……Vn相邻且未被访问的顶点.如此继续,找到所要找的顶点或者遍历完整个图. 由此可以看出,用BFS进行搜索所搜索的顶点都是按深度进行扩展的,先找到到V0距离为1的所有顶点,然后找到距离V0为2的顶点……所以BFS所搜索到的都是最短的路径. 由于要将距离V0为d(d>0)的且未被方位的点都记录起来,我们采用队列这种数据结构.队列的特点是先进先出(FIFO),从某个顶点出

15 图-图的遍历-基于邻接矩阵实现的BFS与DFS算法

算法分析和具体步骤解说直接写在代码注释上了 TvT 没时间了等下还要去洗衣服 就先不赘述了 有不明白的欢迎留言交流!(估计是没人看的了) 直接上代码: 1 #include<stdio.h> 2 #include<queue> 3 #include<iostream> 4 using namespace std; 5 typedef struct{ 6 int Vex[10];//顶点表 7 int Edge[10][10]; 8 int vexnum,arcnum;

PAT Advanced 1034 Head of a Gang (30) [图的遍历,BFS,DFS,并查集]

题目 One way that the police finds the head of a gang is to check people's phone calls. If there is a phone call between A and B, we say that A and B is related. The weight of a relation is defined to be the total time length of all the phone calls mad

java 数据结构 图中使用的一些常用算法 图的存储结构 邻接矩阵:图的邻接矩阵存储方式是用两个数组来标示图。一个一位数组存储图顶点的信息,一个二维数组(称为邻接矩阵)存储图中边或者弧的信息。 设图G有n个顶点,则邻接矩阵是一个n*n的方阵,定义为: 实例如下,左图是一个无向图。右图是邻接矩阵表示:

以下内容主要来自大话数据结构之中,部分内容参考互联网中其他前辈的博客. 图的定义 图是由顶点的有穷非空集合和顶点之间边的集合组成,通过表示为G(V,E),其中,G标示一个图,V是图G中顶点的集合,E是图G中边的集合. 无边图:若顶点Vi到Vj之间的边没有方向,则称这条边为无项边(Edge),用序偶对(Vi,Vj)标示. 对于下图无向图G1来说,G1=(V1, {E1}),其中顶点集合V1={A,B,C,D}:边集合E1={(A,B),(B,C),(C,D),(D,A),(A,C)}: 有向图:若

ural 1145 Rope in the Labyrinth 图中 bfs求树的直径

1145. Rope in the Labyrinth Time limit: 0.5 second Memory limit: 64 MB A labyrinth with rectangular form and size m × n is divided into square cells with sides' length 1 by lines that are parallel with the labyrinth's sides. Each cell of the grid is

[Algorithms] Graph Traversal (BFS and DFS)

Graph is an important data structure and has many important applications. Moreover, grach traversal is key to many graph algorithms. There are two systematic ways to traverse a graph, breadth-first search (BFS) and depth-frist search (DFS). Before fo

HDU 2102 A计划 (BFS或DFS)

题意:中文题. 析:是一个简单的搜索,BFS 和 DFS都可行, 主要是这个题有一个坑点,那就是如果有一层是#,另一个层是#或者*,都是过不去的,就可以直接路过, 剩下的就是一个简单的搜索,只不过是两层而已,可能一个就是在#必须传送,这个题目已经说的很清楚了. 代码如下: BFS: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string>

ppt 图的基本算法 dfs

#include <stdio.h>#include<string.h>#define N 10int g[N][N];int bz[N];int n,m ;void DFS(int cur){ int j; bz[cur]=1; printf("V%d",cur); for(j=1;j<=n ;j++ ) if(g[cur][j] && !bz[j]) DFS(j); } void input(){ int i,j,f,t ; scanf