深度搜索算法,分别用C语言,C++,java,python编写出来

深度优先搜索的原理是:首先选择一个顶点作为起始点,接着从他各个相邻点出发进行依次访问,直到所有与起始点有路径相通的顶点都被访问到。若此时有没被访问到的节点,则选择一个其他顶点进行再次访问。

深度搜索算法,分别用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);
}
}
}

SVIP价 5 折 永久SVIP免费

已有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);
}
}
}
}
}
};

SVIP价 5 折 永久SVIP免费

已有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);
}
}
}
}
}
}

SVIP价 5 折 永久SVIP免费

已有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

SVIP价 5 折 永久SVIP免费

已有0人支付

这些示例展示了使用不同编程语言实现深度优先搜索算法。每个示例都使用类似的思路,通过使用堆栈(或递归)来遍历图中的节点。您可以根据需要修改算法和输入图形。请注意,这只是深度优先搜索算法的简单示例,实际应用中可能会有更复杂的问题和算法。


扫码关注微信公众号,免费查看完整算法内容。

下载说明:
1.本站资源都是白菜价出售,同样的东西,我们不卖几百,也不卖几十,甚至才卖几块钱,一个永久会员能下载全站100%源码了,所以单独购买也好,会员也好均不提供相关技术服务。
2.如果源码下载地址失效请联系站长QQ进行补发。
3.本站所有资源仅用于学习及研究使用,请必须在24小时内删除所下载资源,切勿用于商业用途,否则由此引发的法律纠纷及连带责任本站和发布者概不承担。资源除标明原创外均来自网络整理,版权归原作者或本站特约原创作者所有,如侵犯到您权益请联系本站删除!
4.本站站内提供的所有可下载资源(软件等等)本站保证未做任何负面改动(不包含修复bug和完善功能等正面优化或二次开发);但本网站不能保证资源的准确性、安全性和完整性,用户下载后自行斟酌,我们以交流学习为目的,并不是所有的源码都100%无错或无bug;同时本站用户必须明白,【安安资源网】对提供下载的软件等不拥有任何权利(本站原创和特约原创作者除外),其版权归该资源的合法拥有者所有。
5.请您认真阅读上述内容,购买即以为着您同意上述内容。
安安资源网 » 深度搜索算法,分别用C语言,C++,java,python编写出来