0% found this document useful (0 votes)
19 views3 pages

Task 4 in C

This document describes an algorithm to find the epsilon-closure of all states in a non-deterministic finite automaton (NFA) with epsilon transitions. It defines a state structure containing information like the state number, whether it is an epsilon state, its transitions, and number of transitions. It then presents a computeEpsilonClosure function that takes the NFA, a visited array, current state, and closure array to recursively find all states reachable by epsilon transitions from the current state. The main function reads in an NFA, calls computeEpsilonClosure on each state to find its epsilon-closure, and prints the results.

Uploaded by

cs docs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views3 pages

Task 4 in C

This document describes an algorithm to find the epsilon-closure of all states in a non-deterministic finite automaton (NFA) with epsilon transitions. It defines a state structure containing information like the state number, whether it is an epsilon state, its transitions, and number of transitions. It then presents a computeEpsilonClosure function that takes the NFA, a visited array, current state, and closure array to recursively find all states reachable by epsilon transitions from the current state. The main function reads in an NFA, calls computeEpsilonClosure on each state to find its epsilon-closure, and prints the results.

Uploaded by

cs docs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Finding ε – closure of all states of any given NFA with ε transition.

#include <stdio.h>

#include <stdbool.h>

#include <string.h>

#define MAX_STATES 100

// Structure to represent a state in the NFA

typedef struct {

int state;

bool epsilon;

int transitions[MAX_STATES];

int numTransitions;

} State;

// Function to compute the ε-closure of a state

void computeEpsilonClosure(State nfa[], bool visited[], int currentState, int closure[])

visited[currentState] = true; // Mark the current state as visited

closure[currentState] = 1; // Add the current state to the closure

// For each transition from the current state

for (int i = 0; i < nfa[currentState].numTransitions; i++) {

int nextState = nfa[currentState].transitions[i];

// If the transition is an ε transition and the next state hasn't been visited

if (nfa[currentState].epsilon && !visited[nextState]) {

computeEpsilonClosure(nfa, visited, nextState, closure);

int main() {

int numStates;

State nfa[MAX_STATES];

printf("Enter the number of states in the NFA: ");


scanf("%d", &numStates);

// Read the NFA

for (int i = 0; i < numStates; i++) {

printf("Enter details for State %d:\n", i);

nfa[i].state = i;

printf("Is State %d an epsilon state? (0 for no, 1 for yes): ", i);

scanf("%d", &nfa[i].epsilon);

printf("Enter the number of transitions for State %d: ", i);

scanf("%d", &nfa[i].numTransitions);

printf("Enter the transition states for State %d:\n", i);

for (int j = 0; j < nfa[i].numTransitions; j++)

{ scanf("%d", &nfa[i].transitions[j]);

printf("\n");

// Compute the ε-closure for each state

for (int i = 0; i < numStates; i++)

bool visited[MAX_STATES] = { false };

int closure[MAX_STATES] = { 0 };

computeEpsilonClosure(nfa, visited, i, closure);

printf("ε-Closure of State %d: ", i);

for (int j = 0; j < numStates; j++) {

if (closure[j]) {

printf("%d ", j);

printf("\n");

return 0;
}

You might also like