Need some help trying to understand this code of matching for a graph

user2923535

The code can be found here: https://sites.google.com/site/indy256/algo/kuhn_matching2

More specifically, I'm not sure I understand what the int n2 is for and how it is being used.

Alex Pakka

Here, for bipartite graphs, n1 denotes number of vertices of the first set (partition) and n2 denotes number of vertices of the second set. E.g. you would have a set of workers and a set of tasks they would perform. In the example above, there are 2 workers (say John=0 and Bob=1) and three tasks (say, coding=0, QA=1, support=2). John can do coding and support. Bob can do only support. None of them can do QA (there is no g[i] == 1)

Then the algorithm follows Kuhn's proposal to find a maximum matching (do not confuse with maximal matching). In our example case, maximum matching has two edges (e.g. John->coding and Bob->support).

The algorithm above would not work for weighted bipartite graphs.

Update

To accommodate the input rules from the question, following needs to be done. Simpy ensure that in g[x]=y : 0 <= x < n1 and 0 <= y < n2

public static void main(String[] args) {
  Scanner sc = new Scanner(System.in);
  int n1 = sc.nextInt();
  int n2 = sc.nextInt();
  int m = sc.nextInt();
  LinkedList<Integer>[] g = new LinkedList[n1];
  for (int j = 0; j < n1; j++) {
    g[j] = new LinkedList<Integer>();
  }
  int i = 0;
  while(i != m){
    int v = sc.nextInt();
    int v2 = sc.nextInt();
    if(v>=1 && v<=n1) { 
        //v belongs in first set 
        g[v-1].add(v2-n1-1);
    }else if(v>=n1+1 && v<=n1+n2) { 
        //v belongs in the second set, v2 into the first
        g[v2-1].add(v-n1-1);
    }
    i++;
  }
  System.out.println(maxMatching(g, n2));
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Need help to understand code

From Dev

Need help to understand this code

From Dev

I need help trying to understand this piece of code about structures and pointers

From Dev

Need some help to understand recursion

From Dev

Need help to understand code in python

From Dev

Need Help to understand the piece of code

From Dev

Need help to understand code in python

From Dev

Perl print %hash - need some help to understand this

From Dev

I need some help on this code

From Dev

Need help to understand the code with pointers and arrays in c

From Dev

c# Need help to understand this code

From Dev

PHP - Need help to understand injected code

From Dev

CSV Parsing, trying to understand some code

From Java

I need some help diagnosing the problem with this code

From Java

need some help to vectorize this code for images

From Dev

I need some help to optimize a python code

From Dev

need some help understanding an aspect of this code

From Dev

I need some help improving this 8085 code

From Dev

Need help understanding some code sample?

From Dev

Trying to understand the regex. Need Help in understanding the regex

From Dev

Need help to understand LISP

From Dev

Gradle: need help to understand

From Dev

I am trying to implement this interface, but need some help:

From Dev

Need help to understand this code to find the position of 1's in a given number

From Dev

I don't understand JSON. Need some help clearing up some things

From Dev

Need some help on RegEx

From Dev

Need Help Converting some F# code to C#

From Dev

Need some help regarding an if-else piece of code

From Dev

Need help to understand LPeg and PEGs

Related Related

HotTag

Archive