|
| 1 | +// Get Path DFS |
| 2 | + |
| 3 | +#include <iostream> |
| 4 | +#include <vector> |
| 5 | +using namespace std; |
| 6 | + |
| 7 | +vector<int> Get_Path(int **edges, int n, int s, int d, bool *visited) |
| 8 | +{ |
| 9 | + if (s == d) |
| 10 | + { |
| 11 | + vector<int> ans; |
| 12 | + visited[s] = true; |
| 13 | + ans.push_back(s); |
| 14 | + return ans; |
| 15 | + } |
| 16 | + |
| 17 | + visited[s] = true; |
| 18 | + |
| 19 | + for (int i = 0; i < n; ++i) |
| 20 | + { |
| 21 | + if (edges[s][i] == 1 && !visited[i]) |
| 22 | + { |
| 23 | + vector<int> ans = Get_Path(edges, n, i, d, visited); |
| 24 | + |
| 25 | + if (!ans.empty()) |
| 26 | + { |
| 27 | + ans.push_back(s); |
| 28 | + return ans; |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + return {}; |
| 34 | +} |
| 35 | + |
| 36 | +int main() |
| 37 | +{ |
| 38 | + // KNOCKCAT \\ |
| 39 | +
|
| 40 | + int n, e; |
| 41 | + cout << "Enter number of vertices & edges : " << endl; |
| 42 | + cin >> n >> e; |
| 43 | + |
| 44 | + // for storing we need a 2d arary n*n; |
| 45 | + int **edges = new int *[n]; |
| 46 | + |
| 47 | + for (int i = 0; i < n; ++i) |
| 48 | + { |
| 49 | + edges[i] = new int[n]; |
| 50 | + for (int j = 0; j < n; ++j) |
| 51 | + edges[i][j] = 0; |
| 52 | + } |
| 53 | + |
| 54 | + for (int i = 0; i < e; ++i) // e edges |
| 55 | + { |
| 56 | + int f, s; // first vertex second vertes |
| 57 | + cout << "Enter edge from first vertex to second vertex" << endl; |
| 58 | + |
| 59 | + cin >> f >> s; |
| 60 | + edges[f][s] = 1; |
| 61 | + edges[s][f] = 1; |
| 62 | + } |
| 63 | + |
| 64 | + bool *visited = new bool[n]; |
| 65 | + |
| 66 | + for (int i = 0; i < n; ++i) |
| 67 | + visited[i] = false; |
| 68 | + |
| 69 | + int s, d; |
| 70 | + vector<int> ans; |
| 71 | + cout << "Enter source vertex & destination vertex for path " << endl; |
| 72 | + cin >> s >> d; |
| 73 | + |
| 74 | + ans = Get_Path(edges, n, s, d, visited); |
| 75 | + |
| 76 | + cout << "Path from " << s << " to " << d << endl; |
| 77 | + |
| 78 | + if (!ans.empty()) |
| 79 | + { |
| 80 | + for (auto i : ans) |
| 81 | + cout << i << " "; |
| 82 | + } |
| 83 | + else |
| 84 | + cout << "No Path" << endl; |
| 85 | + |
| 86 | + // deleting memory |
| 87 | + for (int i = 0; i < n; ++i) |
| 88 | + delete[] edges[i]; |
| 89 | + |
| 90 | + delete[] edges; |
| 91 | + |
| 92 | + delete[] visited; |
| 93 | + |
| 94 | + return 0; |
| 95 | +} |
| 96 | + |
| 97 | +/* |
| 98 | +
|
| 99 | +OUTPUT |
| 100 | +
|
| 101 | +PS E:\DOCUMENTS\C-Plus-Plus-Programming\Graph> cd "e:\DOCUMENTS\C-Plus-Plus-Programming\Graph\" ; if ($?) { g++ Get_Path_DFS.cpp -o Get_Path_DFS } ; if ($?) { .\Get_Path_DFS } |
| 102 | +Enter number of vertices & edges : |
| 103 | +4 4 |
| 104 | +Enter edge from first vertex to second vertex |
| 105 | +0 1 |
| 106 | +Enter edge from first vertex to second vertex |
| 107 | +0 3 |
| 108 | +Enter edge from first vertex to second vertex |
| 109 | +1 2 |
| 110 | +Enter edge from first vertex to second vertex |
| 111 | +2 3 |
| 112 | +Enter source vertex & destination vertex for path |
| 113 | +1 3 |
| 114 | +Path from 1 to 3 |
| 115 | +3 0 1 |
| 116 | +PS E:\DOCUMENTS\C-Plus-Plus-Programming\Graph> cd "e:\DOCUMENTS\C-Plus-Plus-Programming\Graph\" ; if ($?) { g++ Get_Path_DFS.cpp -o Get_Path_DFS } ; if ($?) { .\Get_Path_DFS } |
| 117 | +Enter number of vertices & edges : |
| 118 | +6 3 |
| 119 | +Enter edge from first vertex to second vertex |
| 120 | +5 3 |
| 121 | +Enter edge from first vertex to second vertex |
| 122 | +0 1 |
| 123 | +Enter edge from first vertex to second vertex |
| 124 | +3 4 |
| 125 | +Enter source vertex & destination vertex for path |
| 126 | +0 3 |
| 127 | +Path from 0 to 3 |
| 128 | +No Path |
| 129 | +PS E:\DOCUMENTS\C-Plus-Plus-Programming\Graph> |
| 130 | +
|
| 131 | +*/ |
0 commit comments