Breadth-first Search Algorithm - C plus plus Code, Projects for Computer Programming. Biju Patnaik University of Technology, Rourkela
vishv
vishv8 October 2013

Breadth-first Search Algorithm - C plus plus Code, Projects for Computer Programming. Biju Patnaik University of Technology, Rourkela

PDF (47 KB)
2 pages
19Number of download
1000+Number of visits
100%on 1 votesNumber of votes
Description
Breadth-first Search Algorithm is used to search in a graph using two operations. Here you can find its working code. Related keywords are: Breadth-First Search Algorithm, Breadth-First Search, BFS, Search Algorithm, Alg...
20points
Download points needed to download
this document
Download the document
Preview2 pages / 2
Download the document
Microsoft Word - BFS algorithm

/* BFS algorithm in C++ */ #include<stdio.h> #define maxn 101 int Q[maxn]; int top ,back; int Node, Edge; char link[maxn][maxn]; char Fg[maxn]; int Path[maxn]; void Ini() { int i, j; for(i = 1; i<= Node; i++) { for(j = 1; j<= Node; j++) { link[i][j] = 0; } Path[i] = i; Fg[i] = 0; } } void Insert(int n) { Q[back++] = n; back %= maxn; } int Pop() { int n = Q[top++]; top %= maxn; return n; } int isEmpty() { if(top == back) return 0; return 1; } void PrintPath(int n) { if(n == Path[n]) { printf("%d",n); return; } PrintPath(Path[n]); printf(" %d",n); } int BFS(int s, int ter) { int u, v, i; Insert(s); Fg[s] = 1; while(isEmpty() == 1) {

docsity.com

v = Pop(); if(v == ter) { PrintPath(v); return 1; } for(i = 1; i<= Node; i++) { if(link[v][i] == 1 && Fg[i] == 0) { Fg[i] = 1; Insert(i); Path[i] = v; } } } return -1; } void main() { int i, u, v; scanf("%d%d",&Node,&Edge); Ini(); while(Edge--) { scanf("%d%d",&u,&v); link[u][v] = link[v][u] = 1; } scanf("%d%d",&u,&v); BFS(u,v); printf("\n"); } /* 4 5 1 2 2 3 1 3 3 4 2 4 1 4 */ 

docsity.com

comments (0)
no comments were posted
be the one to write the first!
Download the document