forked from Srajan-Jaiswal/Minor-II-Project
-
Notifications
You must be signed in to change notification settings - Fork 1
/
IDDFS.cpp
95 lines (95 loc) · 1.97 KB
/
IDDFS.cpp
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
#include <bits/stdc++.h>
using namespace std;
vector<int> res;
class Graph
{
int V;
unordered_map<int, vector<pair<int, int>>> adjlst;
public:
Graph(int V)
{
}
void addEdge(int v, int w, int wt , bool bidrc)
{
adjlst[v].push_back(make_pair(w,wt));
if(bidrc){
adjlst[w].push_back(make_pair(v,wt));
}
}
bool DLS(int src, int target, int limit){
if (src == target){
return true;
}
if (limit <= 0){
return false;
}
for (auto i : adjlst[src])
if (DLS(i.first, target, limit-1) == true){
res.push_back(src);
return true;
}
return false;
}
bool IDDFS(int src, int target, int max_depth){
for (int i = 0; i <= max_depth; i++)
if (DLS(src, target, i) == true){
return true;
}
return false;
}
};
int main()
{
clock_t tStart = clock();
int n, e, a, b, c;
int source, target;
cout << "Enter the no. of vertices:";
cin >> n;
cout << "Enter the no. of edges:";
cin >> e;
Graph g(n);
cout << "Enter the list of edges in the format : v1 v2 weight:";
for (int i = 0; i < e; i++)
{
cin >> a >> b >> c;
g.addEdge(a, b, c,true);
}
cout << "Enter the source vertex:";
cin >> source;
cout << "Enter the target vertex:";
cin >> target;
int max_depth;
cout << "Enter the max depth:";
cin>>max_depth;
cout << endl;
res.push_back(source);
if(g.IDDFS(target, source,max_depth)){
cout<<"True"<<endl;
cout<<"Path is : ";
for(auto it : res){
cout<<it<<"->";
}
}
else{
cout<<"False"<<endl;
}
cout << endl;
cout << "Time taken: ";
double ans = (double)(clock() - tStart) / CLOCKS_PER_SEC;
cout << ans;
return 0;
}
/*
10
9
0 1 12
0 2 8
0 3 7
1 4 8
1 5 7
2 6 6
2 7 5
3 8 4
3 9 3
1 9
*/