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

  • SEARCH
  • Home
  • 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 905511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:13:55+00:00 2026-05-15T16:13:55+00:00

I am trying to do edge contraction on a graph. n is the number

  • 0

I am trying to do edge contraction on a graph.
n is the number of vertices in the graph.
The vector f containing pairs of vertices representing an edge contains the edges which are to be contracted.

The graph does not contain any self loops.

Please point out any logical mistakes if any in the code.
If the method is entirely wrong then please let me know the correct algorithm.

void modify(vector<pair<int, int> > &f)
{
    // sorting elements of each pair
    for(vector<pair<int, int> >::iterator it = f.begin(); it != f.end(); it++)
    {
      if(it->first > it->second)
        swap(it->first, it->second);
    }

    // sorting elements of set f
    sort(f.begin(), f.end());
    reverse(f.begin(), f.end());

    for(vector<pair<int, int> >::iterator it = f.begin(); it != f.end(); it++)
    {
      int x, y;
      x = it->first;
      y = it->second;

      for(int i = 0; i < n; i++)
        if(x != i)
        {
          graph[x][i] = graph[y][i] = graph[x][i] + graph[y][i];
          graph[i][x] = graph[i][y] = graph[i][x] + graph[i][y];
        }
    }


    vector<bool> pos(n, false); // array of positions to be deleted

    for(vector<pair<int, int> >::iterator it = f.begin(); it != f.end(); it++)
      pos[it->second] = true;

    // deleting rows
    int count = 0;
    for(int i = 0; i < n; i++)
    {
            for(int j = 0; j < n; j++)
              graph[i-count][j] = graph[i][j];
            if(pos[i])
              count++;
    }

    // deleting columns
    count = 0;
    for(int j = 0; j < n; j++)
    {
            for(int i = 0; i < n; i++)
              graph[i][j-count] = graph[i][j];
            if(pos[j])
              count++;
    }

    // finally dimension of the matrix becomes n - count
    n = n - count;
}

Thanks in advance.

  • 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. Editorial Team
    Editorial Team
    2026-05-15T16:13:56+00:00Added an answer on May 15, 2026 at 4:13 pm

    Given the following example graph:

    a       d
     \     /
      b---e
     /     \
    c       f

    Would “contracting” the edge {b, e} result in the following?:

    a   d
     \ /
      g
     / \
    c   f

    “Yes, it is exactly that” – amit. Thx, I wanted to get the specification right, before I answer Your question. Further assumptions are:

    • Graph is directed
    • Vertices are represented through integers.
    • The graph is stored in the two dimensional array graph, graph[x][y] being the weight of the edge (x, y); 0 indicating that there is no edge from x to y

    Lets give it a try with some pseudocode:

    void contractEdge(int v1, int v2) {
        for (int i = 0; i < n; i++) {
            if (graph[v2][i] > 0) {
                graph[v1][i] = graph[v1][i] > 0 ? min(graph[v1][i], graph[v2][i]) :
                                                  graph[v2][i];
                graph[v2][i] = 0;
            }
            if (graph[i][v2] > 0) {
                graph[i][v1] = graph[i][v1] > 0 ? min(graph[i][v1], graph[i][v2]) :
                                                  graph[i][v2];
                graph[i][v2] = 0;
            }
            graph[v1][v2] = graph[v2][v1] = 0;
        }
    }

    The code works like this: Given the edge {v1, v2}, v1 becomes the “contracted” vertex. That means, instead of inserting a new vertex (“g” in my first example), v1 remains in the graph and v2 is deleted from it (by assigning 0 weights on edges to all other vertices, the actual vertex count doesn’t change). Further, all edges that contain v2 are bend to contain v1.

    Now one can call this method for a set of edges. The runtime will be O(n * #edges_to_contract). I hope this is the behavior You desired.

    Important: If You use a different representation for Your edge weights, namely 0 meaning an edge with 0 weight and ∞(infinite) indicating the absence of an edge, then Your problem becomes trivial because all You have to do is:

    graph[v1][v2] = graph[v2][v1] = 0

    which effectively contracts the edge {v1, v2}, since now it doesn’t cost anything to travel between v1 and v2

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

Sidebar

Related Questions

I'm trying to make a vector drawing application using OpenGL which will allow the
I'm trying to compute de Bruijn sequences for alphabets which have a number of
I'm trying to use XSLT to create Edge Side Includes html blocks. Here is
I am trying to aligning contents of a div to right edge, but I'm
I am trying to use FactoryGirl instead of default fixtures in edge Rails 3.
I am trying to use qsort from STL to sort array of edge: struct
Im trying to create a layout in flex using mxml, the layout contains a
I'm trying to emulate this effect: http://meyerweb.com/eric/css/edge/complexspiral/demo.html on my blog: http://segment6.blogspot.com/ It works, but
I'm trying to write a function in Python that finds the first number in
I'm trying to run refinery-edge on my Ubuntu 10.04 64bit. I have RVM installed,

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.