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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:22:48+00:00 2026-05-23T20:22:48+00:00

i have a vector of vertices from mygraph, and i topologically sort the vertices.

  • 0

i have a vector of vertices from mygraph, and i topologically sort the vertices.

typedef typename boost::adjacency_list<boost::listS, boost::vecS, 
       boost::directedS,Vertex_t*> Graph_t;

typedef typename boost::graph_traits<Graph_t>::vertex_descriptor Vd_t;
Graph_t mygraph;
std::vector<Vd_t> v1; 
//initialize the vertices and get a copy of vertices in vector v1
//...
//and then i use

std::vector<Vd_t> v2;
boost::topological_sort(mygraph,std::back_inserter(v2));

to topologically sort the vertices ‘in order’.
So what is the best way to compare v1 and v2 to test if my original vector of vertices(v1) was ‘in order’ or ‘out of order’.

EDIT:
For example:
if vector v1 has vertices {A, B, C} and we have edges
(A,B)
(C,A)

so after topological sort I’ll have in v2

{C, A, B}

There will be cases where i may not get a unique topological order, for example if i have
(A,C) as the only edge, then v2 might end up with
{A , B , C} or {B , A , C}

Now i need to look into v1 and v2 to find out if both v1 and v2 represent the same order or not.

SECOND EDIT:
I tried an approach and it works for now, please tell me if this is a good approach or not.

template <typename Vertex_t>
void DepAnalyzer<Vertex_t>::CheckTotalOrder()
{
  //Vd_t is the vertex descriptor type
  typename std::vector<Vd_t> topo_order;
  typename std::vector<Vd_t>::iterator ordered_vertices_iter;

  //puts the topologically sorted vertices into topo_order
  DoTopologicalSort(topo_order);

  //will point to the vertices in the order they were inserted
  typename boost::graph_traits<Graph_t>::vertex_iterator vi, vi_end;
  std::unordered_map<Vertex_t*,int,std::hash<Vertex_t*>,
                      VertexEqual> orig_order_map, topo_order_map;  

  int i = 0;
  for(std::tie(vi, vi_end) = boost::vertices(depGraph); vi != vi_end; ++vi) {
    orig_order_map[depGraph[*vi]] = ++i;
    //std::cout<<depGraph[*vi]->get_identifier_str()<<"\n";
  }
  i = 0;
  //will point to the vertices in the topological order
  ordered_vertices_iter = topo_order.begin();
  for(;ordered_vertices_iter != topo_order.end(); ordered_vertices_iter++) {
    topo_order_map[depGraph[*ordered_vertices_iter]] = ++i;
    //std::cout<<depGraph[*ordered_vertices_iter]->get_identifier_str()<<"\n";
  }
  //checking the order now
  ordered_vertices_iter = topo_order.begin();
  for(;ordered_vertices_iter != topo_order.end(); ordered_vertices_iter++) {
    //if the vertex in topologically sorted list occurs before(w.r.t. position)
    //the macro in the original list of vertices, then it's okay
    if(orig_order_map[depGraph[*ordered_vertices_iter]] >
      topo_order_map[depGraph[*ordered_vertices_iter]]) {
      std::cerr << depGraph[*ordered_vertices_iter]->get_identifier_str() 
                << ": out of order\n";
    }
  }
}
  • 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-23T20:22:49+00:00Added an answer on May 23, 2026 at 8:22 pm

    So what is the best way to compare v1 and v2 to test if my original vector of vertices(v1) was ‘in order’ or ‘out of order’.

    Not comparing the vectors. The test is flawed in that if it succeeds (both vectors are the same) you will know that it was in order, but if it fails (vectors differ) you will not know whether it was out of order or if it was in a different order.

    Consider the graph (imagine edges going down):

      A
     / \
    B   C
     \ /
      D
    

    The lists { A, B, C, D } and { A, C, B, D } are both in topological order, because there is no ordering constraint between B and C. If you had one of the options and the boost::topological_sort yielded the other variant you will not know whether you had an incorrect order or rather a different correct one.

    It is probably better to write a simple algorithm that will check the order for correctness and reorder if needed.

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

Sidebar

Related Questions

Hi I have a boost graph like: struct Vertex; struct Edge; typedef boost::adjacency_list<boost::vecS, boost::vecS,
I have the vector d<-1:100 I want to sample k=3 times from this vector
I have a vector of dates: Date = seq(from=as.POSIXct(2011-01-01 00:00), to=as.POSIXct(2011-12-31 23:00), length=8760) It
I have a multimap defined by typedef std::pair<int, int> au_pair; //vertices typedef std::pair<int, int>
I have a vector of vertices and I wish to set vertice.setVisit as false
I have a pointer array of a custom Vector typedef (just 3 floats). I'm
I have vector< pair<int, int>> myVec (N); I want to have all pairs initialized
in clojure i have vector [myfn1 myfn2 myfn3] how can i call functions named
I have a vector class and I defined the __mul__ method to multiply a
I have a vector of objects (objects are term nodes that amongst other fields

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.