Open In App

Channel Assignment Problem

Last Updated : 31 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

There are M transmitter and N receiver stations. Given a matrix that keeps track of the number of packets to be transmitted from a given transmitter to a receiver. If the (i; j)-th entry of the matrix is k, it means at that time the station i has k packets for transmission to station j. 
During a time slot, a transmitter can send only one packet and a receiver can receive only one packet. Find the channel assignments so that maximum number of packets are transferred from transmitters to receivers during the next time slot. 

Example: 

0 2 0
3 0 1
2 4 0

The above is the input format. We call the above matrix M. Each value M[i; j] represents the number of packets Transmitter ‘i’ has to send to Receiver ‘j’. The output should be:

The number of maximum packets sent in the time slot is 3
T1 -> R2
T2 -> R3
T3 -> R1 

Note that the maximum number of packets that can be transferred in any slot is min(M, N).

Algorithm: 

The channel assignment problem between sender and receiver can be easily transformed into Maximum Bipartite Matching(MBP) problem that can be solved by converting it into a flow network.

Step 1: Build a Flow Network 

There must be a source and sink in a flow network. So we add a dummy source and add edges from source to all senders. Similarly, add edges from all receivers to dummy sink. The capacity of all added edges is marked as 1 unit.

Step 2: Find the maximum flow. 

We use Ford-Fulkerson algorithm to find the maximum flow in the flow network built in step 1. The maximum flow is actually the maximum number of packets that can be transmitted without bandwidth interference in a time slot.

Implementation: 

Let us first define input and output forms. Input is in the form of Edmonds matrix which is a 2D array ‘table[M][N]‘ with M rows (for M senders) and N columns (for N receivers). The value table[i][j] is the number of packets that has to be sent from transmitter ‘i’ to receiver ‘j’. Output is the maximum number of packets that can be transmitted without bandwidth interference in a time slot. 
A simple way to implement this is to create a matrix that represents adjacency matrix representation of a directed graph with M+N+2 vertices. Call the fordFulkerson() for the matrix. This implementation requires O((M+N)*(M+N)) extra space. 

Extra space can be reduced and code can be simplified using the fact that the graph is bipartite. The idea is to use DFS traversal to find a receiver for a transmitter (similar to augmenting path in Ford-Fulkerson). We call bpm() for every applicant, bpm() is the DFS based function that tries all possibilities to assign a receiver to the sender. In bpm(), we one by one try all receivers that a sender ‘u’ is interested in until we find a receiver or all receivers are tried without luck. 

For every receiver we try, we do following: 

If a receiver is not assigned to anybody, we simply assign it to the sender and return true. If a receiver is assigned to somebody else say x, then we recursively check whether x can be assigned some other receiver. To make sure that x doesn’t get the same receiver again, we mark the receiver ‘v’ as seen before we make recursive call for x. If x can get other receiver, we change the sender for receiver ‘v’ and return true. We use an array maxR[0..N-1] that stores the senders assigned to different receivers. 

If bmp() returns true, then it means that there is an augmenting path in flow network and 1 unit of flow is added to the result in maxBPM().

Time and space complexity analysis: 

In case of bipartite matching problem, F ? |V| since there can be only |V| possible edges coming out from source node. So the total running time is O(m’n) = O((m + n)n). The space complexity is also substantially reduced from O ((M+N)*(M+N)) to just a single dimensional array of size M thus storing the mapping between M and N.

C++




#include <iostream>
#include <string.h>
#include <vector>
#define M 3
#define N 4
using namespace std;
 
// A Depth First Search based recursive function that returns true
// if a matching for vertex u is possible
bool bpm(int table[M][N], int u, bool seen[], int matchR[])
{
    // Try every receiver one by one
    for (int v = 0; v < N; v++)
    {
        // If sender u has packets to send to receiver v and
        // receiver v is not already mapped to any other sender
        // just check if the number of packets is greater than '0'
        // because only one packet can be sent in a time frame anyways
        if (table[u][v]>0 && !seen[v])
        {
            seen[v] = true; // Mark v as visited
 
            // If receiver 'v' is not assigned to any sender OR
            // previously assigned sender for receiver v (which is
            // matchR[v]) has an alternate receiver available. Since
            // v is marked as visited in the above line, matchR[v] in
            // the following recursive call will not get receiver 'v' again
            if (matchR[v] < 0 || bpm(table, matchR[v], seen, matchR))
            {
                matchR[v] = u;
                return true;
            }
        }
    }
    return false;
}
 
// Returns maximum number of packets that can be sent parallelly in 1
// time slot from sender to receiver
int maxBPM(int table[M][N])
{
    // An array to keep track of the receivers assigned to the senders.
    // The value of matchR[i] is the sender ID assigned to receiver i.
    // the value -1 indicates nobody is assigned.
    int matchR[N];
 
    // Initially all receivers are not mapped to any senders
    memset(matchR, -1, sizeof(matchR));
 
    int result = 0; // Count of receivers assigned to senders
    for (int u = 0; u < M; u++)
    {
        // Mark all receivers as not seen for next sender
        bool seen[N];
        memset(seen, 0, sizeof(seen));
 
        // Find if the sender 'u' can be assigned to the receiver
        if (bpm(table, u, seen, matchR))
            result++;
    }
 
    cout << "The number of maximum packets sent in the time slot is "
         << result << "\n";
 
    for (int x=0; x<N; x++)
        if (matchR[x]+1!=0)
            cout << "T" << matchR[x]+1 << "-> R" << x+1 << "\n";
    return result;
}
 
// Driver program to test above function
int main()
{
    int table[M][N] = {{0, 2, 0}, {3, 0, 1}, {2, 4, 0}};
    int max_flow = maxBPM(table);
    return 0;
}


Java




import java.util.*;
class GFG
{
static int M = 3;
static int N = 3;
 
// A Depth First Search based recursive function
// that returns true if a matching for vertex u is possible
static boolean bpm(int table[][], int u,
                   boolean seen[], int matchR[])
{
    // Try every receiver one by one
    for (int v = 0; v < N; v++)
    {
        // If sender u has packets to send
        // to receiver v and receiver v is not
        // already mapped to any other sender
        // just check if the number of packets is
        // greater than '0' because only one packet
        // can be sent in a time frame anyways
        if (table[u][v] > 0 && !seen[v])
        {
            seen[v] = true; // Mark v as visited
 
            // If receiver 'v' is not assigned to
            // any sender OR previously assigned sender
            // for receiver v (which is matchR[v]) has an
            // alternate receiver available. Since v is
            // marked as visited in the above line,
            // matchR[v] in the following recursive call
            // will not get receiver 'v' again
            if (matchR[v] < 0 || bpm(table, matchR[v],
                                      seen, matchR))
            {
                matchR[v] = u;
                return true;
            }
        }
    }
    return false;
}
 
// Returns maximum number of packets
// that can be sent parallelly in
// 1 time slot from sender to receiver
static int maxBPM(int table[][])
{
    // An array to keep track of the receivers
    // assigned to the senders. The value of matchR[i]
    // is the sender ID assigned to receiver i.
    // The value -1 indicates nobody is assigned.
    int []matchR = new int[N];
 
    // Initially all receivers are
    // not mapped to any senders
    Arrays.fill(matchR, -1);
 
    int result = 0; // Count of receivers assigned to senders
    for (int u = 0; u < M; u++)
    {
        // Mark all receivers as not seen for next sender
        boolean []seen = new boolean[N];
        Arrays.fill(seen, false);
 
        // Find if the sender 'u' can be
        // assigned to the receiver
        if (bpm(table, u, seen, matchR))
            result++;
    }
 
    System.out.println("The number of maximum packets" +
                       " sent in the time slot is " + result);
 
    for (int x = 0; x < N; x++)
        if (matchR[x] + 1 != 0)
            System.out.println("T" + (matchR[x] + 1) +
                               "-> R" + (x + 1));
    return result;
}
 
// Driver Code
public static void main(String[] args)
{
    int table[][] = {{0, 2, 0},
                     {3, 0, 1},
                     {2, 4, 0}};
     
    maxBPM(table);
}
}
 
// This code is contributed by Rajput-Ji


Python3




# A Depth First Search based recursive
# function that returns true if a matching
# for vertex u is possible
def bpm(table, u, seen, matchR):
    global M, N
     
    # Try every receiver one by one
    for v in range(N):
         
        # If sender u has packets to send to
        # receiver v and receiver v is not
        # already mapped to any other sender
        # just check if the number of packets
        # is greater than '0' because only one
        # packet can be sent in a time frame anyways
        if (table[u][v] > 0 and not seen[v]):
            seen[v] = True # Mark v as visited
 
            # If receiver 'v' is not assigned to any
            # sender OR previously assigned sender
            # for receiver v (which is matchR[v]) has
            # an alternate receiver available. Since
            # v is marked as visited in the above line, 
            # matchR[v] in the following recursive call
            # will not get receiver 'v' again
            if (matchR[v] < 0 or bpm(table, matchR[v],
                                       seen, matchR)):
                matchR[v] = u
                return True
    return False
 
# Returns maximum number of packets
# that can be sent parallelly in 1
# time slot from sender to receiver
def maxBPM(table):
    global M, N
     
    # An array to keep track of the receivers
    # assigned to the senders. The value of
    # matchR[i] is the sender ID assigned to
    # receiver i. The value -1 indicates nobody
    # is assigned.
 
    # Initially all receivers are not mapped
    # to any senders
    matchR = [-1] * N
 
    result = 0 # Count of receivers assigned to senders
    for u in range(M):
         
        # Mark all receivers as not seen
        # for next sender
        seen = [0] * N
 
        # Find if the sender 'u' can be assigned
        # to the receiver
        if (bpm(table, u, seen, matchR)):
            result += 1
 
    print("The number of maximum packets sent",
          "in the time slot is", result)
 
    for x in range(N):
        if (matchR[x] + 1 != 0):
            print("T", matchR[x] + 1, "-> R", x + 1)
    return result
 
# Driver Code
M = 3
N = 4
 
table = [[0, 2, 0], [3, 0, 1], [2, 4, 0]]
max_flow = maxBPM(table)
 
# This code is contributed by PranchalK


C#




// C# implementation of the above approach
using System;
     
class GFG
{
static int M = 3;
static int N = 3;
 
// A Depth First Search based recursive function
// that returns true if a matching for vertex u is possible
static Boolean bpm(int [,]table, int u,
                   Boolean []seen, int []matchR)
{
    // Try every receiver one by one
    for (int v = 0; v < N; v++)
    {
        // If sender u has packets to send
        // to receiver v and receiver v is not
        // already mapped to any other sender
        // just check if the number of packets is
        // greater than '0' because only one packet
        // can be sent in a time frame anyways
        if (table[u, v] > 0 && !seen[v])
        {
            seen[v] = true; // Mark v as visited
 
            // If receiver 'v' is not assigned to
            // any sender OR previously assigned sender
            // for receiver v (which is matchR[v]) has an
            // alternate receiver available. Since v is
            // marked as visited in the above line,
            // matchR[v] in the following recursive call
            // will not get receiver 'v' again
            if (matchR[v] < 0 || bpm(table, matchR[v],
                                     seen, matchR))
            {
                matchR[v] = u;
                return true;
            }
        }
    }
    return false;
}
 
// Returns maximum number of packets
// that can be sent parallelly in
// 1 time slot from sender to receiver
static int maxBPM(int [,]table)
{
    // An array to keep track of the receivers
    // assigned to the senders. The value of matchR[i]
    // is the sender ID assigned to receiver i.
    // The value -1 indicates nobody is assigned.
    int []matchR = new int[N];
 
    // Initially all receivers are
    // not mapped to any senders
    for(int i = 0; i < N; i++)
        matchR[i] = -1;
 
    int result = 0; // Count of receivers assigned to senders
    for (int u = 0; u < M; u++)
    {
        // Mark all receivers as not seen for next sender
        Boolean []seen = new Boolean[N];
 
        // Find if the sender 'u' can be
        // assigned to the receiver
        if (bpm(table, u, seen, matchR))
            result++;
    }
 
    Console.WriteLine("The number of maximum packets" +
                      " sent in the time slot is " + result);
 
    for (int x = 0; x < N; x++)
        if (matchR[x] + 1 != 0)
            Console.WriteLine("T" + (matchR[x] + 1) +
                              "-> R" + (x + 1));
    return result;
}
 
// Driver Code
public static void Main(String[] args)
{
    int [,]table = {{0, 2, 0},
                    {3, 0, 1},
                    {2, 4, 0}};
     
    maxBPM(table);
}
}
 
// This code is contributed by 29AjayKumar


Javascript




<script>
 
let M = 3;
let N = 3;
 
// A Depth First Search based recursive function
// that returns true if a matching for vertex u is possible
function bpm(table,u,seen,matchR)
{
    // Try every receiver one by one
    for (let v = 0; v < N; v++)
    {
        // If sender u has packets to send
        // to receiver v and receiver v is not
        // already mapped to any other sender
        // just check if the number of packets is
        // greater than '0' because only one packet
        // can be sent in a time frame anyways
        if (table[u][v] > 0 && !seen[v])
        {
            seen[v] = true; // Mark v as visited
   
            // If receiver 'v' is not assigned to
            // any sender OR previously assigned sender
            // for receiver v (which is matchR[v]) has an
            // alternate receiver available. Since v is
            // marked as visited in the above line,
            // matchR[v] in the following recursive call
            // will not get receiver 'v' again
            if (matchR[v] < 0 || bpm(table, matchR[v],
                                      seen, matchR))
            {
                matchR[v] = u;
                return true;
            }
        }
    }
    return false;
}
 
// Returns maximum number of packets
// that can be sent parallelly in
// 1 time slot from sender to receiver
function maxBPM(table)
{
    // An array to keep track of the receivers
    // assigned to the senders. The value of matchR[i]
    // is the sender ID assigned to receiver i.
    // The value -1 indicates nobody is assigned.
    let matchR = new Array(N);
   
    // Initially all receivers are
    // not mapped to any senders
    for(let i=0;i<N;i++)
    {
        matchR[i]=-1;
    }
   
    let result = 0; // Count of receivers assigned to senders
    for (let u = 0; u < M; u++)
    {
        // Mark all receivers as not seen for next sender
        let seen = new Array(N);
        for(let i=0;i<N;i++)
        {
            seen[i]=false;
        }
         
   
        // Find if the sender 'u' can be
        // assigned to the receiver
        if (bpm(table, u, seen, matchR))
            result++;
    }
   
    document.write("The number of maximum packets" +
                   " sent in the time slot is " + result+"<br>");
   
    for (let x = 0; x < N; x++)
        if (matchR[x] + 1 != 0)
            document.write("T" + (matchR[x] + 1) +
                               "-> R" + (x + 1)+"<br>");
    return result;
}
 
// Driver Code
let table= [[0, 2, 0],
                     [3, 0, 1],
                     [2, 4, 0]];
maxBPM(table);
 
 
// This code is contributed by rag2127
 
</script>


Output

The number of maximum packets sent in the time slot is 3
T3-> R1
T1-> R2
T2-> R3

Time Complexity:O((M + N) * N), Where M and N is the number of senders and number of receivers respectively.

Space Complexity: O(M) , here M is the number of senders.



Previous Article
Next Article

Similar Reads

Hungarian Algorithm for Assignment Problem | Set 2 (Implementation)
Given a 2D array, arr of size N*N where arr[i][j] denotes the cost to complete the jth job by the ith worker. Any worker can be assigned to perform any job. The task is to assign the jobs such that exactly one worker can perform exactly one job in such a way that the total cost of the assignment is minimized. Example Input: arr[][] = {{3, 5}, {10,
7 min read
Quadratic Assignment Problem (QAP)
The Quadratic Assignment Problem (QAP) is an optimization problem that deals with assigning a set of facilities to a set of locations, considering the pairwise distances and flows between them. The problem is to find the assignment that minimizes the total cost or distance, taking into account both the distances and the flows. The distance matrix a
11 min read
Hungarian Algorithm for Assignment Problem | Set 1 (Introduction)
Let there be n agents and n tasks. Any agent can be assigned to perform any task, incurring some cost that may vary depending on the agent-task assignment. It is required to perform all tasks by assigning exactly one agent to each task and exactly one task to each agent in such a way that the total cost of the assignment is minimized. Example: You
15+ min read
Job Assignment Problem using Branch And Bound
Let there be N workers and N jobs. Any worker can be assigned to perform any job, incurring some cost that may vary depending on the work-job assignment. It is required to perform all jobs by assigning exactly one worker to each job and exactly one job to each agent in such a way that the total cost of the assignment is minimized. Let us explore al
15+ min read
Which is the best YouTube channel to Learn DSA?
In the expanding world of programming, it has become essential to master Data Structures and Algorithms (DSA). It's not an achievement, but also a necessary skill. YouTube has emerged as a platform, for self-paced learning offering channels dedicated to DSA tutorials. This blog aims to assist aspiring developers in finding a YouTube channel by exam
6 min read
Passing the Assignment
Rachel is submitting assignment from every student. She has given them a few minutes to arrange their assignments before submission. Just then Rahul remembers that his assignment is with Rohan. Rohan has to pass the assignment to Rahul. All the students are sitting in a straight line. He cannot pass the assignment in front of the teacher because th
11 min read
std::move in Utility in C++ | Move Semantics, Move Constructors and Move Assignment Operators
Prerequisites: lvalue referencervalue referenceCopy Semantics (Copy Constructor) References: In C++ there are two types of references- lvalue reference:An lvalue is an expression that will appear on the left-hand side or on the right-hand side of an assignment.Simply, a variable or object that has a name and memory address.It uses one ampersand (
11 min read
Range Minimum Query with Range Assignment
There is an array of n elements, initially filled with zeros. The task is to perform q queries from given queries[][] and there are two types of queries: Type 1 (i.e queries[i][0] == 1): Assign value val = queries[i][3] to all elements on the segment l = queries[i][1] to r = queries[i][2].Type 2 (i.e queries[i][0] == 2): Find the minimum on the seg
15+ min read
Segment Tree for Range Assignment and Range Sum Queries
Given an array of size N filled with all 0s, the task is to answer Q queries, where the queries can be one of the two types: Type 1 (1, L, R, X): Assign value X to all elements on the segment from L to R−1, andType 2 (2, L, R): Find the sum on the segment from L to R−1.Examples: Input: N = 5, Q = 3, queries[][] = {{1, 0, 3, 5}, {2, 1, 4}, {1, 2, 4,
15+ min read
Secretary Problem (A Optimal Stopping Problem)
The Secretary Problem also known as marriage problem, the sultan's dowry problem, and the best choice problem is an example of Optimal Stopping Problem. This problem can be stated in the following form: Imagine an administrator who wants to hire the best secretary out of n rankable applicants for a position. The applicants are interviewed one by on
12 min read
Article Tags :
Practice Tags :