0% found this document useful (0 votes)
17 views5 pages

Ass 3

The document contains four C programs for graph representation and traversal techniques. The first program creates and displays an adjacency matrix, the second calculates in-degrees and out-degrees, the third implements Depth First Search (DFS), and the fourth implements Breadth First Search (BFS). Each program prompts the user for the number of vertices and edge connections between them.

Uploaded by

yashodapawar10
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views5 pages

Ass 3

The document contains four C programs for graph representation and traversal techniques. The first program creates and displays an adjacency matrix, the second calculates in-degrees and out-degrees, the third implements Depth First Search (DFS), and the fourth implements Breadth First Search (BFS). Each program prompts the user for the number of vertices and edge connections between them.

Uploaded by

yashodapawar10
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

matrix
#include<stdio.h>
#include<malloc.h>
void create(int a[10][10],int n){
printf("\n****TYPE 1 FOR YES & 0 FOR NO****\n");
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
a[i][j]=0;
if(i!=j){
printf("\nIs there any edge between %d & %d: ",i+1,j+1);
scanf("%d",&a[i][j]);
}
}
}
}
void display(int a[10][10],int n){
printf("\nThe Matrix is: \n");
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
printf("%d\t",a[i][j]);
}printf("\n");
}
}
int main(){
int a[10][10],n;
printf("\nEnter the no. of vertex: ");
scanf("%d",&n);
create(a,n);
display(a,n);
}
2.inout matrix

#include<stdio.h>
#include<malloc.h>
void create(int a[10][10],int n){
printf("\n****TYPE 1 FOR YES & 0 FOR NO****\n");
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
a[i][j]=0;
if(i!=j){
printf("\nIs there any edge between %d & %d: ",i+1,j+1);
scanf("%d",&a[i][j]);
}
}
}
}
void inout(int a[10][10],int n){
int in=0,out=0;
printf("\nVertex\tIndegree\tOutdegree\t");
for(int i=0;i<n;i++){
in=out=0;
for(int j=0;j<n;j++){
in=in+a[j][i];
out=out+a[i][j];
}
printf("\n%d\t%d\t%d\t",i+1,in,out);
}
}
int main(){
int a[10][10],n;
printf("\nEnter the no. of vertex: ");
scanf("%d",&n);
create(a,n);
inout(a,n);
}
3.dfs
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
int vertex;
struct node *next;
}NODE;
void create(int a[20][20],int n){
printf("\n***Enter 1 for Yes & 0 for No**\n");
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
a[i][j]=0;
if(i!=j){
printf("\nIs there edge between v%d & v%d: ",i+1,j+1);
scanf("%d",&a[i][j]);
}
}
}
}
void recdfs(int a[20][20],int n,int v){
static int visited[20]={0};
visited[v]=1;
printf("v%d\t",v+1);
for(int w=0;w<n;w++){
if((a[v][w]==1)&&(visited[w]==0))
recdfs(a,n,w);
}
}
int main(){
int a[20][20],n;
printf("\nEnter the no. of vertex: ");
scanf("%d",&n);
create(a,n);
printf("\nThe Depth First Search Traversal is: \n");
recdfs(a,n,0);
}
4.bfs
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 20
typedef struct{
int data[MAXSIZE];
int front, rear;
}QUEUE;
void initq(QUEUE *pq){
pq->front=pq->rear=-1;
}
void addq(QUEUE *pq,int n){
pq->data[++pq->rear]=n;
}
int removeq(QUEUE *pq){
return pq->data[++pq->front];
}
int isempty(QUEUE *pq){
return(pq->front==pq->rear);
}
void create(int a[10][10],int n){
printf("\n****TYPE 1 FOR YES & 0 FOR NO****\n");
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
a[i][j]=0;
if(i!=j){
printf("\nIs there any edge between %d & %d: ",i+1,j+1);
scanf("%d",&a[i][j]);
}
}
}
}
void bfs(int a[10][10],int n){
int v=0;
int visited[20]={0};
QUEUE q;
initq(&q);
printf("\nThe Breadth First Traversal is:\n");
visited[v]=1;
addq(&q,v);
while(!isempty(&q)){
v=removeq(&q);
printf("v%d\t",v+1);
for(int w=0;w<n;w++){
if((a[v][w]==1)&&(visited[w]==0)){
addq(&q,w);
visited[w]=1;
}
}
}
}
int main(){
int a[10][10],n;
printf("\nEnter the no. of vertex: ");
scanf("%d",&n);
create(a,n);
bfs(a,n);
}

You might also like