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
88 changes: 88 additions & 0 deletions breadthfirstsearch c++.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Program to print BFS traversal from a given
// source vertex. BFS(int s) traverses vertices
// reachable from s.
#include<bits/stdc++.h>
using namespace std;

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

// Pointer to an array containing adjacency
// lists
vector<list<int>> adj;
public:
Graph(int V); // Constructor

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

// prints BFS traversal from a given source s
void BFS(int s);
};

Graph::Graph(int V)
{
this->V = V;
adj.resize(V);
}

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

void Graph::BFS(int s)
{
// Mark all the vertices as not visited
vector<bool> visited;
visited.resize(V,false);

// Create a queue for BFS
list<int> queue;

// Mark the current node as visited and enqueue it
visited[s] = true;
queue.push_back(s);

while(!queue.empty())
{
// Dequeue a vertex from queue and print it
s = queue.front();
cout << s << " ";
queue.pop_front();

// Get all adjacent vertices of the dequeued
// vertex s. If a adjacent has not been visited,
// then mark it visited and enqueue it
for (auto adjecent: adj[s])
{
if (!visited[adjecent])
{
visited[adjecent] = true;
queue.push_back(adjecent);
}
}
}
}

// Driver program to test methods of graph class
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 Breadth First Traversal "
<< "(starting from vertex 2) \n";
g.BFS(2);

return 0;
}
62 changes: 62 additions & 0 deletions depthfirsstsearch c++.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// C++ program to print DFS traversal from
// a given vertex in a given graph
#include <bits/stdc++.h>
using namespace std;

// Graph class represents a directed graph
// using adjacency list representation
class Graph {
public:
map<int, bool> visited;
map<int, list<int> > adj;

// 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);
};

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

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

// Recur for all the vertices adjacent
// to this vertex
list<int>::iterator i;
for (i = adj[v].begin(); i != adj[v].end(); ++i)
if (!visited[*i])
DFS(*i);
}

// Driver's code
int main()
{
// Create a graph given in the above diagram
Graph g;
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";

// Function call
g.DFS(2);

return 0;
}

// improved by Vishnudev C
23 changes: 23 additions & 0 deletions valid-palindrome.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution {
public:
bool isPalindrome(string s) {
int n = size(s);

if(n == 1) return true; // if string size 1 then simple return true if symbol is present no need worry about we don't need remove question ask return
//true or false

string str = "";
for(int i=0; i<n; i++) if(isalnum(s[i])) str +=s[i]; // only put number and char not any symbol

// converting whole string into uppercase
transform(str.begin(), str.end(), str.begin(), ::toupper);

int i=0,j=size(str)-1;

while(i<j) {
if(str[i] != str[j]) return false; // checking where two char are not same then return false
i++,j--;
}
return true;
}
};
47 changes: 47 additions & 0 deletions valid-parentheses.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
class Solution {
vector<char> my_stack;
public:
bool isValid(string s) {
if (s.size() % 2 != 0)
return false;
if (s.size() == 0)
return true;
for (int i = 0; i < s.size(); i++)
{
my_stack.push_back(s[i]);
if (my_stack[my_stack.size() - 1] == '}')
{
if (my_stack.size() > 1){
if(my_stack[my_stack.size() - 2] == '{')
{
my_stack.pop_back();
my_stack.pop_back();
}
}
}
else if (my_stack[my_stack.size() - 1] == ')')
{
if (my_stack.size() > 1){
if(my_stack[my_stack.size() - 2] == '(')
{
my_stack.pop_back();
my_stack.pop_back();
}
}
}
else if (my_stack[my_stack.size() - 1] == ']')
{
if (my_stack.size() > 1){
if(my_stack[my_stack.size() - 2] == '[')
{
my_stack.pop_back();
my_stack.pop_back();
}
}
}
}
if (!my_stack.empty())
return false;
return true;
}
};