0% found this document useful (0 votes)
386 views

Program-1 Write A Program in C To Create Two Sets and Perform The Union Operation On Sets

The document contains 9 programs related to graph algorithms and data structures written in C language. Program 1-3 create two sets and perform union, intersection, and difference operations on the sets respectively. Program 4 performs symmetric difference operation on two sets. Program 5 performs power set operation on a set. Program 6 displays truth tables for logical AND, OR, and NOT gates. Program 7 finds cartesian product of two sets. Program 8 finds minimum cost spanning tree using Prim's algorithm. Program 9 finds shortest path in a graph using Dijkstra's algorithm.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
386 views

Program-1 Write A Program in C To Create Two Sets and Perform The Union Operation On Sets

The document contains 9 programs related to graph algorithms and data structures written in C language. Program 1-3 create two sets and perform union, intersection, and difference operations on the sets respectively. Program 4 performs symmetric difference operation on two sets. Program 5 performs power set operation on a set. Program 6 displays truth tables for logical AND, OR, and NOT gates. Program 7 finds cartesian product of two sets. Program 8 finds minimum cost spanning tree using Prim's algorithm. Program 9 finds shortest path in a graph using Dijkstra's algorithm.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Program-1

Write a program in C to create two sets and perform the Union operation on
sets.
#include <stdio.h>
int main()
{
int a[10], b[10], flag = 0, n1, n2, i, j;
printf("Enter array1 size : ");
scanf("%d",&n1);
printf("\nEnter array2 size : ");
scanf("%d",&n2);
printf("\nEnter array1 element : ");
for(i = 0;i < n1;i++)
scanf("%d",&a[i]);
printf("\nEnter array2 element : ");
for(i = 0;i < n2;i++)
scanf("%d",&b[i]);
printf("\nUnion:");
for(i = 0;i < n1;i++)
printf("%d, ",a[i]);
for(i = 0;i < n2;i++)
{
for(j = 0;j < n1;j++)
{
if(b[i] == a[j])
{
flag = 1;
}

1
}
if(flag == 0)
{
printf("%d, ",b[i]);
}
flag = 0;
}
return 0;
}

2
Program-2
Write a program in C to create two sets and perform the Intersection
operation on sets
#include <stdio.h>
int main()
{
int a[10], b[10], flag = 0, n1, n2, i, j;
printf("Enter array1 size : ");
scanf("%d",&n1);
printf("\nEnter array2 size : ");
scanf("%d",&n2);
printf("\nEnter array1 element : ");
for(i = 0;i < n1;i++)
scanf("%d",&a[i]);
printf("\nEnter array2 element : ");
for(i = 0;i < n2;i++)
scanf("%d",&b[i]);
printf("Intersection: ");
for(i = 0;i < n1;i++)
{
for(j = 0;j < n2;j++)
{
if(b[i] == a[j])
{
flag = 1;
}
}
if(flag == 1)

3
{
printf("%d ", b[i]);
}
flag = 0;
}
return 0;
}

4
Program-3
Write a program in C to create two sets and perform the Difference operation
on sets.
#include<stdio.h>
#define max 100

int ifexists(int z[], int u, int v)


{
int i;
if (u==0) return 0;
for (i=0; i<=u;i++)
if (z[i]==v) return (1);
return (0);
}

void main()
{
int p[max], q[max], r[max];
int m,n;
int i,j,k;
printf("Enter length of first array:");
scanf("%d",&m);
printf("Enter %d elements of first array\n",m);
for(i=0;i<m;i++ )
scanf("%d",&p[i]);
printf("\nEnter length of second array:");
scanf("%d",&n);
printf("Enter %d elements of second array\n",n);
for(i=0;i<n;i++ )
scanf("%d",&q[i]);
k=0;
for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
{
if (p[i]==q[j])
{
break;
}
}
if(j==n)
{
if(!ifexists(r,k,p[i]))
{
r[k]=p[i];

5
k++;
}
}
}
printf("\nThe difference of the two array is:\n");
for(i = 0;i<k;i++)
printf("%d\n",r[i]);
}

