Skip to content
Open

Graph #248

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
50 changes: 50 additions & 0 deletions graph/graph/bfs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include<bits/stdc++.h>
using namespace std;


void bfs(int s,vector<int> adj[],bool vis[],int n)
{
queue<int> q;
q.push(s);

vis[s]=true;
while(!q.empty())
{
int curr=q.front();
q.pop();
cout<<curr<<" ";
//for adjacent elements
for(int i=0;i<adj[curr].size();i++)
{
if(vis[adj[curr][i]]==false)
{
q.push(adj[curr][i]);
vis[adj[curr][i]]=true;

}
}

}

}


int main()
{
int n,e;
cin>>n,e;

vector<int> adj[n];
bool vis[n]={false};
for(int i=0;i<e;i++)
{
int u,v;
cin>>u>>v;
adj[u].push_back(v);

}

bfs(0,adj,vis,n);
}


44 changes: 44 additions & 0 deletions graph/graph/bfsinc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include<iostream>




void bfs(int g[][7],int start,int n)
{
int i=start;
struct queue q;
int visited[7]={0};

print("%d",i);
visited[i]=1;

enqueue(i);
while(!isEmpty())
{
i=dequeue();
for(int j=1;j<n;j++)
{
if(g[i][j]==1 && visited[j]==0)
{
printf("%d",j);
visited[j]=1;
enqueue(j);

}
}
}
}

int main()
{
int g[7][7]={{0,0,0,0,0,0,0},
{0,0,1,1,0,0,0},
{0,1,0,0,1,0,0},
{0,1,0,1,0,0,0}
{0,0,1,1,0,1,1},
{0,0,0,0,1,0,0},
{0,0,0,0,1,0,0}
};

bfs(g,1,7);
}
65 changes: 65 additions & 0 deletions graph/graph/bfst.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include<bits/stdc++.h>
using namespace std;

void printbfs(int** edges,int n,int sv)
{
queue<int> q;
bool *visited=new bool[n];
for(int i=0;i<n;i++)
{
visited[i]=false;
}
q.push(sv);
visited[sv]=true;
while(!q.empty())
{
int currentvertex=q.front();
q.pop();
cout<<currentvertex<<endl;

for(int i=0;i<n;i++)
{
if(edges[currentvertex][i]==1 && !visited[i])
q.push(i);
visited[i]=true;

}
}
}
int main()
{
int n,e;
cout<<"\n enter the number of vertices \n";
cin>>n;
cout<<"\n enter the number of edges \n";
cin>>e;

int** edges=new int*[n];
for(int i=0;i<n;i++){
edges[i]=new int[n];

for(int j=0;j<n;j++)
{
edges[i][j]=0;
}

}
for(int i=0;i<e;i++)
{
int f,s;
cout<<"\n enter first vertex \n"<<endl;
cin>>f;
cout<<"\n enter second vertex \n"<<endl;
cin>>s;
edges[f][s]=1;
edges[s][f]=1;

}
bool *visited=new bool[n];
for(int i=0;i<n;i++)
{
visited[i]=0;
}

printbfs(edges,n,0);
}
72 changes: 72 additions & 0 deletions graph/graph/bfstraversal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include<stdio.h>
#include<limits.h>
#include<stdlib.h>
struct node{
int data;
struct node* next;
};

struct node* front=NULL;
struct node* rear=NULL;

void enqueue(int x){
struct node* newnode=(struct node*)malloc(sizeof(node));
newnode->data=x;
newnode->next=NULL;
if(front==NULL && rear==NULL)
{
front=newnode;
rear=newnode;

}
else{
rear->next=newnode;
rear=newnode;
}


}

void display()
{
struct node* temp;
if(front==NULL && rear==NULL)
{
printf("\n queue is empty\n");

}
else{
temp=front;
while(temp!=NULL)
{
printf("%d",temp->data);
temp=temp->next;
}
}
}

void dequeue(){
struct node* temp=front;
if(front==NULL && rear==NULL)
{
printf("\n queue underflow\n");

}
else{
printf("%d",front->data);
front=front->next;
free(temp);
}
}

int peek(){
if(front==NULL)
{
return INT_MAX;
}
else{
return front->data;
}
}


108 changes: 108 additions & 0 deletions graph/graph/demo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#ifndef Queue_h
#define Queue_h
#include <stdlib.h>
struct Node
{
int data;
struct Node *next;

}*front=NULL,*rear=NULL;
void enqueue(int x)
{
struct Node *t;
t=(struct Node*)malloc(sizeof(struct Node));
if(t==NULL)
printf("Queue is FUll\n");
else
{
t->data=x;
t->next=NULL;
if(front==NULL)
front=rear=t;
else
{
rear->next=t;
rear=t;
}
}

}
int dequeue()
{
int x=-1;
struct Node* t;

if(front==NULL)
printf("Queue is Empty\n");
else
{
x=front->data;
t=front;
front=front->next;
free(t);
}
return x;
}
int isEmpty()
{
return front==NULL;
}
#endif /* Queue_h */
#include <stdio.h>
//#include "Queue.h"
void BFS(int G[][7],int start,int n)
{
int i=start,j;

int visited[7]={0};

printf("%d ",i);
visited[i]=1;
enqueue(i);

while(!isEmpty())
{
i=dequeue();
for(j=1;j<n;j++)
{
if(G[i][j]==1 && visited[j]==0)
{
printf("%d ",j);
visited[j]=1;
enqueue(j);
}
}
}


}
void DFS(int G[][7],int start,int n)
{
static int visited[7]={0};
int j;

if(visited[start]==0)
{
printf("%d ",start);
visited[start]=1;

for(j=1;j<n;j++)
{
if(G[start][j]==1 && visited[j]==0)
DFS(G,j,n);
}
}
}
int main()
{
int G[7][7]={{0,0,0,0,0,0,0},
{0,0,1,1,0,0,0},
{0,1,0,0,1,0,0},
{0,1,0,0,1,0,0},
{0,0,1,1,0,1,1},
{0,0,0,0,1,0,0},
{0,0,0,0,1,0,0}};
DFS(G,4,7);

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

class graph{
public:
map<int,bool> visited;
map<int,list<int>> adj;

void addedge(int v,int w);
void dfs(int v);

};

void graph::addedge(int v,int w)
{
adj[v].push_back(w);
}
void graph::dfs(int v)
{
visited[v]=true;
cout<<v<<" ";


list<int>::iterator i;
for(i=adj[v].begin();i!=adj[v].end();i++)
{
if(!visited[*i])
dfs(*i);
}
}




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";
g.dfs(2);

return 0;
}
Loading