How to Make an Online Company Website Design Free Download Template HTML CSS JavaScript

Complete Responsive Online Company Website Design Template with HTML-CSS-JS – 100% Free Download

**Tree diameter**

A tree is an undirected graph in which all nodes are visitable and there are no cycles. Now we need to find the two farthest nodes of the tree, which is called the diameter of the tree.

Consider some computers with network cables attached as shown in the image below. Now you may want to know which two computers are farthest away.

## How to Make an Online Company Website Design Free Download Template HTML CSS JavaScript

It is very easy to figure out, and for that, you need to know either BFS or DFS. Any node in an undirected tree can be considered a root, we consider the top gray node to be the root of the tree.

Our first task is to find the farthest node from the root. Consider that node as X. It does not matter if the distance of multiple nodes is equal to the distance of the farthest node, any one must be selected. We can figure this out by running DFS/BFS.

Now the 2nd task is to run another DFS/BFS starting from node X to find the farthest node from X. Suppose the node is Y.

The distance between X and Y is the diameter of the tree! In the above image, the diameter is 7.

Evidence:

The algorithm is proven if we can prove that X found in step 1 will always be an edge of the diameter. Because step 2 must be correct if X is definitely an edge of the diameter, the farthest node from X will be the diameter of the tree.

We will prove this with the help of “Proof by Contradiction”. It is a widely used method. We want to prove that X must be the edge of a diameter. But if it is not, i.e. if X is not an edge of diameter then there is a diameter whose two edges are (say) a and b, where a and b can be any two nodes (except X). Using the a-b diameter we will show that it is possible to find another diameter that is greater than or equal to a-b and has X at one end. (Note, X is the farthest node from the root).

Now arbitrarily select two nodes a and b. Name the node that meets the path between the two nodes h, that is, h is the common ancestor of the two nodes. (Many of you may have understood that we are talking about the lowest common ancestor here).

But the distance of b from the root must be less than or equal to the distance of X from the root, not more because X is the farthest node from the root.

ie:

distance(root,b)<=distance(root,X)

Again it is also certain that the distance b is from the root, the distance h from b is less than or equal to it. (Equal if h and the root are the same node i.e. if a and b are on both sides of the root)

distance(h,b)<=distance(root,b)

then,

distance(h,b)<=distance(root,b)<=distance(root,X)

distance(h,b)<=distance(root,X)

Then if we replace the h-b path with the root-X path and insert the path between h-x (if h and x are not equal), we will definitely get a path equal to or greater than the previous one!

That means no matter which two nodes we select except X, a path greater than or equal to X will be found. That means X must be an edge of the diameter!

Complexity:

The complexity of BFS/DFS is equal to: O(V+E) where V is the number of nodes and E is the number of edges.

This algorithm will not work on general graphs, only on trees. The problem of finding the longest path in a general graph is n-p hard, and the problem of finding the diameter is a special case of the longest path problem.

Farthest Nodes in a Tree

Farthest Nodes in a Tree (II)

Happy Coding!

Data Structure: Tri (Prefix Tree/Radix Tree)

Strings can be easily retrieved from a database using a trie or prefix tree. Suppose a phone book contains the phone numbers of all the people in a city. There are people in the city, maybe a few targets, but each person’s name can be written using a maximum of 20 characters. We will search for names using a data structure that depends only on how many characters are in the name. For example searching for the name “Alice” requires only 5 operations no matter how large the database is.

Before reading this article you should have an understanding of linked list and recursion.

*How to Make an Online Company Website Design Free Download Template HTML CSS JavaScript*

*How to Make an Online Company Website Design Free Download Template HTML CSS JavaScript*

Suppose you are given a dictionary containing the following words:

algo

algae

also

tom

to

Now we will try to keep this dictionary in memory in such a way that any word can be found very easily. One way is to sort words that are kept in paper dictionaries, then we can find a word by doing binary search. Another way is to use the prefix tree or trie for short. The word trie comes from the word “retrieval”. As such it should be pronounced “tree” but in graph theory tree has a more widespread use so it is called “tri”. Prefix means creating a new string from the beginning of a string with some characters. For example, the prefix of blog is b,bl,blo and blog.

In the beginning we have nothing but a root node.

Now we will add the word algo. See how the words are added in the image below. From the root node we will give an edge which will be named “a”. Then we will create an edge named “l” from the newly created node. In this way, I will create two edges “g” and “o”. Note that we are not storing any data in the node, just extracting the edge from the empty node.

Now we want to add the word algea. Need edge named “a” from root, it is already there, no need to add new one. Similarly a to l and l to g also have edges. That means “alg” is already in try, we just add e and a.

I will add the word also now. The “al” prefix from the root is already there, just add “so”.

Now add “tom”. Now a new edge must be created from the root because no prefix of tom has been added before.

How to add the word “to” now? “to” is completely a prefix of tom so no new edge needs to be added. What we can do is put some end-marks on the nodes. At the nodes where at least one word is complete we put endmarks, endmarks are indicated by gray color in the image. Adding endmarks to all previous words plus the new word “to” would look like this:

Surely you understand why I put the end marks. By looking at the mark, it is easy to understand which words are in trie. The information about which character is being taken will be at the edge, and the endmarks will be at the node.

## Before Download

## You must Join our Facebook Group and Subscribe YouTube Channel

**All Links in Below:**

**Join Our FreeWebsiteCreate Facebook Group to get an instant update for projects, templates, design resources, and solutions.**

**Join Our YouTube Channel & Subscribe with Bell Icon for New Video:**

## Join Our Official Facebook Page For the Latest updates All Code Projects are Free:

## Visit our service page to get **premium services.**

Free Website Create – HTML CSS, PHP, JavaScript Programming Projects For Free

**Follow Us**

**Thank You,**

## Before Download

## You must Join our Facebook Group and Subscribe YouTube Channel

FreeWebsiteCreate.net tries to provide HTML, CSS, SCSS, JavaScript, React, Android Studio, Java, PHP, Laravel, Python, Django, C#(C Sharp), and ASP.net-related projects 100% free. We try to make learning easier. Free Website Create always tries to give free projects to new learners. Free projects and source code will help to learn quickly.

They can save time and learn more. In this post, we share a free portfolio project website code with HTML and CSS. This free code portfolio contains a single landing page with a responsive design. In this post, we get a free best carpenter and craftsman service website designed by FreeWebsiteCreate with HTML, CSS, Bootstrap, and JavaScript.