Sie sind auf Seite 1von 11

/* Breadth First Search, Depth First Search, Iterative Deepening Search, Depth Limited Search */

#include<stdio.h> #include<stdlib.h> #include<malloc.h> #include<string.h> int n; //n no of nodes of the tree int noNode; //no of nodes of the tree at each point of time int **graph; char **name; int n; int no_of_edge = 0; int no_of_node = 0; int *level; int max_level=-1; int name_search(char *nam) { int i; for(i=0;i<no_of_node;i++) if(strcmp(nam,name[i])==0) return i; strcpy(name[no_of_node],nam); no_of_node++; return no_of_node-1; } void node_level(int s,int d) { level[d] = level[s]+1; // level of d will be the level of parent s, +1 if(level[d]>max_level) max_level = level[d]; } void addAdjacent(int node,int **arr,int *ptr) /* add the adjacent nodes of the node 'node' into arr node => removed node arr => queue in case of Q, stack in case of S ptr => rear in case of Q, top in case of S */ { int i, j; for(i=node+1;i<n;i++) if(graph[node][i] == 1) (*arr)[(++ *ptr)] = i; } int *searchPathBFS; int *searchPathDFS; int *searchPathId; int insptr; /* for Queue (BFS) */ int *queue; int front, rear; //traversal path for BFS //traversal path for DFS //insert ptr for searchPath(B/D)FS array

