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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:16:04+00:00 2026-05-29T10:16:04+00:00

Suppose we have a graph with bidirectional edges, no weights. How can I store

  • 0

Suppose we have a graph with bidirectional edges, no weights. How can I store it so that I don’t waste tons of memory, make it fast and have a fast access to every vertex’s neighbors? I mean, until now for sth like this: {(1,2)(1,5)(1,3)(2,4)(2,3)} I have been using an array: array[1][2]=1 meaning that there is a connection between 1 and 2. There are two problems with that:

  • a) as the graph is bidirectional, (1,2) means (2,1) exists as well. If I want to have easy access to 2’s neighbors later, I have to make two changes per iteration: array[1][2]=1, array[2][1]=1

  • b) when I know some vertex (say 5) has only one neighbor left, I have to search through the whole array[5][x] checking every possible x

  • c) for a graph of a million vertexes, this monster becomes too vast to be used in any competition

Could you please help me and point me the solution to my problems?

  • 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-29T10:16:05+00:00Added an answer on May 29, 2026 at 10:16 am

    Looks like you want a map of sets.

    std::map< int, std::set< int > >

    So for an int you can store a collection of all its neighbours in the set. You will want functions to manipulate this collection.

    If the number of nodes is countable, i.e. they range from 0 to N and include all these numbers then you can use std::vector< std::set<int> > and it would be more efficient to do so. You could also use std::vector< std::bitset<N> > or std::vector< boost::dynamic_bitset > > if you have, say, 20,000 nodes and can therefore afford 20,000 bitsets of 2500 bytes (plus a bit of overhead) each = 50MB of memory (approx).

    This is a slightly more compact model to the one you have but not by a lot. If you have a million vertices it will be about 125GB so obviously you can’t use this model but should use the set. Also, iterating through a vertex to see what its neighbours are is a much faster operation with a set than a bitset.

    Unless there are many vertices with no neighbours at all though and that they are sequentially numbered, there is no advantage of map over vector though.

    Not sure how much memory you call “tons”. The model I just outlaid uses constant memory whereas the map of sets uses memory proportional to the number of neighbourhood-relationships you have but as it gets full will be far less compact than the vector of bitsets so will consume more.

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

Sidebar

Related Questions

Suppose I have two columns in a table that represents a graph, the first
Suppose I have: Toby Tiny Tory Tily Is there an algorithm that can easily
Suppose that you have a directed graph with nonnegative, integer edge lengths that are
I have one page that displays a bar graph, and the values can vary
Suppose I have a graph composed of nodes and edges, and I want to
Suppose I have a graph where the minimum edge weight is −100. Can I
For instance, suppose I have a graph G = (V, E) where V =
Suppose I have a stringbuilder in C# that does this: StringBuilder sb = new
Let's suppose I have a Winforms app with a Dictionary that looks like this:
Suppose I have a weighted non-directed graph G = (V,E). Each vertex has a

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.