-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathep.c
54 lines (47 loc) · 1.25 KB
/
ep.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <stdio.h>
void dfs(int i, int n);
void reset_visited(int n);
int et[100][100]; // for storing epsilon transtions
int visited[100]; // wether a state is visited or not
int main()
{
int number_of_states, number_of_transistions;
printf("Enter the number of states: ");
scanf("%d", &number_of_states);
printf("Enter the number of transitions: ");
scanf("%d", &number_of_transistions);
// format: i symbol j (use e for epsilon)
printf("Enter the transitions: \n\n");
int m,n;
char symbol;
// get the epsilon closure for each state
for(int i=0;i<number_of_transistions;i++){
scanf("%d %c %d",&m, &symbol, &n);
if(symbol == 'e')
et[m][n] = 1;
}
// finding the epsilon closure
for(int i=1; i<=number_of_states; i++){
printf("\nEpsilon closure of state %d: ",i);
reset_visited(number_of_states);
dfs(i, number_of_states); printf("{ ");
for(int j=1;j<=number_of_states;j++){
if(visited[j])
printf("%d ", j);
} printf("}");
}
return 0;
}
void dfs(int v, int n) {
visited[v] = 1;
for(int i=1;i<=n;i++){
if(et[v][i] == 1 && visited[i] == 0){
dfs(i, n);
}
}
}
void reset_visited(int n) {
for(int i=1;i<=n;i++){
visited[i] = 0;
}
}