6
Program-4
Write a program in C to create two sets and perform the Symmetric
Difference operation.
#include<stdio.h>
int main()
{
int a[10],b[10],c[10],d[10],m=0,k=0,n=0,n1,n2,l,i,j,sy[100];
printf("Enter size of set A");
scanf("%d",&n1);
printf("Enter element of set");
for( i=0;i<n1;i++)
scanf("%d",&a[i]);
printf("Enter size of set B");
scanf("%d",&n2);
printf("Enter element of set");
for( i=0;i<n2;i++)
scanf("%d",&b[i]);

// logic for find A-B

for( i=0;i<n1;i++)
{
// here we check that is b[i] already present in the ans set
// if present then ignore it otherwise add it to the ans set
for(j=0;j<n2;j++)
{
if(b[j]==a[i])
break;
}
if(j==n2)
{

for(l=0;l<k;l++)
{
if(c[l]==a[i])
break;
}
if(l==k)
{
c[k]=a[i];
k++;
}
}

7
// logic for find B-A

for( i=0;i<n2;i++)
{
for(j=0;j<n1;j++)
{
if(b[i]==a[j])
break;
}
if(j==n1)
{
// here we check that is b[i] already present in the ans set
// if present then ignore it otherwise add it to the ans set
for(l=0;l<m;l++)
{
if(d[l]==b[i])
break;
}
if(l==m)
{
d[m]=b[i];
m++;
}
}

//logic for symmetric Difference


for(i=0;i<k;i++)
{
sy[n]=c[i];
n++;
}
for(i=0;i<m;i++)
{
sy[n]=d[i];
n++;
}

printf("\nsymmetric Difference of sets is:-\n");


for(i=0;i<n;i++)
printf("%d ",sy[i]);
return0;

8
Program-5
Write a program in C to perform the Power Set operation on a set.
#include <stdio.h>
#include <math.h>

void printPowerSet(char *set, int set_size)


{
/*set_size of power set of a set with set_size
n is (2**n -1)*/
unsigned int pow_set_size = pow(2, set_size);
int counter, j;

/*Run from counter 000..0 to 111..1*/


for(counter = 0; counter < pow_set_size; counter++)
{
for(j = 0; j < set_size; j++)
{
/* Check if jth bit in the counter is set
If set then print jth element from set */
if(counter & (1<<j))
printf("%c", set[j]);
}
printf("\n");
}
}

/*Driver program to test printPowerSet*/


int main()

9
{
char set[] = {'a','b','c'};
printPowerSet(set, 3);
return 0;
}

10
Program-6
Write a program in C to Display the Boolean Truth Table for AND, OR,
NOT.
#include<stdio.h>
void main()
{
int a[2][2],b[2][2],c[2];
int i,j;
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
a[i][j]=(i&&j);
b[i][j]=(i||j);
}
}

for(i=0;i<=1;i++)
{
c[i]=(!i);
}
printf("\nThe Truth Table for AND Gate( && ) is..\n");
printf(" A B : C=A&&B\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
printf(" %d %d : %d\n",i,j,a[i][j]);

11
}
}
printf("\nThe Truth Table for OR Gate( || ) is..\n");
printf(" A B : C=A||B\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
printf(" %d %d : %d\n",i,j,b[i][j]);
}
}
printf("\nThe Truth Table for NOT Gate (!) is..\n");
printf(" A = !A\n");
for(i=0;i<=1;i++)
{
printf(" %d : %d\n",i,c[i]);
}
}

12
Program-7
Write a C Program to find Cartesian Product of two sets.
#include<stdio.h>
int main()
{
int a[10],b[10],n1,n2;
printf("Enter size of set A\n");
scanf("%d",&n1);
printf("Enter element of set A\n");
for(int i=0;i<n1;i++)
scanf("%d",&a[i]);
printf("Enter size of set B\n");
scanf("%d",&n2);
printf("Enter element of set B\n");
for(int i=0;i<n2;i++)
scanf("%d",&b[i]);

// logic for cartesian product


printf("Cartesian Product is:\n");
printf("{");
for(int i=0;i<n1;i++)
{
for(int j=0;j<n2;j++)
{
printf(" (%d %d) ",a[i],b[j]);
}
}
printf("}");

13
return 0;
}

14
Program-8
Write a program in C for minimum cost spanning tree.
//Using Prim’s Algorithm
#include<stdio.h>
int main()
{
int cost[10][10],visited[10]={0},i,j,n,no_e=1,min,a,b,min_cost=0;
printf("Enter number of nodes ");
scanf("%d",&n);
printf("Enter cost in form of adjacency matrix\n");
//input graph
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
// cost is 0 then initialize it by maximum value
if(cost[i][j]==0)
cost[i][j]=1000;
}
}

