Skip to content

Commit

Permalink
Merge pull request #24 from swatantra-coder/main
Browse files Browse the repository at this point in the history
Hacktoberfest-2022
  • Loading branch information
Chinmay-03 authored Oct 14, 2022
2 parents 47eb115 + 25a4747 commit 7df290e
Show file tree
Hide file tree
Showing 35 changed files with 1,622 additions and 0 deletions.
82 changes: 82 additions & 0 deletions Uploads/DSA-main (1)/DSA-main/Graph/BFS.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;


// print function arry some arguments such as matrix, number of vertex, strting vertex and visited array

void print(int **arr, int n, int starting_vertex, bool *visited)
{
queue<int> q; // creat queue in which we will store the vertex
q.push(starting_vertex); // push sv
visited[starting_vertex] = true; // mark sv visited

// run while loop untill it gets empty

while (!q.empty()) {

cout << q.front() << " "; // display q front
int current_element = q.front(); // store the front into variable
q.pop(); // pop the front of queue

// run the for loop from 1 to n check every matrix weather it has connection or not along with that it is visited or not

for (int i = 1; i < n; i++) {
if (arr[current_element][i] == 1 && !visited[i] && i != current_element){
q.push(i); // if the ith index have edage and not visisted then we will push that index into queue
visited[i] = true; // and mark that index visited

}
}
}
}



void BFS( int **arr , bool* visited , int starting_index , int v){
// in this function we will check first weather it is visited or not by using loop
// and will call print function one by one
for (int i = 0; i < v; i++){
if (!visited[i]) {
print(arr, v, i, visited);
}
}
}
int main()
{
int v, e; // v = number of vertices in graph: e= number of edges in graph
cin >> v >> e; // take input from the graph
int **arr = new int *[v]; // creat dynamic matrix of size v i.e number of vertices

// make all value of matrix initially zero

for (int i = 0; i < v; i++){
arr[i] = new int[v];
for (int j = 0; j < v; j++){
arr[i][j] = 0;
}
}

// now its the time to store the edages between verices

while (e--){
int a, b;
cin >> a >> b;
arr[a][b] = 1;
arr[b][a] = 1;
}

// creat the visited array which will help us to know weather the vertex is visited or not
// to do this we can either use for loop or memset function to assign all value zero initially, as mentioned below.

bool *visited = new bool[v];
// for (int i = 0; i < v; i++){
// visited[i] = false;
// }
memset(visited,false,sizeof(visited));

// call the bfs function in which we will pass the matrix:'arr', 'visited' starting index and number of vertex

BFS( arr , visited ,0 , v );
}
27 changes: 27 additions & 0 deletions Uploads/DSA-main (1)/DSA-main/Graph/BFSprint.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

vector<int> bfsOfGraph(int V, vector<int> adj[]) {
vector<int> v;
vector<int> visited(V,0);
queue<int> q;
q.push(0);
visited[0]=1;

while(!q.empty())
{
int node=q.front();
v.push_back(node);
q.pop();

for(auto it:adj[node])
{
if(!visited[it])
{
q.push(it);
visited[it]=1;
}
}
}

return v;

}
61 changes: 61 additions & 0 deletions Uploads/DSA-main (1)/DSA-main/Graph/BipertiteGraphBFS.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
************************************************ BFS Bipertite *********************************************

