Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
#include <stdio.h>
#include <malloc.h>
#define N 9
typedef enum {white, gray, black} Color;
typedef enum {FALSE, TRUE} bool;
typedef struct vertex {
char name;
Color clr;
struct vertex* pred; // index of previous vertex in the traversal
bool critical; //
bool hidden; // usefull in hidding the node
struct vertex* adjacent[N];//adjacent vertex as per DFS
int time;/*While used as time Counter in DFS, it is reused as the
dist counter in BFS*/
} Vertex;
/* adjacency matrix */
int Adj[N][N] = {
/*S B C D E F G H T */
{0, 1, 1, 0, 0, 0, 0, 0, 0}, // S
{1, 0, 0, 1, 1, 0, 0, 0, 0}, // B
{1, 0, 0, 0, 1, 0, 0, 0, 0}, // C
{0, 1, 0, 0, 0, 1, 0, 0, 0}, // D
{0, 1, 1, 0, 0, 1, 0, 0, 0}, // E
{0, 0, 0, 1, 1, 0, 1, 1, 0}, // F
{0, 0, 0, 0, 0, 1, 0, 1, 1}, // G
{0, 0, 0, 0, 0, 1, 1, 0, 1}, // H
{0, 0, 0, 0, 0, 0, 1, 1, 0}, // T
};
/*---common function---*/
void initMemory(void);// populate the vertex array with vertex
void init(void); // set the color and predecessor to starting conditions
void setName(void); // set the label of vertex
bool isUnreliableConnected(Vertex* u, Vertex* v,int Al);/*the third
parameter chooses which graph algorithm is used. BSF==0, DFS ==1*/
/*---BFS---*/
void enque(int data); // fifo queue : enque function
int deque(void); // fifo queue : deque function
void BFS(int u); // BFS algorithm.using the adjacency matrix.
/*---DFS---*/
void setAdjList(void);//converting the adjacency matrix into adjacent list for DFS
void DFS(Vertex* u); //DFS Algorithm
//A function to test if two vertex are reliably connected using DFS
if(isUnreliableConnected(V[0],V[8],0)==TRUE)
{
printf("\n(BFS)*is Unreliable*(BFS)\n\n");
}
else printf("\n(BFS)*|* Reliable *|*(BFS)\n\n");
void initMemory(void){
int i;
for(i = 0; i < N; i++){
V[i]=(Vertex*)malloc(sizeof(Vertex));
V[i]->hidden=FALSE;
V[i]->time=-1;
}
}
void init(void){
int i;
for(i = 0; i < N; i++){
V[i]->clr = white;
V[i]->pred = NULL;
V[i]->time =-1;
}
}
void setAdjList(void)//converting the adjacency matrix into adjacent list for DFS
{
int j,i,k;
for(i = 0; i < N; i++)
{ k=0;
for(j = 0; j < N; j++)
{
if(Adj[i][j]==1){
V[i]->adjacent[k]=V[j];
k++;
// printf("\n %d , %d , K %d",i , j,k);
}
}V[i]->adjacent[k]=NULL;
}
}
void setName(void)
{
int i;
char names [N+1]="SBCDEFGHT";
for(i = 0; i < N; i++){
V[i]->name=names[i];
}
}
printf("\n b = %c",(deque())->name);
printf("\n c = %c",(deque())->name);
printf("\n s = %c",(deque())->name);
printf("\n b = %c",(deque())->name);
printf("\n T = %c",(deque())->name);
enque(V[0]);
enque(V[1]);
enque(V[2]);
enque(V[0]);
enque(V[1]);
enque(V[8]);
printf("\n s = %c",(deque())->name);
printf("\n b = %c",(deque())->name);
printf("\n c = %c",(deque())->name);
printf("\n s = %c",(deque())->name);
printf("\n b = %c",(deque())->name);
printf("\n T = %c",(deque())->name);*/