Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Take from: http://www.c-program-example.com/2011/10/c-program-to-implement-depth-first.html
- */
- #include<stdio.h>
- #include<conio.h>
- int a[20][20],reach[20],n;
- void dfs(int v)
- {
- int i;
- reach[v]=1;
- for(i=1;i<=n;i++)
- if(a[v][i] && !reach[i])
- {
- printf("\n %d->%d",v,i);
- dfs(i);
- }
- }
- void main()
- {
- int i,j,count=0;
- clrscr();
- printf("\n Masukkan jumlah vertex:");
- scanf("%d",&n);
- for(i=1;i<=n;i++)
- {
- reach[i]=0;
- for(j=1;j<=n;j++)
- a[i][j]=0;
- }
- printf("\n Masukkan matriks:\n");
- for(i=1;i<=n;i++)
- for(j=1;j<=n;j++)
- scanf("%d",&a[i][j]);
- dfs(1);
- printf("\n");
- for(i=1;i<=n;i++)
- {
- if(reach[i])
- count++;
- }
- if(count==n)
- printf("\n Graf terhubung");
- else
- printf("\n Graf tidak terhubung");
- getch();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement