Implementing Water Supply Problem using Breadth First Search

Given N cities which are connected using N-1 roads. Between Cities [i, i+1], there exists an edge for all i from 1 to N-1.

The task is to set up a connection for water supply. Set the water supply in one city and water gets transported from it to other cities using road transport. Certain cities are blocked which means that water cannot pass through that particular city. Determine the maximum number of cities to which water can be supplied.

Input format:



  • The first line contains an integer >strong>N denoting the number of cities.
  • The next N-1 lines contain two space-separated integers u v denoting a road between
    city u and v.
  • The next line contains N space-separated integers where it is 1 if the ith city is
    blocked, else it is 0.

Examples:

Input :
4
1 2
2 3
3 4
0 1 1 0
Output :
2
Explanation : If city 1 is chosen, then water is supplied from
city 1 to 2. If city 4 is chosen, water is supplied from city 4 to 3
hence maximum of 2 cities can be supplied with water.

Input :
7
1 2
2 3
3 4
4 5
5 6
6 7
0 1 1 0 0 0 0
Output :
5
Explanation : If city 1 is chosen than water is supplied from
city 1 to 2 or if city 4 is chosen water is supplied from city 4 to
3, 5, 6 and 7 hence maximum of 5 cities are supplied with water.

Approach:

In this post a BFS based solution is discussed.

We run a breadth-first search on each city and check for two things: The city is not blocked and the city is not visited. If both these conditions return true then we run a breadth-first search from that city and count the number of cities up to which water can be supplied.

This solution can also be achieved using a depth-first search.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to solve water 
// supply problem using BFS
  
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
  
// Function to perform BFS
int bfsUtil(int v[], bool vis[], vector<int> adj[], 
                                            int src)
{
    // Mark current source visited
    vis[src] = true;
      
    queue<int> q; //Queue for BFS
    q.push(src); // Push src to queue
      
    int count = 0;
    while (!q.empty()) {
          
        int p = q.front();
          
        for (int i = 0; i < adj[p].size(); i++) {
              
            // When the adjacent city not visited and 
            // not blocked, push city in the queue.
            if (!vis[adj[p][i]] && v[adj[p][i]] == 0) {
                count++;
                vis[adj[p][i]] = true;
                q.push(adj[p][i]);
            }
              
            // when the adjacent city is not visited 
            // but blocked so the blocked city is
            // not pushed in queue
            else if (!vis[adj[p][i]] && v[adj[p][i]] == 1) {
                count++;
            }
        }
        q.pop();
    }
      
    return count + 1;
}
  
// Utility function to perform BFS
int bfs(int N, int v[], vector<int> adj[])
{
    bool vis[N + 1];
    int max = 1, res;
      
    // marking visited array false
    for (int i = 1; i <= N; i++)
        vis[i] = false;
          
    // Check for each and every city
    for (int i = 1; i <= N; i++) {
        // Checks that city is not blocked
        // and not visited.
        if (v[i] == 0 && !vis[i]) {
            res = bfsUtil(v, vis, adj, i);
            if (res > max) {
                max = res;
            }
        }
    }
      
    return max;
}
  
// Driver Code
int main()
{
    int N = 4; // Denotes the number of cities
    vector<int> adj[N + 1];
    int v[N + 1];
  
    // Adjacency list denoting road
    // between city u and v
    adj[1].push_back(2);
    adj[2].push_back(1);
    adj[2].push_back(3);
    adj[3].push_back(2);
    adj[3].push_back(4);
    adj[4].push_back(3);
  
    // array for storing whether ith
    // city is blocked or not
    v[1] = 0;
    v[2] = 1;
    v[3] = 1;
    v[4] = 0;
      
    cout<<bfs(N, v, adj);
      
    return 0;
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to solve water 
# supply problem using BFS
  
# Function to perform BFS
def bfsUtil(v, vis, adj, src):
      
    # Mark current source visited
    vis[src] = True
  
    # Queue for BFS    
    q = []
      
    # Push src to queue
    q.append(src)
      
    count = 0
    while (len(q) != 0):
        p = q[0]
          
        for i in range(len(adj[p])):
              
            # When the adjacent city not visited and 
            # not blocked, push city in the queue.
            if (vis[adj[p][i]] == False and v[adj[p][i]] == 0):
                count += 1
                vis[adj[p][i]] = True
                q.push(adj[p][i])
              
            # when the adjacent city is not visited 
            # but blocked so the blocked city is
            # not pushed in queue
            elif(vis[adj[p][i]] == False and v[adj[p][i]] == 1):
                count += 1
        q.remove(q[0])
      
    return count + 1
  
# Utility function to perform BFS
def bfs(N, v, adj):
    vis = [ 0 for i in range(N + 1)]
    mx = 1
      
    # marking visited array false
    for i in range(1, N + 1, 1):
        vis[i] = False
          
    # Check for each and every city
    for i in range(1, N + 1, 1):
          
        # Checks that city is not blocked
        # and not visited.
        if (v[i] == 0 and vis[i] == False):
            res = bfsUtil(v, vis, adj, i)
            if (res > mx):
                mx = res
  
    return mx
  
# Driver Code
if __name__ == '__main__':
    N = 4
      
    # Denotes the number of cities
    adj = [[] for i in range(N + 1)]
    v = [0 for i in range(N + 1)]
  
    # Adjacency list denoting road
    # between city u and v
    adj[1].append(2)
    adj[2].append(1)
    adj[2].append(3)
    adj[3].append(2)
    adj[3].append(4)
    adj[4].append(3)
  
    # array for storing whether ith
    # city is blocked or not
    v[1] = 0
    v[2] = 1
    v[3] = 1
    v[4] = 0
      
    print(bfs(N, v, adj))
  
# This code is contributed by Bhupendra_Singh

chevron_right


Output:

2

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : bgangwar59

Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.