0% found this document useful (0 votes)
14 views8 pages

Ass 4

The document contains multiple C programs that implement graph data structures and algorithms, including adjacency lists, depth-first search (DFS), and breadth-first search (BFS). Each program allows the user to input the number of vertices and edges, and then displays the graph's adjacency list or performs a traversal. The programs utilize structures for nodes and queues, and include functions for creating graphs, traversing them, and calculating in-degrees and out-degrees.

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)
14 views8 pages

Ass 4

The document contains multiple C programs that implement graph data structures and algorithms, including adjacency lists, depth-first search (DFS), and breadth-first search (BFS). Each program allows the user to input the number of vertices and edges, and then displays the graph's adjacency list or performs a traversal. The programs utilize structures for nodes and queues, and include functions for creating graphs, traversing them, and calculating in-degrees and out-degrees.

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/ 8

2.

Inout list
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
int vertex;
struct node *next;
}NODE;
NODE *list[10];

int create(int a[10][10],int 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 %d & %d: ",i+1,j+1);
scanf("%d",&a[i][j]);
}
}
}
}
void clist(int a[10][10],int n){
NODE *temp,*newnode;
for(int i=0;i<n;i++){
list[i]=NULL;
for(int j=0;j<n;j++){
if(a[i][j]==1){
newnode=(NODE *)malloc(sizeof(NODE));
newnode->vertex=j+1;
newnode->next=NULL;
if(list[i]==NULL)
list[i]=temp=newnode;
else
temp->next=newnode;
temp=newnode;
}
}
}
}
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);
clist(a,n);
inout(a,n);
}
1.list
include<stdio.h>
#include<stdlib.h>
typedef struct node{
int vertex;
struct node *next;
}NODE;
NODE *list[10];
int create(int a[10][10],int 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 %d & %d: ",i+1,j+1);
scanf("%d",&a[i][j]);
}
}
}
}
void clist(int a[10][10],int n){
NODE *temp,*newnode;
for(int i=0;i<n;i++){
list[i]=NULL;
for(int j=0;j<n;j++){
if(a[i][j]==1){
newnode=(NODE *)malloc(sizeof(NODE));
newnode->vertex=j+1;
newnode->next=NULL;
if(list[i]==NULL)
list[i]=temp=newnode;
else
temp->next=newnode;
temp=newnode;
}
}
}
}
void dlist(int n){
NODE *temp;
printf("\nThe Adjacency list is: ");
for(int i=0;i<n;i++){
printf("v%d-->",i+1);
temp=list[i];
while(temp){
printf("v%d-->",temp->vertex);
temp=temp->next;
}printf("NULL\n");
}
}
int main(){
int a[10][10],n;
printf("\nEnter the no. of vertex: ");
scanf("%d",&n);
create(a,n);
clist(a,n);
dlist(n);
}
3.listdfs

#include<stdio.h>
#include<stdlib.h>
typedef struct node{
int vertex;
struct node *next;
}NODE;
NODE *list[10];
int create(int a[10][10],int 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 %d & %d: ",i+1,j+1);
scanf("%d",&a[i][j]);
}
}
}
}
void clist(int a[10][10],int n){
NODE *temp,*newnode;
for(int i=0;i<n;i++){
list[i]=NULL;
for(int j=0;j<n;j++){
if(a[i][j]==1){
newnode=(NODE *)malloc(sizeof(NODE));
newnode->vertex=j+1;
newnode->next=NULL;
if(list[i]==NULL)
list[i]=temp=newnode;
else
temp->next=newnode;
temp=newnode;
}
}
}
}
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);
clist(a,n);
recdfs(a,n,0);
}
4.listbfs
#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);
}

typedef struct node{


int vertex;
struct node *next;
}NODE;
NODE *list[10];

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 clist(int a[10][10],int n){
NODE *temp,*newnode;
for(int i=0;i<n;i++){
list[i]=NULL;
for(int j=0;j<n;j++){
if(a[i][j]==1){
newnode=(NODE *)malloc(sizeof(NODE));
newnode->vertex=j+1;
newnode->next=NULL;
if(list[i]==NULL)
list[i]=temp=newnode;
else
temp->next=newnode;
temp=newnode;
}
}
}
}
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;
}
}0
}
}
int main(){
int a[10][10],n;
printf("\nEnter the no. of vertex: ");
scanf("%d",&n);
create(a,n);
clist(a,n);
bfs(a,n);
}

You might also like