bool bipertite(int s, vector<int> adj[],int colo[])
{
queue<int> q;
q.push(s);
colo[s]=1;

while(!q.empty())
{
int current=q.front();
q.pop();
for(auto it:adj[current])
{
if(colo[it]==-1;
{
colo[it]=1-colo[current];
q.push(it);
}
else if(colo[it]==colo[current])
return false;
}
return true;
}
bool checkbipertite(vector<int> adj[],int n)
{
int colo[n];
memset(colo,-1,sizeofcolo);

for(int i=0;i<n;i++)
{
if(colo[i]==-1)
{
if(!bipertite(i,adj,colo))
return false;
}
}

return true;
}
************************************************* DFS Bipertite ******************************************************
bool bipertiteDFS(int s,vector<int> adj[],int colo[])
{
if(colo[s]==-1)
colo[s]=1;

for(auto it:adj[s])
{
if(colo[it]==-1)
{
colo[it]=1-colo[s];
if(!bipertiteDFS(it,adj,colo)
return false;
}
else if(colo[it]==colo[s])
return false;
}

return true;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
bool ifcheckCycle(int s,vector<int> adj[], vector<int>& visited)
{
queue<pair<int,int>> q;

q.push({s,-1});
visited[s]=1;

while(!q.empty())
{
int node=q.front().first;
int prev=q.front().second;
q.pop();

for(auto it:adj[node])
{
if(!visited[it])
{
q.push({it,node});
visited[it]=1;

}
else if(prev != it)
{
return true;
}
}
}
return false;
}

bool isCycle(int V, vector<int> adj[])
{
vector<int> visited(V,0);

for(int i=0; i<V; i++)
{
if(!visited[i])
{
if(ifcheckCycle(i,adj,visited))
{
return true;
}
}
}

return false;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
bool iscycleDFS(int sv,int prev,vector<int>& visited,vector<int> adj[])
{
visited[sv]=1;
for(auto it:adj[sv])
{
if(!visited[it])
{
if(iscycleDFS(it,sv,visited,adj))
return true;
}
else if(it != prev)
{
return false;
}
}

return false;
}
bool isCyclic(int V, vector<int> adj[])
{
vector<int> visited(V,0);

for(int i=0;i<V;i++)
{
if(!visited[i])
{
if(iscycleDFS(i,-1,visited,adj))
{
return true;
}
}
}

return false;


}
23 changes: 23 additions & 0 deletions Uploads/DSA-main (1)/DSA-main/Graph/DFSprint.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
void dfs(int node, vector<int> &vis, vector<int> adj[], vector<int> &v)
{
v.push_back(node);
vis[node] = 1;
for(auto it : adj[node])
{
if(!vis[it])
{
dfs(it, vis, adj, v);
}
}
}
vector<int> dfsOfGraph(int V, vector<int> adj[]) {

vector<int> v;
vector<int> vis(V, 0);
for(int i = 0;i<V;i++)
{
if(!vis[i])
dfs(i, vis, adj, v);
}
return v;
}
88 changes: 88 additions & 0 deletions Uploads/DSA-main (1)/DSA-main/Graph/FloodFillAlgorithm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
****************************-----------DFS----------*********************************

void dfs(int row,int col,int color,vector<vector<int>>& image,int k)
{
int n=image.size(),m=image[0].size();

if(row<0 || col<0 ||row>=n || col>=m)
return;

if(image[row][col]==color)
return;

if(image[row][col]!=k)
return;

image[row][col]=color;

dfs(row+1,col,color,image,k);
dfs(row-1,col,color,image,k);
dfs(row,col+1,color,image,k);
dfs(row,col-1,color,image,k);

}
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor)
{
int k=image[sr][sc];
dfs(sr,sc,newColor,image,k);

return image;

}

******************************----------BFS------------********************************


void bfs(int sr,int sc,int color,vector<vector<int>>& visited,vector<vector<int>>& image,int source)
{
visited[sr][sc]=1;
int n=image.size();
int m=image[0].size();

queue<pair<int,int>> q;
q.push({sr,sc});

while(!q.empty())
{
int row=q.front().first;
int col=q.front().second;
q.pop();

if(row-1>=0 and image[row][col]==image[row-1][col] and visited[row-1][col]==0)
{
visited[row-1][col]=1;
q.push({row-1,col});
}

if(row+1<n and image[row][col]==image[row+1][col] and visited[row+1][col]==0)
{
visited[row+1][col]=1;
q.push({row+1,col});
}

if(col-1>=0 and image[row][col]==image[row][col-1] and visited[row][col-1]==0)
{
visited[row][col-1]=1;
q.push({row,col-1});
}

if(col+1<m and image[row][col]==image[row][col+1] and visited[row][col+1]==0)
{
visited[row][col+1]=1;
q.push({row,col+1});
}
image[row][col]=color;
}
}
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int color)
{
int n=image.size();
int m=image[0].size();

vector<vector<int>> visited(n,vector<int>(m,0));
int source=image[sr][sc];
bfs(sr,sc,color,visited,image,source);

return image;

}
Loading

0 comments on commit 7df290e

Please sign in to comment.