Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 117545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:21:12+00:00 2026-05-11T03:21:12+00:00

How can I compute the minimum bipartite vertex cover in C#? Is there a

  • 0

How can I compute the minimum bipartite vertex cover in C#? Is there a code snippet to do so?

EDIT: while the problem is NP-complete for general graphs, it is solvable in polynomial time for bipartite graphs. I know that it’s somehow related to maximum matching in bipartite graphs (by Konig’s theorem) but I can’t understand the theorem correctly to be able to convert the result of maximum bipartite matching to vertex cover.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T03:21:13+00:00Added an answer on May 11, 2026 at 3:21 am

    I could figure it out:

    using System; using System.Collections.Generic; using System.Linq; using System.Text;  class VertexCover {     static void Main(string[] args)     {         var v = new VertexCover();         v.ParseInput();         v.FindVertexCover();         v.PrintResults();     }      private void PrintResults()     {         Console.WriteLine(String.Join(' ', VertexCoverResult.Select(x => x.ToString()).ToArray()));     }      private void FindVertexCover()     {         FindBipartiteMatching();          var TreeSet = new HashSet<int>();         foreach (var v in LeftVertices)             if (Matching[v] < 0)                 DepthFirstSearch(TreeSet, v, false);          VertexCoverResult = new HashSet<int>(LeftVertices.Except(TreeSet).Union(RightVertices.Intersect(TreeSet)));     }      private void DepthFirstSearch(HashSet<int> TreeSet, int v, bool left)     {         if (TreeSet.Contains(v))             return;         TreeSet.Add(v);         if (left) {             foreach (var u in Edges[v])                 if (u != Matching[v])                     DepthFirstSearch(TreeSet, u, true);         } else if (Matching[v] >= 0)             DepthFirstSearch(TreeSet, Matching[v], false);      }      private void FindBipartiteMatching()     {         Bicolorate();         Matching = Enumerable.Repeat(-1, VertexCount).ToArray();         var cnt = 0;         foreach (var i in LeftVertices) {             var seen = new bool[VertexCount];             if (BipartiteMatchingInternal(seen, i)) cnt++;         }     }      private bool BipartiteMatchingInternal(bool[] seen, int u)     {         foreach (var v in Edges[u]) {             if (seen[v]) continue;             seen[v] = true;             if (Matching[v] < 0 || BipartiteMatchingInternal(seen, Matching[v])) {                 Matching[u] = v;                 Matching[v] = u;                 return true;             }         }         return false;     }      private void Bicolorate()     {         LeftVertices = new HashSet<int>();         RightVertices = new HashSet<int>();          var colors = new int[VertexCount];         for (int i = 0; i < VertexCount; ++i)             if (colors[i] == 0 && !BicolorateInternal(colors, i, 1))                 throw new InvalidOperationException('Graph is NOT bipartite.');     }      private bool BicolorateInternal(int[] colors, int i, int color)     {         if (colors[i] == 0) {             if (color == 1) LeftVertices.Add(i);             else RightVertices.Add(i);             colors[i] = color;         } else if (colors[i] != color)             return false;         else             return true;         foreach (var j in Edges[i])             if (!BicolorateInternal(colors, j, 3 - color))                 return false;         return true;     }      private int VertexCount;     private HashSet<int>[] Edges;     private HashSet<int> LeftVertices;     private HashSet<int> RightVertices;     private HashSet<int> VertexCoverResult;     private int[] Matching;      private void ReadIntegerPair(out int x, out int y)     {         var input = Console.ReadLine();         var splitted = input.Split(new char[] { ' ' }, 2);         x = int.Parse(splitted[0]);         y = int.Parse(splitted[1]);     }      private void ParseInput()     {         int EdgeCount;         ReadIntegerPair(out VertexCount, out EdgeCount);         Edges = new HashSet<int>[VertexCount];         for (int i = 0; i < Edges.Length; ++i)             Edges[i] = new HashSet<int>();          for (int i = 0; i < EdgeCount; i++) {             int x, y;             ReadIntegerPair(out x, out y);             Edges[x].Add(y);             Edges[y].Add(x);         }     } } 

    As you can see, this code solves the problem in polynomial time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a build in library in .NET that can compute secure one-way hash
I recently found a contest problem that asks you to compute the minimum number
Is there a built in method in a java library that can compute 'N
Given any two sequences/vectors of M real numbers, I can easily compute their closeness
I can't find any PHP script to compute Krippendorff's Alpha . I need this
I can't seem to find an algorithm to compute the number of arrays within
What does TEMP0_FILES below compute to? SOURCE_FILES can equal to multiple source files. Please
I can list my own computer's shares no problem with: gwmi Win32_Share But when
How can I code to see how long the computer has been on. Simple
I am looking for a simple java class that can compute tf-idf calculation. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.