Archives
Trending
Support
Login
clear text
XML
Django
JavaScript
MATLAB
C
C++
C#
Python
SQL
Shell
Bash
Markdown
YAML
JSON
HTML
CSS
PHP
Java
Ruby
Go
Rust
Swift
Kotlin
Arduino
TypeScript
Perl
Autohotkey
Lua
SQF
R
Scala
Haskell
Groovy
Dart
Clojure
VB.NET
Objective-C
PowerShell
Bash
CoffeeScript
Verilog
#include
#include
#include
using namespace std; #define pb push_back class Graph{ int nodes; vector
*edges; void printAllPath(int src, int dest, bool *visited, int *path, int &pathIdx){ visited[src] = true; path[pathIdx] = src; pathIdx++; if(src == dest){ for(int i=0; i
nodes = vertex; edges = new vector
[vertex + 1]; } void addEdge(int src, int to){ edges[src].pb(to); } void printPath(int src, int to){ int pathIdx = 0; bool *visited = new bool[nodes](); int *path = new int[nodes](); cout << "Path : "; printAllPath(src, to, visited, path, pathIdx); } bool DLS(int src, int dest, int limit){ if(src == dest){ return true; } if(limit <= 0){ return false; } for(auto i=edges[src].begin(); i != edges[src].end(); i++){ if(DLS(*i, dest, limit - 1)){ return true; } } return false; } void IDDFS(int src, int dest, int maxDepth){ for(int i=0; i<=maxDepth; i++){ if(DLS(src, dest, i)){ cout << "Node found at level " << i << '!' << endl; printPath(src, dest); return; } cout << "No Intended node found from level 0 to level " << i << '!' << endl; } } }; int main(){ int edges, from, to, depth; cout << "How many edges ? "; cin >> edges; Graph graph(edges); cout << "How many levels are in the tree (root is level zero) ? "; cin >> depth; cout << "Enter " << edges << " edges (from-to) separated by a space:" << endl; while(edges--){ cin >> from >> to; graph.addEdge(from, to); } cout << "Enter source and destination node: "; cin >> from >> to; graph.IDDFS(from, to, depth); return 0; }
Mark as private (unlisted)
for 30 minutes
for 6 hours
for 1 day
for 1 week
for 1 month
for 1 year
everlasting (like CCCP)