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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:20:59+00:00 2026-06-10T22:20:59+00:00

I am representing a graph in Postgres 9.1 (happens to be bidirectional and cyclic):

  • 0

I am representing a graph in Postgres 9.1 (happens to be bidirectional and cyclic):

CREATE TABLE nodes (
  id          SERIAL PRIMARY KEY,
  name        text
);

CREATE TABLE edges (
  id          SERIAL PRIMARY KEY,
  node1_id    int REFERENCES nodes(id),
  node2_id    int REFERENCES nodes(id)
);

Given a particular node ID, want to retrieve all other nodes in that cluster. I started with the “Paths from a single node” example here, and this is where I got:

WITH RECURSIVE search_graph(id, path) AS (
    SELECT id, ARRAY[id]
    FROM nodes
  UNION
    SELECT e.node2_id, sg.path || e.node2_id
    FROM search_graph sg
    JOIN edges e
    ON e.node1_id = sg.id
)
-- find all nodes connected to node 3
SELECT DISTINCT id FROM search_graph WHERE path @> ARRAY[3];

I can’t figure out a) if there is a simpler way to write this since I don’t care about collecting the full path, and b) how to make it traverse in both directions (node1->node2 and node2->node1 for each edge). Shedding any light on a good approach would be appreciated. Thanks!

  • 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-10T22:21:01+00:00Added an answer on June 10, 2026 at 10:21 pm

    A couple points.

    First, you really want to make sure your path traversal is not going to go into a loop. Secondly handling both sides is not too bad. Finally depending on what you are doing, you may want to push the where clause into the CTE somehow to reduce generating every possible graph network and then picking the one you want.

    Traversing itself both directions is not too hard. I haven’t tested this but it should be possible with something like:

    WITH RECURSIVE search_graph(path, last_node1, last_node2) AS (
         SELECT ARRAY[id], id, id
         FROM nodes WHERE id = 3 -- start where we want to start!
         UNION ALL
         SELECT sg.path || e.node2_id || e.node1_id, e.node1_id, e.node2_id
         FROM search_graph sg
         JOIN edges e
         ON (e.node1_id = sg.last_node2 AND NOT path @> array[e.node2_id]) 
            OR (e.node2_id = sg.last_node1 AND NOT path @> array[e.node1_id])
     )
    -- Moved where clause to start of graph search
    SELECT distinct unnest(path) FROM search_graph;  -- duplicates possible
    

    Hope this helps.

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

Sidebar

Related Questions

I have a sqlite table representing a weighted directed graph. The columns are as
When should I consider representing the primary-key as classes ? Should we only represent
I'm trying to create a model representing graph node. class Node(models.model): ins = models.ManyToManyField(self,
I want to create a graph representing the frequency of updates to a site
I have a graph representing users and some articles they wrote. I need to
I've accessed the Facebook Graph API to get a JSON object representing the latest
I'm trying to create a system for representing and designing graphs in an easy
I have chosen to represent a graph in Haskell by a list of nodes
I have a class that represents undirected edges in a graph. Every edge has
I just got some crazy ideas for analyzing the Twitter social graph (i.e., representing

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.