क्यों जानें डेटा संरचनाएं और एल्गोरिदम?

इस लेख में, हम सीखेंगे कि प्रत्येक प्रोग्रामर को उदाहरणों की मदद से डेटा संरचना और एल्गोरिदम क्यों सीखना चाहिए।

यह लेख उन लोगों के लिए है, जिन्होंने अभी-अभी एल्गोरिदम सीखना शुरू किया है और सोचा है कि यह उनके करियर / प्रोग्रामिंग कौशल को बढ़ावा देने के लिए कितना प्रभावी होगा। यह उन लोगों के लिए भी है जो आश्चर्यचकित हैं कि Google, फेसबुक और अमेज़ॅन जैसी बड़ी कंपनियां प्रोग्रामर्स को क्यों किराया करती हैं, जो एल्गोरिदम को अनुकूलित करने में असाधारण रूप से अच्छे हैं।

एल्गोरिदम क्या हैं?

अनौपचारिक रूप से, एक एल्गोरिथ्म एक समस्या को हल करने के लिए कदमों के अलावा कुछ भी नहीं है। वे अनिवार्य रूप से एक समाधान हैं।

उदाहरण के लिए, भाज्य की समस्या को हल करने के लिए एक एल्गोरिथ्म कुछ इस तरह दिख सकता है:

समस्या: n के भाज्य का पता लगाएं

 आरम्भिक तथ्य = 1 से प्रत्येक मान v में 1 से n तक: गुणनखण्ड में v तथ्य से गुणा करने पर n का भाज्य होता है 

यहाँ, एल्गोरिथ्म अंग्रेजी में लिखा गया है। यदि यह एक प्रोग्रामिंग भाषा में लिखा गया था, तो हम इसे कोड के बजाय कहेंगे । यहाँ C ++ में एक संख्या के भाज्य को खोजने के लिए एक कोड है।

 int factorial(int n) ( int fact = 1; for (int v = 1; v <= n; v++) ( fact = fact * v; ) return fact; ) 

प्रोग्रामिंग सभी डेटा संरचनाओं और एल्गोरिदम के बारे में है। डेटा संरचनाओं का उपयोग डेटा धारण करने के लिए किया जाता है जबकि एल्गोरिदम का उपयोग उस डेटा का उपयोग करके समस्या को हल करने के लिए किया जाता है।

डेटा संरचना और एल्गोरिदम (डीएसए) विस्तार से मानक समस्याओं के समाधान के माध्यम से जाता है और आपको यह जानकारी देता है कि उनमें से प्रत्येक का उपयोग करना कितना कुशल है। यह आपको एक एल्गोरिथ्म की दक्षता का मूल्यांकन करने का विज्ञान भी सिखाता है। यह आपको विभिन्न विकल्पों में से सर्वश्रेष्ठ चुनने में सक्षम बनाता है।

आपका कोड स्केलेबल बनाने के लिए डेटा स्ट्रक्चर्स और एल्गोरिदम का उपयोग

समय कीमती है।

मान लीजिए, ऐलिस और बॉब पहले 10 11 प्राकृतिक संख्याओं का योग खोजने की एक सरल समस्या को हल करने की कोशिश कर रहे हैं । जबकि बॉब एल्गोरिथ्म लिख रहा था, ऐलिस ने यह साबित कर दिया कि यह डोनाल्ड ट्रम्प की आलोचना करने जितना आसान है।

एल्गोरिथम (बॉब द्वारा)

 1 से 1011 (समावेशी) में प्रत्येक प्राकृतिक संख्या n के लिए प्रारंभिक योग = 0: योग राशि में n जोड़ें आपका उत्तर है 

कोड (ऐलिस द्वारा)

 int findSum() ( int sum = 0; for (int v = 1; v <= 100000000000; v++) ( sum += v; ) return sum; ) 

एलिस और बॉब खुद को इस बात का अहसास करा रहे हैं कि वे लगभग कुछ ही समय में अपना कुछ बना सकते हैं। आइए उनके कार्यक्षेत्र में घुसते हैं और उनकी बातचीत सुनते हैं।

 ऐलिस: चलो इस कोड को चलाते हैं और योग का पता लगाते हैं। बॉब: मैंने कुछ मिनट पहले इस कोड को चलाया था लेकिन यह अभी भी आउटपुट नहीं दिखा रहा है। इसके साथ गलत क्या है?

ओह! कुछ गलत हो गया है! एक कंप्यूटर सबसे नियतांक मशीन है। वापस जा रहे हैं और इसे फिर से चलाने की कोशिश करने से मदद नहीं मिलेगी। तो आइए विश्लेषण करें कि इस सरल कोड में क्या गलत है।

कंप्यूटर प्रोग्राम के लिए दो सबसे मूल्यवान संसाधन समय और मेमोरी हैं

कोड चलाने के लिए कंप्यूटर द्वारा लिया गया समय है:

 कोड चलाने का समय = निर्देशों की संख्या * प्रत्येक निर्देश को निष्पादित करने का समय 

निर्देशों की संख्या आपके द्वारा उपयोग किए गए कोड पर निर्भर करती है, और प्रत्येक कोड को निष्पादित करने में लगने वाला समय आपके मशीन और कंपाइलर पर निर्भर करता है।

इस मामले में, निष्पादित किए गए निर्देशों की कुल संख्या (मान लीजिए x) है , जो हैx = 1 + (1011 + 1) + (1011) + 1x = 2 * 1011 + 3

आइए मान लें कि एक कंप्यूटर एक सेकंड में निर्देशों को निष्पादित कर सकता है (यह मशीन कॉन्फ़िगरेशन के अधीन भिन्न हो सकता है)। कोड से ऊपर चलने का समय हैy = 108

 कोड चलाने के लिए लिया गया समय = x / y (16 मिनट से अधिक) 

क्या एल्गोरिथ्म को अनुकूलित करना संभव है ताकि एलिस और बॉब को हर बार इस कोड को चलाने के लिए 16 मिनट तक इंतजार न करना पड़े?

मुझे यकीन है कि आपने पहले ही सही विधि का अनुमान लगा लिया था। प्रथम N प्राकृतिक संख्याओं का योग सूत्र द्वारा दिया गया है:

 सम = एन * (एन + १) / २ 

इसे कोड में बदलने से कुछ इस तरह दिखेगा:

 int sum (इंट एन) (रिटर्न एन * (एन + 1) / 2;) 

यह कोड सिर्फ एक निर्देश में निष्पादित होता है और कार्य को बिना किसी मूल्य के किया जाता है। ब्रह्मांड में परमाणुओं की कुल संख्या से अधिक होने दें। इसका परिणाम कुछ ही समय में मिल जाएगा।

समस्या को हल करने में लगने वाला समय, इस मामले में, 1/y(जो कि 10 नैनोसेकंड है)। वैसे, हाइड्रोजन बम की संलयन प्रतिक्रिया 40-50 एनएस लेती है, जिसका अर्थ है कि आपका कार्यक्रम सफलतापूर्वक पूरा होगा भले ही कोई आपके कंप्यूटर पर हाइड्रोजन बम फेंके उसी समय जब आप अपना कोड चलाते थे। :)

नोट: कंप्यूटर गुणन और विभाजन की गणना करने के लिए कुछ निर्देश (1 नहीं) लेते हैं। मैंने सिर्फ़ सादगी के लिए 1 कहा है।

स्केलेबिलिटी पर अधिक

स्केलेबिलिटी स्केल प्लस क्षमता है, जिसका अर्थ बड़े आकार की समस्या को संभालने के लिए एक एल्गोरिथ्म / सिस्टम की गुणवत्ता है।

50 छात्रों की कक्षा स्थापित करने की समस्या पर विचार करें। सबसे सरल समाधानों में से एक कमरा बुक करना, एक ब्लैकबोर्ड, कुछ चाक, और समस्या हल करना है।

लेकिन क्या होगा अगर समस्या का आकार बढ़ता है? यदि छात्रों की संख्या 200 हो गई तो क्या होगा?

समाधान अभी भी है, लेकिन इसे और अधिक संसाधनों की आवश्यकता है। इस मामले में, आपको संभवतः बहुत बड़े कमरे (शायद एक थिएटर), एक प्रोजेक्टर स्क्रीन और एक डिजिटल पेन की आवश्यकता होगी।

यदि छात्रों की संख्या बढ़कर 1000 हो गई तो क्या होगा?

समस्या के आकार में वृद्धि होने पर समाधान विफल हो जाता है या बहुत सारे संसाधनों का उपयोग करता है। इसका मतलब है, आपका समाधान मापनीय नहीं था।

फिर एक मापनीय समाधान क्या है?

