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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:06:36+00:00 2026-06-14T22:06:36+00:00

I need to efficiently define an ordering on the std::set<Edge> . Edge represents an

  • 0

I need to efficiently define an ordering on the std::set<Edge>. Edge represents an edge in a graph (not multigraph).

class Edge
    {
        friend class Graph;
        string from;
        string to;
        EdgeInfo edge_length; //constructor is `EdgeInfo(int edge_length)`
    public:
        bool operator==(const Edge& rhs) {
            return (from==rhs.from && to==rhs.to);               
        }
    };

The problem is to efficiently find

  • whether the std::set<Edge> contains an edge with given “from” and “to”
  • edges that go from given “from” to some “to”, where “to” is not inside a given set<string>

using std::set.count() and std::set.find(). I need to somehow define the appropriate ordering on the std::set. Is this possible?


EDIT: I figured I should have used map or multimap instead of set. Eventually I used map. The solution is inspired by @tom’s suggestion to use map of maps.


SOLUTION:

typedef int EdgeInfo; //just for the sake of this example (EdgeInfo can be length,price,...) 
map< string, map<string, EdgeInfo> > edges;

whether the std::set<Edge> contains an edge with given “from” and
“to”

if (edges.count(from)!=0 && edges[from].count(to)!=0) {
        return true; 
}

or in case the function is const

if (edges.count(from)!=0 && ((edges.find(top.second))->second).count(to)!=0) {
        return true; 
}

edges that go from given “from” to some “to”, where “to” is not inside
a given set

in case the function is const

//if there are any edges from "from"
if (edges.count(from)!=0) {  

    //iterate over all edges from "from"
    for (map<string,EdgeInfo>::const_iterator
                 edge=((edges.find(from))->second).begin();
                 edge!=((edges.find(from))->second).end();
                 ++edge) {

        //if the edge goes to some vertex V that has not been discarded
        if (discarded.count(edge->first)==0) { //edge->first means "to"
  • 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-06-14T22:06:36+00:00Added an answer on June 14, 2026 at 10:06 pm

    Adjacency List

    map< string, set<string> > edges;
    // edges["a"] is the set of all nodes that can be reached from "a"
    
    // O(log n)
    bool exists(string from, string to)
    {
        return edges[from].count(to) > 0;
    }
    
    // Ends of edges that start at 'from' and do not finish in 'exclude', O(n)
    set<string> edgesExcept(string from, set<string>& exclude)
    {
        set<string>& fromSet = edges[from];
        set<string> results;
        // set_difference from <algorithm>, inserter from <iterator>
        set_difference(fromSet.begin(), fromSet.end(),
                exclude.begin(), exclude.end(),
                inserter(results, results.end()));
        return results;
    }
    

    Adjacency Matrix

    map< string, map<string, Edge*> > edgesMatrix;
    // edgesMatrix["a"]["b"] is the Edge* from "a" to "b"
    // e.g. Edge* e = new Edge(...); edgesMatrix[e->from][e->to] = e;
    
    bool exists(string from, string to)
    {
        return edgesMatrix[from].count(to) > 0;
    }
    
    vector<Edge*> edgesExcept(string from, set<string>& exclude)
    {
        map<string, Edge*>& all = edgesMatrix[from];
        vector<Edge*> results;
    
        map<string, Edge*>::iterator allIt = all.begin();
        set<string>::iterator excludeIt = exclude.begin();
    
        while (allIt != all.end())
        {
            while (excludeIt != exclude.end() && *excludeIt < allIt->first)
            {
                ++excludeIt;
            }
    
            if (excludeIt == exclude.end() || allIt->first < *excludeIt)
            {
                results.push_back(allIt->second);
            }
            ++allIt;
        }
    
        return results;
    }
    

    One Ordered Set

    This is more in line with the OP’s original request, but I feel it is much uglier than the other options.
    I have included this only for the sake of completeness.

    // sorted first by 'from', then by 'to'
    class Edge {
        // ...
    public:
        bool operator<(const Edge& r) const {
            return from < r.from || (from == r.from && to < r.to);
        }
    };
    
    set<Edge> edges;
    
    bool exists(string from, string to) {
        Edge temp(from, to, -1);
        return edges.count(temp) > 0;
    }
    
    set<Edge> edgesExcept(string from, set<string>& exclude) {
        Edge first = Edge(from, "", -1); // ugly hack: "" sorts before other to's
        set<Edge> results;
    
        set<Edge>::iterator allIt = edges.lower_bound(first);
        set<string>::iterator excludeIt = exclude.begin();
    
        while (allIt != edges.end() && allIt->from == from) {
            while (excludeIt != exclude.end() && *excludeIt < allIt->to) {
                ++excludeIt;
            }
            if (excludeIt == exclude.end() || allIt->to < *excludeIt) {
                results.insert(results.end(), *allIt);
            }
            ++allIt;
        }
        return results;
    }
    

    Explanation of edgesExcept()

    Here is a pseudo code version:

    for each edge e in edges_of_interest (in sorted order)
        get rid of edges in exclude_edges that sort before e
        if e is equal to the first edge in exclude_edges
            e is in exclude_edges, so
            ignore e (i.e. do nothing)
        otherwise
            e is not in exclude_edges, so
            add e to good_edges
    

    Instead of actually removing edges that are no longer relevant from exclude_edges, the C++ version uses an iterator to remember which edges in exclude_edges are no longer relevant (those smaller than all the edges of interest that are yet to be examined). Once all the edges in exclude_edges that are smaller than e have been removed / skipped over, checking if e appears in exclude_edges can be done simply by comparing it to the first (smallest) element of exclude_edges.

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

Sidebar

Related Questions

In Python I need to efficiently and generically test whether an attribute of a
I need to multiply several 1000s digits long integers as efficiently as possible in
I need to put together a data structure that will efficiently provide keyword search
I am having trouble efficiently selecting the information I need to display. Hopefully someone
In Python, if I define three classes: class A: name = 'oliver' hailstone_ending =
I am attempting to enumerate the set of all pairs made of elements from
Need to define a seek(u,v) function, where u is the new node within the
I'm building an email filter and I need a way to efficiently match a
I need an efficient mechanism for detecting changes to the DOM. Preferably cross-browser, but
I am doing a project at the moment and I need an efficient method

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.