1+ #include < bits/stdc++.h>
2+ using namespace std ;
3+
4+ typedef struct Node {
5+ char key;
6+ struct Node * next;
7+ }node;
8+
9+ typedef struct Sta {
10+ char data[100 ];
11+ int top;
12+ }sta;
13+
14+ void Push (sta* s,char k){
15+ s->top =s->top +1 ;
16+ if (s->top >99 ){
17+ cout<<" Stack is full. Cannot push " <<k<<endl;
18+ return ;
19+ }
20+ s->data [s->top ]=k;
21+ }
22+
23+ char pop (sta* s){
24+ if (s->top ==-1 ){
25+ cout<<" Stack is empty. Cannot pop." <<endl;
26+ return ' .' ;
27+ }
28+ s->top =s->top -1 ;
29+ return s->data [s->top +1 ];
30+ }
31+
32+ node* CreateNode (char key){
33+ node* k=new node;
34+ k->next =NULL ;
35+ k->key =key;
36+ return k;
37+ }
38+
39+ void Read_Graph (int n,int e,node** List){
40+ char a,b;
41+ for (int i=0 ;i<e;i++){
42+ cin>>a>>b;
43+ int k=a-' a' ;
44+ node* m=CreateNode (b);
45+ if (List[k]==NULL ){
46+ List[k]=m;
47+ }
48+ else {
49+ node* temp=List[k];
50+ if (List[k]->next ==NULL || b<List[k]->key ){
51+ if (List[k]->key <b){
52+ List[k]->next =m;
53+ }
54+ else {
55+ m->next =List[k];
56+ List[k]=m;
57+ }
58+ }
59+ else {
60+ while (temp->next !=NULL && temp->next ->key <b){
61+ temp=temp->next ;
62+ }
63+ m->next =temp->next ;
64+ temp->next =m;
65+ }
66+ }
67+ }
68+ }
69+
70+ void DFS_Non_Recursive (int i,node** List,int * color,char * parent,int n){
71+ sta* s=new sta;
72+ string ss=" " ;
73+ s->top =-1 ;
74+ char d=' a' +i;
75+ Push (s,d);
76+ int p=0 ;
77+ node*temp=NULL ;
78+ while (s->top !=-1 ){
79+ p=0 ;
80+ i=s->data [s->top ]-' a' ;
81+ color[i]=1 ;
82+ temp=List[i];
83+ while (temp!=NULL ){
84+ if (color[temp->key -' a' ]==0 ){
85+ if (ss.length ()!=0 ){
86+ ss+=" , " ;
87+ }
88+ ss=ss+" (" +s->data [s->top ]+" ," +temp->key +" )" ;
89+ // cout<<"("<<s->data[s->top]<<","<<temp->key<<"), ";
90+ Push (s,temp->key );
91+ p=1 ;
92+ break ;
93+ }
94+ temp=temp->next ;
95+ }
96+ if (p==1 ){continue ;}
97+ else {
98+ color[i]=2 ;
99+ pop (s);
100+ }
101+ }
102+ cout<<ss;
103+ }
104+
105+ void DFS (node** List,int * color,char * parent,int n){
106+ for (int i=0 ;i<26 ;i++){
107+ color[i]=0 ;
108+ parent[i]=' .' ;
109+ }
110+ int p=0 ;
111+ for (int i=0 ;i<26 ;i++){
112+ if (color[i]==0 && List[i]!=NULL ){
113+ if (p){cout<<" , " ;}
114+ p=1 ;
115+ DFS_Recursive (i,List,color,parent,n);
116+ }
117+ }
118+ cout<<endl;
119+ }
120+
121+ void DFS_Enhanced_Non_Recursive (int i,node** List,int * color,char * parent,int * dd,int n){
122+ sta* s=new sta;
123+ s->top =-1 ;
124+ static int time=0 ;
125+ char d=' a' +i;
126+ Push (s,d);
127+ time++;
128+ dd[i]=time;
129+ int p=0 ;
130+ node*temp=NULL ;
131+ while (s->top !=-1 ){
132+ p=0 ;
133+ i=s->data [s->top ]-' a' ;
134+ color[i]=1 ;
135+ temp=List[i];
136+ while (temp!=NULL ){
137+ if (temp->key ==' .' ){}
138+ else if (color[temp->key -' a' ]==0 ){
139+ Push (s,temp->key );
140+ time++;
141+ dd[temp->key -' a' ]=time;
142+ p=1 ;
143+ temp->key =' .' ;
144+ break ;
145+ }
146+ else if (color[temp->key -' a' ]==1 ){
147+ cout<<" Back Edge: (" <<s->data [s->top ]<<" ," <<temp->key <<" )" <<endl;
148+ }
149+ else {
150+ if (dd[i]<dd[temp->key -' a' ]){
151+ cout<<" Forward Edge: (" <<s->data [s->top ]<<" ," <<temp->key <<" )" <<endl;
152+ }
153+ else {
154+ cout<<" Cross Edge: (" <<s->data [s->top ]<<" ," <<temp->key <<" )" <<endl;
155+ }
156+ }
157+ temp=temp->next ;
158+ }
159+ if (p==1 ){continue ;}
160+ else {
161+ time++;
162+ color[i]=2 ;
163+ pop (s);
164+ }
165+ }
166+ }
167+
168+ void DFS_Enhanced (node** List,int * color,char * parent,int n){
169+ int * d=new int [26 ];
170+ for (int i=0 ;i<26 ;i++){
171+ color[i]=0 ;
172+ d[i]=INT_MAX;
173+ parent[i]=' .' ;
174+ }
175+ for (int i=0 ;i<26 ;i++){
176+ if (color[i]==0 && List[i]!=NULL ){
177+ DFS_Enhanced_Recursive (i,List,color,parent,d,n);
178+ }
179+ }
180+ cout<<endl;
181+ }
182+
183+ int main (){
184+ int n,e;
185+ cin>>n>>e;
186+ int * color=new int [26 ];
187+ char * parent=new char [26 ];
188+ node** List=new node*[26 ];
189+ Read_Graph (n,e,List);
190+ string* ss;
191+ DFS (List,color,parent,n);
192+ cout<<" Tree edges:" ;
193+ DFS (List,color,parent,n);
194+ DFS_Enhanced (List,color,parent,n);
195+ return 0 ;
196+ }
0 commit comments