1、实验内容
1)编程实现图的深度优先算法。
2)修改算法,使之可以判断无向图有几个连通分量。
1、 实验要求
1) 通过实例,懂得深度优先搜索的机制。
2) 用2-3个实例验证算法。
<Graph.h>
#include <iostream>
#include<queue>
//图的基本算法类封装实现
//这里图用邻接表表示法(且是不带权的无向图)
template <class ElemType>
class CraphClass
{
public:
//图邻接表表示中节点数据结构
typedf struct StructGraphNode
{
ElemType elem;
struct StructGraphNode *next;
}GraphNode, *GraphNodeLink;
//图像遍历时节点颜色标记
enum NodeColor
{
WHITE,//未被发现
GRAY,//已被发现但是未访问
BLACK//已被访问
};
static const int MaxVal = 99999;
GraphClass();//默认构造函数
~CraphClass()//析构函数
//依据图中包含的节点以及邻接矩阵创建邻接表表示的图
void createGraph(ElemType*a, int n, char *matrix);
void BFSGraph();//图的广度优先搜索
void DFSGraph();//图的深度优先搜索
void _printAdjacencyList();//输出图的当前邻接表
private:
GraphNodeLink *root;//
};
<Graph.cpp>
#include <Graph.h>
using namespace std;
#define N 6
#define INFINITE 0x7fffffff
#define WHITE 1
#define GRAY 2
#define BLACK 3
struct Vertex
{
Vertex * next;
int id;
Vertex() :next(NULL), id(0){}
};
struct Graph
{
Vertex *Adj[N + 1];
int color[N + 1];
int p[N + 1];
int d[N + 1];
int f[N + 1];
Graph()
{
for (int i = 1; i <= N; i++)
{
Adj[i] = new Vertex;
color[i] = WHITE;
d[i] = 0;
f[i] = 0;
p[i] = 0;
}
}
~Graph()
{
for (int i = 1; i <= N; i++)
delete Adj[i];
}
};
void Print(Graph *g);
bool Init(Graph *g);
bool InsertEdge(Graph *g, int start, int end);
void PaintColor(Graph *g, int vertex, int color);
void DepthFirstVisit(Graph *g, int vertex, int& v_time);
//插入边
bool InsertEdge(Graph *g, int start, int end)
{
Vertex* v = new Vertex();
v->id = end;
if (g->Adj[start]->next == NULL)
{
Vertex* s = new Vertex();
s->id = start;
g->Adj[start] = s;
}
Vertex* tmp = g->Adj[start];
while (tmp->next)
{
tmp = tmp->next;
}
tmp->next = v;
return true;
}
bool graph(Graph *g)
{
InsertEdge(g, 1, 2);
InsertEdge(g, 1, 4);
InsertEdge(g, 2, 5);
InsertEdge(g, 3, 5);
InsertEdge(g, 3, 6);
InsertEdge(g, 4, 2);
InsertEdge(g, 5, 4);
InsertEdge(g, 3, 1);
InsertEdge(g, 6, 6);
return true;
}
bool DepthFirstSearch(Graph *g, int vertex)
{
for (int i = 1; i <= N; i++)
{
g->color[i] = WHITE;
}
int time = 0;
for (int i = 1; i <= N; i++)
{
if (g->color[i] == WHITE)
{
DepthFirstVisit(g, i, time);
}
}
return true;
}
void DepthFirstVisit(Graph *g, int vertex, int& v_time)
{
g->color[vertex] = GRAY;
v_time++;
g->d[vertex] = v_time;
Vertex * v = g->Adj[vertex];
int node = v->id;
std::cout << node << "\t";
v = v->next;
while (v)
{
if (g->color[v->id] == WHITE)
{
g->p[v->id] = vertex;
DepthFirstVisit(g, v->id, v_time);
}
v = v->next;
}
g->color[node] = BLACK;
g->f[node] = v_time++;
}
int main()
{
Graph *g = new Graph;
graph(g);
DepthFirstSearch(g, 1);
getchar();
return 0;
}
GraphDFS java 算法:
package exercise04;
public class DFS {
public static void main(String[] args) {
int [][]c = new int[9][9];
for(int i = 0;i < c.length;i++){
for(int j = 0;j < c[0].length;j++){
c[i][j] = 0;
}
}
c[1][2] = c[2][1] = 1;
c[1][3] = c[3][1] = 1;
c[2][4] = c[4][2] = 1;
c[2][5] = c[5][2] = 1;
c[3][6] = c[6][3] = 1;
c[3][7] = c[7][3] = 1;
c[4][8] = c[8][4] = 1;
c[5][8] = c[8][5] = 1;
c[6][7] = c[7][6] = 1;
System.out.println("The c :");
for(int i = 1;i < c.length;i++){
for(int j = 1;j < c.length;j++){
System.out.print(c[i][j] + " ");
}System.out.println();
}
System.out.println("the count = " + DFSSearch(c));
}
public static int DFSSearch(int [][]c){
int []visited = new int[c.length];
for(int i = 0;i < c.length;i++){
visited[i] = 0;
}
for(int i = 1;i < c.length;i++){
if(visited[i] == 0){
DFS(c,i,visited);
visited[0]++;
}
}
return visited[0];
}
public static void DFS(int [][]c,int v,int []visited){
System.out.println("visit : " + v);
visited[v] = 1;
int w = getFirstNeibor(c,v,visited);
while(w != -1){
if(visited[w] == 0){
DFS(c,w,visited);
}
else{
w = getNextNeibor(c,v,w,visited);
}
}
}
public static int getFirstNeibor(int [][]c,int v,int []visited){
for(int i = 0;i < c.length;i++){
if(c[i][v] == 1 && visited[i] == 0){
return i;
}
}
return -1;
}
public static int getNextNeibor(int [][]c,int v,int w,int []visited){
for(int i = 0;i < c.length;i++){
if(c[i][v] == 1 && i != w && visited[i] == 0){
return i;
}
}
return -1;
}
}