Ever wondered why data structures and algorithms are clubbed together? You have interviews or exams which tests your grasp of data structures and algorithms. We have books which teaches both. How are they related? Let's find out.
How to prepare a cup of tea?
Where's that coming from? How is making tea even related to data-strutures and algorithms, you'll ask. I will request you hold on for a few minutes - just a few minutes.
This morning, I decided to prepare a cup of chaai (tea) for myself but I had no idea how to make it. I searched on Google for the same and found a number of articles about it. I made a really good cup of chaai by referring to this recipe.
I am quoting the method and ingredients specified on the site.
- Add water in pan.
- Add sugar, tea leaves and spices.
- Bring to boil and simmer for a minute or so.
- Add milk.
- Bring to boil and simmer for 3-5 minutes.
- Strain tea in teapot.
- 1 cup of water
- 1 teaspoon of sugar
- 1 teaspoon of tea leaves
- 1/4 teaspoon of spices
- 1/4 cup of milk
I have collected all the ingredients required. I know the method to prepare a cup. What else do I need? Think.
I need a container where I can keep all my ingredients as they are spread in all directions. I want to keep it in some container so that I can access easily.
Data, Data-Structure and Algorithm
I'll cheat here and tell you something or you must have realised it by now if you have followed our data-structure article.
The task or problem here was to prepare tea. The ingredients collected can be seen as data. The recipe is your algorithm and the container where you kept all the ingredients is your data-structure.
|The Chaai Example||Computer Science|
|Task (Preparing a cup of tea)||Problem (Solving a problem)|
Similar to a recipe which is required to prepare tea, we need algorithms which is a step-by-step guide to solve a problem. We also need ingredients (which may be used in one or more step described above) just as we need data. We also require a container to keep our ingredients which can be thought of as a data-structure.
There can be a number of other ways which we could use to prepare tea. Similary, there can be a number of algorithms or methods or step-by-step guide to solve a problem. Each recipe (algorithm) uses the ingredients (data) in different order and thus may need different types of container (data strutures).
In this article, we learnt about relationship between data strcutures and algorithm. We also conclude that there can be a different choice of data structures depending on the algorithm being used to solve a problem.