// logic for finding minimum cost spanning tree


visited[1]=1; // visited first node
while(no_e<n)
{
min=1000;
// in each cycle find minimum cost

15
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(cost[i][j]<min)
{
if(visited[i]!=0)
{
min=cost[i][j];
a=i;
b=j;
}
}
}
} //if node is not visited
if(visited[b]==0)
{
printf("\n%d to %d cost=%d",a,b,min);
min_cost=min_cost+min;
no_e++;
}
visited[b]=1;
// initialize with maximum value you can also use any other value
cost[a][b]=cost[b][a]=1000;
}
printf("\nminimum weight is %d",min_cost);
return 0;
}

16
Program-9
Write a program in C for finding shortest path in a Graph.
//Using Dijkstras Algorithm
#include<stdio.h>
#define MAX 100
#define TEMP 0
#define PERM 1
#define infinity 9999
#define NIL -1
void findPath(int s, int v );
void Dijkstra( int s);
int min_temp( );
void create_graph();
int n; /* Denotes number of vertices in the graph */
int adj[MAX][MAX];
int predecessor[MAX]; /*predecessor of each vertex in shortest path*/
int pathLength[MAX];
int status[MAX];

int main()
{
int s,v;
create_graph();
printf("\nEnter source vertex : ");
scanf("%d",&s);
Dijkstra(s);
while(1)
{

17
printf("\nEnter destination vertex(-1 to quit): ");
scanf("%d",&v);
if(v == -1)
break;
if(v < 0 || v >= n )
printf("\nThis vertex does not exist\n");
else if(v == s)
printf("\nSource and destination vertices are same\n");
else if( pathLength[v] == infinity )
printf("\nThere is no path from source to destination vertex\n");
else
findPath(s,v);
}
return 0;
}/*End of main()*/

void Dijkstra( int s)


{
int i,current;
/* Make all vertices temporary */
for(i=0; i<n; i++)
{
predecessor[i] = NIL;
pathLength[i] = infinity;
status[i] = TEMP;
}
/* Make pathLength of source vertex equal to 0 */
pathLength[s] = 0;

18
while(1)
{
/*Search for temporary vertex with minimum pathLength
and make it current vertex*/
current = min_temp( );

if( current == NIL )


return;
status[current] = PERM;

for(i=0; i<n; i++)


{
/*Checks for adjacent temporary vertices */
if ( adj[current][i] !=0 && status[i] == TEMP )
if( pathLength[current] + adj[current][i] < pathLength[i] )
{
predecessor[i] = current; /*Relabel*/
pathLength[i] = pathLength[current] + adj[current][i];
}
}
}
}/*End of Dijkstra( )*/
/*Returns the temporary vertex with minimum value of pathLength
Returns NIL if no temporary vertex left or
all temporary vertices left have pathLength infinity*/
int min_temp( )
{

19
int i;
int min = infinity;
int k = NIL;
for(i=0;i<n;i++)
{
if(status[i] == TEMP && pathLength[i] < min)
{
min = pathLength[i];
k = i;
}
}
return k;
}/*End of min_temp( )*/

void findPath(int s, int v )


{
int i,u;
int path[MAX]; /*stores the shortest path*/
int shortdist = 0; /*length of shortest path*/
int count = 0; /*number of vertices in the shortest path*/

/*Store the full path in the array path*/


while( v != s )
{
count++;
path[count] = v;
u = predecessor[v];
shortdist += adj[u][v];

20
v = u;
}
count++;
path[count]=s;

printf("\nShortest Path is : ");


for(i=count; i>=1; i--)
printf("%d ",path[i]);
printf("\nShortest distance is : %d\n", shortdist);
}/*End of findPath()*/

void create_graph()
{
int i,max_edges,origin,destin, wt;
printf("\nEnter number of vertices : ");
scanf("%d",&n);
max_edges = n*(n-1);
for(i=1;i<=max_edges;i++)
{
printf("\nEnter edge %d( -1 -1 to quit ) : ",i);
scanf("%d %d",&origin,&destin);
if( (origin == -1) && (destin == -1) )
break;
printf("\nEnter weight for this edge : ");
scanf("%d",&wt);

if( origin >= n || destin >= n || origin<0 || destin<0)


{

21
printf("\nInvalid edge!\n");
i--;
}
else
adj[origin][destin] = wt;
}
}

22

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy