forked from Sirsho29/DSA-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdfs.cpp
More file actions
74 lines (60 loc) · 1.28 KB
/
dfs.cpp
File metadata and controls
74 lines (60 loc) · 1.28 KB
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
#include <bits/stdc++.h>
using namespace std;
class Graph
{
public:
map<int, bool> visited;
map<int, list<int>> adj;
void addEdge(int v, int w);
void dfs(int v);
};
void Graph::addEdge(int v, int w)
{
adj[v].push_back(w);
}
void Graph::dfs(int v)
{
visited[v] = true;
cout << v << " ";
list<int>::iterator i;
for (i = adj[v].begin(); i != adj[v].end(); ++i)
if (!visited[*i])
dfs(*i);
}
int main()
{
srand(time(0));
int n = 10;
double time, t;
FILE *fp;
fp = fopen("q1_plot_c.txt", "w");
if(fp == NULL){
printf("ERROR Opening File.\n");
}
while (n <= 100000)
{
time = t = 0.0;
for (int k = 1; k <= 10; k++)
{
Graph g;
for (int i = 0; i < n; i++)
{
g.addEdge(0, i);
}
///MERGE SORT BEGINS
time = clock();
///START
g.dfs(rand() % k);
///END
time = clock() - time;
///MERGE SORT ENDS
t += ((double)time) / CLOCKS_PER_SEC;
}
time = t / 10;
printf("\nTIME:%d, %f\n\n", n, time);
fprintf(fp, "%d, %lf\n", n, time);
n *= 10;
}
fclose(fp);
return 0;
}