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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:33:34+00:00 2026-05-13T22:33:34+00:00

I have a simple weighted graph A 1 / \\ 0.5 / \\0.5 B

  • 0

I have a simple weighted graph

    A
 1 / \\ 0.5
  /   \\0.5
 B     C

Suppose this describes a family and A is the father, B is the son and C is the mother. Let’s say B is studying in an university and A has bought an apartment for him. A is living with C in a house which is commonly owned, 50-50.

I want to transform the graph into a tree, starting from A: ie.

  • A owns 50% of the place C is living in
  • A owns 100% of the place B is living in
  • C owns 50% of the place A is living in

The graph and the generated tree could be more elaborate but I hope you get the more general picture.

On SQL Server 2005 I have

Drop Table #graph;
Create Table #graph
    (FirstVertex VarChar(1) Not Null, 
     SecondVertex VarChar(1) Not Null, 
     Weight float);

Insert #graph Values('A','B',1);
Insert #graph Values('A','C',0.5);
Insert #graph Values('C','A',0.5);

and I’m using the following common table expression to traverse the graph, starting from ‘A’:

With GraphRecursion (FirstVertex, SecondVertex, Weight, Level)
As
(
    Select FirstVertex, SecondVertex, Weight, 0 As Level
    From #graph
    Where FirstVertex='A'

    Union all

    Select a.FirstVertex, a.SecondVertex, a.Weight, b.Level+1
    From #graph a 
    Inner Join GraphRecursion b
    On a.FirstVertex=b.SecondVertex --And b.Level<=1
)
Select * From GraphRecursion;

This causes

Msg 530, Level 16, State 1, Line 11
The statement terminated. The maximum recursion 100 has 
been exhausted before statement completion.

Limiting the level of recursion by uncommenting the And b.Level<=1 gives the expected results, but that’s obviously not very useful for any practical use.

Is there a way to reference the previous iterations so that in the above example edges (ie. the FirstVertex, SecondVertex pairs) would not be repeated?

  • 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-13T22:33:34+00:00Added an answer on May 13, 2026 at 10:33 pm

    You could build up a list of visited nodes in another column and then prevent re-visiting them in your recursion. Something like this (not sure I’ve picked the right columns):

    With GraphRecursion (FirstVertex, SecondVertex, Weight, Level,Nodes)
    As
    (
        Select FirstVertex, SecondVertex, Weight, 0 As Level,CONVERT(varchar(8000),':' + FirstVertex + ':' + SecondVertex + ':')
        From #graph
        Where FirstVertex='A'
    
        Union all
    
        Select a.FirstVertex, a.SecondVertex, a.Weight, b.Level+1,b.Nodes + ':' + a.SecondVertex  + ':'
        From #graph a 
        Inner Join GraphRecursion b
        On a.FirstVertex=b.SecondVertex --And b.Level<=1
        where not b.Nodes like '%:' + a.SecondVertex + ':%'
    )
    Select * From GraphRecursion;
    

    If you want to avoid every re-traversing an edge, rather than ever re-visiting a vertex, you’d build up your links as e.g. ‘:’ + FirstVertex + ‘@’ + SecondVertex + ‘:’. In these examples, I’m just using ‘:’ and ‘@’ as characters that don’t appear in vertex names. (Avoiding re-traversal – closer to results with b.Level <= 1, but not quite):

    With GraphRecursion (FirstVertex, SecondVertex, Weight, Level,Nodes)
    As
    (
        Select FirstVertex, SecondVertex, Weight, 0 As Level,CONVERT(varchar(8000),':' + FirstVertex + '@' + SecondVertex + ':')
        From #graph
        Where FirstVertex='A'
    
        Union all
    
        Select a.FirstVertex, a.SecondVertex, a.Weight, b.Level+1,b.Nodes + ':' + a.FirstVertex + '@' + a.SecondVertex  + ':'
        From #graph a 
        Inner Join GraphRecursion b
        On a.FirstVertex=b.SecondVertex --And b.Level<=1
        where not b.Nodes like '%:' + a.FirstVertex + '@' + a.SecondVertex + ':%'
    )
    

    (Note that the original b.Level <= 1 version gets 5 rows from this sample, rather than the four from my second example above). But I believe it’s correct. The b.Level <= 1 version returns a Level 2 row for a->c, c->a, a->c, which I don’t think we want

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

Sidebar

Related Questions

I have a simple weighted tree with a root, nodes and leafs. Now I
I have simple regex \.*\ for me its says select everything between and ,
Ok, i have simple scenario: have two pages: login and welcome pages. im using
In general, is it a best practice to have simple POJO Java classes implement
I develop tools in Autodesk Maya. Many of the tools I build have simple
Should simple JavaBeans that have only simple getters and setters be unit tested?? What
I have a simple webform that will allow unauthenticated users to input their information,
I have a simple 2-column layout with a footer that clears both the right
I have a simple page with my ScriptManager and my UpdatePanel , and my
I have a simple little test app written in Flex 3 (MXML and some

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.