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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:42:16+00:00 2026-05-27T18:42:16+00:00

I am working on the shortest path problem , this means that I have

  • 0

I am working on the shortest path problem, this means that I have to create a data type or class to simulate my map. To do this currently I am using a map<string, map<string, int>> where the first string is the name of the node (call it node A), and the second parameter is a map of the nodes that A connects with to their distances to A.

While this works decently, I find it a rather cumbersome way to deal with my nodes, and creating the map is more time consuming than the actual algorithm. Can anyone suggest any decent ways of representing something like this elegantly in a C++ data structure?

Sorry, to clarify. Firstly, both the syntax is manageable but if someone could suggest something more elegant it’d be welcome, iterating over the map is extremely frustrating as I must also iterate over “sub maps” within each pair on the larger map. Secondly, I measured the time to create a map of 10 nodes and compared it to the time it took for the actual algorithm to run, it took longer to create then run.

  • 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-27T18:42:17+00:00Added an answer on May 27, 2026 at 6:42 pm

    One very simple approach is… a table.

        +---+---+--
        | A | B |
    +---+---+---+--
    | A | 0 |100|
    +---+---+---+--
    | B |100| 0 |
    +---+---+---+--
    

    This can be implemented rather trivially, you then just need to devise a way to represent the absence of connection, which can be done by using a sentinel value.

    Second, the way you represent nodes is not very adequate. Strings are heavyweight beasts. I would suggest to create a mapping from the real names to simple numbers, and then only use numbers in the table.

    For example, you could use such an approach:

    class Graph {
    public:
      Graph() {}
    
      void addEdge(std::string const& left,
                   std::string const& right,
                   unsigned weight)
      {
        unsigned const leftIndex = this->getNode(left);
        unsigned const rightIndex = this->getNode(right);
    
        if (_graph.size() <= leftIndex) {
          _graph.resize(leftIndex + 1, Sentinel);
        }
    
        std::vector<unsigned>& leftEdges = _graph[leftIndex];
        if (leftEdges.size() <= rightIndex) {
          leftEdges.resize(rightIndex + 1, Sentinel);
        }
    
        unsigned& edge = leftEdges[rightIndex];
        if (edge != Sentinel) {
          std::cerr << "There was already a weight for '"
                    << left << "' -> '" << right "'\n";
        } else {
          edge = weight;
        }
      }
    
      // Returns (weight, true) if the edge exists and (0u, false) otherwise
      std::pair<unsigned,bool> getWeight(std::string const& left,
                                         std::string const& right) const
      {
        unsigned const leftIndex = this->getNode(left);
        unsigned const rightIndex = this->getNode(right);
    
        try {
          unsigned const weight = _graph.at(leftIndex).at(rightIndex);
          return weight == Sentinel ? std::make_pair(0u, false) :
                                      std::make_pair(weight, true);
        } catch(std::out_of_range const&) {
          return std::make_pair(0u, false);
        }
      }
    
    private:
      static unsigned const Sentinel = (unsigned)-1;
    
      unsigned getNode(std::string const& node) const {
        std::map<std::string, unsigned>::const_iterator it = _nodes.find(node);
        return it == _nodes.end() ? _graph.size() : it->second;
      }
    
      unsigned getNode(std::string const& node) {
        return _nodes.insert(std::make_pair(node, _nodes.size()));
      }
    
      std::map<std::string, unsigned> _nodes;
      std::vector< std::vector<unsigned> > _graph;
    };
    

    Of course, it would be much simpler if you encoded the nodes straight away and only dealt with integrals (this would remove all this string->unsigned conversion from within the class).

    Also note that I created a class for a directed graph, if the graph is not directed, you have to chose between:

    • representing both edges (A -> B and B -> A)
    • representing only a single edge, for example, only (A -> B), you can chose arbitrarily to only represent edges from a lower indice to a higher indice for example
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a program for class that involves solving the Chinese Postman problem
I'm working with some binary data that I have stored in arbitrarily long arrays
I'm concerned that this might be working on an NP-Complete problem. I'm hoping someone
I'm working on a shortest path a* algorithm in java with a mysql db.
I'm currently working on some MPI code for a graph theory problem in which
I'm working on a academic project: writing a library for finding the shortest path
I'm working on this problem: TSP: Input: A matrix of distances; a budget b
In Python, I have a Graph class that has a dictionary of vertex objects.
I have service with working MEX at: net.tcp://remotehost:4508 What is the shortest C#/F# code
I'm working on data structure for graph cut algorithm. Problem is to make different

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.