Hi guys.
I have tried to write a code in C for a directed graph:
Code:
#include <stdio.h>
#include <stdlib.h>
#define VOL 20
/* Adjacency Matrix */
int source_vert, V, E, visited[VOL], graph[VOL][VOL];
void dfs(int i)
{
int j;
visited[i]=1;
printf("%d", i+1);
for(j=0;j<V;j++)
{
if(graph[i][j]==1&&visited[j]==0)
dfs(j);
}
}
int main()
{
int i, j, vertex1, vertex2;
printf("Directed graph\n");
printf("Please, enter the number of vertices:\n");
scanf("%d",&V);
printf("Please, enter the number of edges:\n");
scanf("%d", &E);
for(i=0;i<V;i++)
{
for(j=0;j<V;j++)
graph[i][j]=0;
}
/* Creating edges */
for(i=0;i<E;i++)
{
printf("Please, enter the edges(format: vertex1 vertex2):");
scanf("%d %d", &vertex1, &vertex2);
graph[vertex1-1][vertex2-1]=1;
}
for(i=0;i<V;i++)
{
for(j=0;j<V;j++){
printf("%d", graph[i][j]);
printf("\n");
}
printf("Enter the source vertex: \n");
scanf("%d", &source_vert);
dfs(source_vert-1);
}
return 0;
}
Something was wrong.
My adjacency matrix is not a proper matrix, but an array
Furthermore, I need to select a vertex, in order to randomly select a new neighbour for it.
Could you please help me? Thank you.
[/FONT]