You are on page 1of 2

Military Institute of Science & Technology Course Name: Algorithms Sessional Course Code: CSE-216 Assignment # 2

Date:11/07/20011
1. Write a C program that will the following:

a. Input the graph. b. Display the adjacency matrix c. Traverse the given graph using Depth First Search (DFS) algorithm. d. Show the number of connected components.

Sample Program: #include<stdio.h> int top = -1, a[ 20 ][ 20 ], vis[ 20 ], stack[ 20 ]; int k=1; //counter for no of connected components. void create(int s,int n);//create the graph void display(int n); //Display the adjacency matrix void dfs( int s, int n );//traverse the graph void push( int item ); int pop(); main() { int n, i, s, ch, j; char c, dummy; printf( "ENTER THE NUMBER VERTICES " ); scanf( "%d", &n ); do { for ( i = 1;i <= n;i++ ) vis[ i ] = 0; printf( "\nMENU" ); printf( "\n1.Input the graph:" );

printf( "\n2.Display the adjacency matrix:" ); printf( "\n3.D.F.S:" ); printf( "\n4.No of Connected Components:" ); printf( "\nENTER YOUR CHOICE" ); scanf( "%d", &ch ); printf( "ENTER THE SOURCE VERTEX :" ); scanf( "%d", &s ); switch ( ch ) { case 1: create( s, n ); break; case 2: display( n ); break; case 3: dfs( s, n ); break; case 4: printf(\n The number of connected components: %d,k); break; } printf( "DO U WANT TO CONTINUE(Y/N) ? " ); scanf( "%c", &dummy ); scanf( "%c", &c ); } while ( ( c == 'y' ) || ( c == 'Y' ) ); } void dfs( int s, int n ) { //write the code } void push( int item ) { //write the code } int pop() { //write the code }

You might also like