Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

Pouring water.

We have three containers whose sizes are 10 pints, 7 pints, and 4 pints, respectively. The 7-pint and 4-pint containers start out full of water, but the 10-pint container is initially empty. We are allowed one type of operation: pouring the contents of one container into another, stopping only when the source container is empty or the destination container is full. We want to know if there is a sequence of pouring’s that leaves exactly 2 pints in the 7- or 4-pint container.

(a) Model this as a graph problem: give a precise definition of the graph involved and state the specific question about this graph that needs to be answered.

(b) What algorithm should be applied to solve the problem?

(c) Find the answer by applying the algorithm.

Short Answer

Expert verified

(a)Model of this as a graph problem by using breadth-firstis involved and states are shown below in the description.

(b) Algorithm-Breadth-firstsearch by using arbitrarily deep recursion with endless loops is used here.

(c) Answer is in recursive loops.

Step by step solution

01

Step 1: Breadth-First Search

In this question, the solution is provided by using breadth-first search and also by using arbitrarily deep recursion with going into endless loops.

Breadth First Search traverses the tree from level one to the end level in a horizontal order so it is also called level order traversal.

The queue is used as a data structure in thebreadth-first search.

Here start from any one of the nodes, by choosing it randomly and then start traversing by its adjacent unvisited vertex. After that mark, it visited. and stored the visited list in the queue.

02

Step 2: Model of a graph problem

  1. There are three containers whose sizes are 10 pints, 7 pints and 4 pints respectively. The 7 -pint and 4 -pint containers start out full of water, but the 10 pint container is initially empty. Since there are no marks on the containers, then pour the contents of one container into another and stop under the following conditions that are the source container is empty. And the other is the destination container is full. By using a directed graph as a data structure in which the vertices contain tuples where it shows its state.

Starts from the initial state or are said to be the source vertex and after that creates a vertex as a node which shows a possible next state. And connect all nodes and after that apply breadth-first search on the vertices and evaluate the shortest path.The nodes connected to the initial state or node: 7,0,4 and 4,7,0 as is shown below:

  • Pattern of each state or node:{10 -pint container, 7 -pint container, 4-pint container}
  • Initial state or node: 0,7,4.
  • Final state or node: 8,3,0.
03

BFS Algorithm

b)

In this question the solution is provided by using breadth first search and also by using arbitrarily deep recursion with going into endless loops.

The breadth first search is applied here each step are given as:

1). In stating each container is given as a node or vertex are of different sizes of ten, seven and four.

Let’s consider it as a node.

2). Traverse all the node which follows breadth first search.

3). Starting from the source vertex to the end vertex, when we reach to the final destination that is treat as the final answer.

4). The initial vertex as a(0,7,4)while traversing.

5).Start traversing by itsadjacent unvisited vertex. After that mark, it visited.

6).By using arbitrarily deep recursion with endless loops start filling container(0,7,4)whenever the source vertex is empty and the destination vertex is full.

And again, fill whenever the leaves exactly two pints in the 7 - or 4 -pint container.

04

Applying the algorithm

c)

In stating each container is given as a node or vertex are of different sizes of ten, seven and four. And let’s consider it as a node of each container.

start out full of water, but the 10-pint container is initially empty.

And type of operation:

a). Pouring the contents of one container into another.

b). Stopping only when the source container is empty or the destination container is full.

c). If there is a sequence of pourings that leaves exactly two pints in the 7 - or 4 -pint container.

Since there are no marks on the containers, then pour the contents of one container into another and stop under the following conditions that arethe source container is empty. And the other is the destination container is full. By usingdirected graph as a data structure in which the vertices contain tuples where it shows its state.

Let’s this scenario:

  • Pattern of each state or node: {10 -pint container, 7 -pint container, 4-pint container}
  • Initial state or node: (0,7,4).
  • Final state or node: 8,3,0.

Applying Breadth first search on the adjacency nodes which are near by the source vertex. And also apply arbitrarily deep recursion in simultaneously which applied on the endless loops. Traverse all the vertices of container which follows breadth first search. whose sizes are 10 pints, 7 pints, and 4 pints, respectively. The 7 -pint and 4 -pint containers start out full of water.

And the sequence of pouring’s that leaves exactly two pints in the 7 - or 4 -pint container then again starts filling the container.

So here the final result is in the recursive loops.

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

You are given tree T=(V,E) along with a designated root node rV. The parent of any node Vr, denoted p(V), is defined to be the node adjacent to v in the path from r to v . By convention, p(r)=r. For k>1, define pk(v)pk-1(pv)andp1(v)=p(v)(so pk(v)is the k th ancestor of v ). Each vertex v of the tree has an associated non-negative integer label l(v). Given a linear-time algorithm to update the labels of all the vertices T according to the following rule: lnew(v)=l(plvv).

Give an efficient algorithm that takes as input a directed acyclic graph G=V,E, and two vertices s,tV, and outputs the number of different directed paths from S to t in G.

For each node in an undirected graph, let twodegreeube the sum of the degrees of’s neighbors. Show how to compute the entire array of two degree. values in linear time, given a graph in adjacency list format

Rewrite the explore procedure (Figure 3.3) so that it is non-recursive (that is, explicitly use a stack). The calls to pre visit and post visit should be positioned so that they have the same effect as in the recursive procedure.

In the 2SAT problem, you are given a set of clauses, where each clause is the disjunction (OR) of two literals (a literal is a Boolean variable of or the negation of a Boolean variable). You are looking for a way to assign a valuetrueorfalseto each of the variables so that all clauses are satisfied- that is, there is at least one true literal in each clause. For example, here’s an instance of 2SAT:

x1x2¯x1¯x3¯x1x2x3¯x4x1¯x4

This instance has a satisfying assignment: set x1,x2,x3, and x4 totrue, false, false,andtrue,respectively.

  1. Are there other satisfying truth assignments of this 2SAT formula? If so, find them all.
  2. Give an instance of 2SAT with four variables, and with no satisfying assignment.

The purpose of this problem is to lead you to a way of solving 2SAT efficiently by reducing it to the problem of finding the strongly connected components of a directed graph. Given an instance l of 2SAT with n variables and m clauses, construct a directed graph GI=V,E as follows.

  • GIhas 2nnodes, one for each variable and its negation.
  • GIhas 2m edges: for each clause αβof l (where α,βare literals), G1has an edge from the negation of α to β, and one from the negation ofβ to α.

Note that the clause αβis equivalent to either of the implications α¯β or β¯α. In this sense, data-custom-editor="chemistry" GI records all implications in l .

(C). Carry out this construction for the instance of 2SAT given above, and for the instance you constructed in (b).

(d). Show that if GI has a strongly connected component containing both x and X¯ for some variable x , then l has no satisfying assignment.

(e). Now show the converse of (d): namely, that if none of GI’s strongly connected components contain both a literal and its negation, then the instance l must be satisfiable.(Hint: Assign values to the variables as follows: repeatedly pick a sink strongly connected component of GI. Assign valuetrueto all literals in the sink, assignfalseto their negations, and delete all of these. Show that this ends up discovering a satisfying assignment.)

(f). Conclude that there is a linear-time algorithm for solving 2SAT.

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.

Sign-up for free