Skip to content

Instantly share code, notes, and snippets.

@myselfshravan
Created June 28, 2023 07:36
Show Gist options
  • Save myselfshravan/864cafc8393858b1534f473631171f53 to your computer and use it in GitHub Desktop.
Save myselfshravan/864cafc8393858b1534f473631171f53 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#define MAX_VERTICES 10
int cost[MAX_VERTICES][MAX_VERTICES], i, j, k, n, stk[MAX_VERTICES], top, v, visit[MAX_VERTICES], visited[MAX_VERTICES];
int main()
{
int m;
printf("Enter the number of vertices: ");
scanf("%d", &n);
printf("Enter the number of edges: ");
scanf("%d", &m);
printf("\nEDGES:\n");
for (k = 1; k <= m; k++)
{
scanf("%d %d", &i, &j);
cost[i][j] = 1;
}
printf("Enter the initial vertex to traverse from: ");
scanf("%d", &v);
printf("DFS ORDER OF VISITED VERTICES: ");
printf("%d ", v);
visited[v] = 1;
k = 1;
while (k < n)
{
for (j = n; j >= 1; j--)
{
if (cost[v][j] != 0 && visited[j] != 1 && visit[j] != 1)
{
visit[j] = 1;
stk[top] = j;
top++;
}
}
v = stk[--top];
printf("%d ", v);
k++;
visit[v] = 0;
visited[v] = 1;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment