-
Notifications
You must be signed in to change notification settings - Fork 130
/
2-Union-intersection-ring.cpp
136 lines (128 loc) · 2.69 KB
/
2-Union-intersection-ring.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// PROGRAM 2
// .Write a program to find UNION, INTERSECTION and RING
// SUM of two graphs.
#include <bits/stdc++.h>
using namespace std;
#define crap \
ios::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL)
//cout<<fixed<<showpoint<<setprecision(12)<<ans<<endl;
#define dbg(x) cerr << #x << " = " << x << endl
#define endl "\n"
#define int long long int
#define double long double
#define pb push_back
#define mp make_pair
#define PI acos(-1)
int printIntersection(int arr1[], int arr2[], int m, int n)
{
int i = 0, j = 0;
while (i < m && j < n)
{
if (arr1[i] < arr2[j])
i++;
else if (arr2[j] < arr1[i])
j++;
else /* if arr1[i] == arr2[j] */
{
printf(" %d ", arr2[j++]);
i++;
}
}
}
int printUnion(int arr1[], int arr2[], int m, int n)
{
int i = 0, j = 0;
while (i < m && j < n)
{
if (arr1[i] < arr2[j])
printf(" %lld ", arr1[i++]);
else if (arr2[j] < arr1[i])
printf(" %lld ", arr2[j++]);
else
{
printf(" %lld ", arr2[j++]);
i++;
}
}
while (i < m)
printf(" %lld ", arr1[i++]);
while (j < n)
printf(" %lld ", arr2[j++]);
}
signed main()
{
// freopen("input.txt", "r", stdin);
int V1[] = {0, 1};
int V2[] = {0, 1, 2};
int m = sizeof(V1) / sizeof(V1[0]);
int n = sizeof(V2) / sizeof(V2[0]);
int G1[m][m], G2[n][n], E3[m + n][m + n];
int i, j, k;
printf("Enter the adjacency matrix for graph G1:\n");
for (i = 0; i < m; i++)
{
for (j = 0; j < m; j++)
{
printf("G1[%lld][%lld]=", i, j);
scanf("%lld", &G1[i][j]);
}
}
printf("Enter the adjacency matrix for graph G2:\n");
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
printf("G2[%lld][%lld]=", i, j);
scanf("%lld", &G2[i][j]);
}
}
printf("\nSet of vertices in union of the graphs G1 and G2 is:\n");
printUnion(V1, V2, m, n);
printf("\n");
printf("\nSet of vertices in intersection of the graphs G1 and G2 is:\n");
printIntersection(V1, V2, m, n);
printf("\n");
printf("\nSet of vertices in ring sum of the graphs G1 and G2 is:\n");
printUnion(V1, V2, m, n);
printf("\n");
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
if (G1[i][j] == G2[i][j] && i < m && j < m)
E3[i][j] = 0;
else if (G1[i][j] < G2[i][j] && i < m && j < m)
E3[i][j] = G2[i][j];
if (G1[i][j] < G2[i][j] && i < m && j < m)
E3[i][j] = G1[i][j];
else
E3[i][j] = G2[i][j];
}
}
printf("Adjacency matrix of ring sum of graphs G1 and G2 is:\n\t");
for (i = 0; i < n; i++)
{
printf("%lld\t", i);
}
printf("\n\t");
for (i = 0; i < n; i++)
{
printf(" ");
}
for (i = 0; i < n; i++)
{
printf("\n%lld|\t", i);
for (j = 0; j < n; j++)
{
printf("%lld\t", E3[i][j]);
}
}
return 0;
}
// 1 1
// 1 1
// 1 1 1
// 0 0 0
// 1 2 1