int isEmptyQ() /* returns 1 if Q is empty; else returns 0 */ { if(front > rear) return 1; else return 0; } void insert(int no) { queue[++rear] = no; } int rem() { int del; del = queue[front]; front++; return del; // returning the index of the removed node } void bfs(int searchNode) { int i, j; int searchFlag; int deleted; front = 0; rear = -1; queue = (int*)malloc(n * sizeof(int)); searchPathBFS = (int*)malloc(n * sizeof(int)); for(i=0;i<n;i++) queue[i] = searchPathBFS[i] = -1; searchFlag = 0; insptr = -1; queue[++rear] = 0; do { for(i=front;i<=rear&&searchFlag==0;i++) { deleted = rem(); searchPathBFS[++insptr] = deleted; if(searchNode == deleted) { searchFlag = 1; break; } else addAdjacent(deleted,&queue,&rear); } } while(!isEmptyQ()&&searchFlag==0); if(searchFlag == 1) { printf("Search successful\n"); } else { printf("Search unsuccessful\n"); } }

//searchFlag = 1 if search is successful // deleted node index

/* for Stack (DFS) */ int *stack; int top = -1; int isEmptyS() /* returns 1 if S is empty; else returns 0 */ { if(top==-1) return 1; else return 0; } int pop() { return stack[top--]; } void push(int n) { stack[++top] = n; } void dfs(int searchNode) { int i, j; int searchFlag; int deleted;

//searchFlag = 1 if search is successful // deleted node index

stack = (int*)malloc(n * sizeof(int)); searchPathDFS = (int*)malloc(n * sizeof(int)); for(i=0;i<n;i++) stack[i] = searchPathDFS[i] = -1; searchFlag = 0; insptr = -1; stack[++top] = 0; //root is inserted in the S do { for(i=0;i<=top;i++) { deleted = pop(); searchPathDFS[++insptr] = deleted; if(searchNode == deleted) { searchFlag = 1; break; } else addAdjacent(deleted,&stack,&top); } } while(searchFlag==0 && !isEmptyS()); if(searchFlag == 1) { printf("Search successful\n"); } else { printf("Search unsuccessful\n"); } }

void id(int searchNode) { int i, j; int searchFlag; int deleted; int k;

//searchFlag = 1 if search is successful // deleted node index

stack = (int*)malloc(n * sizeof(int)); searchPathId = (int*)malloc(n * sizeof(int)); for(i=0;i<n;i++) stack[i] = searchPathId[i] = -1; top = -1; searchFlag = 0; insptr = -1; stack[++top] = 0;

//root is inserted in the S

for(k=0;k<=max_level;k++) { top = -1; searchFlag = 0; insptr = -1; stack = (int*)malloc(n * sizeof(int)); searchPathId = (int*)malloc(n * sizeof(int)); for(i=0;i<n;i++) stack[i] = searchPathId[i] = -1; stack[++top] = 0; //root is inserted in the S while(top>=0) { deleted = pop(); searchPathId[++insptr] = deleted; if(searchNode == deleted) { searchFlag = 1; break; } else { if(level[deleted]<k) addAdjacent(deleted,&stack,&top); } } printf("Upto level: %d:\n",k); for(i=0;i<=insptr;i++) printf("%d ",searchPathId[i]); printf("\n"); } if(searchFlag == 1) { printf("Search successful\n"); } else { printf("Search unsuccessful\n"); } }

void dls(int searchNode) { int i, j; int searchFlag; int deleted; int k; int limit;

//searchFlag = 1 if search is successful // deleted node index

stack = (int*)malloc(n * sizeof(int)); searchPathId = (int*)malloc(n * sizeof(int)); for(i=0;i<n;i++) stack[i] = searchPathId[i] = -1; printf("Enter the limit upto which the search will be done: "); scanf("%d",&limit); top = -1; searchFlag = 0; insptr = -1; stack[++top] = 0; //root is inserted in the S top = -1; searchFlag = 0; insptr = -1; stack = (int*)malloc(n * sizeof(int)); searchPathId = (int*)malloc(n * sizeof(int)); for(i=0;i<n;i++) stack[i] = searchPathId[i] = -1; stack[++top] = 0; //root is inserted in the S while(top>=0) { deleted = pop(); searchPathId[++insptr] = deleted; if(searchNode == deleted) { searchFlag = 1; break; } else { if(level[deleted]<limit) addAdjacent(deleted,&stack,&top); } } printf("DLS traversal path:\n"); for(i=0;i<=insptr;i++) printf("%d ",searchPathId[i]); printf("\n"); if(searchFlag == 1) { printf("Search successful\n"); } else { printf("Search unsuccessful\n"); } }

void init_graph() { int i, j; int namptr = 0; char src[10], dest[10]; int source, desti; printf("Enter the number of nodes: "); scanf("%d",&n); name = (char**)malloc(sizeof(char *)*n); for(i=0;i<n;i++) name[i] = (char*)malloc(sizeof(char)*10); graph = (int**)malloc(sizeof(int*)*n); for(i=0;i<n;i++) graph[i] = (int*)calloc(n,sizeof(int)*n); level = (int*)malloc(sizeof(int)*n); level[0] = 0; printf("Enter the edges of the graph:\n"); for(i=0;i<2*n-1;i++) { printf("Enter the source: (-1 if completed): "); scanf("%s",src); if(strcmp(src,"-1")==0) break; source = name_search(src); printf("Enter the destination: "); scanf("%s",dest); desti = name_search(dest); node_level(source,desti); graph[source][desti] = graph[desti][source] = 1; no_of_edge++; } printf("The adjacency matrix of the input graph is as follows:\n"); for(i=0;i<n;i++) { for(j=0;j<n;j++) printf("%d\t",graph[i][j]); printf("\n"); } printf("Level of nodes:\n"); for(i=0;i<no_of_node;i++) printf("%s -> %d\n",name[i],level[i]); printf("Max Level: %d\n",max_level); }

int main() { int g, i, ch; init_graph(); printf("Enter the goal node to search the path: "); scanf("%d",&g); do { printf("1-> BFS\n"); printf("2-> DFS\n"); printf("3-> ID\n"); printf("4-> DLS\n"); printf("5-> New Goal Node\n"); printf("Enter your choice: "); scanf("%d",&ch); switch(ch) { case 1: bfs(g); printf("\nSearch path for BFS: Goal node: %d\n",g); for(i=0;i<=insptr;i++) printf("%d ",searchPathBFS[i]); printf("\n"); break; case 2: dfs(g); printf("\nSearch path for DFS: Goal node: %d\n",g); for(i=0;i<=insptr;i++) printf("%d ",searchPathDFS[i]); printf("\n"); break; case 3: id(g); break; case 4: dls(g); break; case 5: printf("Enter the goal node to search the path: "); scanf("%d",&g); break; } } while(ch>=1&&ch<=5); return 0; }

/* Uniform Cost Search */


#include<stdio.h> #include<string.h> #include<malloc.h> #define TRUE 1 #define FALSE 0 char **name; int **graph; int n; int start, goal; int *pr_q_index; //contain the node's index int *pr_q_cost; //contain the cost int *parent; //contain the parent's index int rear=-1,front=0; int no_of_edge = 0; int no_of_node = 0; //no of added nodes(traversed node) int parent_cost=0; void q_insert(int no, int par, int co) /* index of the node, its parent, cost */ { int i,j; if(rear>=front) { for(i=front;i<=rear&&pr_q_cost[i]<=co+parent_cost;i++) ; for(j=rear;j>=i&&pr_q_cost[i]>=co+parent_cost;j--) { pr_q_cost[j+1] = pr_q_cost[j]; parent[j+1] = parent[j]; pr_q_index[j+1] = pr_q_index[j]; } if(i<j) { pr_q_cost[j] = co+parent_cost; parent[j] = par; pr_q_index[j] = no; } else { pr_q_cost[i] = co+parent_cost; parent[i] = par; pr_q_index[i] = no; } rear++; } else { ++rear; pr_q_cost[rear] = co; parent[rear] = par; pr_q_index[rear] = no; } }

int q_remove() { int del; if(front<=rear) { front++; del = pr_q_index[front-1]; parent_cost = pr_q_cost[front-1]; return del; } } int name_search(char *nam) { int i; for(i=0;i<no_of_node;i++) if(strcmp(nam,name[i])==0) return i; strcpy(name[no_of_node],nam); no_of_node++; return no_of_node-1; } void init_graph() { int i, j; int namptr = 0; char src[10], dest[10]; int source, desti, cost; printf("Enter the number of nodes: "); scanf("%d",&n); name = (char**)malloc(sizeof(char *)*n); for(i=0;i<n;i++) name[i] = (char*)malloc(sizeof(char)*10); graph = (int**)malloc(sizeof(int*)*n); for(i=0;i<n;i++) graph[i] = (int*)calloc(n,sizeof(int)*n); pr_q_index = (int*)malloc(sizeof(int)*2*n); pr_q_cost = (int*)malloc(sizeof(int)*2*n); parent = (int*)malloc(sizeof(int)*2*n); printf("Enter the edges of the graph:\n"); for(i=0;i<2*n-1;i++) { printf("Enter the source: (-1 if completed): "); scanf("%s",src); if(strcmp(src,"-1")==0) break; source = name_search(src); printf("Enter the destination: "); scanf("%s",dest); desti = name_search(dest); printf("Enter the cost: "); scanf("%d",&cost); graph[source][desti] = graph[desti][source] = cost; no_of_edge++; } printf("The adjacency matrix of the input graph is as follows:\n"); for(i=0;i<n;i++) { for(j=0;j<n;j++) printf("%d\t",graph[i][j]); printf("\n"); } }

void ucs() { int i, j, k; int node; char str[10], gol[10]; int found_flag = FALSE; printf("Enter the staring node: "); scanf("%s",str); start = name_search(str); printf("Enter the goal node: "); scanf("%s",gol); goal = name_search(gol); i = start; q_insert(start,-1,0); do { i = q_remove(); if(i == goal) { found_flag = TRUE; break; } for(j=i;j<n;j++) { if(graph[i][j]>0) { q_insert(j,i,graph[i][j]); } } printf("\n"); } while(found_flag==FALSE); if(found_flag==TRUE) { printf("\nCost:%d\n",pr_q_cost[i]); printf("Found goal\n"); } } int main() { init_graph(); ucs(); return 0; }

OUTPUT
Enter the number of nodes: 7 Enter the edges of the graph: Enter the source: (-1 if completed): A Enter the destination: B Enter the cost: 2 Enter the source: (-1 if completed): A Enter the destination: C Enter the cost: 1 Enter the source: (-1 if completed): A Enter the destination: D Enter the cost: 3 Enter the source: (-1 if completed): B Enter the destination: E Enter the cost: 3 Enter the source: (-1 if completed): C Enter the destination: F Enter the cost: 8 Enter the source: (-1 if completed): D Enter the destination: G Enter the cost: 11 Enter the source: (-1 if completed): E Enter the destination: G Enter the cost: 8 Enter the source: (-1 if completed): F Enter the destination: G Enter the cost: 3 Enter the source: (-1 if completed): -1 The adjacency matrix of the input graph is as follows: 0 2 1 3 0 0 0 2 0 0 0 3 0 0 1 0 0 0 0 8 0 3 0 0 0 0 0 11 0 3 0 0 0 0 8 0 0 8 0 0 0 3 0 0 0 11 8 3 0 Enter the staring node: A Enter the goal node: G Cost:12 Found goal

Das könnte Ihnen auch gefallen