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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:01:48+00:00 2026-05-24T07:01:48+00:00

I need to know if I am reading this problem correctly. I am preparing

  • 0

I need to know if I am reading this problem correctly. I am preparing for an interview, and I need to brush up on my reading comprehension skills I guess.

The problem states:

Suppose that we are given a sequence of pairs of integers where each integer represents an object of some type and we are to interpret the pair p-q as meaning “p is connected to q” We assume the relation “is connected to” to be transitive: If p is connected to q, and q is connected to r, then p is connected to r. Our goal is to write a program to filter out extraneous pairs from the set: When the program inputs a pair p-q, it should output the pair only if the pairs it has seen to that point do not imply that p is connected to q. If the previous pairs do imply that p is connected to q, then the program should ignore p-q and should proceed to input the next pair.

I personally, don’t think I have addressed the transitivity portion in the code below, but then I have the tendency to make things more complicated than they need to be. Can I get a second interpretation of what this question from “Algorithms in C++” is asking.

/* 1 */ #include <iostream>
/* 2 */ #include <set>
/* 3 */ #include <algorithm>
/* 4 */ 
/* 5 */ using namespace std;
/* 6 */ 
/* 7 */ #define PAIRS 7
/* 8 */ 
/* 9 */ pair<int,int> pair_list[PAIRS] = {
/*10 */     pair<int,int>(0,2),
/*11 */     pair<int,int>(1,4),
/*12 */     pair<int,int>(2,5),
/*13 */     pair<int,int>(3,6),
/*14 */     pair<int,int>(0,4),
/*15 */     pair<int,int>(6,0),
/*16 */     pair<int,int>(2,0) 
/*17 */ };
/*18 */ 
/*19 */ void print( const pair<int,int> &out ) {
/*20 */     cout << "<" << out.first << ", " << out.second << ">" << endl;
/*21 */ }
/*22 */ 
/*23 */ bool contains( set<pair<int,int> > &_set, pair<int,int> &ordered_pair ) {
/*24 */     set<pair<int,int> >::iterator find = _set.find( ordered_pair );
/*25 */     bool ret = false;
/*26 */     if( find != _set.end( ) ) {
/*27 */         ret = true;
/*28 */     }
/*29 */     return ret;
/*30 */ }
/*31 */ 
/*32 */ int main( int argc, char **argv ) {
/*33 */     set<pair<int,int> > SET;
/*34 */     SET.clear( ); 
/*35 */     pair<int,int> *iter = &pair_list[0]; 
/*36 */     while( iter != &pair_list[PAIRS-1] ) {
/*37 */         if( !contains( SET,(*iter) ) ){
/*38 */                 SET.insert( (*iter) );
/*39 */         }
/*40 */         iter++;
/*41 */     }
/*42 */     
/*43 */     for_each( SET.begin( ), SET.end( ), print );
/*44 */     return ( 0 );
/*45 */ }

==================================================================================

UPDATE: 1

Okay I think I came up with a solution I like. Took me way to long, and that is going to be bad for interviews, but I still got it.

#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <vector>
#include <iterator>

using namespace std;

void print_set( set<int>* _set ) {
    copy( _set->begin( ), _set->end( ), ostream_iterator<int>(cout, " ") );
    cout << endl;
}

void print_sets( set<set<int>*> _sets ) {
    for_each( _sets.begin( ), _sets.end( ), print_set );
}

void connectivity( queue<pair<int,int> > pairs ) {
    set<set<int>* > connected_items;
    while( pairs.size( ) ) {
        int first = pairs.front( ).first;
        int second = pairs.front( ).second;
        set<set<int>* >::iterator S=connected_items.begin( );
        bool found = false;
        bool dupli = false;
        set<int>* adj = new set<int>;
        while( S != connected_items.end( ) ) { //Go through all connected sets
            set<int>::iterator f=(*S)->find( first );
            set<int>::iterator s=(*S)->find( second );
            if( f!=(*S)->end( )&&s!=(*S)->end( ) ) {
                S++;
                dupli = true;
                continue;
            }
            if( f!=(*S)->end( )||s!=(*S)->end( ) ) {
                found = true;
                adj->insert( first );
                adj->insert( second );
                //copy( (*S)->begin( ), (*S)->end( ), ostream_iterator<int>( cout," ")  );
                set<int>::iterator num = (*S)->begin( );
                while( num != (*S)->end( ) ) {
                    adj->insert( (*num) );
                    num++;
                }
                connected_items.erase( S );
            }
            S++;
        }
        if( !found&&!dupli ) {
            set<int>* insert = new set<int>;
            connected_items.insert( insert );
            insert->insert( first );
            insert->insert( second );
        } else {
            connected_items.insert( adj );
        }
        pairs.pop( );
    }
    print_sets( connected_items );
}

int main( int argc, char **argv ) {
    queue<pair<int,int> > pairs;
    pairs.push( pair<int,int>( 1,2 ) );
    pairs.push( pair<int,int>( 2,3 ) );
    pairs.push( pair<int,int>( 2,4 ) );
    pairs.push( pair<int,int>( 2,5 ) );
    pairs.push( pair<int,int>( 6,7 ) );
    pairs.push( pair<int,int>( 6,8 ) );
    pairs.push( pair<int,int>( 6,9 ) );
    pairs.push( pair<int,int>( 9,10 ) );
    pairs.push( pair<int,int>( 11,12 ) );
    pairs.push( pair<int,int>( 12,13 ) );
    pairs.push( pair<int,int>( 14,15 ) );
    pairs.push( pair<int,int>( 2,12) );
    pairs.push( pair<int,int>( 2,1) );
    connectivity( pairs );
}


[mehoggan@desktop Connectivity]$ g++ -o connected -Wall connected.cpp; ./connected
6 7 8 9 10 
14 15 
1 2 3 4 5 11 12 13 
  • 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-24T07:01:48+00:00Added an answer on May 24, 2026 at 7:01 am

    While you are inputting the pairs, you do not check for transitivity nor if the path is correctly entered.

    If you used Boost’s Graph library, you could create an undirected graph and traverse it in depth-first-search (DFS) order:

    // Boost DFS example on an undirected graph.
    // Create a sample graph, traverse its nodes
    // in DFS order and print out their values.
    
    #include <boost/graph/adjacency_list.hpp>
    #include <boost/graph/depth_first_search.hpp>
    #include <iostream>
    using namespace std;
    
    typedef boost::adjacency_list<boost::listS, boost::vecS, boost::undirectedS> MyGraph;
    typedef boost::graph_traits<MyGraph>::vertex_descriptor MyVertex;
    
    class MyVisitor : public boost::default_dfs_visitor
    {
    public:
      void discover_vertex(MyVertex v, const MyGraph& g) const
      {
        cerr << v << endl;
        return;
      }
    };
    
    int main()
    {
      MyGraph g;
      boost::add_edge(0, 1, g);
      boost::add_edge(0, 2, g);
      boost::add_edge(1, 2, g);
      boost::add_edge(1, 3, g);
    
      MyVisitor vis;
      boost::depth_first_search(g, boost::visitor(vis));
    
      return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Need to know this so that i could send DTMF and that is going
I know I need to use the . something like .live to get this
I need to know about Epoll On linux System. Could you recommend manual or
I need to know how the performance of different XML tools (parsers, validators, XPath
I need to know how much space occupies all the databases inside an SQL
I need to know how to turn on Code Coverage when running TFS builds
I need to know when the memory will be allocated for a particular program.
I need to know, from within Powershell, if the current drive is a mapped
I need to know how I get the stream of the headers and footers
I need to know what internet connection is available when my application is running.

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.