Skip to content

Commit 4c0b22d

Browse files
committed
dfs added
1 parent 20adfe4 commit 4c0b22d

File tree

1 file changed

+140
-0
lines changed

1 file changed

+140
-0
lines changed

dfs.cpp

+140
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
#include <iostream>
2+
#include <limits.h>
3+
using namespace std;
4+
#define black 0
5+
#define grey 1
6+
#define white 2
7+
struct adjlistnode
8+
{
9+
int data;
10+
struct adjlistnode *next;
11+
};
12+
struct adjlist
13+
{
14+
struct adjlistnode *head;
15+
};
16+
struct graph
17+
{
18+
int vertices;
19+
struct adjlist* array;
20+
int *color;
21+
};
22+
struct adjlistnode* c_node(int v)
23+
{
24+
struct adjlistnode* node=new struct adjlistnode;
25+
node->data=v;
26+
node->next=NULL;
27+
return node;
28+
}
29+
struct graph* creategraph(int numv)
30+
{
31+
struct graph* g= new graph;
32+
g->vertices=numv;
33+
g->array=new struct adjlist[(numv)*sizeof(adjlist)];
34+
g->color=new int[numv*sizeof(int)];
35+
for(int i=0;i<numv;i++)
36+
{
37+
g->array[i].head=NULL;
38+
g->color[i]=white;
39+
}
40+
return g;
41+
}
42+
void addedge(struct graph* g,int v1,int v2)
43+
{
44+
struct adjlistnode* temp=c_node(v1);
45+
temp->next=g->array[v2].head;
46+
g->array[v2].head=temp;
47+
temp=c_node(v2);
48+
temp->next=g->array[v1].head;
49+
g->array[v1].head=temp;
50+
}
51+
void print(struct graph* g,int v)
52+
{
53+
int i;
54+
for(i=1;i<=v;i++)
55+
{
56+
struct adjlistnode *q=(struct adjlistnode*)malloc(sizeof(struct adjlistnode));
57+
q=g->array[i].head;
58+
cout<<"for node"<<i<<endl;
59+
while(q)
60+
{
61+
cout<<q->data;
62+
q=q->next;
63+
}
64+
cout<<endl;
65+
}
66+
}
67+
struct stack
68+
{
69+
int top;
70+
int len;
71+
int *array;
72+
};
73+
struct stack* createstack(int numv)
74+
{
75+
struct stack* s=new struct stack;
76+
s->top=-1;
77+
s->len=numv;
78+
s->array = new int[numv*sizeof(int)];
79+
return s;
80+
}
81+
int isEmpty(struct stack* stack) //checks if the stack is empty
82+
{
83+
return stack->top == -1;
84+
}
85+
int pop(struct stack* stack) //pops and removes the top element
86+
{
87+
if (isEmpty(stack))
88+
return INT_MIN;
89+
return stack->array[stack->top--];
90+
}
91+
int peek(struct stack* stack) /* fucntion returns the top without removing it*/
92+
{
93+
if (isEmpty(stack))
94+
return INT_MIN;
95+
return stack->array[stack->top];
96+
}
97+
void push(struct stack* stack, int item) // pushes the item at the top
98+
{
99+
stack->array[++stack->top] = item;
100+
}
101+
void dfs(struct graph* g,int startvertex,struct stack* s)
102+
{
103+
push(s,startvertex);
104+
g->color[startvertex]=grey;
105+
while(!isEmpty(s))
106+
{
107+
int u=peek(s);
108+
struct adjlistnode* v=g->array[u].head;
109+
while(v)
110+
{
111+
if(g->color[v->data]==white)
112+
dfs(g,v->data,s);
113+
else if(g->color[v->data]==grey && v->data!=u)
114+
{
115+
pop(s);
116+
g->color[u]=black;
117+
cout<<"cycle detected"<<endl;
118+
return;
119+
}
120+
v=v->next;
121+
}
122+
pop(s);
123+
}
124+
}
125+
int main()
126+
{
127+
struct graph* graph = creategraph(7);
128+
struct stack* s=createstack(7);
129+
addedge(graph, 1, 2);
130+
addedge(graph, 1, 5);
131+
addedge(graph, 2, 3);
132+
addedge(graph, 2, 6);
133+
addedge(graph, 3, 7);
134+
addedge(graph, 3, 4);
135+
addedge(graph, 5, 6);
136+
addedge(graph, 6, 7);
137+
print(graph,7);
138+
dfs(graph, 1,s);
139+
140+
}

0 commit comments

Comments
 (0)