Google Mail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
How is map<vector<int>, int> stored? (for graph algorithms)
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  7 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Follow-up To:
Add Cc | Add Follow-up to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers that you hear
 
Digital Puer  
View profile   Translate to Translated (View Original)
 More options 8 Nov, 05:34
Newsgroups: comp.lang.c++
From: Digital Puer <digital_p...@hotmail.com>
Date: Sat, 7 Nov 2009 21:34:14 -0800 (PST)
Local: Sun 8 Nov 2009 05:34
Subject: How is map<vector<int>, int> stored? (for graph algorithms)
I am trying to implement some graph algorithms, and I need
to manage a typical weighted adjacency data structure,
such that A[v1, v2] = w, where w  is the weight of the
directed edge that connects vertex v1 to vertex v2.

I know the standard implementations of this data structure,
namely the adjacency matrix and the adjacency list.

http://en.wikipedia.org/wiki/Adjacency_matrix
http://en.wikipedia.org/wiki/Adjacency_list

Then I got to thinking how C++ STL would handle the following:

map<vector<int>, int> A;
int v1, v2, w;
vector<int> edge;
edge.push_back(v1);
edge.push_back(v2);
A[edge] = w;

Yes, I know that I can use pair<int, int> instead of vector<int>.

How does STL internally manage map<vector<int>, int>
or map<pair<int, int>, int>?

How well does they compare in memory and lookup time to
an adjacency matrix and an adjacency list?


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Vaclav Haisman  
View profile   Translate to Translated (View Original)
 More options 8 Nov, 07:51
Newsgroups: comp.lang.c++
From: Vaclav Haisman <v.hais...@sh.cvut.cz>
Date: Sun, 08 Nov 2009 08:51:17 +0100
Local: Sun 8 Nov 2009 07:51
Subject: Re: How is map<vector<int>, int> stored? (for graph algorithms)
Digital Puer wrote, On 8.11.2009 6:34:

Using std::vector<> as a key for two integere elements is very suboptimal.
There is extra indirection, which means its copy ctor and other operations
have lots more overhead than that of std::pair<>. Also,
sizeof(std::vector<int>) > sizeof(std::pair<int,int>). The access to the
elements is harder, too.

Instead of std::vector<>, use either the std::pair<> or your own Edge class.

--
VH


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Digital Puer  
View profile   Translate to Translated (View Original)
 More options 8 Nov, 17:55
Newsgroups: comp.lang.c++
From: Digital Puer <digital_p...@hotmail.com>
Date: Sun, 8 Nov 2009 09:55:51 -0800 (PST)
Local: Sun 8 Nov 2009 17:55
Subject: Re: How is map<vector<int>, int> stored? (for graph algorithms)
On Nov 7, 11:51 pm, Vaclav Haisman <v.hais...@sh.cvut.cz> wrote:

How does STL hash pair<int, int> for use as a map key?

    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
AnonMail2005@gmail.com  
View profile   Translate to Translated (View Original)
 More options 8 Nov, 18:19
Newsgroups: comp.lang.c++
From: "AnonMail2...@gmail.com" <anonmail2...@gmail.com>
Date: Sun, 8 Nov 2009 10:19:19 -0800 (PST)
Local: Sun 8 Nov 2009 18:19
Subject: Re: How is map<vector<int>, int> stored? (for graph algorithms)
On Nov 8, 12:55 pm, Digital Puer <digital_p...@hotmail.com> wrote:

std::map uses the operator<() (less than) function to order it's
elements.  The term hash does not apply.  std::pair defines this as:

template<class Ty1, class Ty2>
bool operator<(const pair<Ty1, Ty2>& left, const pair<Ty1, Ty2>&
right)
{
  return left.first < right.first || !(right.first < left.first) &&
left.second < right.second;

}

which is right from the dinkumware.com website.  That is, the first
element is the high order part of the key, and the second element is
the low order part of the key.

HTH


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Joshua Maurice  
View profile   Translate to Translated (View Original)
 More options 8 Nov, 21:11
Newsgroups: comp.lang.c++
From: Joshua Maurice <joshuamaur...@gmail.com>
Date: Sun, 8 Nov 2009 13:11:59 -0800 (PST)
Local: Sun 8 Nov 2009 21:11
Subject: Re: How is map<vector<int>, int> stored? (for graph algorithms)
On Nov 8, 10:19 am, "AnonMail2...@gmail.com" <anonmail2...@gmail.com>
wrote:

Well, C++03 the term hash does not apply. For TR2 or C++0x, we're
getting hash sets and hash maps, so it does apply in these cases.
However, the OP is still wrong as he implied that std::map is a hash
map. It is not. It is a red-black binary tree (or at least almost
certainly is because of the complexity requirements in the standard).

    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Digital Puer  
View profile   Translate to Translated (View Original)
 More options 8 Nov, 21:35
Newsgroups: comp.lang.c++
From: Digital Puer <digital_p...@hotmail.com>
Date: Sun, 8 Nov 2009 13:35:44 -0800 (PST)
Local: Sun 8 Nov 2009 21:35
Subject: Re: How is map<vector<int>, int> stored? (for graph algorithms)
On Nov 8, 1:11 pm, Joshua Maurice <joshuamaur...@gmail.com> wrote:

By "hash", I meant to ask how are the keys compared? For a
map<vector<int>, int>, how would the keys be compared? I assume the
comparer must walk down both vectors and do element-wise comparison,
and if two vectors are the same through N elements but one vector
is longer, then the shorter one wins the comparison?

    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
James Kanze  
View profile   Translate to Translated (View Original)
 More options 9 Nov, 01:07
Newsgroups: comp.lang.c++
From: James Kanze <james.ka...@gmail.com>
Date: Sun, 8 Nov 2009 17:07:11 -0800 (PST)
Local: Mon 9 Nov 2009 01:07
Subject: Re: How is map<vector<int>, int> stored? (for graph algorithms)
On Nov 8, 10:35 pm, Digital Puer <digital_p...@hotmail.com> wrote:

More logical than either would be to define an Edge class.  But
vector is a very poor approximation of an edge, since it can
have any number of values, not just exactly two.

It's not necessarily a red-black tree, but in practice, it must
be some form of more or less balanced tree to meet the
complexity requirements.

> By "hash", I meant to ask how are the keys compared? For a
> map<vector<int>, int>, how would the keys be compared?

Straightforward lexographical comparison.

> I assume the comparer must walk down both vectors and do
> element-wise comparison, and if two vectors are the same
> through N elements but one vector is longer, then the shorter
> one wins the comparison?

Exactly.

Note that that's more or less what std::pair does as well.
Except that one pair will never be longer than the other, and
since the class knows the length, and the length is small, it
won't bother with a loop, but will simply do the two
comparisons.

--
James Kanze


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google