Experiments
Experiments
AIM:
To convert the given NFA to DFA.
PROCEDURE:
Here is the step-by-step procedure for converting an NFA to a DFA:
Step 1: Input NFA Details
1. Take input for the number of states (n) in the NFA.
2. Take input for the number of symbols in the alphabet (alpha).
3. Construct the transition table for the NFA:
4. Epsilon-closure of a state q is the set of all states reachable from q using only epsilon
transitions.
5. Compute the epsilon-closure for each state in the NFA.
6. The initial state of the DFA is the epsilon-closure of the NFA’s start state.
7. Use a queue to process each new state
8. A DFA state is a final state if it contains any NFA final state.
9. Store all DFA states and transitions in a structured format.
10. Print the DFA transition table.
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#define MAX_STATES 100
#define MAX_ALPHABET 10
void printTransitionTable(int table[MAX_STATES][MAX_ALPHABET +
1][MAX_STATES], int n, int alpha) {
printf(" STATE/INPUT |");
char a = 'a';
for (int i = 0; i < alpha; i++) {
printf(" %c |", a++);
}
printf(" ^ \n\n");
for (int i = 0; i < n; i++) {
printf(" %d ", i);
for (int j = 0; j <= alpha; j++) {
printf(" | ");
for (int k = 0; k < MAX_STATES; k++) {
if (table[i][j][k] != -1) {
printf("%d ", table[i][j][k]);
}
}
}
printf("\n");
}
}
void closure(int s, int table[MAX_STATES][MAX_ALPHABET + 1][MAX_STATES], bool
visited[MAX_STATES], int *closureSet, int *count) {
if (!visited[s]) {
visited[s] = true;
closureSet[(*count)++] = s;
for (int i = 0; table[s][MAX_ALPHABET][i] != -1; i++) {
closure(table[s][MAX_ALPHABET][i], table, visited, closureSet, count);
}
}
}
int main() {
int n, alpha;
printf("************************* NFA to DFA *************************\n\n");
printf("Enter total number of states in NFA: ");
scanf("%d", &n);
printf("Enter number of elements in alphabet: ");
scanf("%d", &alpha);
int table[MAX_STATES][MAX_ALPHABET + 1][MAX_STATES];
memset(table, -1, sizeof(table));
for (int i = 0; i < n; i++) {
printf("For state %d:\n", i);
char a = 'a';
int y, yn;
for (int j = 0; j < alpha; j++) {
printf("Enter no. of output states for input %c: ", a++);
scanf("%d", &yn);
printf("Enter output states: ");
for (int k = 0; k < yn; k++) {
scanf("%d", &y);
table[i][j][k] = y;
}
}
printf("Enter no. of output states for input ^: ");
scanf("%d", &yn);
printf("Enter output states: ");
for (int k = 0; k < yn; k++) {
scanf("%d", &y);
table[i][alpha][k] = y;
}
}
printf("\n***** TRANSITION TABLE OF NFA *****\n");
printTransitionTable(table, n, alpha);
return 0;
}
OUTPUT: