From aac69ece0689cb40e4392d8b23d98100566c702b Mon Sep 17 00:00:00 2001 From: Abhilasha Srivastava Date: Sat, 15 Jan 2022 09:11:03 +0000 Subject: [PATCH] Update 4-1- Route between Nodes In the current node, if there is a cycle, the nodes are added twice i.e. we add duplicate nodes to the queue --- Ch 4. Trees and Graphs/4-1-Route-Between-Nodes.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Ch 4. Trees and Graphs/4-1-Route-Between-Nodes.cpp b/Ch 4. Trees and Graphs/4-1-Route-Between-Nodes.cpp index 9984685..65425b2 100644 --- a/Ch 4. Trees and Graphs/4-1-Route-Between-Nodes.cpp +++ b/Ch 4. Trees and Graphs/4-1-Route-Between-Nodes.cpp @@ -34,7 +34,7 @@ bool Graph::isRoute(int x, int y){ queue q; q.push(x); - + visited[x]= true; while(!q.empty()){ int curr = q.front(); if (curr == y) @@ -42,13 +42,13 @@ bool Graph::isRoute(int x, int y){ return true; } q.pop(); - visited[curr]= true; int n_size =adj[curr].size(); for (int i = 0; i < n_size; ++i) { if (!visited[adj[curr][i]]) { q.push(adj[curr][i]); + visited[adj[curr][i]] = true; // we don't want to re-add the elemenets in case of a cycle } } }