News
  • Submission of Theory Assignments 6,7,8 of OS on 1st April
  • Case Study Presentation of OS on 4st April
  • CN and PNS Journal Submission on 6th April

DS (SV) Program - Adjecancy matrix for a graph

This code is to find Adjecancy matrix for a graph.









 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
#include<iostream.h>
#include<conio.h>
#define SIZE 20
int main()
{

 clrscr();
 int vertex[SIZE],edge[SIZE][SIZE];
 int i,j,k,no_edges,no_vertex,from,to;
 //clrscr();
 cout<<"Enter number of vertices\n";
 cin>>no_vertex;
 cout<<"Enter vertices: "<<endl;
 for(i=0;i<no_vertex;i++)
  cin>>vertex[i];
 cout<<"Enter number of edges\n";
 cin>>no_edges;
 for(i=1;i<=no_vertex;i++)
  for(j=1;j<=no_vertex;j++)
   edge[i][j]=0;
 for(i=1;i<=no_edges;i++)
 {
  cout<<"Enter from vertex\n";
  cin>>from;
  cout<<"Enter to vertex\n";
  cin>>to;
  edge[from][to]=1;
  edge[to][from]=0;
 }

 cout<<"\n\nOutput:\n";
 cout<<"The vertices in graph are:\n"<<endl;
 for(i=0;i<no_vertex;i++)
  cout<<vertex[i]<<"\t";

 cout<<"\n Adjecancy matrix for a graph is\n";
 for(i=1;i<=no_vertex;i++)
 {
  for(j=1;j<=no_vertex;j++)
   cout<<"\t"<<edge[i][j];
  cout<<"\n";
 }
 getch();
 return 0;
}
© Copyright NMITD MCA Notes Published.. Blogger Templates
Back To Top