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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:17:20+00:00 2026-05-15T14:17:20+00:00

I have the adjacency list of a large graph stored in a table. v1

  • 0

I have the adjacency list of a large graph stored in a table.

v1 |  v2
1  |  2
1  |  3
1  |  4
2  |  5
3  |  5
3  |  6
4  |  7
7  |  9
5  |  10

I am trying to extract the graph 2-hops away from say vertex 1 so that it returns all the edges from the above list except (7,9) and (5,10). I managed this:

SELECT * FROM stub_graph WHERE v1 IN (SELECT v1 FROM stub_graph WHERE v1=1 UNION select v2 FROM stub_graph WHERE v1=1) ;

I am not particularly happy with my solution because of two reasons:

  1. Presence of IN
  2. This is able to
    extract links such as 1-2 and 2-5
    but am not able to extract links
    such as 6-7.

Is there a good way to do this?

Just in case someone is interested in creating the table, here’s the sql code:

create table stub_graph(v1 int(11), v2 int(11));
insert into stub_graph VALUES(1,2);
insert into stub_graph VALUES(1,3);
insert into stub_graph VALUES(1,4);
insert into stub_graph VALUES(2,5);
insert into stub_graph VALUES(3,5);
insert into stub_graph VALUES(3,6);
insert into stub_graph VALUES(4,7);
insert into stub_graph VALUES(6,7);
insert into stub_graph VALUES(7,9);
insert into stub_graph VALUES(5,10);
  • 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-15T14:17:20+00:00Added an answer on May 15, 2026 at 2:17 pm

    Try this:

    SELECT g1.v1 as Root, g2.v1 as Next g3.v1 as Final FROM stub_graph g1 
      LEFT JOIN stub_graph g2 on g2.v1 = g1.v2
      LEFT JOIN stub_graph g3 on g3.v1 = g2.v2
    WHERE g1.v1 = 1
    

    If you want (6-7) though you need to go three levels deep as it is 3 hops away from 1 (1-3, 3-6, 6-7).

    If you want to go arbitrarily deep you will need to look at a recursive stored proc which I think the later versions of MySQL support, but this looks suitable for your needs. Alternatively the link below has some other ideas.

    That should yield:

    Root | Next | Final
       1 |    3 |     5
       1 |    3 |     6
       1 |    2 |     5
       1 |    4 |     7
    

    See also this link on Hierarchical Data

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

Sidebar

Related Questions

I have an undirected graph on matris by vertex adjacency relations like that; /*
In python I have a graph as adjacency list presentation. and a distance from
I have a table with data stored hierarchicaly using adjacency list like in the
I have an adjacency list of objects (rows loaded from SQL database with the
In mysql, I have a tree that is represented using the adjacency list model.
I have designed a weighted graph using a normalized adjacency list in mysql. Now
I have Adjacency list mode structure like that and i want to count all
I have a table relating records using the adjacency list method (table A ),
I am trying to implement the adjacency list for a non-weighted graph and a
I have this hadoop map reduce code that works on graph data (in adjacency

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.