Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 74 additions & 74 deletions DFS.cpp
Original file line number Diff line number Diff line change
@@ -1,80 +1,80 @@
#include<bits/stdc++.h>
using namespace std;
// Graph class represents a directed graph
// using adjacency list representation
class Graph {
int V; // No. of vertices
// Pointer to an array containing
// adjacency lists
list<int> *adj;
// A recursive function used by DFS
void DFSUtil(int v, bool visited[]);
public:
Graph(int V); // Constructor
// function to add an edge to graph
void addEdge(int v, int w);
// DFS traversal of the vertices
// reachable from v
void DFS(int v);
};
Graph::Graph(int V) {
this->V = V;
adj = new list<int>[V];
}
void Graph::addEdge(int v, int w) {
adj[v].push_back(w); // Add w to v’s list.
}
void Graph::DFSUtil(int v, bool visited[]) {
// Mark the current node as visited and
// print it
visited[v] = true;
cout << v << " ";
#include<bits/stdc++.h>
using namespace std;

// Graph class represents a directed graph
// using adjacency list representation
class Graph {
int V; // No. of vertices

// Pointer to an array containing
// adjacency lists
list<int> *adj;

// A recursive function used by DFS
void DFSUtil(int v, bool visited[]);

public:
Graph(int V); // Constructor

// function to add an edge to graph
void addEdge(int v, int w);

// DFS traversal of the vertices
// reachable from v
void DFS(int v);

};

Graph::Graph(int V) {
this->V = V;
adj = new list<int>[V];
}

void Graph::addEdge(int v, int w) {
adj[v].push_back(w); // Add w to v’s list.
}

void Graph::DFSUtil(int v, bool visited[]) {
// Mark the current node as visited and
// print it
visited[v] = true;
cout << v << " ";

// Recur for all the vertices adjacent neighbors
// to this vertex
for ( int neighbor : adj[v]){
if (!visited[neighbor]){
// to this vertex
for ( int neighbor : adj[v]) {
if (!visited[neighbor]) {
DFSUtil(neighbor, visited);
}
}
}
// DFS traversal of the vertices reachable from v.
// It uses recursive DFSUtil()
void Graph::DFS(int v) {
// Mark all the vertices as not visited
bool visited[V];
}

// DFS traversal of the vertices reachable from v.
// It uses recursive DFSUtil()
void Graph::DFS(int v) {
// Mark all the vertices as not visited
bool visited[V];

memset(visited, false, sizeof(visited));
// Call the recursive helper function
// to print DFS traversal
DFSUtil(v, visited);
}
// Driver code
int main() {
// Create a graph given in the above diagram
Graph g(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);

// Call the recursive helper function
// to print DFS traversal
DFSUtil(v, visited);
}

// Driver code
int main() {
// Create a graph given in the above diagram
Graph g(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);

cout << "Following is Depth First Traversal"
" (starting from vertex 2) \n";
g.DFS(2);
return 0;
" (starting from vertex 2) \n";
g.DFS(2);

return 0;
80 changes: 80 additions & 0 deletions DFS.cpp.orig
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include<bits/stdc++.h>
using namespace std;

// Graph class represents a directed graph
// using adjacency list representation
class Graph {
int V; // No. of vertices

// Pointer to an array containing
// adjacency lists
list<int> *adj;

// A recursive function used by DFS
void DFSUtil(int v, bool visited[]);

public:
Graph(int V); // Constructor

// function to add an edge to graph
void addEdge(int v, int w);

// DFS traversal of the vertices
// reachable from v
void DFS(int v);

};

Graph::Graph(int V) {
this->V = V;
adj = new list<int>[V];
}

void Graph::addEdge(int v, int w) {
adj[v].push_back(w); // Add w to v’s list.
}

void Graph::DFSUtil(int v, bool visited[]) {
// Mark the current node as visited and
// print it
visited[v] = true;
cout << v << " ";

// Recur for all the vertices adjacent neighbors
// to this vertex
for ( int neighbor : adj[v]){
if (!visited[neighbor]){
DFSUtil(neighbor, visited);
}
}
}

// DFS traversal of the vertices reachable from v.
// It uses recursive DFSUtil()
void Graph::DFS(int v) {
// Mark all the vertices as not visited
bool visited[V];

memset(visited, false, sizeof(visited));

// Call the recursive helper function
// to print DFS traversal
DFSUtil(v, visited);
}

// Driver code
int main() {
// Create a graph given in the above diagram
Graph g(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);

cout << "Following is Depth First Traversal"
" (starting from vertex 2) \n";
g.DFS(2);

return 0;
41 changes: 21 additions & 20 deletions dataabstruction.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
#include <iostream>
using namespace std;
class Sum
{
private: int x, y, z; // private variables are providing data abstraction in this program
public:
void add()
{
cout<<"Enter two numbers: ";
cin>>x>>y;
z= x+y;
cout<<"Sum of two number is: "<<z<<endl;
}
};
int main()
{
Sum sm;
sm.add();
return 0;
}
#include <iostream>
using namespace std;
class Sum
{
private:
int x, y, z; // private variables are providing data abstraction in this program
public:
void add()
{
cout<<"Enter two numbers: ";
cin>>x>>y;
z= x+y;
cout<<"Sum of two number is: "<<z<<endl;
}
};
int main()
{
Sum sm;
sm.add();
return 0;
}
20 changes: 20 additions & 0 deletions dataabstruction.cpp.orig
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <iostream>
using namespace std;
class Sum
{
private: int x, y, z; // private variables are providing data abstraction in this program
public:
void add()
{
cout<<"Enter two numbers: ";
cin>>x>>y;
z= x+y;
cout<<"Sum of two number is: "<<z<<endl;
}
};
int main()
{
Sum sm;
sm.add();
return 0;
}
Loading