daa project file

49
 1. Write a program in C to perform linear search in an unsorted array.  #include <stdio.h> #include <conio.h> void main( ) { int arr[10] = { 11, 2, 9, 1, !", 2!, 1", 1, 90, $ int i, num $ clrscr( ) $ %rint& ( 'nter numer to search* ' ) $ scan& ( '+d', num ) $ &or ( i = 0 $ i <= 9 $ i-- ) { i& ( arr[i] == num ) rea $ i& ( i == 10 ) %rint& ( '/umer is not %resent in the arra.' ) $ else %rint& ( 'he numer is at %osition +d in the arra.', i ) $ etch( ) $ Output:- nter numer to search*11  he numer is at %osition 0 in the ar ra .  3 4567 68/5 7 : 7: 3 : ;4 /41 :5 ? 1

Transcript of daa project file

Page 1: daa project file

8/9/2019 daa project file

http://slidepdf.com/reader/full/daa-project-file 1/49

 

1. Write a program in C to perform linear search in

an unsorted array.

 #include <stdio.h>#include <conio.h>

void main( ){int arr[10] = { 11, 2, 9, 1, !", 2!, 1", 1, 90, $int i, num $

clrscr( ) $

%rint& ( 'nter numer to search* ' ) $scan& ( '+d', num ) $&or ( i = 0 $ i <= 9 $ i-- ){i& ( arr[i] == num )rea $i& ( i == 10 )%rint& ( '/umer is not %resent in the arra.' ) $else%rint& ( 'he numer is at %osition +d in the arra.', i ) $

etch( ) $

Output:-nter numer to search*11 he numer is at %osition 0 in the arra.

 34567 68/57 :7:3: ;4 /41:5 ? 1

Page 2: daa project file

8/9/2019 daa project file

http://slidepdf.com/reader/full/daa-project-file 2/49

2. Write a program in C to perform Binary search in

a sorted array.

#include <stdio.h>#include <conio.h>

void main( ){

int arr[10] = { 1, 2, , 9, 11, 1, 1", 2!, !", 90 $int mid, lo@er = 0 , u%%er = 9, num, Aa = 1 $

clrscr( ) $

%rint& ( 'nter numer to search* ' ) $

scan& ( '+d', num ) $

&or ( mid = ( lo@er - u%%er ) B 2 $ lo@er <= u%%er $mid = ( lo@er - u%%er ) B 2 )

{i& ( arr[mid] == num ){

%rint& ( 'he numer is at %osition +d in the arra.', mid ) $Aa = 0 $rea $

i& ( arr[mid] > num )

u%%er = mid 1 $else

lo@er = mid - 1 $

i& ( Aa )%rint& ( 'lement is not %resent in the arra.' ) $

etch( ) $

Output:-

nter numer to search*90 he numer is at %osition 9 in the arra.

 34567 68/57 :7:3: ;4 /41:5 ? 2

Page 3: daa project file

8/9/2019 daa project file

http://slidepdf.com/reader/full/daa-project-file 3/49

3. Write a program in C language to perform

Insertion Sort.

#include <stdio.h>#include <conio.h>

void main( ){

int arr[!] = { 2!, 1", 1, 1, 2 $int i, C, , tem% $clrscr( ) $

%rint& ( '8nsertion sort.Dn' ) $%rint& ( 'Dn:rra e&ore sortin*Dn') $

  &or ( i = 0 $ i <= E $ i-- )%rint& ( '+dDt', arr[i] ) $

  &or ( i = 1 $ i <= E $ i-- )

{&or ( C = 0 $ C < i $ C-- ){

i& ( arr[C] > arr[i] ){

tem% = arr[C] $arr[C] = arr[i] $

&or ( = i $ > C $ )arr[] = arr[ 1] $

arr[ - 1] = tem% $

%rint& ( 'DnDn:rra a&ter sortin*Dn') $

  &or ( i = 0 $ i <= E $ i-- )%rint& ( '+dDt', arr[i] ) $

etch( ) $

Output:-8nsertion sort*:rra e&ore sortin*2! 1" 1 1 2:rra a&ter sortin*2 1 1" 2! 1

 34567 68/57 :7:3: ;4 /41:5 ?

Page 4: daa project file

8/9/2019 daa project file

http://slidepdf.com/reader/full/daa-project-file 4/49

. Write a program in C language to perform

Selection sort.

#include <stdio.h>#include <conio.h>

void main( ){

int arr[!] = { 2!, 1", 1, 1, 2 $int i, C, tem% $

  clrscr( ) $

%rint& ( '6election sort.Dn' ) $%rint& ( 'Dn:rra e&ore sortin*Dn') $

&or ( i = 0 $ i <= E $ i-- )%rint& ( '+dDt', arr[i] ) $

  &or ( i = 0 $ i <= $ i-- ){

&or ( C = i - 1 $ C <= E $ C-- ){

i& ( arr[i] > arr[C] ){

tem% = arr[i] $arr[i] = arr[C] $arr[C] = tem% $

   

%rint& ( 'DnDn:rra a&ter sortin*Dn') $  &or ( i = 0 $ i <= E $ i-- )

%rint& ( '+dDt', arr[i] ) $  etch( ) $

Output:-

6election sort*:rra e&ore sortin*

2! 1" 1 1 2

:rra a&ter sortin*

2 1 1" 2! 1

 34567 68/57 :7:3: ;4 /41:5 ? E

Page 5: daa project file

8/9/2019 daa project file

http://slidepdf.com/reader/full/daa-project-file 5/49

!. Write a program in C language to perform

Bu""le Sort.

#include <stdio.h>#include <conio.h>

void main( ){

int arr[!] = { 2!, 1", 1, 1, 2 $int i, C, tem% $

  clrscr( ) $

%rint& ( 'Fule sort.Dn' ) $%rint& ( 'Dn:rra e&ore sortin*Dn') $

&or ( i = 0 $ i <= E $ i-- )

%rint& ( '+dDt', arr[i] ) $  &or ( i = 0 $ i <= $ i-- )

{&or ( C = 0 $ C <= i $ C-- ){

i& ( arr[C] > arr[C - 1] ){

tem% = arr[C] $arr[C] = arr[C - 1] $arr[C - 1] = tem% $

 

%rint& ( 'DnDn:rra a&ter sortin*Dn') $  &or ( i = 0 $ i <= E $ i-- )

%rint& ( '+dDt', arr[i] ) $  etch( ) $

Output:-Fule sort*

:rra e&ore sortin:

2! 1" 1 1 2

:rra a&ter sortin*

2 1 1" 2! 1

 34567 68/57 :7:3: ;4 /41:5 ? !

Page 6: daa project file

8/9/2019 daa project file

http://slidepdf.com/reader/full/daa-project-file 6/49

#. Write a program in C to implement di$ide and

con%uer approach: &erge Sort.

#include <stdio.h>#include <conio.h>

void main( ){

int a[!] = { 11, 2, 9, 1, !" $int [!] = { 2!, 1", 1, 90, $int c[10] $int i, C, , tem% $

clrscr( ) $

%rint& ( 'Gere sort.Dn' ) $

%rint& ( 'DnHirst arra*Dn' ) $&or ( i = 0 $ i <= E $ i-- )

%rint& ( '+dDt', a[i] ) $

%rint& ( 'DnDn6econd arra*Dn' ) $&or ( i = 0 $ i <= E $ i-- )

%rint& ( '+dDt', [i] ) $

&or ( i = 0 $ i <= $ i-- ){

&or ( C = i - 1 $ C <= E $ C-- ){

i& ( a[i] > a[C] ){

tem% = a[i] $a[i] = a[C] $a[C] = tem% $

i& ( [i] > [C] ){

tem% = [i] $[i] = [C] $[C] = tem% $

&or ( i = C = = 0 $ i <= 9 $ )

 34567 68/57 :7:3: ;4 /41:5 ? I

Page 7: daa project file

8/9/2019 daa project file

http://slidepdf.com/reader/full/daa-project-file 7/49

{i& ( a[C] <= [] )

c[i--] = a[C--] $else

c[i--] = [--] $

i& ( C == ! ?? == ! )rea $

&or ( $ C <= E $ )c[i--] = a[C--] $

&or ( $ <= E $ )c[i--] = [--] $

%rint& ( 'DnDn:rra a&ter sortin*Dn') $&or ( i = 0 $ i <= 9 $ i-- )

%rint& ( '+dDt', c[i] ) $

etch( ) $

Output:-

Gere sort*Hirst arra

11 2 9 1 !"

6econd arra*

2! 1" 1 90

:rra a&ter sortin

1 2 9 11 1 1" 2! !" 90

'. Write a program in C to implement di$ide and

con%uer pproach:(uic) Sort. 34567 68/57 :7:3: ;4 /41:5 ? "

Page 8: daa project file

8/9/2019 daa project file

http://slidepdf.com/reader/full/daa-project-file 8/49

#include <stdio.h>#include <conio.h>

int s%lit ( intJ, int, int ) $

void main( ){int arr[10] = { 11, 2, 9, 1, !", 2!, 1", 1, 90, $int i $

void Kuicsort ( int J, int, int ) $

clrscr( ) $

%rint& ( 'Luic sort.Dn' ) $%rint& ( 'Dn:rra e&ore sortin*Dn') $

&or ( i = 0 $ i <= 9 $ i-- )%rint& ( '+dDt', arr[i] ) $

Kuicsort ( arr, 0, 9 ) $

%rint& ( 'Dn:rra a&ter sortin*Dn') $

&or ( i = 0 $ i <= 9 $ i-- )%rint& ( '+dDt', arr[i] ) $

etch( ) $

void Kuicsort ( int a[ ], int lo@er, int u%%er ){

int i $i& ( u%%er > lo@er ){

i = s%lit ( a, lo@er, u%%er ) $Kuicsort ( a, lo@er, i 1 ) $Kuicsort ( a, i - 1, u%%er ) $

int s%lit ( int a[ ], int lo@er, int u%%er ){

int i, %, K, t $

% = lo@er - 1 $ 34567 68/57 :7:3: ;4 /41:5 ? M

Page 9: daa project file

8/9/2019 daa project file

http://slidepdf.com/reader/full/daa-project-file 9/49

K = u%%er $i = a[lo@er] $

@hile ( K >= % ){

@hile ( a[%] < i )%-- $

@hile ( a[K] > i )K $

i& ( K > % ){

t = a[%] $a[%] = a[K] $a[K] = t $

t = a[lo@er] $a[lo@er] = a[K] $a[K] = t $

return K $

Output:-Luic sort

:rra e&ore sortin

11 2 9 1 !" 2! 1" 1 90

:rra a&ter sortin

1 2 9 11 1 1" 2! !" 90

*. Write a program in C to implement Sorting: +eapSort.

#include <stdio.h>#include <conio.h>

 34567 68/57 :7:3: ;4 /41:5 ? 9

Page 10: daa project file

8/9/2019 daa project file

http://slidepdf.com/reader/full/daa-project-file 10/49

void maehea% ( int [ ], int ) $void hea%sort ( int [ ], int ) $

void main( ){

int arr[10] = { 11, 2, 9, 1, !", 2!, 1", 1, 90, $int i $clrscr( ) $%rint& ( '7ea% 6ort.Dn' ) $

maehea% ( arr, 10 ) $

%rint& ( 'DnFe&ore 6ortin*Dn' ) $&or ( i = 0 $ i <= 9 $ i-- )

%rint& ( '+dDt', arr[i] ) $

hea%sort ( arr, 10 ) $

%rint& ( 'Dn:&ter 6ortin*Dn' ) $&or ( i = 0 $ i <= 9 $ i-- )

%rint& ( '+dDt', arr[i] ) $etch( )$

void maehea% ( int N[ ], int n ){

int i, val, s, & $&or ( i = 1 $ i < n $ i-- )

{val = N[i] $s = i $& = ( s 1 ) B 2 $@hile ( s > 0 N[&] < val ){

N[s] = N[&] $s = & $& = ( s 1 ) B 2 $

N[s] = val $

void hea%sort ( int N[ ], int n ){ int i, s, &, ivalue $

&or ( i = n 1 $ i > 0 $ i ){

ivalue = N[i] $ 34567 68/57 :7:3: ;4 /41:5 ? 10

Page 11: daa project file

8/9/2019 daa project file

http://slidepdf.com/reader/full/daa-project-file 11/49

N[i] = N[0] $& = 0 $i& ( i == 1 )

s = 1 $else

s = 1 $

i& ( i > 2 N[2] > N[1] )s = 2 $

@hile ( s >= 0 ivalue < N[s] ){

N[&] = N[s] $& = s $s = 2 J & - 1 $i& ( s - 1 <= i 1 N[s] < N[s - 1] )

s-- $i& ( s > i 1 )

s = 1 $N[&] = ivalue $

Output:-7ea% sort*

Fe&ore sortin

90 !" 2! 1 11 9 1" 1 2

:&ter sortin

1 2 9 11 1 1" 2! !" 90

,. Write a program in C to implement Sorting: radi

sort.

# include<stdio.h># include<malloc.h>

struct node{

int in&o $ 34567 68/57 :7:3: ;4 /41:5 ? 11

Page 12: daa project file

8/9/2019 daa project file

http://slidepdf.com/reader/full/daa-project-file 12/49

struct node Jlin$Jstart=/O$

main(){

struct node Jtm%,JK$int i,n,item$

%rint&('nter the numer o& elements in the list * ')$scan&('+d', n)$

&or(i=0$i<n$i--){

%rint&('nter element +d * ',i-1)$scan&('+d',item)$

BJ 8nsertin elements in the lined list JBtm%= malloc(siPeo&(struct node))$tm%>in&o=item$tm%>lin=/O$

i&(start==/O) BJ 8nsertin Qrst element JBstart=tm%$

else{

K=start$@hile(K>linR=/O)

K=K>lin$K>lin=tm%$

BJnd o& &orJB

%rint&('Onsorted list is *Dn')$dis%la()$radiNSsort()$%rint&('6orted list is *Dn')$dis%la ()$

BJnd o& main()JB

dis%la(){

struct node J%=start$@hile( % R=/O){

%rint&('+d ', %>in&o)$%= %>lin$

 34567 68/57 :7:3: ;4 /41:5 ? 12

Page 13: daa project file

8/9/2019 daa project file

http://slidepdf.com/reader/full/daa-project-file 13/49

%rint&('Dn')$

BJnd o& dis%la()JB

radiNSsort()

{ int i,,di,maNdi,mindi,leastSsi,mostSsi$struct node J%, Jrear[10], J&ront[10]$

leastSsi=1$mostSsi=lareSdi(start)$

&or( = leastSsi$ <= mostSsi $ --){

%rint&(':66 +d * Naminin +dth diit &rom riht ',,)$&or(i = 0 $ i <= 9 $ i--)

{rear[i] = /O$&ront[i] = /O $

maNdi=0$mindi=9$% = start $@hile( % R= /O){

BJHind th diit in the numerJBdi = diit(%>in&o, )$

i&(di>maNdi)maNdi=di$

i&(di<mindi)mindi=di$

BJ:dd the numer to Kueue o& diJBi&(&ront[di] == /O)

&ront[di] = % $else

rear[di]>lin = % $rear[di] = % $

%=%>lin$BJ5o to neNt numer in the listJBBJnd @hile JBBJ maNdi and mindi are the maNimum amd minimum  diits o& the th diits o& all the numersJB%rint&('mindi=+d maNdi=+dDn',mindi,maNdi)$BJToin all the Kueues to &orm the ne@ lined listJBstart=&ront[mindi]$&or(i=mindi$i<maNdi$i--)

 34567 68/57 :7:3: ;4 /41:5 ? 1

Page 14: daa project file

8/9/2019 daa project file

http://slidepdf.com/reader/full/daa-project-file 14/49

{i&(rear[i-1]R=/O)

rear[i]>lin=&ront[i-1]$else

rear[i-1]=rear[i]$

rear[maNdi]>lin=/O$%rint&('/e@ list * ')$dis%la()$

BJ nd &or JB

BJnd o& radiNSsortJB

BJ his &unction Qnds numer o& diits in the larest element o& the list JBint lareSdi(){

struct node J%=start $int lare = 0,ndi = 0 $

@hile(% R= /O){

i&(% >in&o > lare)lare = %>in&o$

% = %>lin $%rint&('arest lement is +d , ',lare)$@hile(lare R= 0)

{ndi--$lare = lareB10 $

%rint&('/umer o& diits in it are +dDn',ndi)$return(ndi)$

BJnd o& lareSdi()JB

BJhis &unction returns th diit o& a numerJBint diit(int numer, int )

{int diit, i $&or(i = 1 $ i <= $ i--){

diit = numer + 10 $numer = numer B10 $

return(diit)$

 34567 68/57 :7:3: ;4 /41:5 ? 1E

Page 15: daa project file

8/9/2019 daa project file

http://slidepdf.com/reader/full/daa-project-file 15/49

BJnd o& diit()JB

Output:-

nter the numer o& elements in the list*!nter elements 1 2nter elements 2 Enter elements 12nter elements E nter elements ! !Onsorted list is*2 E 12 !arest element 8s E,numer o& diit in it are 2ass 1*eNamin 1th diit &rom riht mindi=2 maNdi=!/e@ list* 12 2 E !ass 2* eNaminin 2th diit &rom riht mindi maNdi=E/e@ list* ! 12 2 E6orted list is*! 12 2 E

1. Write a program in C to implement Shell Sort.

#deQne G:U 20void main(){

int arr[G:U], i,C,,n,incr$%rint&('nter the numer o& elements * ')$

scan&('+d',n)$&or(i=0$i<n$i--){

%rint&('nter element +d * ',i-1)$scan&('+d',arr[i])$

%rint&('Onsorted list is *Dn')$&or (i = 0$ i < n$ i--)

 34567 68/57 :7:3: ;4 /41:5 ? 1!

Page 16: daa project file

8/9/2019 daa project file

http://slidepdf.com/reader/full/daa-project-file 16/49

%rint&('+d ', arr[i])$%rint&('Dnnter maNimum increment (odd value) * ')$scan&('+d',incr)$

@hile(incr>=1) BJ6hell sortJB{

&or(C=incr$C<n$C--){=arr[C]$&or(i = Cincr$ i >= 0 < arr[i]$ i = iincr)

arr[i-incr]=arr[i]$arr[i-incr]=$

%rint&('8ncrement=+d Dn',incr)$&or (i = 0$ i < n$ i--)

%rint&('+d ', arr[i])$%rint&('Dn')$

incr=incr2$ BJVecrease the incrementJBBJnd o& @hileJB%rint&('6orted list is *Dn')$&or (i = 0$ i < n$ i--)

%rint&('+d ', arr[i])$%rint&('Dn')$

Output:-

nter ede !(0 0 to Kuit)*

2

nter ede I(0 0 to Kuit)*2

2nter ede "(0 0 to Kuit)*1

1

nter ede M(0 0 to Kuit)*2

 34567 68/57 :7:3: ;4 /41:5 ? 1I

Page 17: daa project file

8/9/2019 daa project file

http://slidepdf.com/reader/full/daa-project-file 17/49

nter ede 9(0 0 to Kuit)*E

nter ede 10(0 0 to Kuit)*2

E

11. Write a /rogram to "uild a "inary search tree

from an array.

#include <stdio.h>#include <conio.h>#include <alloc.h>

struct node{

struct node Jle&t $

char data $struct node Jriht $

$

struct node J uildtree ( int ) $

void inorder ( struct node J ) $

 34567 68/57 :7:3: ;4 /41:5 ? 1"

Page 18: daa project file

8/9/2019 daa project file

http://slidepdf.com/reader/full/daa-project-file 18/49

char a[ ] = {W:W, WFW, WXW, WVW, WW, WHW, W5W, WD0W, WD0W, W7W, WD0W,WD0W, WD0W, WD0W, WD0W, WD0W, WD0W, WD0W, WD0W, WD0W, WD0W

  $void main( )

{struct node Jroot $

clrscr( ) $

root = uildtree ( 0 ) $%rint& ( '8norder raversal*Dn' ) $inorder ( root ) $etch( ) $

struct node J uildtree ( int n )

{struct node Jtem% = /O $

i& ( a[n] R= WD0W ){

tem% = ( struct node J ) malloc ( siPeo& ( struct node ) ) $tem% > le&t = uildtree ( 2 J n - 1 ) $tem% > data = a[n] $tem% > riht = uildtree ( 2 J n - 2 ) $

return tem% $

void inorder ( struct node Jroot ){

i& ( root R= /O ){

inorder ( root > le&t ) $%rint& ( '+cDt', root > data ) $inorder ( root > riht ) $

4ut%ut*

 34567 68/57 :7:3: ;4 /41:5 ? 1M

Page 19: daa project file

8/9/2019 daa project file

http://slidepdf.com/reader/full/daa-project-file 19/49

8norder traversal

 V F 7 : H X 5

12. Write a program in C to 0nd minimum

maimum  th smallest element in gi$en array.

I..Maximum and Minimum Element

#include<stdio.h>

int main(){

  int a[!0],siPe,i,i,small$

  %rint&('Dnnter the siPe o& the arra* ')$  scan&('+d',siPe)$

  %rint&('Dnnter +d elements in to the arra* ', siPe)$

  &or(i=0$i<siPe$i--)

  scan&('+d',a[i])$

 34567 68/57 :7:3: ;4 /41:5 ? 19

Page 20: daa project file

8/9/2019 daa project file

http://slidepdf.com/reader/full/daa-project-file 20/49

  i=a[0]$

  &or(i=1$i<siPe$i--){

  i&(i<a[i])  i=a[i]$

 

  %rint&('arest element* +dDn',i)$

  small=a[0]$

  &or(i=1$i<siPe$i--){

  i&(small>a[i])

  small=a[i]$

 

%rint&('6mallest element* +dDn',small)$

 return 0$

II. th minimum element

#include<stdio.h>

#include<conio.h>

int main()

{

  int arra[100],lenth,i,C,tem%,n$

  %rint&('nter he lenth 4& he :rra ')$

  scan&('+d',lenth)$

  %rint&('Dnnter he /umers*')$

  &or(i=0$i<lenth$i--)

  {

  scan&('+d',arra[i])$

 

 34567 68/57 :7:3: ;4 /41:5 ? 20

Page 21: daa project file

8/9/2019 daa project file

http://slidepdf.com/reader/full/daa-project-file 21/49

  &or(i=0$i<lenth$i--)

  {

  &or(C=0$C<lenth1$C--)

  {  i&(arra[C]>arra[C-1])

  {

  tem%=arra[C]$

  arra[C]=arra[C-1]$

  arra[C-1]=tem%$

 

 

 

  %rint&('he ne@ arra is*')$

  &or(i=0$i<lenth$i--)

  {

  %rint&(' +d ',arra[i])$

 

  %rint&('Dnnter Yhich 6mallest /umer 3ou @ant')$

  scan&('+d',n)$

  %rint&('Dnhe +d th smallest numer is* +d',n,arra[n1])$

  etch()$

  return 0$

 

O4/4:-

/; the siPe o& the arra*E

nter E elements in to the arra*1

2

 34567 68/57 :7:3: ;4 /41:5 ? 21

Page 22: daa project file

8/9/2019 daa project file

http://slidepdf.com/reader/full/daa-project-file 22/49

1

arest elements*E

6mallest elements*1

13. Write a program in C to insert and delete a

node from the "inary search tree.

#include <stdio.h>#include <conio.h>#include <alloc.h>#deQne ;O 1

#deQne H:6 0struct treenode{

struct treenode Jle&tchild $int data $struct treenode Jrihtchild $

$void insert ( struct treenode JJ, int ) $void delete ( struct treenode JJ, int ) $void search ( struct treenode JJ, int, struct treenode JJ,struct treenodeJJ, int J ) $

void inorder ( struct treenode J ) $void main( ){

struct treenode Jt $int reK, i = 0, num, a[ ] = { 11, 9, 1, M, 10, 12, 1E, 1!, " $t = /O $ BJ em%t tree JBclrscr( ) $

 34567 68/57 :7:3: ;4 /41:5 ? 22

Page 23: daa project file

8/9/2019 daa project file

http://slidepdf.com/reader/full/daa-project-file 23/49

@hile ( i <= M ){

insert ( t, a[i] ) $i-- $

clrscr( ) $%rint& ( 'Finar tree e&ore deletion*Dn' ) $inorder ( t ) $delete ( t, 10 ) $%rint& ( 'DnFinar tree a&ter deletion*Dn' ) $inorder ( t ) $

delete ( t, 1E ) $%rint& ( 'DnFinar tree a&ter deletion*Dn' ) $inorder ( t ) $delete ( t, M ) $

%rint& ( 'DnFinar tree a&ter deletion*Dn' ) $inorder ( t ) $delete ( t, 1 ) $%rint& ( 'DnFinar tree a&ter deletion*Dn' ) $inorder ( t ) $

BJ inserts a ne@ node in a inar search tree JBvoid insert ( struct treenode JJsr, int num ){

i& ( Jsr == /O )

{Jsr = malloc ( siPeo& ( struct treenode ) ) $( Jsr ) > le&tchild = /O $( Jsr ) > data = num $( Jsr ) > rihtchild = /O $

else BJ search the node to @hich ne@ node @ill e attached JB{

BJ i& ne@ data is less, traverse to le&t JBi& ( num < ( Jsr ) > data )

insert ( ( ( Jsr ) > le&tchild ), num ) $

elseBJ else traverse to riht JBinsert ( ( ( Jsr ) > rihtchild ), num ) $

BJ deletes a node &rom the inar search tree JBvoid delete ( struct treenode JJroot, int num ) 34567 68/57 :7:3: ;4 /41:5 ? 2

Page 24: daa project file

8/9/2019 daa project file

http://slidepdf.com/reader/full/daa-project-file 24/49

{

int &ound $

struct treenode J%arent, JN, JNsucc $

BJ i& tree is em%t JBi& ( Jroot == /O )

{

%rint& ( 'Dnree is em%t' ) $

return $

%arent = N = /O $

BJ call to search &unction to Qnd the node to e deleted JB

search ( root, num, %arent, N, &ound ) $

BJ i& the node to deleted is not &ound JB

i& ( &ound == H:6 )

{

%rint& ( 'DnVata to e deleted, not &ound' ) $

return $

BJ i& the node to e deleted has t@o children JB

i& ( N > le&tchild R= /O N > rihtchild R= /O )

{ %arent = N $

Nsucc = N > rihtchild $

@hile ( Nsucc > le&tchild R= /O )

{ %arent = Nsucc $

Nsucc = Nsucc > le&tchild $

N > data = Nsucc > data $

N = Nsucc $

 34567 68/57 :7:3: ;4 /41:5 ? 2E

Page 25: daa project file

8/9/2019 daa project file

http://slidepdf.com/reader/full/daa-project-file 25/49

BJ i& the node to e deleted has no child JB

i& ( N > le&tchild == /O N > rihtchild == /O )

{ i& ( %arent > rihtchild == N )%arent > rihtchild = /O $

else

%arent > le&tchild = /O $

&ree ( N ) $

return $

BJ i& the node to e deleted has onl rihtchild JB

i& ( N > le&tchild == /O N > rihtchild R= /O )

{ i& ( %arent > le&tchild == N )

%arent > le&tchild = N > rihtchild $

else

%arent > rihtchild = N > rihtchild $

&ree ( N ) $

return $

BJ i& the node to e deleted has onl le&t child JB

i& ( N > le&tchild R= /O N > rihtchild == /O )

{ i& ( %arent > le&tchild == N )

%arent > le&tchild = N > le&tchild $

else

%arent > rihtchild = N > le&tchild $

&ree ( N ) $

return $

 34567 68/57 :7:3: ;4 /41:5 ? 2!

Page 26: daa project file

8/9/2019 daa project file

http://slidepdf.com/reader/full/daa-project-file 26/49

BJreturns the address o& the node to e deleted, address o& its %arent and

  @hether the node is &ound or not JB

void search ( struct treenode JJroot, int num, struct treenode JJ%ar, structtreenode JJN, int J&ound )

{ struct treenode JK $

K = Jroot $

J&ound = H:6 $

J%ar = /O $

@hile ( K R= /O )

{ BJ i& the node to e deleted is &ound JB

i& ( K > data == num )

{

J&ound = ;O $

JN = K $

return $

J%ar = K $

i& ( K > data > num )

K = K > le&tchild $

else

K = K > rihtchild $

BJ traverse a inar search tree in a V; (e&tVata;iht) &ashion JB

void inorder ( struct treenode Jsr )

{ i& ( sr R= /O )

{

 34567 68/57 :7:3: ;4 /41:5 ? 2I

Page 27: daa project file

8/9/2019 daa project file

http://slidepdf.com/reader/full/daa-project-file 27/49

inorder ( sr > le&tchild ) $

BJ %rint the data o& the node @hose le&tchild is /O or the %ath

has

alread een traversed JB

%rint& ( '+dDt', sr > data ) $

inorder ( sr > rihtchild ) $

Output:-

Finar tree e&ore delection*

" M 9 10 11 12 1 1E 1!

Finar tree a&ter delection*

" M 9 11 12 1 1E 1!

Finar tree a&ter delection*

" M 9 11 12 1 1!

Finar tree a&ter delection*

" 9 11 12 1 1!

Finar tree a&ter delection*

" 9 11 12 1!

 34567 68/57 :7:3: ;4 /41:5 ? 2"

Page 28: daa project file

8/9/2019 daa project file

http://slidepdf.com/reader/full/daa-project-file 28/49

1. Write a program in C of fractional )napsac)

pro"lem to implement greedy strategy of

algorithm de$elopment.

#include <stdio.h>

#include <malloc.h>

int n = !$ BJ he numer o& oCects JB

int c[10] = {12, 1, 2, 1, E$ BJ c[i] is the JX46J o& the ith oCect$ i.e. @hat

 34O :3 to tae the oCect JB

int v[10] = {E, 2, 2, 1, 10$ BJ v[i] is the JZ:OJ o& the ith oCect$ i.e.

 34O 5 &or tain the oCect JB

int Y = 1!$ BJ he maNimum @eiht ou can tae JB

void sim%leSQll() {

int curS@$

Aoat totSv$

int i, maNi$

int used[10]$

&or (i = 0$ i < n$ --i)

used[i] = 0$ BJ 8 have not used the ith oCect et JB

curS@ = Y$

@hile (curS@ > 0) { BJ @hile thereWs still roomJB

BJ Hind the est oCect JB

 34567 68/57 :7:3: ;4 /41:5 ? 2M

Page 29: daa project file

8/9/2019 daa project file

http://slidepdf.com/reader/full/daa-project-file 29/49

maNi = 1$

&or (i = 0$ i < n$ --i)

i& ((used[i] == 0)

((maNi == 1) ?? ((Aoat)v[i]Bc[i] > (Aoat)v[maNi]Bc[maNi])))maNi = i$

used[maNi] = 1$ BJ mar the maNith oCect as used JB

curS@ = c[maNi]$ BJ @ith the oCect in the a, 8 can carr less JB

totSv -= v[maNi]$

i& (curS@ >= 0)

%rint&(':dded oCect +d (+d, +d) com%letl in the a. 6%ace le&t*

+d.Dn', maNi - 1, v[maNi], c[maNi], curS@)$else {

%rint&(':dded +d++ (+d, +d) o& oCect +d in the a.Dn', (int)((1 -(Aoat)curS@Bc[maNi]) J 100), v[maNi], c[maNi], maNi - 1)$

totSv = v[maNi]$

totSv -= (1 - (Aoat)curS@Bc[maNi]) J v[maNi]$

%rint&('Hilled the a @ith oCects @orth +.2&.Dn', totSv)$

int main(int arc, char Jarv[]) {

sim%leSQll()$

return 0$

 34567 68/57 :7:3: ;4 /41:5 ? 29

Page 30: daa project file

8/9/2019 daa project file

http://slidepdf.com/reader/full/daa-project-file 30/49

1!. Write a program in C to implement minimum

spanning tree algorithm: rus)al5s 6lgorithm.

#include<stdio.h>#deQne G:U 20struct ede

{ int u$int v$int @eiht$struct ede Jlin$

J&ront = /O$int &ather[G:U]$ BJ7olds &ather o& each node JBstruct ede tree[G:U]$ BJ Yill contain the edes o& s%annin tree JBint n$ BJVenotes total numer o& nodes in the ra%h JBint @tStree=0$ BJYeiht o& the s%annin tree JBint count=0$ BJ Venotes numer o& edes included in the tree JBBJ Hunctions JB

void maeStree()$void insertStree(int i,int C,int @t)$void insertS%Kue(int i,int C,int @t)$struct ede JdelS%Kue()$main(){ int i$

createSra%h()$maeStree()$%rint&('des to e included in s%annin tree are *Dn')$&or(i=1$i<=count$i--){ %rint&('+d>',tree[i].u)$

%rint&('+dDn',tree[i].v)$%rint&('Yeiht o& this minimum s%annin tree is * +dDn', @tStree)$

BJnd o& main()JBcreateSra%h(){ int i,@t,maNSedes,oriin,destin$

%rint&('nter numer o& nodes * ')$scan&('+d',n)$

 34567 68/57 :7:3: ;4 /41:5 ? 0

Page 31: daa project file

8/9/2019 daa project file

http://slidepdf.com/reader/full/daa-project-file 31/49

maNSedes=nJ(n1)B2$&or(i=1$i<=maNSedes$i--){ %rint&('nter ede +d(0 0 to Kuit)* ',i)$

scan&('+d +d',oriin,destin)$i&( (oriin==0) (destin==0) )

rea$%rint&('nter @eiht &or this ede * ')$scan&('+d',@t)$i&( oriin > n ?? destin > n ?? oriin<=0 ?? destin<=0){ %rint&('8nvalid edeRDn')$

i$else

insertS%Kue(oriin,destin,@t)$BJnd o& &orJBi&(i<n1)

{%rint&('6%annin tree is not %ossileDn')$eNit(1)$

BJnd o& createSra%h()JBvoid maeStree(){ struct ede Jtm%$

int node1,node2,rootSn1,rootSn2$@hile( count < n1) BJoo% till n1 edes included in the treeJB{ tm%=delS%Kue()$

node1=tm%>u$

node2=tm%>v$%rint&('n1=+d ',node1)$%rint&('n2=+d ',node2)$@hile( node1 > 0){ rootSn1=node1$

node1=&ather[node1]$@hile( node2 >0 ){

rootSn2=node2$node2=&ather[node2]$

%rint&('rootn1=+d ',rootSn1)$%rint&('rootn2=+dDn',rootSn2)$i&(rootSn1R=rootSn2){  insertStree(tm%>u,tm%>v,tm%>@eiht)$  @tStree=@tStree-tm%>@eiht$  &ather[rootSn2]=rootSn1$

 34567 68/57 :7:3: ;4 /41:5 ? 1

Page 32: daa project file

8/9/2019 daa project file

http://slidepdf.com/reader/full/daa-project-file 32/49

BJnd o& @hileJB

BJnd o& maeStree()JBBJ8nsertin an ede in the tree JBvoid insertStree(int i,int C,int @t)

{ %rint&('his ede inserted in the s%annin treeDn')$count--$tree[count].u=i$tree[count].v=C$tree[count].@eiht=@t$

BJnd o& insertStree()JBBJ8nsertin edes in the %riorit Kueue JBvoid insertS%Kue(int i,int C,int @t){

struct ede Jtm%,JK$

tm% = (struct ede J)malloc(siPeo&(struct ede))$tm%>u=i$tm%>v=C$tm%>@eiht = @t$BJLueue is em%t or ede to e added has @eiht less than Qrst edeJBi&( &ront == /O ?? tm%>@eiht < &ront>@eiht ){

tm%>lin = &ront$&ront = tm%$

else

{K = &ront$@hile( K>lin R= /O K>lin>@eiht <= tm%>@eiht )

K=K>lin$tm%>lin = K>lin$K>lin = tm%$i&(K>lin == /O) BJde to e added at the endJB

tm%>lin = /O$BJnd o& elseJB

BJnd o& insertS%Kue()JBBJVeletin an ede &rom the %riorit KueueJB

struct ede JdelS%Kue(){

struct ede Jtm%$tm% = &ront$%rint&('de %rocessed is +d>+d +dDn',tm%>u,tm%>v,tm%

>@eiht)$&ront = &ront>lin$return tm%$

 34567 68/57 :7:3: ;4 /41:5 ? 2

Page 33: daa project file

8/9/2019 daa project file

http://slidepdf.com/reader/full/daa-project-file 33/49

BJnd o& delS%Kue()JB

Output:-

nter @eiht &or this ede*Inter ede 2(0 0 to Kuit)* 12nter @eiht &or this ede*nter ede (0 0 to Kuit)* !nter @eiht &or this ede*I8nvalid edeRnter ede 2(0 0 to Kuit)* 1

2nter @eiht &or this ede*de %rocessed is 1>2 /11 n2=2 root1=1 root2=2 his ede inserted in the s%annin treede %rocessed is 1>2 /1=1 n2=2 root1=1 root2=1de %rocessed is 2> I/1=2 n2= rootn1=1 root2=

 his ede inserted in the s%annin treede to e include in s%annin tree are*1>22>Yeiht o& this minimum s%anin tree is 9

 34567 68/57 :7:3: ;4 /41:5 ?

Page 34: daa project file

8/9/2019 daa project file

http://slidepdf.com/reader/full/daa-project-file 34/49

1#. Write a program in C to implement minimum

spanning tree algorithm: /rim5s 6lgorithm.

#include<stdio.h>#deQne G:U 10

#deQne G 0#deQne ;G 1#deQne H:6 0#deQne ;O 1#deQne inQnit 9999struct node{ int %redecessor$

int dist$ BJVistance &rom %redecessor JBint status$

$struct ede

{ int u$int v$

$int adC[G:U][G:U]$int n$main(){ int i,C$

int %ath[G:U]$int @tStree,count$struct ede tree[G:U]$createSra%h()$%rint&(':dCacenc matriN is *Dn')$dis%la()$count = maetree(tree,@tStree)$%rint&('Yeiht o& s%annin tree is * +dDn', @tStree)$%rint&('des to e included in s%annin tree are * Dn')$&or(i=1$i<=count$i--){ %rint&('+d>',tree[i].u)$

%rint&('+dDn',tree[i].v)$

BJnd o& main()JBcreateSra%h(){ int i,maNSedes,oriin,destin,@t$

%rint&('nter numer o& vertices * ')$scan&('+d',n)$maNSedes=nJ(n1)B2$&or(i=1$i<=maNSedes$i--){ %rint&('nter ede +d(0 0 to Kuit) * ',i)$

scan&('+d +d',oriin,destin)$

 34567 68/57 :7:3: ;4 /41:5 ? E

Page 35: daa project file

8/9/2019 daa project file

http://slidepdf.com/reader/full/daa-project-file 35/49

i&((oriin==0) (destin==0))rea$

%rint&('nter @eiht &or this ede * ')$scan&('+d',@t)$i&( oriin > n ?? destin > n ?? oriin<=0 ?? destin<=0)

{ %rint&('8nvalid edeRDn')$i$else{ adC[oriin][destin]=@t$

adC[destin][oriin]=@t$

BJnd o& &orJBi&(i<n1){ %rint&('6%annin tree is not %ossileDn')$

eNit(1)$

BJnd o& createSra%h()JBdis%la(){ int i,C$

&or(i=1$i<=n$i--){ &or(C=1$C<=n$C--)

%rint&('+d',adC[i][C])$%rint&('Dn')$

BJnd o& dis%la()JBint maetree(struct ede tree[G:U],int J@eiht)

{ struct node state[G:U]$int i,,min,count,current,ne@dist$int m$int u1,v1$J@eiht=0$BJGae all nodes tem%orarJB&or(i=1$i<=n$i--){ state[i].%redecessor=0$

state[i].dist = inQnit$state[i].status = G$

BJGae Qrst node %ermanentJBstate[1].%redecessor=0$state[1].dist = 0$state[1].status = ;G$BJ6tart &rom Qrst nodeJBcurrent=1$count=0$ BJcount re%resents numer o& nodes in tree JB

 34567 68/57 :7:3: ;4 /41:5 ? !

Page 36: daa project file

8/9/2019 daa project file

http://slidepdf.com/reader/full/daa-project-file 36/49

@hile( allS%erm(state) R= ;O ) BJoo% till all the nodes ecome;GJB

{ &or(i=1$i<=n$i--){

i& ( adC[current][i] > 0 state[i].status == G )

{ i&( adC[current][i] < state[i].dist ){

state[i].%redecessor = current$state[i].dist = adC[current][i]$

BJ6earch &or tem%orar node @ith minimum distanceand mae it current nodeJB

min=inQnit$&or(i=1$i<=n$i--)

{ i&(state[i].status == G state[i].dist < min){ min = state[i].dist$

current=i$

BJnd o& &orJBstate[current].status=;G$BJ8nsert this ede(u1,v1) into the tree JBu1=state[current].%redecessor$v1=current$count--$tree[count].u=u1$

tree[count].v=v1$BJ:dd @t on this ede to @eiht o& tree JBJ@eiht=J@eiht-adC[u1][v1]$

BJnd o& @hileJBreturn (count)$

BJnd o& maetree()JBint allS%erm(struct node state[G:U] ){ int i$

&or(i=1$i<=n$i--)  i&( state[i].status == G )  return H:6$

return ;O$BJnd o& allS%erm()JB

Output:-

nter ede !(0 0 to Kuit)*1

 34567 68/57 :7:3: ;4 /41:5 ? I

Page 37: daa project file

8/9/2019 daa project file

http://slidepdf.com/reader/full/daa-project-file 37/49

nter @eiht &or this ede*Enter ede I(0 0 to Kuit)*!Enter @eiht &or this ede*

8nvalid edeRnter ede I(0 0 to Kuit)*E!nter @eiht &or this ede*8nvalid edeRnter ede I(0 0 to Kuit)*21nter @eiht &or this ede*:dCacenc matriN is*

 0 E 0

  0 0 E E 0 0 E 0 E E 0

Yeiht o& a%anin tree is * 11des to e include in a%anin tree are*1>21>2>E

1'. Write a program to implement elementarygraph algorithm: Breadth 0rst search.#include <stdio.h>#include <conio.h>#include <alloc.h>#deQne ;O 1

 34567 68/57 :7:3: ;4 /41:5 ? "

Page 38: daa project file

8/9/2019 daa project file

http://slidepdf.com/reader/full/daa-project-file 38/49

#deQne H:6 0#deQne G:U Mstruct node{ int data $

struct node JneNt $

$int visited[G:U] $int K[M] $int &ront, rear $void &s ( int, struct node JJ ) $struct node J etnodeS@rite ( int ) $void addKueue ( int ) $int deleteKueue( ) $int isem%t( ) $void del ( struct node J ) $void main( )

{ struct node Jarr[G:U] $struct node Jv1, Jv2, Jv, JvE $ int i $

  clrscr( ) $v1 = etnodeS@rite ( 2 ) $arr[0] = v1 $v1 > neNt = v2 = etnodeS@rite ( ) $v2 > neNt = /O $v1 = etnodeS@rite ( 1 ) $arr[1] = v1 $v1 > neNt = v2 = etnodeS@rite ( E ) $

v2 > neNt = v = etnodeS@rite ( ! ) $v > neNt = /O $v1 = etnodeS@rite ( 1 ) $arr[2] = v1 $v1 > neNt = v2 = etnodeS@rite ( I ) $v2 > neNt = v = etnodeS@rite ( " ) $v > neNt = /O $v1 = etnodeS@rite ( 2 ) $arr[] = v1 $v1 > neNt = v2 = etnodeS@rite ( M ) $v2 > neNt = /O $

v1 = etnodeS@rite ( 2 ) $arr[E] = v1 $v1 > neNt = v2 = etnodeS@rite ( M ) $v2 > neNt = /O $v1 = etnodeS@rite ( ) $arr[!] = v1 $v1 > neNt = v2 = etnodeS@rite ( M ) $v2 > neNt = /O $

 34567 68/57 :7:3: ;4 /41:5 ? M

Page 39: daa project file

8/9/2019 daa project file

http://slidepdf.com/reader/full/daa-project-file 39/49

v1 = etnodeS@rite ( ) $arr[I] = v1 $v1 > neNt = v2 = etnodeS@rite ( M ) $v2 > neNt = /O $v1 = etnodeS@rite ( E ) $

arr["] = v1 $v1 > neNt = v2 = etnodeS@rite ( ! ) $v2 > neNt = v = etnodeS@rite ( I ) $v > neNt = vE = etnodeS@rite ( " ) $vE > neNt = /O $&ront = rear = 1 $&s ( 1, arr ) $&or ( i = 0 $ i < G:U $ i-- )

del ( arr[i] ) $etch( ) $

void &s ( int v, struct node JJ% ){ struct node Ju $

visited[v 1] = ;O $%rint& ( '+dDt', v ) $addKueue ( v ) $@hile ( isem%t( ) == H:6 ){ v = deleteKueue( ) $

u = J ( % - v 1 ) $@hile ( u R= /O ){ i& ( visited [ u > data 1 ] == H:6 )

{ addKueue ( u > data ) $

visited [ u > data 1 ] = ;O $%rint& ( '+dDt', u > data ) $

u = u > neNt $

struct node J etnodeS@rite ( int val ){  struct node Jne@node $

ne@node = ( struct node J ) malloc ( siPeo& ( struct node ) ) $

ne@node > data = val $return ne@node $

void addKueue ( int verteN ){

i& ( rear == G:U 1 ){

%rint& ( 'DnLueue 4verAo@.' ) $ 34567 68/57 :7:3: ;4 /41:5 ? 9

Page 40: daa project file

8/9/2019 daa project file

http://slidepdf.com/reader/full/daa-project-file 40/49

eNit( ) $rear-- $K[rear] = verteN $i& ( &ront == 1 )

&ront = 0 $int deleteKueue( ){ int data $

i& ( &ront == 1 ){ %rint& ( 'DnLueue OnderAo@.' ) $

eNit( ) $data = K[&ront] $i& ( &ront == rear )

&ront = rear = 1 $

else&ront-- $return data $

int isem%t( ){ i& ( &ront == 1 )

return ;O $return H:6 $

void del ( struct node Jn ){ struct node Jtem% $

@hile ( n R= /O ){ tem% = n > neNt $

&ree ( n ) $n = tem% $

Output:- 1 2 E ! I " M

 1*. Write a program to implement elementarygraph algorithm: 7epth 8irst Search.

#include <stdio.h>#include <alloc.h>

#deQne ;O 1#deQne H:6 0#deQne G:U M

struct node{ int data $

struct node JneNt $ $ 34567 68/57 :7:3: ;4 /41:5 ? E0

Page 41: daa project file

8/9/2019 daa project file

http://slidepdf.com/reader/full/daa-project-file 41/49

int visited[G:U] $void d&s ( int, struct node JJ ) $struct node J etnodeS@rite ( int ) $void del ( struct node J ) $void main( )

{ struct node Jarr[G:U] $struct node Jv1, Jv2, Jv, JvE $  int i $

v1 = etnodeS@rite ( 2 ) $arr[0] = v1 $v1 > neNt = v2 = etnodeS@rite ( ) $v2 > neNt = /O $v1 = etnodeS@rite ( 1 ) $

arr[1] = v1 $v1 > neNt = v2 = etnodeS@rite ( E ) $

v2 > neNt = v = etnodeS@rite ( ! ) $v > neNt = /O $v1 = etnodeS@rite ( 1 ) $

arr[2] = v1 $v1 > neNt = v2 = etnodeS@rite ( I ) $v2 > neNt = v = etnodeS@rite ( " ) $v > neNt = /O $v1 = etnodeS@rite ( 2 ) $arr[] = v1 $v1 > neNt = v2 = etnodeS@rite ( M ) $

v2 > neNt = /O $v1 = etnodeS@rite ( 2 ) $

arr[E] = v1 $v1 > neNt = v2 = etnodeS@rite ( M ) $v2 > neNt = /O $

  v1 = etnodeS@rite ( ) $

  arr[!] = v1 $v1 > neNt = v2 = etnodeS@rite ( M ) $v2 > neNt = /O $

v1 = etnodeS@rite ( ) $

  arr[I] = v1 $v1 > neNt = v2 = etnodeS@rite ( M ) $v2 > neNt = /O $v1 = etnodeS@rite ( E ) $ 

arr["] = v1 $

 34567 68/57 :7:3: ;4 /41:5 ? E1

Page 42: daa project file

8/9/2019 daa project file

http://slidepdf.com/reader/full/daa-project-file 42/49

v1 > neNt = v2 = etnodeS@rite ( ! ) $v2 > neNt = v = etnodeS@rite ( I ) $v > neNt = vE = etnodeS@rite ( " ) $vE > neNt = /O $d&s ( 1, arr ) $

 

&or ( i = 0 $ i < G:U $ i-- )  del ( arr[i] ) $

  etch( ) $

void d&s ( int v, struct node JJ% ){ struct node JK $

visited[v 1] = ;O $%rint& ( '+dDt', v ) $

K = J ( % - v 1 ) $@hile ( K R= /O ){ i& ( visited[K > data 1] == H:6 )

d&s ( K > data, % ) $else

K = K > neNt $

struct node J etnodeS@rite ( int val ){

struct node Jne@node $ne@node = ( struct node J ) malloc ( siPeo& ( struct node ) ) $ne@node > data = val $return ne@node $

void del ( struct node Jn ){ struct node Jtem% $  @hile ( n R= /O )  { tem% = n > neNt $

  &ree ( n ) $  n = tem% $ 

Output:-

 34567 68/57 :7:3: ;4 /41:5 ? E2

Page 43: daa project file

8/9/2019 daa project file

http://slidepdf.com/reader/full/daa-project-file 43/49

 1 2 E M ! I "

1,. Write a program in C for topological sorting.

#include<stdio.h>#deQne G:U 20int n,adC[G:U][G:U]$int &ront=1,rear=1,Kueue[G:U]$main(){

int i,C=0,$int to%sort[G:U],inde[G:U]$

  createSra%h()$

%rint&('he adCacenc matriN is *Dn')$dis%la()$BJHind the inderee o& each nodeJB&or(i=1$i<=n$i--){

inde[i]=inderee(i)$i&( inde[i]==0 )

insertSKueue(i)$ 34567 68/57 :7:3: ;4 /41:5 ? E

Page 44: daa project file

8/9/2019 daa project file

http://slidepdf.com/reader/full/daa-project-file 44/49

@hile(&ront<=rear) BJoo% till Kueue is not em%t JB{

=deleteSKueue()$to%sort[C--]=$ BJ:dd node to to%sort arraJB

BJVelete all edes oin &ron node JB&or(i=1$i<=n$i--){

i&( adC[][i]==1 ){

adC[][i]=0$inde[i]=inde[i]1$i&(inde[i]==0)

insertSKueue(i)$

BJnd o& &orJB

BJnd o& @hileJB%rint&('/odes a&ter to%oloical sortin are *Dn')$&or(i=0$i<C$i--)

%rint&( '+d ',to%sort[i] )$%rint&('Dn')$

BJnd o& main()JBcreateSra%h(){

int i,maNSedes,oriin,destin$%rint&('nter numer o& vertices * ')$

scan&('+d',n)$

maNSedes=nJ(n1)$&or(i=1$i<=maNSedes$i--){

%rint&('nter ede +d(0 0 to Kuit)* ',i)$scan&('+d +d',oriin,destin)$

  i&((oriin==0) (destin==0))rea$

i&( oriin > n ?? destin > n ?? oriin<=0 ?? destin<=0){

%rint&('8nvalid edeRDn')$i$

else

adC[oriin][destin]=1$BJnd o& &orJB

BJnd o& createSra%h()JBdis%la(){

int i,C$ 34567 68/57 :7:3: ;4 /41:5 ? EE

Page 45: daa project file

8/9/2019 daa project file

http://slidepdf.com/reader/full/daa-project-file 45/49

&or(i=1$i<=n$i--){

&or(C=1$C<=n$C--)%rint&('+d',adC[i][C])$

%rint&('Dn')$

BJnd o& dis%la()JBinsertSKueue(int node){

i& (rear==G:U1)%rint&('Lueue 4verAo@Dn')$

else  {

i& (&ront==1) BJ8& Kueue is initiall em%t JB&ront=0$

rear=rear-1$

Kueue[rear] = node $ 

BJnd o& insertSKueue()JBdeleteSKueue(){

int delSitem$i& (&ront == 1 ?? &ront > rear) {

%rint&('Lueue OnderAo@Dn')$return $

else

{delSitem=Kueue[&ront]$&ront=&ront-1$return delSitem$

BJnd o& deleteSKueue() JBint inderee(int node){

int i,inSde=0$&or(i=1$i<=n$i--)

i&( adC[i][node] == 1 )

inSde--$return inSde$

BJnd o& inderee() JB

Output:-

nter ede !(0 0 to Kuit)*

 34567 68/57 :7:3: ;4 /41:5 ? E!

Page 46: daa project file

8/9/2019 daa project file

http://slidepdf.com/reader/full/daa-project-file 46/49

2nter ede !(0 0 to Kuit)*22nter ede !(0 0 to Kuit)*11

nter ede !(0 0 to Kuit)*2nter ede !(0 0 to Kuit)*Enter ede !(0 0 to Kuit)*21nter ede !(0 0 to Kuit)*2nter ede !(0 0 to Kuit)*12 he adCacenc matriNis *

 1 1 0 0 1 1 1 0 0 1 0 0 0 0 1 0/odes a&ter to%oloical sortin are*

2. Write a program in C to 0nd path matri "yWarshall9s algorithm.

#include<stdio.h> BB /umer o& vertices in the ra%h

#deQne Z E BJ VeQne 8nQnite as a lare enouh value. his value @ill eused  &or vertices not connected to each other JB#deQne 8/H 99999 BB : &unction to %rint the solution matriNvoid %rint6olution(lon dist[][Z])$ BB 6olves the all%airs shortest %ath %rolem usin HlodYarshall alorithmvoid AodYarshell (lon ra%h[][Z]){  BJ dist[][] @ill e the out%ut matriN that @ill Qnall havethe shortest  distances et@een ever %air o& vertices JB  lon dist[Z][Z], i, C, $ 

 34567 68/57 :7:3: ;4 /41:5 ? EI

Page 47: daa project file

8/9/2019 daa project file

http://slidepdf.com/reader/full/daa-project-file 47/49

  BJ 8nitialiPe the solution matriN same as in%ut ra%hmatriN. 4r  @e can sa the initial values o& shortest distances areased  on shortest %aths considerin no intermediate verteN. JB

  &or (i = 0$ i < Z$ i--)  &or (C = 0$ C < Z$ C--)  dist[i][C] = ra%h[i][C]$

  BJ :dd all vertices one one to the set o& intermediatevertices.  > Fe&ore start o& a iteration, @e have shortestdistances et@een all  %airs o& vertices such that the shortest distancesconsider onl the  vertices in set {0, 1, 2, .. 1 as intermediate vertices.

  > :&ter the end o& a iteration, verteN no. is added tothe set o&   intermediate vertices and the set ecomes {0, 1, 2, .. JB

  &or ( = 0$ < Z$ --)  {  BB ic all vertices as source one one  &or (i = 0$ i < Z$ i--)  {  BB ic all vertices as destination &or the

  BB aove %iced source  &or (C = 0$ C < Z$ C--)  {  BB 8& verteN is on the shortest %ath &rom  BB i to C, then u%date the value o& dist[i][C]  i& (dist[i][] - dist[][C] < dist[i][C])  dist[i][C] = dist[i][] - dist[][C]$        BB rint the shortest distance matriN

  %rint6olution(dist)$ BJ : utilit &unction to %rint solution JBvoid %rint6olution(lon dist[][Z]){lon C,i$  %rint& ('Hollo@in matriN sho@s the shortest distances'  ' et@een ever %air o& vertices Dn')$ 34567 68/57 :7:3: ;4 /41:5 ? E"

Page 48: daa project file

8/9/2019 daa project file

http://slidepdf.com/reader/full/daa-project-file 48/49

  &or (i = 0$ i < Z$ i--)  {  &or (C = 0$ C < Z$ C--)  {  i& (dist[i][C] == 8/H)

  %rint&('+"s', '8/H')$  else  %rint& ('+"d', dist[i][C])$    %rint&('Dn')$   BB driver %roram to test aove &unction

int main()

{  BJ et us create the &ollo@in @eihted ra%h  10  (0)>()  ? B?D  ! ? ?  ? ? 1  D?B ?  (1)>(2)  JB 

lon ra%h[Z][Z] = { {0, !, 8/H, 10,  {8/H, 0, , 8/H,  {8/H, 8/H, 0, 1,  {8/H, 8/H, 8/H, 0  $  BB rint the solution  AodYarshell(ra%h)$  return 0$

Output:

Hollo@in matriN sho@s the shortest distances et@een ever %air o& vertices  0 ! M 9  8/H 0 E

 34567 68/57 :7:3: ;4 /41:5 ? EM

Page 49: daa project file

8/9/2019 daa project file

http://slidepdf.com/reader/full/daa-project-file 49/49

  8/H 8/H 0 1  8/H 8/H 8/H 0

 ime Xom%leNit* 4(Z\)