Advertisement
hok00age

Contoh program untuk DFS

Oct 7th, 2012
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.77 KB | None | 0 0
  1. /*
  2. * Take from: http://www.c-program-example.com/2011/10/c-program-to-implement-depth-first.html
  3. */
  4. #include<stdio.h>
  5. #include<conio.h>
  6. int a[20][20],reach[20],n;
  7. void dfs(int v)
  8. {
  9.  int i;
  10.  reach[v]=1;
  11.  for(i=1;i<=n;i++)
  12.   if(a[v][i] && !reach[i])
  13.   {
  14.    printf("\n %d->%d",v,i);
  15.    dfs(i);
  16.   }
  17. }
  18. void main()
  19. {
  20.  int i,j,count=0;
  21.  clrscr();
  22.  printf("\n Masukkan jumlah vertex:");
  23.  scanf("%d",&n);
  24.  for(i=1;i<=n;i++)
  25.  {
  26.   reach[i]=0;
  27.   for(j=1;j<=n;j++)
  28.    a[i][j]=0;
  29.  }
  30.  printf("\n Masukkan matriks:\n");
  31.  for(i=1;i<=n;i++)
  32.   for(j=1;j<=n;j++)
  33.    scanf("%d",&a[i][j]);
  34.  dfs(1);
  35.  printf("\n");
  36.  for(i=1;i<=n;i++)
  37.  {
  38.   if(reach[i])
  39.    count++;
  40.  }
  41.  if(count==n)
  42.   printf("\n Graf terhubung");
  43.  else
  44.   printf("\n Graf tidak terhubung");
  45.  getch();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement