邻接表的两种遍历算法
来源:互联网 发布:m1a2sep各项数据 编辑:程序博客网 时间:2024/05/15 00:18
#include<iostream>
using namespace std;
struct arcnode
{
int adjvex;
arcnode*next;
};
template<class type>
struct vertexnode
{
type vertex;
arcnode*firstedge;
};
const int maxsize=10;
template<class type>
class algraph
{
public:
algraph(type a[],int n,int e);
~algraph(){}
void dfstravese(int v);
void bfstravese(int v);
private:
type vertex[maxsize];
int vertexnum,arcnum;
int visited[maxsize];
};
template<class type>
algraph<type>::algraph(type a[],int n,int e)
{
vertexnum=n;arcnum=e;
for(int i=0;i<vertexnum;i++)
visited[i]=0;
for( i=0;i<vertexnum;i++)
vertex[i]=a[i];
for(i=0;i<vertexnum;i++)
for(int j=0;j<vertexnum;j++)
arc[i][j]=0;
for(int k=0;k<arcnum;k++)
{
int j;
cin>>i>>j;
arc[i][j]=1;
arc[j][i]=1;
}
}
template<class type>
void algraph<type>::dfstravese(int v)
{
cout<<vertex[v];
visited[v]=1;
for(int i=0;i<vertexnum;i++)
if(arc[v][i]==1&&visited[i]==0)
dfstravese(i);
}
template<class type>
void algraph<type>::bfstravese(int v)
{
int q[maxsize];
int front=-1,rear=-1;
cout<<vertex[v];
visited[v]=1;
q[++rear]=v;
while(front!=rear)
{
v=q[++front];
for(int j=0;j<vertexnum;j++)
if(arc[v][j]==1&&visited[j]==0)
{
cout<<vertex[j];
visited[j]=1;
q[++rear]=j;
}
}
}
int main()
{
int a[6]={0,1,2,3,4,5};
algraph<int> obj1(a,6,8);
obj1.dfstravese(0);
return 0;
}
using namespace std;
struct arcnode
{
int adjvex;
arcnode*next;
};
template<class type>
struct vertexnode
{
type vertex;
arcnode*firstedge;
};
const int maxsize=10;
template<class type>
class algraph
{
public:
algraph(type a[],int n,int e);
~algraph(){}
void dfstravese(int v);
void bfstravese(int v);
private:
type vertex[maxsize];
int vertexnum,arcnum;
int visited[maxsize];
};
template<class type>
algraph<type>::algraph(type a[],int n,int e)
{
vertexnum=n;arcnum=e;
for(int i=0;i<vertexnum;i++)
visited[i]=0;
for( i=0;i<vertexnum;i++)
vertex[i]=a[i];
for(i=0;i<vertexnum;i++)
for(int j=0;j<vertexnum;j++)
arc[i][j]=0;
for(int k=0;k<arcnum;k++)
{
int j;
cin>>i>>j;
arc[i][j]=1;
arc[j][i]=1;
}
}
template<class type>
void algraph<type>::dfstravese(int v)
{
cout<<vertex[v];
visited[v]=1;
for(int i=0;i<vertexnum;i++)
if(arc[v][i]==1&&visited[i]==0)
dfstravese(i);
}
template<class type>
void algraph<type>::bfstravese(int v)
{
int q[maxsize];
int front=-1,rear=-1;
cout<<vertex[v];
visited[v]=1;
q[++rear]=v;
while(front!=rear)
{
v=q[++front];
for(int j=0;j<vertexnum;j++)
if(arc[v][j]==1&&visited[j]==0)
{
cout<<vertex[j];
visited[j]=1;
q[++rear]=j;
}
}
}
int main()
{
int a[6]={0,1,2,3,4,5};
algraph<int> obj1(a,6,8);
obj1.dfstravese(0);
return 0;
}
0 0
- 邻接表的两种遍历算法
- 数据结构邻接表的建立以及两种遍历
- 邻接表存储有向图的两种遍历方式
- 基于邻接表的图的广度优先遍历算法
- 邻接表的两种模拟写法
- 建邻接表的两种方式
- 图的邻接表存储与深度优先遍历算法
- SDUT图的深度遍历之邻接表算法
- 图的两种结构(邻接矩阵、邻接表)DFS、BFS算法
- 邻接表的图遍历
- 邻接表的dfs遍历
- 邻接表的bfs遍历
- 图的邻接表遍历
- 邻接矩阵(以顶点为中心),比较稀疏时,采用邻接表;图的两种遍历(邻接矩阵实现)
- 图的邻接表(广度优先遍历,深度优先遍历,最小生成树(Kruskal算法))
- 图算法:1、邻接表实现图的深度优先遍历,广度优先遍历
- 邻接矩阵的两种遍历算法
- 递归深度优先遍历图(DFS)(邻接表和数组两种方式)
- 2014年江苏移动暑期实习笔试部分题目
- String池与iterator对集合的迭代
- java调用本地应用学习
- PHP的语法
- 若干个数 以“字符串形式‘从键盘输入,将其转换为 double 型,并求平均数
- 邻接表的两种遍历算法
- Win7系统安装PHPnow的解决方法
- NYOJ 417 死神来了
- JavaMail: Access to default session denied
- 【线性表】链式存储结构之:循环链表
- extjs日期控件
- 邻接矩阵的两种遍历算法
- Linux下安装软件的逻辑和过程
- 染色问题的算法