Algorithm - David-Chae/Algorithms_Notes_Solutions GitHub Wiki
Define algorithm and give an example to explain it.
- "Algorithm refers to a sequence of finite steps to solve a particular problem."
- "An Algorithm is a procedure to solve a particular problem in a finite number of steps for a finite-sized input."
- "A set of rules to be followed in calculations or other problem-solving operations".
- "A procedure for solving a mathematical problem in a finite number of steps that frequently by recursive operations".
- "An algorithm is a well-defined sequential computational technique that accepts a value or a collection of values as input and produces output(s) needed to solve a problem. Or we can say that an algorithm is said to be accurate if and only if it stops with the proper output for each input instance."
Real-life examples that define the use of algorithms:
- "Consider a box where no one can see what’s happening inside; we say a black box. We give input to the box and it gives us the output we need but the procedure that we might need to know behind the conversion of input to desired output is an ALGORITHM."
- "Seen someone cooking your favorite food for you? Is the recipe necessary for it? Yes, it is necessary as a recipe is a sequential procedure that turns a raw potato into a chilly potato. This is what an algorithm is: following a procedure to get the desired output. Is the sequence necessary to be followed? Yes, the sequence is the most important thing that has to be followed to get what we want as we know WYWIWYG (What you want is what you get)."
Why learn algorithms and data structures?
- you want to crack the interviews and get into the product based companies
- you love to solve real-world complex problems.
To Crack the Interviews of the Top Product Based Companies
Do you know that under the hood all your SQL and Linux commands are algorithms and data structures? You might not realize this, but that’s how the software works.
Data structures and algorithms play a major role in implementing software and in the hiring process as well. A lot of students and professionals have the question of why these companies’ interviews are focused on DSA instead of language/frameworks/tools specific questions? Let us explain why it happens…
When you ask someone to make a decision for something the good one will be able to tell you “I choose to do X because it’s better than A, B in these ways. I could have gone with C, but I felt this was a better choice because of this“. In our daily life, we always go with that person who can complete the task in a short amount of time with efficiency and using fewer resources. The same things happen with these companies. The problem faced by these companies is much harder and on a much larger scale. Software developers also have to make the right decisions when it comes to solving the problems of these companies.
Knowledge of DS and Algo like Hash Tables, Trees, Graphs, and various algorithms goes a long way in solving these problems efficiently and the interviewers are more interested in seeing how candidates use these tools to solve a problem. Just like a car mechanic needs the right tool to fix a car and make it run properly, a programmer needs the right tool (algorithm and data structure) to make the software run properly. So the interviewer wants to find a candidate who can apply the right set of tools to solve the given problem. . If you know the characteristics of one data structure in contrast to another you will be able to make the right decision in choosing the right data structure to solve a problem.
Engineers working in Google, Microsoft, Facebook, Amazon-like such companies are different than others and paid higher as compared to other companies…but why? In these companies coding is just the implementation and roughly takes 20-30% of the time allotted to a project. Most of the time goes into designing things with the best and optimum algorithms to save on the company’s resources (servers, computation power, etc). This is the main reason why interviews in these companies are focused on algorithms as they want people who can think out of the box to design algorithms that can save the company thousands of dollars. Youtube, Facebook, Twitter, Instagram, GoogleMaps all these sites have the highest number of users in the world. To handle more users on these sites it requires more optimization to be done and that’s the reason product-based companies only hire candidates who can optimize their software as per user demand.
Example: Suppose you are working in a Facebook company. You come up with an optimal solution of a problem (like sorting a list of users from India) with time complexity of O(nLogn) instead of O(n^2) and assume that n for the problem here for the company in real life scenario is 100 million (very fair assumption considering the number of users registered on Facebook exceeds 1 billion). nLogn would be 800 million, while n^2 would be 10^7 billion. In cost terms, you can see that the efficiency has been improved more than 10^7 times, which could be a huge saving in terms of server cost and time. Now you might have got that companies want to hire a smart developer who can make the right decision and save company resources, time, and money. So before you give the solution to use a Hash table instead of List to solve a specific problem think about the big scale and all the case scenarios carefully. It can generate revenue for the company or the company can lose a huge amount of money.
To Solve Some Real-World Complex Problems
Have you ever been scolded by your parents when you were unable to find your book or clothes in your messed-up room? Definitely yes…your parents are right when they give the advice to keep everything in the right place so the next time you can get your stuff easily. Here you need to arrange and keep everything (data) in such a structure that whenever you need to search for something you get that easily and as soon as possible. This example gives a clear idea that how important it is to arrange or structure the data in real life.
Now take the example of a library. If you need to find a book on Set Theory from a library, you will go to the maths section first, then the Set Theory section. If these books are not organized in this manner and just distributed randomly then it will be frustrating to find a specific book. So data structures refer to the way we organize information on our computers. Computer scientists process and look for the best way we can organize the data we have, so it can be better processed based on the input provided.
A lot of newbie programmers have this question that where we use all the stuff of data structure and algorithms in our daily life and how it’s useful in solving the real-world complex problem. We need to mention that whether you are interested in getting into the top tech giant companies or not DSA concepts still help a lot in your day-to-day life. Don’t you believe us…Let’s consider some examples…
Facebook (Yes… we are talking about your favourite application). Can you just imagine that your friends on Facebook, friends of friends, mutual friends they all can be represented easily by Graph? Relax….sit for a couple of moments and think again…you can apply a graph to represent friends’ connections on Facebook. If you need to keep a deck of cards and arrange it properly how would you do that? You will throw it randomly or you will arrange the cards one over another and from a proper deck. You can use Stack here to make a proper arrangement of cards one over another. If you need to search for a word in the dictionary, what would be your approach? Do you go page by page or do you open some page and if the word is not found you open a page prior to/later to one opened depending upon the order of words to the current page (Binary Search). The first two were a good example of choosing the right data structure for a real-world problem and the third one is a good example of choosing the right algorithm to solve a specific problem in less amount time.
All the above examples give you a clear understanding that how the organization of data is really important in our day-to-day life. Arranging data in a specific structure is really helpful in saving a lot of time and it becomes easier to manipulate or use them. The same goes for the algorithm…we all want to save our time, energy and resources. We all want to choose the best approach to solve the problems in our daily life. A lot of problems exist in the world that can take hours or days to be solved with the native solution, it also may take years ! can you imagine! watch this: Importance of Data Structure and Algorithms We are surrounded by a lot of real-world complex problems for which no one has the solution. Observe the problems in-depth and you can help this world by giving the solution that no one has given before.
Data structure and algorithms help in understanding the nature of the problem at a deeper level and thereby a better understanding of the world.
If you want to know more about Why Data Structures and Algorithms then you must watch this video of Mr. Sandeep Jain (CEO & Founder, GeeksforGeeks).
Identify and explain the characteristics of algorithms.
In order for some instructions to be an algorithm, it must have the following characteristics:
- Clear and Unambiguous: The algorithm should be clear and unambiguous. Each of its steps should be clear in all aspects and must lead to only one meaning.
- Well-Defined Inputs: If an algorithm says to take inputs, it should be well-defined inputs.
- Well-Defined Outputs: The algorithm must clearly define what output will be yielded and it should be well-defined as well.
- Finite-ness: The algorithm must be finite, i.e. it should terminate after a finite time.
- Feasible: The algorithm must be simple, generic, and practical, such that it can be executed with the available resources. It must not contain some future technology or anything.
- Language Independent: The Algorithm designed must be language-independent, i.e. it must be just plain instructions that can be implemented in any language, and yet the output will be the same, as expected.
State the properties of algorithm.
It should terminate after a finite time. It should produce at least one output. It should take zero or more input. It should be deterministic means giving the same output for the same input case. Every step in the algorithm must be effective i.e. every step should do some work.
Why do we use algorithms?
Consider two kids, Aman and Rohon, solving the Rubik’s Cube. Aman knows how to solve it in a definite number of steps. On the other hand, Rohon knows that he will do it but is not aware of the procedure. Aman solves the cube within 2 minutes whereas Rohan is still stuck until the end of the day. So the time required to solve with a procedure/algorithm is much more effective than that without any procedure. Hence the need for an algorithm is a must.
Explain algorithm complexity.
An algorithm is analysed using Time Complexity and Space Complexity. Writing an efficient algorithm help to consume minimum amount of time for processing the logic.
For an algorithm A, it is judged on the basis of two parameters for an input of size n :
Time Complexity: Time taken by the algorithm to solve the problem. It is measured by calculating the iteration of loops, number of comparisons etc. Space Complexity: Space taken by the algorithm to solve the problem. It includes space used by necessary input variables and any extra space (excluding the space taken by inputs) that is used by the algorithm. For example, if we use a hash table (a kind of data structure), we need an array to store values so this is an extra space occupied, hence will count towards the space complexity of the algorithm. This extra space is known as Auxiliary Space.
Reference:
https://www.geeksforgeeks.org/fundamentals-of-algorithms/?ref=shm https://www.geeksforgeeks.org/introduction-to-algorithms/?ref=lbp https://www.geeksforgeeks.org/what-is-an-algorithm-definition-types-complexity-examples/?ref=lbp https://www.geeksforgeeks.org/algorithms-design-techniques/?ref=lbp https://www.geeksforgeeks.org/why-data-structures-and-algorithms-are-important-to-learn/?ref=lbp