Professional Documents
Culture Documents
2013-0994
MACHINE PROBLEM 1 IT 169
ALGORITHM
Problem: Make an algorithm to display a graph using an adjacency matrix.
Steps:
1. Start.
2. Read the number of vertices, n.
3. Read the set of edges from a pair of distinct vertices, first and last.
4. Print adjacency matrix.
5. If the pair of vertices is adjacent return 1, otherwise 0.
6. Draw vertices that are represented by nodes.
7 If nodes are adjacent then connect a line, otherwise not.
8 End.
SOURCE CODE
#include<stdio.h>
#include <iostream>
#include <graphics.h>
#include <math.h>
#include <conio.h>
#define max 200
#define pi 3.14159265
int i,j,origin,destin;
int adj[max][max];
int n,max_edges;
int global_x[max], global_y[max];
int x_of_drawing[max],y_of_drawing[max];
char arr[200];
int main(){
for(i=1;i<=max_edges;i++)
{
printf("Enter edge %d( 0 0 to quit ) : ",i);
scanf("%d %d",&origin,&destin);
if( (origin==0) && (destin==0) )
break;
if( origin > n || destin > n || origin<=0 || destin<=0|| origin == destin)
{
printf("Invalid edge!\n");
i--;
}
adj[origin][destin]=1;
adj[destin][origin]=1; /*symmetric*/
global_x[i] = origin;
global_y[i] = destin;
}
for(int i=1,x=90;i<=n;i++){
x = x-40;
double val = pi/180;
setcolor(9);
if(i>9){
int xx = 250+(170*cos(x*val));
int yy = 215+(170*sin(x*val));
circle(xx, yy, 20);
x_of_drawing[i]=xx;
y_of_drawing[i]=yy;
}
else{
int xx = 250+(100*cos(x*val));
int yy = 215+(100*sin(x*val));
circle(xx, yy, 20);
x_of_drawing[i]=xx;
y_of_drawing[i]=yy;
}
}
for(int ii=1; ii<=max_edges;ii++){
char ch[9];
for(int i=1;i<=n;i++){
sprintf(ch,"%d",i);
outtextxy(x_of_drawing[i],y_of_drawing[i],ch);
for(int j = (i+1); j <= n; j++){
if(global_x[ii]==i && global_y[ii]==j||global_x[ii]==j && global_y[ii]==i){
line(x_of_drawing[i],y_of_drawing[i],x_of_drawing[j],y_of_drawing[j]);
}
if(global_x[ii]==0 || global_y[ii]==0){
break;
}
}
}
}
getch();
closegraph();
}
GARCIA, JUAN MIGUEL M. 2013-0994
MACHINE PROBLEM 2 IT 169
ALGORITHM
PROBLEM: Display two graphs graph using an adjacency matrix and compare.
INPUT: Adjacency matrix
OUTPUT: Graph of G and H and their comparison
STEPS: 1. Start
2. Read the number of vertices or size of graph G and H, n and m respectively.
3. Read the set of edges from a pair of distinct vertices for both graphs, first and last.
4. Print adjacency matrix.
5. If the pair of vertices is adjacent return 1, otherwise 0.
6. Draw vertices that are represented by nodes.
7. If nodes are adjacent then connect a line, otherwise not.
8. Compare both graphs if the vertices and edges are the same.
8.1 If the same set of vertices and set of edges then the graphs are equal otherwise not.
9. END
SOURCE CODE
#include<stdio.h>
#include <iostream>
#include <graphics.h>
#include <math.h>
#include <conio.h>
#define max 200
#define pi 3.14159265
int i,j,origin,destin;
//All the variables of Graph1
int adj[max][max];
int n,max_edges;
int global_x[max], global_y[max];
int x_of_drawing[max],y_of_drawing[max];
int adj2[max][max];
int n2,max_edges2;
int global_x2[max],global_y2[max];
int x_of_drawing2[max],y_of_drawing2[max];
char arr[200];
char arr2[200];
int main(){
if(graph_num==1){
max_edges=n*(n-1)/2;
for(i=1;i<=max_edges;i++)
{
printf("Enter edge %d( 0 0 to quit ) : ",i);
scanf("%d %d",&origin,&destin);
if( (origin==0) && (destin==0) )
break;
if( origin > n || destin > n || origin<=0 || destin<=0|| origin == destin)
{
printf("Invalid edge!\n");
i--;
}
adj[origin][destin]=1;
adj[destin][origin]=1; /*symmetric*/
global_x[i] = origin;
global_y[i] = destin;
}
}
else{
max_edges2=n2*(n2-1)/2;
for(i=1;i<=max_edges2;i++)
{
printf("Enter edge %d( 0 0 to quit ) : ",i);
scanf("%d %d",&origin,&destin);
if( (origin==0) && (destin==0) )
break;
if( origin > n2 || destin > n2 || origin<=0 || destin<=0|| origin == destin)
{
printf("Invalid edge!\n");
i--;
}
adj2[origin][destin]=1;
adj2[destin][origin]=1; /*symmetric*/
global_x2[i] = origin;
global_y2[i] = destin;
}
}
for(int i=1,x=90;i<=n;i++){
x = x-40;
double val = pi/180;
setcolor(9);
if(i>9){
int xx = 250+(170*cos(x*val));
int yy = 215+(170*sin(x*val));
circle(xx, yy, 20);
x_of_drawing[i]=xx;
y_of_drawing[i]=yy;
}
else{
int xx = 250+(100*cos(x*val));
int yy = 215+(100*sin(x*val));
circle(xx, yy, 20);
x_of_drawing[i]=xx;
y_of_drawing[i]=yy;
}
}
for(int ii=1; ii<=max_edges;ii++){
char ch[9];
for(int i=1;i<=n;i++){
sprintf(ch,"%d",i);
outtextxy(x_of_drawing[i],y_of_drawing[i],ch);
for(int j = (i+1); j <= n; j++){
if(global_x[ii]==i && global_y[ii]==j||global_x[ii]==j && global_y[ii]==i){
line(x_of_drawing[i],y_of_drawing[i],x_of_drawing[j],y_of_drawing[j]);
}
if(global_x[ii]==0 || global_y[ii]==0){
break;
}
}
}
}
for(int i=1,x=90;i<=n2;i++){
x = x-40;
double val = pi/180;
setcolor(9);
if(i>9){
int xx = 850+(170*cos(x*val));
int yy = 215+(170*sin(x*val));
circle(xx, yy, 20);
x_of_drawing2[i]=xx;
y_of_drawing2[i]=yy;
}
else{
int xx = 850+(100*cos(x*val));
int yy = 215+(100*sin(x*val));
circle(xx, yy, 20);
x_of_drawing2[i]=xx;
y_of_drawing2[i]=yy;
}
}
for(int ii=1; ii<=max_edges2;ii++){
char ch[18];
for(int i=1;i<=n2;i++){
sprintf(ch,"%d",i);
outtextxy(x_of_drawing2[i],y_of_drawing2[i],ch);
for(int j = (i+1); j <= n2; j++){
if(global_x2[ii]==i && global_y2[ii]==j||global_x2[ii]==j && global_y2[ii]==i){
line(x_of_drawing2[i],y_of_drawing2[i],x_of_drawing2[j],y_of_drawing2[j]);
}
if(global_x2[ii]==0 || global_y2[ii]==0){
break;
}
}
}
}
settextstyle(DEFAULT_FONT,HORIZ_DIR,3);
if(max_edges==max_edges2){
for(int i=1,counter=0;i<=max_edges;i++){
for(int j=1;j<=max_edges;j++){
if((global_x[i]==global_x2[j]||global_x[i]==global_y2[j])&&(global_y[i]==global_x2[j]||global_y[i]==global_y2[j]) ){
counter++;
sprintf(arr,"COUNTER# %d",counter);
break;
}
}
if (i==max_edges&&counter!=max_edges ){
sprintf(arr,"The two given graphs are NOT EQUAL");
outtextxy((getmaxwidth()/2)-500,50+(getmaxheight()/2),arr);
break;
}
else if (counter==max_edges){
sprintf(arr,"The two given graphs are EQUAL");
outtextxy((getmaxwidth()/2)-500,50+(getmaxheight()/2),arr);
break;
}
}
}
else{
sprintf(arr,"The two given graphs are NOT EQUAL");
outtextxy((getmaxwidth()/2)-500,50+(getmaxheight()/2),arr);
}
getch();
closegraph();
}
MACHINE PROBLEM 1
IT 169