【深度优先搜索】

文章目录


深度优先搜索

//深度优先搜索递归函数
void DFS(ALGraph& G, int v, bool visited[]) {
	cout << G.vertices[v].data << " ";//访问顶点v
	visited[v] = true;

	//遍历顶点v的所有邻接点
	ArcNode* p = G.vertices[v].firstarc;
	while (p) {
		if (!visited[p->adjvex]) {
			DFS(G, p->adjvex, visited);//递归访问邻接点
		}
		p = p->nextarc;
	}
}

//深度优先搜索遍历图
void DFSTraverse(ALGraph& G) {
	bool visited[MVNum] = { false };//标记数组,标记顶点是否被访问过

	//遍历每一个顶点,进行深度优先搜索
	for (int i = 0; i < G.vexnum; ++i) {
		if (!visited[i]) {
			DFS(G, i, visited);
		}
	}
}

总代码

#include
using namespace std;

#define OtherInfo int
#define VerTextType char
#define MVNum 100
#define Status int

typedef struct ArcNode {
	int adjvex;//存放边结点的序号
	struct ArcNode* nextarc;//存放下一个边结点的地址
	OtherInfo info;
}ArcNode;

//定义顶点
typedef struct VNode {
	VerTextType data;//数据域
	ArcNode* firstarc;//指向第一条依附于结点的边结点
}VNode,AdjList[MVNum];

typedef struct {
	AdjList vertices;//定义一个数组存放结点序号
	int vexnum, arcnum;//存放总结点数和总边结点数
}ALGraph;

Status LocateVex(ALGraph& G, VerTextType v);
void DFS(ALGraph& G, int v, bool visited[]);

//创建无向图
Status CreateUDG(ALGraph& G) {
	int i,k,j;
	VerTextType v1, v2;
	//1.采用邻接表表示法建立无向图
	//1.输入总结点数和总边数
	cin >> G.vexnum >> G.arcnum;
	//2.依次输入点的信息存储在顶点表中,使每一个表头结点的指针域初始化为NULL
	for (i = 0; i < G.vexnum; ++i) {
		cout << "输入第" << i + 1 << "个顶点值:" << endl;
		cin >> G.vertices[i].data;//输入顶点值
		G.vertices[i].firstarc = NULL;
	}
	//3.输入各边构造边表
	for (k = 0; k < G.arcnum; ++k) {
		ArcNode* p1, * p2;
		cout << endl;
		cout << "输入第" << k + 1 << "条边的信息" << endl;
		cout << "输入边依附的第一个结点:" << endl;
		cin >> v1;
		cout << "输入边依附的第二个结点:" << endl;
		cin >> v2;
		i = LocateVex(G, v1);
		j = LocateVex(G, v2);//确定v1,v2在G中的位置,即顶点在G.vertices中的序号
		//生成一个新的结点
		p1 = new ArcNode;
		p1->adjvex = j;
		p1->nextarc = G.vertices[i].firstarc;
		G.vertices[i].firstarc = p1;
		//生成一个新的结点
		p2 = new ArcNode;
		p2->adjvex = i;
		p2->nextarc = G.vertices[j].firstarc;
		G.vertices[j].firstarc = p2;
	}
	return 1;
}

//查找G中是否存在v
Status LocateVex(ALGraph& G, VerTextType v) {
	for (int i = 0; i < G.vexnum; ++i) {
		if (G.vertices[i].data == v) {
			return i;
		}
	}
	return -1;
}

//深度优先搜索遍历图
void DFSTraverse(ALGraph& G) {
	bool visited[MVNum] = { false };//标记数组,标记顶点是否被访问过

	//遍历每一个顶点,进行深度优先搜索
	for (int i = 0; i < G.vexnum; ++i) {
		if (!visited[i]) {
			DFS(G, i, visited);
		}
	}
}

//深度优先搜索递归函数
void DFS(ALGraph& G, int v, bool visited[]) {
	cout << G.vertices[v].data << " ";//访问顶点v
	visited[v] = true;

	//遍历顶点v的所有邻接点
	ArcNode* p = G.vertices[v].firstarc;
	while (p) {
		if (!visited[p->adjvex]) {
			DFS(G, p->adjvex, visited);//递归访问邻接点
		}
		p = p->nextarc;
	}
}

int main() {
	ALGraph G{};
	CreateUDG(G);
	cout << "深度优先搜索遍历结果:" << endl;
	DFSTraverse(G);

	return 0;
}

采用邻接矩阵表示图的深度优先搜索遍历

#include
#include
#include
using namespace std;

class Graph {
private:
	int vertices;
	list<int>* adjacencyList;

public:
	Graph(int v) :vertices(v) {
		adjacencyList = new list<int>[vertices];
	}

	//添加边
	void addEdge(int start, int end)
	{
		adjacencyList[start].push_back(end);
		adjacencyList[end].push_back(start);

	}

	void DFS(int startVertex) {
		bool* visited = new bool[vertices];
		for (int i = 0; i < vertices; ++i) {
			visited[i] = false;
		}
		stack<int> s;
		cout << "DFS Traversal starting from vertex" << startVertex << ": ";
		cout << startVertex << " ";

		visited[startVertex] = true;
		s.push(startVertex);

		while (!s.empty()) {
			int currentVerTex = s.top();
			s.pop();

			for (int neighbor : adjacencyList[currentVerTex]) {
				if (!visited[neighbor]) {
					cout << neighbor << " ";
					visited[neighbor] = true;
					s.push(neighbor);
				}
			}
		}
		cout << endl;
		delete[] visited;
	}
};

int main() {
	Graph g(6);

	g.addEdge(0, 1);
	g.addEdge(0, 2);
	g.addEdge(1, 3);
	g.addEdge(1, 4);
	g.addEdge(2, 4);
	g.addEdge(3, 4);
	g.addEdge(3, 5);
	g.addEdge(4, 5);

	g.DFS(0);

	return 0;
}

你可能感兴趣的:(数据结构)