I have a list of Nodes/Vertices, and a list of lines/edges connecting these nodes.The lists are not sorted or ordered in any way, but contain all the edges and nodes for a particular data set.
The edges are line segments defined by Cartesian coordinates, (x1,y1) and (x2,y2), and each node position is also represented by coordinates in the form (x,y). The image attached depicts a typical test case, clearly showing two trees, with roots R1 and R2, each Node, including leaf nodes (labelled Lx, and highlighted orange text and blue circles) is shown with corresponding coordinates.
class Node
{
Point coordinates; // x,y coordinates of node <int,int>
Node parentNode; // parent node of current node. ( may not be necessary as parentID may suffice to keep reference to parent node)
List<Node> children; // List of all child nodes of this node
List<Edge> edges; // list of all edges connected to this node
string Data; // relevant data of each node
long nodeID; // current nodes ID
long parentID; // ID of current node's parent node
}
And each edge is represented as:
class Edge
{
Point p1; // first end coordinates of line segment
Point p2; // coordinates of the other end of the segment
}
From the image attached, it is clear that Edge N1-N2 will be represented as either p1= (0,0), p2=(20,20) or p1 =(20,20), p2 = (0,0). the order is random.
Assumption 1: Nodes R1 and R2 can be clearly recognized as root nodes, because of the type of node on them. (Concentric circles with Red outer circle). Assumption 2: A list of all edges directly connected to a node are also available, e.g, node N8 will have segments :N8-L7,N8-R2,N8-N9, N8-N7.
My question is how do I write a function in C# that has two inputs,a List of edges and a List of nodes, and returns a root node, or root nodes of trees with reference to the child nodes, which would also be identical/true to the what is depicted in the drawing attached.
List<Node> getRootNodes(List<Node> nodes, List<Edge> edges)
{
// implementation here
List<Node> roots = new List<Node>();
//more logic
//
//
return roots; //returned list may have more than one root node!
}
I have been able to list each nodes edges, but can't figure out a way to construct the tree. I have read about Kruskal's Algorithm, but I'm not sure if i can adapt it to this problem. I'm not sure if it will preserve the order shown in the diagram.
All code is in C#, but any C style language solution will do.
NB:The answers I have seen on this website assume that the ordering of the tree nodes in terms of parent nodes and children is already known. I can tell that two nodes are connected by an edge, but cannot determine which node is the parent and which is the child node.
Thank you,
Greg M