深度搜索算法,分别用C语言,C++,java,python编写出来
深度优先搜索的原理是:首先选择一个顶点作为起始点,接着从他各个相邻点出发进行依次访问,直到所有与起始点有路径相通的顶点都被访问到。若此时有没被访问到的节点,则选择一个其他顶点进行再次访问。http://www.net188.com/data/attachment/forum/202401/09/131329slpg9qp9ub8bp7bp.png.thumb.jpg
深度搜索算法,分别用C语言,C++,java,python编写出来
当涉及深度优先搜索算法时,可以使用多种编程语言来实现。以下是使用C语言、C++、Java和Python编写深度优先搜索算法的示例:
1.**C语言示例:**
#include <stdio.h>
#include <stdbool.h>
#define MAX_VERTICES 100
bool visited;
int graph;
int numVertices;
void dfs(int vertex) {
visited = true;
printf("%d ", vertex);
for (int i = 0; i < numVertices; i++) {
if (graph && !visited) {
dfs(i);
}
}
}
void depthFirstSearch() {
for (int i = 0; i < numVertices; i++) {
visited = false;
}
for (int i = 0; i < numVertices; i++) {
if (!visited) {
dfs(i);
}
}
}
**** Hidden Message *****
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.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) {
visited = true;
cout << currentVertex << " ";
for (int neighbor : adjacencyList) {
if (!visited) {
stack.push(neighbor);
}
}
}
}
}
};
**** Hidden Message *****
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;
Stack<Integer> stack = new Stack<>();
stack.push(startVertex);
while (!stack.isEmpty()) {
int currentVertex = stack.pop();
if (!visited) {
visited = true;
System.out.print(currentVertex + " ");
for (int neighbor : adjacencyList.get(currentVertex)) {
if (!visited) {
stack.push(neighbor);
}
}
}
}
}
}
**** Hidden Message *****
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.append(destination)
def dfs(self, startVertex):
visited = * self.numVertices
stack =
while stack:
currentVertex = stack.pop()
if not visited:
visited = True
print(currentVertex, end=" ")
for neighbor in self.adjacencyList:
if not visited:
stack.append(neighbor)
# Initialize the graph and add edges
**** Hidden Message *****
这些示例展示了使用不同编程语言实现深度优先搜索算法。每个示例都使用类似的思路,通过使用堆栈(或递归)来遍历图中的节点。您可以根据需要修改算法和输入图形。请注意,这只是深度优先搜索算法的简单示例,实际应用中可能会有更复杂的问题和算法。
http://www.net188.com/data/attachment/forum/202401/09/131700ujdajenvj55tj9ti.jpg.thumb.jpg
扫码关注微信公众号,免费查看完整算法内容。
页:
[1]