/* BFS(breadth-first search) and DFS(depth-first search)*/#include<stdio.h> #include<stdlib.h> int q[20],top=-1,front=-1,rear=-1,a[20][20],vis[20],stack[20]; int delete(); void add(int item); void bfs(int s,int n); void dfs(int s,int n); void push(int item); int pop(); void main() { int n,i,s,ch,j; char c,dummy; printf(“ENTER THE NUMBER VERTICES “); scanf(“%d”,&n); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { printf(“ENTER 1 IF %d HAS A NODE WITH %d ELSE 0 “,i,j); scanf(“%d”,&a[i][j]); } } printf(“THE ADJACENCY MATRIX IS\n”); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { printf(” %d”,a[i][j]); } printf(“\n”); } do { for(i=1;i<=n;i++) vis[i]=0; printf(“\nMENU”); printf(“\n1.B.F.S”); printf(“\n2.D.F.S”); printf(“\nENTER YOUR CHOICE”); scanf(“%d”,&ch); printf(“ENTER THE SOURCE VERTEX :”); scanf(“%d”,&s); switch(ch) { case 1:bfs(s,n); break; case 2: dfs(s,n); break; } printf(“DO U WANT TO CONTINUE(Y/N) ? “); scanf(“%c”,&dummy); scanf(“%c”,&c); }while((c==’y’)||(c==’Y’)); }//main exit void bfs(int s,int n) { int p,i; add(s); vis[s]=1; p=delete(); if(p!=0) printf(” %d”,p); while(p!=0) { for(i=1;i<=n;i++) if((a[p][i]!=0)&&(vis[i]==0)) { add(i); vis[i]=1; } p=delete(); if(p!=0) printf(” %d “,p); } for(i=1;i<=n;i++) if(vis[i]==0) bfs(i,n); } void add(int item) { if(rear==19) printf(“QUEUE FULL”); else { if(rear==-1) { q[++rear]=item; front++; } else q[++rear]=item; } } int delete() { int k; if((front>rear)||(front==-1)) return(0); else { k=q[front++]; return(k); } } void dfs(int s,int n) { int i,k; push(s); vis[s]=1; k=pop(); if(k!=0) printf(” %d “,k); while(k!=0) { for(i=1;i<=n;i++) if((a[k][i]!=0)&&(vis[i]==0)) { push(i); vis[i]=1; } k=pop(); if(k!=0) printf(” %d “,k); } for(i=1;i<=n;i++) if(vis[i]==0) dfs(i,n); } void push(int item) { if(top==19) printf(“Stack overflow “); else stack[++top]=item; } int pop() { int k; if(top==-1) return(0); else { k=stack[top–]; return(k); } }
Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++
and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}