Sei sulla pagina 1di 3

/*Task 1. By deleting the vertex F from all s - t are destroyed.

And it shows the separation of the Network.*/

//Task 2.

#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;
struct vertex* adjacent[N];//adjacent vertex as per DFS
int time;
} Vertex;

Vertex* V[N]; // array of all vertexes

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
};
int time = 0; // a global variable to account // adjacency matrix

void initMemory(void){
int i;
for(i = 0; i < N; i++){
V[i]=(Vertex*)malloc(sizeof(Vertex));
}
}
void init(void){
int i;
for(i = 0; i < N; i++){
V[i]->clr = white;
V[i]->pred = NULL;
}
}
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];
}
}

void DFS(Vertex u){


u.clr = gray;
int j;
Vertex* k=NULL;
for(j = 0; j < N; j++)
{ k=u.adjacent[j];
if(k==NULL){
j=N;
}else if(k->clr == white && k->hidden==FALSE)
{
k->pred = &u;
DFS(*k);
}
}
u.clr = black;
return ;
}

bool isUnreliableConnected(Vertex u, Vertex v){


bool answer=FALSE;
DFS(u);
if(v.pred!=NULL)
{
/*keep the list of path nodes and remove one and check.*/
Vertex* path[N];
int i=0;
Vertex* j= &v;
while(j->pred!=&u)
{j=j->pred;
path[i++]=j;
printf(" %c ->",path[i-1]->name);
}
printf("%c \nNodes on path %d \n",path[i-1]->name,i);
int k=0;
for(k=0;k<i-1;k++)
{
v.pred=NULL;
j=path[k];
init();
j->hidden=TRUE;
DFS(u);
j->hidden=FALSE;
if(v.pred==NULL)
{
printf("One critical node found at %c \n",j->name);
j->critical=TRUE;
answer=TRUE;
}else
{
printf("Not a critical node %c \n",j->name);
}

}
}
else
printf(" doesnt exist s-t path\n");
return answer;
}

int main() {
initMemory();
init();
setAdjList();
setName();
if(isUnreliableConnected(*V[0],*V[8])==TRUE){
printf("\n****is Unreliable****");}
else printf("\n*|*|*|* Reliable *|*|*|*");
}

Potrebbero piacerti anche