0

I'm taking this term a course in C, and I have got an assignment which deals namely with pointers - building an m-ary tree.

Some description: We receive at the command line arguments: the file name of a text and two numbers which represent the keys of some two vertices in the graph (I will explain later what we have to do with these two vertices).

The first line of the text is actually the total number of the vertices of the graph, the next line could for example include numbers like "2 5" which implies that vertices 2 and 5 are children of vertex with key 0, the next line may include "6 0" which says that vertex with key of 1 is the father of 6 and 0 vertices, and so on...

If some line contains only '-' then it's a leaf. This part actually deals with parsing and defining the suitable structure for vertex, and I have already done that (but I have to take care of corner cases later on...).

Now, my problem begins - we have to find the number of edges in the tree in Big O of 1 time complexity; find the root in Big O of n (where n is the number of vertices) time complexity; find the simple shortest path between the two vertices (I think it can also be done with BFS) we received at the command line in Big O of n squared time complexity; find the minimal and maximum heights of the tree; find the diameter of the tree in Big O of n squared time complexity.

To implement it, we have to use BFS and we can use their implementation of queue.

Here is my vertex struct:

    typedef struct Vertex {
    size_t key;
    unsigned int amountOfNeighbors; // The current amount of neighbors
    unsigned int capacity; // The capacity of the neighbors (It's updating during run-time)
    struct Vertex* parent; 
    struct Vertex** neighbors; // The possible parent and children of a vertex
    } Vertex;

I have went through the pseudo-code of BFS and it uses the idea of the next and previous vertices of a vertex - it's a concept which is not used in my implementation and I really don't know how I can mingle it with my code properly...

Secondly, I have no idea how I can calculate the number of edges in the tree in O(1) - it seems impossible - it requires me to go through all the vertices at least once which is O(n)...

So I actually need help to adjust the BFS algorithm to my needs, and find a way to calculate the number of edges in constant time complexity.

Thanks in advance!

HelpMe
  • 91
  • 10
  • Re "*find the simple shortest path between the two vertices (I think it can also be done with BFS)*", You can. Start with one of the nodes, and search up and down the tree. Avoid backtracking, which is simple since you already need to keep track of the path you took to reach the current node. Simply avoid visiting the most recently-added node (`Vertex*`). – ikegami Nov 23 '19 at 23:24
  • 1
    Re "*Secondly, I have no idea how I can calculate the number of edges in the tree in O(1)*", If this is truly a tree, there are no cycles, so the number of edges is equal to the number of vertices plus one. So, if you keep track of how many descendants each node has, the tree would have `root->num_descendants` edges. Simply add/delete one of the count of descendants in each ancestors when you add/delete. Of course, this adds a cost to addition and deletion, but not necessarily a change in O(). Remember, O(N)+O(N) is still O(N). – ikegami Nov 23 '19 at 23:29
  • @ikegami Okay. As for the number of edges - you gave me a perspective - the number edges of a tree is the number of vertices plus 1. Actually, during run-time parsing of the text - I catch the number of the vertices - so it can actually be done without this additional field. As for the BFS one, my problem is deeper - I don't know how to implement BFS which is suitable to my Vertex struct. And I'm not sure I understand what you mean with seaching up and down, most recently-added node... Can you explain in detail?.. – HelpMe Nov 23 '19 at 23:35
  • 1
    Re "*so it can actually be done without this additional field.*", Sure, you can keep track of it outside of the tree too. I didn't think that was acceptable :) – ikegami Nov 23 '19 at 23:36
  • up=parent, down=children. Most searches start at the root and repeatedly add the children to the queue. You'd use this approach to find one of the nodes. Then you'd start a second search from there. You'd add the parent and the children to the queue instead of just the children, although you'd skip adding the node which lead you to the current node. – ikegami Nov 23 '19 at 23:39
  • @ikegami What do up and down actually represent in the pseudo-code? prev and next respectively? How do I know which child to choose every time? Sorry, but I don't realize exactly the process... – HelpMe Nov 23 '19 at 23:42
  • How about [valid Perl code](https://pastebin.com/feVeGzUr)? The first loop is your standard root-based search. The second is the one that starts at one of the nodes, and visits both parent and children. – ikegami Nov 23 '19 at 23:58
  • @ikegami Is it a programming language? I really appreciate that, but I don't know this syntax at all... – HelpMe Nov 24 '19 at 00:01
  • It's almost identical to C. Just ignore the symbols you don't recognize and you have pseudo-code :) – ikegami Nov 24 '19 at 00:02
  • @ikegami I don't succeed to catch the logical flow in the Perl code. What queue we start with? What do you check at the first loop? No idea what's going on... Sorry I can't understand it well – HelpMe Nov 24 '19 at 00:08
  • I thought you knew how to do a BFS? You basically keep a queue of things to visit along with any state you need to carry forth. (A DFS is the same, except we use a stack.) In the initial search, we don't have additional states, just the nodes to visit. We keep processing the list until we find the node in which we are interested. If it's not the node of interest, we add all connected nodes to the todo list except the one from which we came. In the first loop, we always came from the parent, so we simply add the children. – ikegami Nov 24 '19 at 00:09
  • @ikegami I know in principle, when it involves the concept of previous and next vertices... – HelpMe Nov 24 '19 at 00:11
  • There's no previous and next vertices in a tree; there are parent and children vertex – ikegami Nov 24 '19 at 00:12
  • https://en.wikipedia.org/wiki/Breadth-first_search – ikegami Nov 24 '19 at 00:13
  • @ikegami What do you mean by "state"? Can you explain in detail the process? We just start simply from the root and go down to some random child of it, and down again to some random child of the former child and so on...? Then if you "meet" one of the two vertices - what do you do? If you don't meet one of them - what do you do?.. – HelpMe Nov 24 '19 at 00:19
  • Re "*What do you mean by "state"?*", Whatever info you need to pass forward as you search. In the search for the first node, there isn't anything you need to pass forward, but in the search for the second node, you need to keep track of the edges used to get to the current point in the search – ikegami Nov 24 '19 at 03:15
  • @ikegami Can you see in this context this topic: https://stackoverflow.com/questions/59019909/finding-the-diameter-of-m-ary-tree-c – HelpMe Nov 24 '19 at 17:13

0 Answers0