Consider a site like Khanacademy, millions of students can see videos, read answers at the same time and no more resources are required. So, the solution can solve the problems of larger size under resource crunch.

If you see our first solution to find the sum of first N natural numbers, it wasn't scalable. It's because it required linear growth in time with the linear growth in the size of the problem. Such algorithms are also known as linearly scalable algorithms.

Our second solution was very scalable and didn't require the use of any more time to solve a problem of larger size. These are known as constant-time algorithms.

Memory is expensive

Memory is not always available in abundance. While dealing with code/system which requires you to store or produce a lot of data, it is critical for your algorithm to save the usage of memory wherever possible. For example: While storing data about people, you can save memory by storing only their age not the date of birth. You can always calculate it on the fly using their age and current date.

Examples of an Algorithm's Efficiency

Here are some examples of what learning algorithms and data structures enable you to do:

Example 1: Age Group Problem

Problems like finding the people of a certain age group can easily be solved with a little modified version of the binary search algorithm (assuming that the data is sorted).

The naive algorithm which goes through all the persons one by one, and checks if it falls in the given age group is linearly scalable. Whereas, binary search claims itself to be a logarithmically scalable algorithm. This means that if the size of the problem is squared, the time taken to solve it is only doubled.

Suppose, it takes 1 second to find all the people at a certain age for a group of 1000. Then for a group of 1 million people,

  • the binary search algorithm will take only 2 seconds to solve the problem
  • the naive algorithm might take 1 million seconds, which is around 12 days

The same binary search algorithm is used to find the square root of a number.

Example 2: Rubik's Cube Problem

Imagine you are writing a program to find the solution of a Rubik's cube.

This cute looking puzzle has annoyingly 43,252,003,274,489,856,000 positions, and these are just positions! Imagine the number of paths one can take to reach the wrong positions.

Fortunately, the way to solve this problem can be represented by the graph data structure. There is a graph algorithm known as Dijkstra's algorithm which allows you to solve this problem in linear time. Yes, you heard it right. It means that it allows you to reach the solved position in a minimum number of states.

Example 3: DNA Problem

DNA is a molecule that carries genetic information. They are made up of smaller units which are represented by Roman characters A, C, T, and G.

Imagine yourself working in the field of bioinformatics. You are assigned the work of finding out the occurrence of a particular pattern in a DNA strand.

It is a famous problem in computer science academia. And, the simplest algorithm takes the time proportional to

 (number of character in DNA strand) * (number of characters in pattern) 

A typical DNA strand has millions of such units. Eh! worry not. KMP algorithm can get this done in time which is proportional to

 (number of character in DNA strand) + (number of characters in pattern) 

The * operator replaced by + makes a lot of change.

Considering that the pattern was of 100 characters, your algorithm is now 100 times faster. If your pattern was of 1000 characters, the KMP algorithm would be almost 1000 times faster. That is, if you were able to find the occurrence of pattern in 1 second, it will now take you just 1 ms. We can also put this in another way. Instead of matching 1 strand, you can match 1000 strands of similar length at the same time.

And there are infinite such stories…

Final Words

Generally, software development involves learning new technologies on a daily basis. You get to learn most of these technologies while using them in one of your projects. However, it is not the case with algorithms.

यदि आप एल्गोरिदम को अच्छी तरह से नहीं जानते हैं, तो आप पहचान नहीं पाएंगे कि क्या आप उस कोड को अनुकूलित कर सकते हैं जिसे आप अभी लिख रहे हैं। आपसे अपेक्षा की जाती है कि आप उन्हें पहले से जान लें और जहाँ भी संभव हो और महत्वपूर्ण है, उन्हें लागू करें।

हमने विशेष रूप से एल्गोरिदम की मापनीयता के बारे में बात की थी। एक सॉफ्टवेयर सिस्टम में कई ऐसे एल्गोरिदम होते हैं। उनमें से किसी एक का अनुकूलन करने से बेहतर प्रणाली बनती है।

हालांकि, यह ध्यान रखना महत्वपूर्ण है कि सिस्टम को स्केलेबल बनाने का यह एकमात्र तरीका नहीं है। उदाहरण के लिए, वितरित कंप्यूटिंग के रूप में जानी जाने वाली तकनीक एक कार्यक्रम के स्वतंत्र भागों को एक साथ कई मशीनों तक चलाने की अनुमति देती है, जिससे यह और भी अधिक स्केलेबल बन जाता है।

दिलचस्प लेख...