Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <conio.h>
- #define size 50
- char visited_array[size];
- int visited_seq = -1, f = 0, r = 0, graph[50][50], n;
- char q[50];
- char vertex[26];
- void visit(char ch)
- {
- visited_seq++;
- visited_array[visited_seq] = ch;
- }
- int is_visit(char ch)
- {
- int i;
- for(i = 0 ; i <= visited_seq ; i++)
- {
- if(ch == visited_array[i])
- return 1; //Already visited
- }
- return 0; //Not visited before
- }
- void enqueue(char ch)
- {
- int s;
- s = (r+1) % (size+1);
- if(s == f)
- return;
- else{
- q[s] = ch;
- r = s;
- }
- }
- char dequeue()
- {
- if(f == r)
- return '\0';
- else
- {
- char ch;
- f = (f+1) % (size+1);
- ch = q[f];
- q[f] = '\0';
- return ch;
- }
- }
- void find_adjecency(char ch)
- {
- int i, j;
- for(i = 0 ; i < n ; i++)
- {
- if(ch == vertex[i])
- {
- for(j = 0 ; j < n ; j++)
- {
- if(graph[i][j] == 1)
- {
- printf("%c ", vertex[j]);
- if(is_visit(vertex[j]) == false)
- enqueue(vertex[j]);
- }
- }
- }
- }
- }
- void bfs()
- {
- char ch;
- int x;
- printf("\nFrom which vertex you want to start?(1-50): ");
- scanf("%d",&x);
- enqueue(vertex[x-1]);
- while(f!=r)
- {
- ch = dequeue();
- if(is_visit(ch) == 0)
- {
- visit(ch);
- }
- find_adjecency(ch);
- }
- }
- void displayVisitedArray()
- {
- int i;
- printf("\nVisited Sequence is: ");
- for(i=0; i<= visited_seq ; i++)
- printf("%c ", visited_array[i]);
- }
- void createGraph()
- {
- int i, j;
- for(i = 0 ; i < 26 ; i++)
- vertex[i] = i+65;
- printf("How many Vertex: ");
- scanf("%d", &n);
- for(i = 0 ; i < n ; i++)
- {
- for(j = 0 ; j < n ;j++)
- graph[i][j] = 0;
- }
- for(i = 0 ; i < n ; i++)
- {
- for(j = i+1 ; j < n ;j++)
- {
- printf("%c-%c exist: ", vertex[i], vertex[j]);
- scanf("%d", &graph[i][j]);
- if(graph[i][j] == 1)
- graph[j][i] = 1;
- }
- }
- }
- int main()
- {
- createGraph();
- bfs();
- displayVisitedArray();
- getch();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement