-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDepth first search
183 lines (148 loc) · 3.47 KB
/
Depth first search
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
Write a program to implement Depth First Traversal for a given graph
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node ** convmattolist(int **,int,char *);
void printlist(struct node **,int);
void printadjmatrix(int **,int);
void DFS(struct node **, int rows, int snode);
void DFSutil(struct node **, int snode, int * visited);
struct node
{
int nodeno;
int edgeweight;
struct node *link;
};
int main()
{
int **A;
int rows,edges,snode,enode,weight;
int i;
char directed[4];
struct node **list;
printf("Enter the number of nodes in the graph\n");
scanf("%d",&rows);
printf("Enter the number of edges in the graph\n");
scanf("%d",&edges);
printf("Is the graph directed(yes/no)\n");
scanf("%s",directed);
A = (int **)malloc(rows * sizeof(int *));
for(i=0;i<rows;i++)
*(A+i) = (int *) calloc(rows,sizeof(int));
for(i = 0;i<edges;i++)
{
printf("Enter the start node, end node and weight of edge no %d\n",i);
scanf("%d%d%d",&snode,&enode,&weight);
if(strcmp(directed,"yes") == 0)
*(*(A+snode)+enode) = weight;
else
{
*(*(A+snode)+enode) = weight;
*(*(A+enode)+snode) = weight;
}
}
list = convmattolist(A,rows,directed);
printadjmatrix(A,rows);
printlist(list,rows);
printf("Enter the starting node / vertex for depth first traversal\n");
scanf("%d", &snode);
printf("Depth First Traversal starting from node %d\n",snode);
DFS(list, rows, snode);
printf("\n");
return 0;
}
struct node ** convmattolist(int **A,int rows,char *dir)
{
int i,j;
struct node **list;
struct node *temp;
list = (struct node **)malloc(rows * sizeof(struct node *));
for(i=0;i<rows;i++)
{
*(list+i)=NULL;
for(j=0;j<rows;j++)
{
if(*(*(A+i)+j) != 0)
{
if(*(list+i) == NULL)
{
*(list+i) = (struct node *) malloc(sizeof(struct node));
temp = *(list+i);
temp->nodeno = j;
temp->edgeweight = *(*(A+i)+j);
temp->link = NULL;
}
else
{
temp = *(list+i);
while(temp->link != NULL)
temp = temp->link;
temp->link = (struct node *) malloc(sizeof(struct node));
temp = temp->link;
temp->nodeno = j;
temp->edgeweight = *(*(A+i)+j);
temp->link = NULL;
}
}
}
}
return list;
}
void printadjmatrix(int **A,int rows)
{
int i,j;
printf("Adjacency Matrix Representation:\n");
for(i=0;i<rows;i++)
{
for(j=0;j<rows;j++)
{
printf("%d ",*(*(A+i)+j));
}
printf("\n");
}
}
void printlist(struct node **list,int rows)
{
int i;
struct node * temp;
printf("Adjacency List Representation:\n");
for(i=0;i<rows;i++)
{
temp = *(list+i);
if(temp !=NULL)
printf("Node %d is connected to the following nodes:\n",i);
while(temp!=NULL)
{
printf("Node %d with edge weight %d\n", temp->nodeno,temp->edgeweight);
temp = temp->link;
}
}
}
void DFS(struct node ** Alist, int rows, int snode)
{
int i;
int * visited;
visited = (int *) calloc(rows,sizeof(int));
DFSutil(Alist, snode, visited);
for(i = 0; i<rows; i++){
if(visited[i] == 0){
DFSutil(Alist, i, visited);
}
}
}
void DFSutil(struct node ** Alist, int snode, int * visited)
{
printf("%d ",snode);
visited[snode] = 1;
struct node *t = Alist[snode];
while(t!=NULL){
if(visited[t->nodeno] == 0){
printf("%d ",t->nodeno);
visited[t->nodeno] = 1;
t = Alist[t->nodeno];
}
else{
t = t->link;
}
}
}