深度优先搜索的原理是:首先选择一个顶点作为起始点,接着从他各个相邻点出发进行依次访问,直到所有与起始点有路径相通的顶点都被访问到。若此时有没被访问到的节点,则选择一个其他顶点进行再次访问。
深度搜索算法,分别用C语言,C++,java,python编写出来
当涉及深度优先搜索算法时,可以使用多种编程语言来实现。以下是使用C语言、C++、Java和Python编写深度优先搜索算法的示例:
1.**C语言示例:**
- #include <stdio.h>
- #include <stdbool.h>
- #define MAX_VERTICES 100
- bool visited[MAX_VERTICES];
- int graph[MAX_VERTICES][MAX_VERTICES];
- int numVertices;
- void dfs(int vertex) {
- visited[vertex] = true;
- printf("%d ", vertex);
- for (int i = 0; i < numVertices; i++) {
- if (graph[vertex][i] && !visited[i]) {
- dfs(i);
- }
- }
- }
- void depthFirstSearch() {
- for (int i = 0; i < numVertices; i++) {
- visited[i] = false;
- }
- for (int i = 0; i < numVertices; i++) {
- if (!visited[i]) {
- dfs(i);
- }
- }
- }
复制代码游客,本帖隐藏的内容需要积分高于 9 才可浏览,您当前积分为 0
2.**C++示例:**
- #include <iostream>
- #include <vector>
- #include <stack>
- using namespace std;
- class Graph {
- int numVertices;
- vector<vector<int>> adjacencyList;
- public:
- Graph(int vertices) {
- numVertices = vertices;
- adjacencyList.resize(numVertices);
- }
- void addEdge(int source, int destination) {
- adjacencyList[source].push_back(destination);
- }
- void dfs(int startVertex) {
- vector<bool> visited(numVertices, false);
- stack<int> stack;
- stack.push(startVertex);
- while (!stack.empty()) {
- int currentVertex = stack.top();
- stack.pop();
- if (!visited[currentVertex]) {
- visited[currentVertex] = true;
- cout << currentVertex << " ";
- for (int neighbor : adjacencyList[currentVertex]) {
- if (!visited[neighbor]) {
- stack.push(neighbor);
- }
- }
- }
- }
- }
- };
复制代码游客,本帖隐藏的内容需要积分高于 9 才可浏览,您当前积分为 0
3.**Java示例:**
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Stack;
- class Graph {
- private int numVertices;
- private List<List<Integer>> adjacencyList;
- public Graph(int vertices) {
- numVertices = vertices;
- adjacencyList = new ArrayList<>(numVertices);
- for (int i = 0; i < numVertices; i++) {
- adjacencyList.add(new ArrayList<>());
- }
- }
- public void addEdge(int source, int destination) {
- adjacencyList.get(source).add(destination);
- }
- public void dfs(int startVertex) {
- boolean[] visited = new boolean[numVertices];
- Stack<Integer> stack = new Stack<>();
- stack.push(startVertex);
- while (!stack.isEmpty()) {
- int currentVertex = stack.pop();
- if (!visited[currentVertex]) {
- visited[currentVertex] = true;
- System.out.print(currentVertex + " ");
- for (int neighbor : adjacencyList.get(currentVertex)) {
- if (!visited[neighbor]) {
- stack.push(neighbor);
- }
- }
- }
- }
- }
- }
复制代码游客,本帖隐藏的内容需要积分高于 9 才可浏览,您当前积分为 0
4.**Python示例:**
- class Graph:
- def __init__(self, vertices):
- self.numVertices = vertices
- self.adjacencyList = [[] for _ in range(self.numVertices)]
- def addEdge(self, source, destination):
- self.adjacencyList[source].append(destination)
- def dfs(self, startVertex):
- visited = [False] * self.numVertices
- stack = [startVertex]
- while stack:
- currentVertex = stack.pop()
- if not visited[currentVertex]:
- visited[currentVertex] = True
- print(currentVertex, end=" ")
- for neighbor in self.adjacencyList[currentVertex]:
- if not visited[neighbor]:
- stack.append(neighbor)
- # Initialize the graph and add edges
复制代码游客,本帖隐藏的内容需要积分高于 9 才可浏览,您当前积分为 0
这些示例展示了使用不同编程语言实现深度优先搜索算法。每个示例都使用类似的思路,通过使用堆栈(或递归)来遍历图中的节点。您可以根据需要修改算法和输入图形。请注意,这只是深度优先搜索算法的简单示例,实际应用中可能会有更复杂的问题和算法。
扫码关注微信公众号,免费查看完整算法内容。 |