TheAlgorithms-C-Plus-Plus/Graph/DFS.cpp

31 lines
554 B
C++
Raw Normal View History

2016-11-30 22:08:15 +08:00
#include <bits/stdc++.h>
using namespace std;
int v = 4;
2019-08-21 10:10:08 +08:00
void DFSUtil_(int graph[4][4], bool visited[], int s)
{
2016-11-30 22:08:15 +08:00
visited[s] = true;
2019-08-21 10:10:08 +08:00
cout << s << " ";
for (int i = 0; i < v; i++)
{
if (graph[s][i] == 1 && visited[i] == false)
{
DFSUtil_(graph, visited, i);
2016-11-30 22:08:15 +08:00
}
}
}
2019-08-21 10:10:08 +08:00
void DFS_(int graph[4][4], int s)
{
2016-11-30 22:08:15 +08:00
bool visited[v];
2019-08-21 10:10:08 +08:00
memset(visited, 0, sizeof(visited));
DFSUtil_(graph, visited, s);
2016-11-30 22:08:15 +08:00
}
int main()
{
2019-08-21 10:10:08 +08:00
int graph[4][4] = {{0, 1, 1, 0}, {0, 0, 1, 0}, {1, 0, 0, 1}, {0, 0, 0, 1}};
cout << "DFS: ";
DFS_(graph, 2);
cout << endl;
return 0;
2016-11-30 22:08:15 +08:00
}