traverse graph code snippet?

Posts   
 
    
MatthewM
User
Posts: 78
Joined: 26-Jul-2006
# Posted on: 01-Sep-2006 17:38:35   

I am searching for code snippets to traverse a graph. If anyone has a thread link or can paste it in that'd be great. I don't mind writing it, but don't like the idea of using a seperate list to track unique nodes visited and pass it through the recursion. Better mousetraps around?

should I just use ProduceTopologyOrderedList ? Is there extra overhead there that would be reason to reinvent this?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 01-Sep-2006 17:54:23   

What do you want to do with the graph/during the traversal? The ProduceTopologyOrderedList is a flat list with the complete graph in the right order (i.e. if A -> B, then B is placed before A). Topology sorting is sorting a directed a-cyclic graph.

Frans Bouma | Lead developer LLBLGen Pro
MatthewM
User
Posts: 78
Joined: 26-Jul-2006
# Posted on: 01-Sep-2006 18:02:16   

Otis wrote:

What do you want to do with the graph/during the traversal? The ProduceTopologyOrderedList is a flat list with the complete graph in the right order (i.e. if A -> B, then B is placed before A). Topology sorting is sorting a directed a-cyclic graph.

I want a list of all nodes in the graph. Order is not relevant.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 01-Sep-2006 18:47:47   

MatthewM wrote:

Otis wrote:

What do you want to do with the graph/during the traversal? The ProduceTopologyOrderedList is a flat list with the complete graph in the right order (i.e. if A -> B, then B is placed before A). Topology sorting is sorting a directed a-cyclic graph.

I want a list of all nodes in the graph. Order is not relevant.

Then that routine should bring you the list simple_smile It will traverse the graph for you and place them in the right order. As order isn't important, that's a list you can use simple_smile

Frans Bouma | Lead developer LLBLGen Pro
MatthewM
User
Posts: 78
Joined: 26-Jul-2006
# Posted on: 01-Sep-2006 18:57:23   

I feel stupid, but I do not see ObjectGraphUtils in SD.LLBLGen.Pro.ORMSupportClasses

So, I just wrote this - untested wink


        private List<IEntity> TraverseEntityCollection(IEntityCollection collection)
        {
            List<IEntity> finalList = new List<IEntity>();
            foreach(IEntity eb in collection)
            {
                finalList.Add(eb);
                subTraverseEntityCollection(eb, finalList);
            }
            return finalList;
        }

        private void subTraverseEntityCollection(IEntity eb, List<IEntity> finalList)
        {
            List<IEntity> list1 = eb.GetDependingRelatedEntities();
            foreach (IEntity subEB in list1)
            {
                finalList.Add(subEB);
                subTraverseEntityCollection(subEB, finalList);
            }

            List<IEntityCollection> list2 = eb.GetMemberEntityCollections();
            foreach (IEntityCollection sublist in list2)
            {
                foreach (IEntity subEB in sublist)
                {
                    finalList.Add(subEB);
                    subTraverseEntityCollection(subEB, finalList);
                }
            }
        }

The problem I see is that I have some entities which refer to themselves. I have the hunch that GetDependingRelatedEntities is going to throw me into infinite recursion. If so, will have to check finalList before adding a node and recursing.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 01-Sep-2006 20:16:51   

The graph sorter is pretty quick and has this build in wink

Frans Bouma | Lead developer LLBLGen Pro
MatthewM
User
Posts: 78
Joined: 26-Jul-2006
# Posted on: 01-Sep-2006 22:56:43   

Truly I would love to use it..but I do not see ObjectGraphUtils in SD.LLBLGen.Pro.ORMSupportClasses

SD.LLBLGen.Pro.ORMSupportClasses.ObjectGraphUtils o = new SD.LLBLGen.Pro.ORMSupportClasses.ObjectGraphUtils();

does not compile. I'm worried I have been staring at my own code too long. Please feel free to point out the obvious.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 02-Sep-2006 00:02:34   

get the latest build simple_smile It's now public simple_smile

Frans Bouma | Lead developer LLBLGen Pro
MatthewM
User
Posts: 78
Joined: 26-Jul-2006
# Posted on: 07-Sep-2006 00:46:20   

Otis wrote:

get the latest build simple_smile It's now public simple_smile

belated thanks.