Directed Graph PDF
Directed Graph PDF
Directed Graph PDF
We have discussed one solution in The Two Water Jug Puzzle (two-water-jug-
puzzle.html)
In this post a BFS (breadth-first-traversal-for-a-graph.html) based solution is
discussed.
We run breadth first search on the states and these states will be created after
applying allowed operations and we also use visited map of pair to keep track
of states that should be visited only once in the search.This solution can also be
achieved using depth first search.
1 of 5 24/04/19, 1:40 am
https://thejeshvenkata.github.io/HTMLPages/water-jug-problem-u...
#include <bits/stdc++.h>
#define pii pair<int, int>
#define mp make_pair
using namespace std;
while (!q.empty()) {
2 of 5 24/04/19, 1:40 am
https://thejeshvenkata.github.io/HTMLPages/water-jug-problem-u...
// Driver code
int main()
{
int Jug1 = 4, Jug2 = 3, target = 2;
cout << "Path from initial state "
"to solution state :\n";
BFS(Jug1, Jug2, target);
return 0;
}
Output:
3 of 5 24/04/19, 1:40 am
https://thejeshvenkata.github.io/HTMLPages/water-jug-problem-u...
Practice Tags :
Article Tags :
Graph
BFS
Please write to us at [email protected] to report any issue with the
above content.
Recommended Posts:
4 of 5 24/04/19, 1:40 am
https://thejeshvenkata.github.io/HTMLPages/water-jug-problem-u...
graph.html)
Proof that Hamiltonian Path is NP-Complete (proof-hamiltonian-path-np-complete.html)
Number of Transpositions in a Permutation (number-of-transpositions-in-a-permutation.html)
Number of single cycle components in an undirected graph (number-of-simple-cyclic-components-in-
an-undirected-graph.html)
Post navigation
<< Previous Post (number-of-cyclic-elements-in-an-array-where-we-can-jump-
according-to-value.html) Next Post >> (degree-centrality-centrality-
measure.html)
(
Login
to Rate)
Mark as DONE
5 of 5 24/04/19, 1:40 am