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 7528459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:23:12+00:00 2026-05-30T04:23:12+00:00

I’ve been coding a solution for the Graph Coloring Problem using Brown’s algorithm. The

  • 0

I’ve been coding a solution for the Graph Coloring Problem using Brown’s algorithm. The algorithm is working just fine but, to add a little efficiency I’m trying to sort the nodes by their degree. To do so I’m using stl::sort(). Nonetheless, after sort has ordered the elements, the adyacency list of each element has been modified. I’ve got 4 files:

nodo.h:

class Nodo{
private:
    int id;
    int color;
    vector<Nodo*> adyacentes;
public:
    int grado;
    vector<Nodo*> getAdyacentes();
    int getId();
    int getColor();
}

nodo.cpp -> Includes the basic implementation of the getters.

grafo.h:

class Grafo{
private:
    Nodo *nodos;
    int tam;
public:
    void colorearBrown();
    void imprimir();
}

grafo.cpp:

void Grafo::imprimir(){
    cout << setw(6) << "Numero" << setw(5) << " Color"  << " Nodos adyacentes" << endl;
    for(int i = 0; i < tam; ++ i){
        cout << setw(6) << nodos[i].getId() << setw(5) << nodos[i].getColor() << "  ";
        vector<Nodo*> ady = nodos[i].getAdyacentes();
        for(vector<Nodo*>::iterator it = ady.begin(); it != ady.end(); ++ it){
            if (it != ady.begin()){
                cout << ",";
            }
            cout << (*it)->getId();
        }
        cout << endl;
    }
}

bool operator<(const Nodo& a, const Nodo& b){
    return (a.grado >= b.grado);
}

void Grafo::colorearBrown(){
    imprimir();
    sort(&nodos[0], &nodos[tam]);
    cout << endl << endl;
    imprimir();
}

And that’s it. Assuming I’ve a correctly loaded graph in a ‘Grafo’ object, I run the method colorearBrown() and get the following output (part):

.
.
.
    16  -1  1,22,36,43,47,48
    17  -1  10,13,20,22,44,47
    18  -1  27,32,48
    19  -1  4,32,36
    20  -1  6,8,10,12,17,33,36,38,43,45,48
    21  -1  4,6,45
    22  -1  6,16,17,26,30,31
    23  -1  3,8,10,14,24,26,32,36
.
.
.

Numero Color Nodos adyacentes
    20  -1  4,42,22,32,10,3,34,50,18,19,25
     7  -1  20,32,6,13,3,50,2,18,31,19
    36  -1  7,14,16,44,12,38,30,1,50,37
    14  -1  22,30,49,24,2,11,40,5
.
.
.

See how the list of adyacent nodes changes on node 20(here), but this is repeated for all nodes.

The program perfectly compiles and, if not sorted, the nodes stay the same way throughout the hole execution.

Any ideas on why the sorting algorithm is messing with my structure would be helpful.

  • 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-30T04:23:14+00:00Added an answer on May 30, 2026 at 4:23 am

    std::sort copies (or moves, in C++11) the objects. However you have pointers in your objects, which point at the place where the object is. After the sort, there’s another object at that place.

    Think of it as people sitting on chairs. You give each person a list of chairs where the “adjacent” people sit. And then you ask the people to change places. People changing places does not change what is written on the paper.

    The people are the node objects, the places are the positions in he array, and the pieces of paper with the lists are your vectors of pointers to adjacent nodes.

    A simple solution to that problem would be using a std::list and sorting with its sort member function. That member function doesn’t move objects around, but just relinks the internal nodes, therefore your pointers will continue to point to the correct object. One disadvantage of that solution (which may or may not matter in your case) is that the list doesn’t offer random access.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.