473,396 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

converting adjacency matrix into a tree

Hi all,

I'm looking for a fast algorithm to do the following:
A DataTable has the following columns: ID, ParentID, Title, Body, etc. It
represents webforum conversation threads. ParentID points to the ID of the
parent post.
I'm trying to convert this into a tree structure in memory (e.g. XML).

I understand this can be done with recursion but my algorithm seems to be
too slow, i.e. there are too many loops. Does anyone have an example of a
better algorithm?

Thanks.

Mine is (pseudocode):

For each row in table
{
if parent == 0 // top element
{
row.visited = true;
AttachToTree()
RecursivelySearchForChildRen(row)
}
}

method RecursivelySearchForChildRen(parent)
{
Foreach row in table // TODO need to eliminate this extra loop, too
much iterating
{
if row.visited == false && row.parent == parent
{
row.visited = true;
AttachToParent()
RecursivelySearchForChildRen(row) // call itself again
}
}
}

-Oleg.
Nov 16 '05 #1
2 3742
This probably has small chance of working without me implementing and
testing it, but it's a welcome break from writing specs for me, and I hope
it helps. :) It should perform better than O(n^2). By the way, if your
data is already in a friendly order (parents before children or vice versa)
you could write something very simple that would be wicked fast.

// "map" is an object that maps from an int id to a node object
// "node" is a class holds a row object and a parent node object
foreach row in table
{
if (!map.Exists(row.id))
{
// create parent placeholder node if necessary
if (!map.Exists(row.parentid))
map[row.parentid] = new node(null, null);

// create new node with row and link to parent node
map[row.id] = new node(row, map[row.parentid]);
}
else if (map[row.id].row is null) // node was created as mere
placeholder earlier?
{
// create parent placeholder node if necessary
if (!map.Exists(row.parentid))
map[row.parentid] = new node(null, null);

// fill in this node now with row and link to parent node
map[row.id].row = row;
map[row.id].parentnode = map[row.parentid]
}
}

Brad Williams
Nov 16 '05 #2

This is not bad at all. I've tested it.
However, in my scenario, I need to convert ID-ParentID rows of a DataTable
into a Row-ChildrenCollection.
In other words, here's the class structure:

class Node
{
int id;
string name;
NodeCollection subnodes;
}

Your code produces a list of leaves with the ability of walking up to the
top element.
I'm trying to write the opposite, e.g. to get a list of top nodes. Each node
has a pointer to all its children.
Thanks,

-Oleg.

"Brad Williams" <sp**@spam.com> wrote in message
news:c8**********@news01.intel.com...
This probably has small chance of working without me implementing and
testing it, but it's a welcome break from writing specs for me, and I hope
it helps. :) It should perform better than O(n^2). By the way, if your
data is already in a friendly order (parents before children or vice versa) you could write something very simple that would be wicked fast.

// "map" is an object that maps from an int id to a node object
// "node" is a class holds a row object and a parent node object
foreach row in table
{
if (!map.Exists(row.id))
{
// create parent placeholder node if necessary
if (!map.Exists(row.parentid))
map[row.parentid] = new node(null, null);

// create new node with row and link to parent node
map[row.id] = new node(row, map[row.parentid]);
}
else if (map[row.id].row is null) // node was created as mere
placeholder earlier?
{
// create parent placeholder node if necessary
if (!map.Exists(row.parentid))
map[row.parentid] = new node(null, null);

// fill in this node now with row and link to parent node
map[row.id].row = row;
map[row.id].parentnode = map[row.parentid]
}
}

Brad Williams

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Lars Tengnagel | last post by:
I'm trying to use the matrix variable to collect a matrix in the file MatrixInfo which contains a lot of different matrices. Yhe commented line is the problem. HOW ??????? import re import...
1
by: SallyBenjamin | last post by:
Hello.. Can anyone help me with this coding. Basically, it needs to have add node, remove node, add edges , remove edges and display the graph But.I have only succedded to add node, add and...
0
by: Zoran Stipanicev | last post by:
Hi! The question is product of laziness. I want to write generic operators for matrix computation which would adapt to different types of matrices i.e. when adding square matrix and lower...
2
by: Stipanicev | last post by:
I want to write generic operators for matrix computation which would adapt to different types (shapes) of matrices i.e. when adding square matrix and lower triangle matrix it would add only...
12
by: Steve | last post by:
I have been studying the Adjacency List Model as a means of achieving a folder structure in a project I am working on. Started with the excellent article by Gijs Van Tulder ...
2
by: sanjeevron | last post by:
Define a class for an adjacency matrix representation of weighted, directed graphs in C++. 2. Implement the following essential methods in your class. Any input validation or exceptions must be...
1
by: madhuparna | last post by:
Plzz give the code for implementing adjacency matrix in C
1
by: mark | last post by:
I have a 5x5 Matrix. Each cell consists a string value(ex:URL of a website) I want to find out which string value occurs( i.e. repeats ) maximum no. of times in the matrix. I have a hint...
0
by: kantai | last post by:
Need help in solving the problem below. Any help would be highly appreciated Implement the adt graph as a C++ class first by using an adjacency matrix to represent the graph then by using an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.