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

Class Assignment 2

This document provides instructions for a C program assignment involving graphs. The program must: 1) Input a graph and store it in an adjacency matrix 2) Display the adjacency matrix 3) Traverse the graph using depth-first search (DFS) and store the results 4) Calculate and display the number of connected components in the graph It includes function prototypes and variable declarations needed to implement the DFS algorithm and track connected components.

Uploaded by

Syeda Nyma
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Class Assignment 2

This document provides instructions for a C program assignment involving graphs. The program must: 1) Input a graph and store it in an adjacency matrix 2) Display the adjacency matrix 3) Traverse the graph using depth-first search (DFS) and store the results 4) Calculate and display the number of connected components in the graph It includes function prototypes and variable declarations needed to implement the DFS algorithm and track connected components.

Uploaded by

Syeda Nyma
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 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