In: Computer Science
C Programming Question:
Write a C - program to implement the following three operations:
a) Breadth-first search using Adjacency List
b) Breadth-first search using Adjacency Matrix
c) Check whether a given graph is Bipartite using Breadth-first search (adjacency list).
Please take your time, but do submit the correct and full code. Thank you very much.
Hello, I am providing the correct answers to above question. If you have any doubt just ask in the comments section. In part (C) I don't got the required output in C language so I have done it in C++ language. Hope You Understand. Once Again if you have any doubt just ask it in comments section.
Ans a) -
#include <stdio.h> #include <stdlib.h> struct node { int val; struct node * next; }; struct node * insert(struct node * head, int num) { // This is exactly as Head Insertion of a Linked List. // We choose Head Insertion to get an O(1) Insertion // into an Adjacency List, Tail Insertion can also be used struct node * p = (struct node *) malloc(sizeof(struct node)); p->val = num; p->next = head; return p; } void breadth_first_search(struct node * list[], int vertices, int parent[], int level[]) { struct node * temp; int i, par, lev, flag = 1; // 'lev' represents the level to be assigned // 'par' represents the parent to be assigned // 'flag' used to indicate if graph is exhausted lev = 0; level[1] = lev; // We start from node - 1 // So, Node - 1 is at level 0 // All immediate neighbours are at // level 1 and so on. while (flag) { flag = 0; for (i = 1; i <= vertices; ++i) { if (level[i] == lev) { flag = 1; temp = list[i]; par = i; while (temp != NULL) { if (level[temp->val] != -1) { temp = temp->next; continue; } level[temp->val] = lev + 1; parent[temp->val] = par; temp = temp->next; } } } ++lev; } } int main() { int vertices, edges, i, j, v1, v2; printf("Enter the number of Vertices -\n"); scanf("%d", &vertices); printf("Enter the number of Edges -\n"); scanf("%d", &edges); struct node * adjacency_list[vertices + 1]; // This is the table of our Adjacency List // Each element holds a Linked List // In C++ it can be made to hold a vector too // in that case, the declaration would be - // vector< vector<int> > adjacency_list; int parent[vertices + 1]; // Each element of Parent Array holds the Node value of its parent int level[vertices + 1]; // Each element of Level Array holds the Level value of that node for (i = 0; i <= vertices; ++i) { //Initialising our arrays adjacency_list[i] = NULL; parent[i] = 0; level[i] = -1; } for (i = 1; i <= edges; ++i) { scanf("%d%d", &v1, &v2); adjacency_list[v1] = insert(adjacency_list[v1], v2); adjacency_list[v2] = insert(adjacency_list[v2], v1); // For edge {v1, v2} = {2, 3}, // Adjacency List holds 2----3 // and 3-----2 as edges } // Printing Adjacency List printf("\nAdjacency List -\n"); for (i = 1; i <= vertices; ++i) { printf("%d -> ", i); struct node * temp = adjacency_list[i]; while (temp != NULL) { printf("%d -> ", temp->val); temp = temp->next; } printf("NULL\n"); } breadth_first_search(adjacency_list, vertices, parent, level); // Level Array printf("\nLevel and Parent Arrays -\n"); for (i = 1; i <= vertices; ++i) { printf("Level of Node %d is %d, Parent is %d\n", i, level[i], parent[i]); } return 0; }
Ans b) -
#include<stdio.h>
#include<stdlib.h>
#define MAX 100
int n;
int adj[MAX][MAX]; //adjacency matrix, where adj[i][j] = 1, denotes
there is an edge from i to j
int visited[MAX]; //visited[i] can be 0 / 1, 0 : it has not yet
printed, 1 : it has been printed
void create_graph();
void BF_Traversal();
void BFS();
int queue[MAX], front = -1,rear = -1;
void push_queue(int vertex);
int pop_queue();
int isEmpty_queue();
int main()
{
create_graph();
BFS();
return 0;
}
void BFS()
{
int v;
for(v=0; v<n; v++)
visited[v] = 0;
printf("Enter Start Vertex for BFS: \n");
scanf("%d", &v);
int i;
push_queue(v);
while(!isEmpty_queue())
{
v = pop_queue( );
if(visited[v]) //if it has already been visited by some other
neighbouring vertex, it should not be printed again.
continue;
printf("%d ",v);
visited[v] = 1;
for(i=0; i<n; i++)
{
if(adj[v][i] == 1 && visited[i] == 0)
{
push_queue(i);
}
}
}
printf("\n");
}
void push_queue(int vertex)
{
if(rear == MAX-1)
printf("Queue Overflow\n");
else
{
if(front == -1)
front = 0;
rear = rear+1;
queue[rear] = vertex ;
}
}
int isEmpty_queue()
{
if(front == -1 || front > rear)
return 1;
else
return 0;
}
int pop_queue()
{
int delete_item;
if(front == -1 || front > rear)
{
printf("Queue Underflow\n");
exit(1);
}
delete_item = queue[front];
front = front+1;
return delete_item;
}
void create_graph()
{
int count,max_edge,origin,destin;
printf("Enter number of vertices : ");
scanf("%d",&n);
max_edge = n*(n-1); //assuming each vertex has an edge with
remaining (n-1) vertices
for(count=1; count<=max_edge; count++)
{
printf("Enter edge %d( -1 -1 to quit ) : ",count);
scanf("%d %d",&origin,&destin);
if((origin == -1) && (destin == -1))
break;
if(origin>=n || destin>=n || origin<0 ||
destin<0)
{
printf("Invalid edge!\n");
count--;
}
else
{
adj[origin][destin] = 1;
adj[destin][origin] = 1; // assuming it is a bi-directional graph,
we are pushing the reverse edges too.
}
}
}
Ans C -
#include<bits/stdc++.h> using namespace std; int n,e,i,j; vector<vector<int> > graph; vector<int> color; bool vis[100011]; bool isBipartite() { color[0] = 1; // Mark colour as 1 for first vertex. queue <int> q; q.push(0); while (!q.empty()) { int temp = q.front(); q.pop(); for (i=0;i<n;i++) { if (graph[temp][i] && color[i] == -1) //if there is an edge, and colour is not assigned { color[i] = 1 - color[temp]; q.push(i); } else if (graph[temp][i] && color[i] == color[temp]) // if there is an edge and both vertices have same colours return 0; // graph is not bipartite } } return 1; } int main() { int x,y; cout<<"Enter number of vertices and edges respectively:"; cin>>n>>e; cout<<"\n"; graph.resize(n); color.resize(n,-1); memset(vis,0,sizeof(vis)); for(i=0;i<n;i++) graph[i].resize(n); for(i=0;i<e;i++) { cout<<"\nEnter edge vertices of edge "<<i+1<<" :"; cin>>x>>y; x--; y--; graph[x][y]=1; graph[y][x]=1; } if(isBipartite()) cout<<"Yes, The given graph is Bipartite.\n"; else cout<<"No, The given graph is not Bipartite.\n"; return 0; }