From 5d48ff86e0779d619a1236119da0506ba6cddf43 Mon Sep 17 00:00:00 2001 From: S4JN Date: Sun, 17 Sep 2023 22:33:26 +0530 Subject: [PATCH 1/2] 17 --- ...EPT17_Shortest_path_visiting_all_nodes.cpp | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 2023 Daily Challenges/September 2023 Challenges/SEPT17_Shortest_path_visiting_all_nodes.cpp diff --git a/2023 Daily Challenges/September 2023 Challenges/SEPT17_Shortest_path_visiting_all_nodes.cpp b/2023 Daily Challenges/September 2023 Challenges/SEPT17_Shortest_path_visiting_all_nodes.cpp new file mode 100644 index 0000000..102d11f --- /dev/null +++ b/2023 Daily Challenges/September 2023 Challenges/SEPT17_Shortest_path_visiting_all_nodes.cpp @@ -0,0 +1,81 @@ +class Solution { +public: + // Class to carry three values at a time + class tuple + { + public: + int node; // on which current node we are standing + int mask; // mask of that node + int cost; // cost of path explore by this node + tuple(int node, int mask, int cost) + { + this -> node = node; + this -> mask = mask; + this -> cost = cost; + } + }; + + + int shortestPathLength(vector>& graph) { + // total number of nodes present + int n = graph.size(); // extracting size of graph + + + queue q; // queue of class tuple type + + // set to take care which path we have already visited + set> vis; + + int all = (1 << n) - 1; // if all nodes visited then + + // we don't know which node will give us shortest path so intially for all nodes we will store in our queue + for(int i = 0; i < n; i++) + { + int maskValue = (1 << i); // 2 ^ i + + tuple thisNode(i, maskValue, 1); // make a tuple for every nod + + q.push(thisNode); // push tuple into our queue + + vis.insert({i, maskValue}); // also update into our set + } + + // Implementing BFS + while(q.empty() == false) // until queue is not empty + { + tuple curr = q.front(); // extracting front tuple + q.pop(); // pop from queuu + + // if mask value becomes all, that means we have visited all of our nodes, so from here return cost - 1 + if(curr.mask == all) + { + return curr.cost - 1; + } + + // if not, start exploring in its adjcant + for(auto &adj: graph[curr.node]) + { + int bothVisitedMask = curr.mask; // current mask + + // we are moving from one node o anthor node + bothVisitedMask = bothVisitedMask | (1 << adj); + + // make tuple of this path + tuple ThisNode(adj, bothVisitedMask, curr.cost + 1); + + // if this path is not explored i.e + // if it is not present in our set then, + if(vis.find({adj, bothVisitedMask}) == vis.end()) + { + vis.insert({adj, bothVisitedMask}); // insert into set + + q.push(ThisNode); // also insert into queue + } + } + + } + + // return -1, but this condition will never come + return -1; + } +}; \ No newline at end of file From 3943cbdf01f0674987185f75674737bbdc79edb3 Mon Sep 17 00:00:00 2001 From: Srajan Acharya <93881028+S4JN@users.noreply.github.com> Date: Wed, 20 Sep 2023 22:28:47 +0530 Subject: [PATCH 2/2] SEPT17_Shortest_path_visiting_all_nodes.cpp --- .../SEPT17_Shortest_path_visiting_all_nodes.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/2023 Daily Challenges/September 2023 Challenges/SEPT17_Shortest_path_visiting_all_nodes.cpp b/2023 Daily Challenges/September 2023 Challenges/SEPT17_Shortest_path_visiting_all_nodes.cpp index 102d11f..c99086e 100644 --- a/2023 Daily Challenges/September 2023 Challenges/SEPT17_Shortest_path_visiting_all_nodes.cpp +++ b/2023 Daily Challenges/September 2023 Challenges/SEPT17_Shortest_path_visiting_all_nodes.cpp @@ -78,4 +78,7 @@ class Solution { // return -1, but this condition will never come return -1; } -}; \ No newline at end of file +}; + + // Time Complexity -> O(n + m), where n is the number of nodes and m is the number of edges in the graph. +// Space Complexity -> O(n * 2^n), primarily due to the space used by the vis set.