Skip to main content
Code Review

Return to Question

Copy edited (e.g. ref. <https://en.wikipedia.org/wiki/Depth-first_search>). Introduced abbr. "DFS".
Source Link

C++ implementation of Depth First Searchdepth-first search

I got the C++ implementation of DFSdepth-first search (DFS) from here and made slight modifications to it. The modified code is as follows:

#include "stdafx.h"
#include <iostream>
#include<map>
#include<list>
class Graph {
 void DFSUtil(int v);
public:
 std::map<int, bool> visited;
 std::map<int, std::list<int>> adj;
 void addEdge(int v, int w);
 void DFS();
};
void Graph::addEdge(int v, int w)
{
 adj[v].push_back(w);
}
void Graph::DFSUtil(int v)
{
 visited[v] = true;
 std::cout << v << " ";
 std::list<int>::iterator i;
 for (i = adj[v].begin(); i != adj[v].end(); ++i)
 if (!visited[*i])
 DFSUtil(*i);
}
void Graph::DFS()
{
 for (auto i : adj)
 if (visited[i.first] == false)
 DFSUtil(i.first);
}
int main()
{
 Graph g;
 g.addEdge(0, 1);
 g.addEdge(0, 9);
 g.addEdge(1, 2);
 g.addEdge(2, 0);
 g.addEdge(2, 3);
 g.addEdge(9, 3);
 std::cout << "Depth First Traversal result\n";
 g.DFS();
 std::cin.get();
 return 0;
}

What further improvements can I make to this code?

C++ implementation of Depth First Search

I got the C++ implementation of DFS from here and made slight modifications to it. The modified code is as follows:

#include "stdafx.h"
#include <iostream>
#include<map>
#include<list>
class Graph {
 void DFSUtil(int v);
public:
 std::map<int, bool> visited;
 std::map<int, std::list<int>> adj;
 void addEdge(int v, int w);
 void DFS();
};
void Graph::addEdge(int v, int w)
{
 adj[v].push_back(w);
}
void Graph::DFSUtil(int v)
{
 visited[v] = true;
 std::cout << v << " ";
 std::list<int>::iterator i;
 for (i = adj[v].begin(); i != adj[v].end(); ++i)
 if (!visited[*i])
 DFSUtil(*i);
}
void Graph::DFS()
{
 for (auto i : adj)
 if (visited[i.first] == false)
 DFSUtil(i.first);
}
int main()
{
 Graph g;
 g.addEdge(0, 1);
 g.addEdge(0, 9);
 g.addEdge(1, 2);
 g.addEdge(2, 0);
 g.addEdge(2, 3);
 g.addEdge(9, 3);
 std::cout << "Depth First Traversal result\n";
 g.DFS();
 std::cin.get();
 return 0;
}

What further improvements can I make to this code?

C++ implementation of depth-first search

I got the C++ implementation of depth-first search (DFS) from here and made slight modifications to it. The modified code is as follows:

#include "stdafx.h"
#include <iostream>
#include<map>
#include<list>
class Graph {
 void DFSUtil(int v);
public:
 std::map<int, bool> visited;
 std::map<int, std::list<int>> adj;
 void addEdge(int v, int w);
 void DFS();
};
void Graph::addEdge(int v, int w)
{
 adj[v].push_back(w);
}
void Graph::DFSUtil(int v)
{
 visited[v] = true;
 std::cout << v << " ";
 std::list<int>::iterator i;
 for (i = adj[v].begin(); i != adj[v].end(); ++i)
 if (!visited[*i])
 DFSUtil(*i);
}
void Graph::DFS()
{
 for (auto i : adj)
 if (visited[i.first] == false)
 DFSUtil(i.first);
}
int main()
{
 Graph g;
 g.addEdge(0, 1);
 g.addEdge(0, 9);
 g.addEdge(1, 2);
 g.addEdge(2, 0);
 g.addEdge(2, 3);
 g.addEdge(9, 3);
 std::cout << "Depth First Traversal result\n";
 g.DFS();
 std::cin.get();
 return 0;
}

What further improvements can I make to this code?

Can this C++ implementation of Depth First Search be further improved?

Tweeted twitter.com/StackCodeReview/status/1375643841454354432
Became Hot Network Question
edited tags
Link
a_sid
  • 435
  • 6
  • 16
Source Link
a_sid
  • 435
  • 6
  • 16
Loading
lang-cpp

AltStyle によって変換されたページ (->オリジナル) /