Fundamentals of Algorithms أسس الخوارزميات (Algorithms)

Understanding the logic, structure, and lifecycle of computational problem solving. فهم المنطق، والهيكل، ودورة حياة حل المشكلات الحسابية.

Definition التعريف 5 Characteristics الخصائص الـ 5 Pseudocode الكود الزائف (Pseudocode) Analysis Basics أساسيات التحليل

The Core Definition التعريف الجوهري

An Algorithm is not just code. It is a sequence of clear instructions for solving a problem to acquire a required output for any legitimate input in a finite amount of time. الـ Algorithm ليست مجرد كود. إنها تسلسل من التعليمات الواضحة لحل مشكلة ما للحصول على مخرجات مطلوبة لأي مدخلات شرعية في فترة زمنية محدودة (Finite).

Think of it as a recipe: The ingredients are the input, the cooking steps are the algorithm, and the delicious meal is the output. فكر فيها كوصفة طعام: المكونات هي المدخلات (Input)، وخطوات الطهي هي الخوارزمية (Algorithm)، والوجبة اللذيذة هي المخرجات (Output).

The Problem المشكلة
Algorithm
In Input
Out Output

Characteristics of an Algorithm خصائص الخوارزمية

Input Input (المدخلات)

Zero or more quantities externally supplied. صفر أو أكثر من الكميات المزودة خارجياً.

Output Output (المخرجات)

At least one quantity produced. كمية واحدة على الأقل يتم إنتاجها.

Definiteness Definiteness (الوضوح)

Instructions must be clear & unambiguous. يجب أن تكون التعليمات واضحة وغير غامضة.

Finiteness Finiteness (المحدودية)

Must terminate after a finite number of steps. يجب أن تتوقف بعد عدد محدود من الخطوات.

Efficiency Efficiency (الكفاءة)

Instructions must be basic and run quickly. يجب أن تكون التعليمات أساسية وتعمل بسرعة.

Specification Methods طرق التوصيف

Natural Language Natural Language (اللغة الطبيعية)

Simple English. Drawback: Often ambiguous and verbose. لغة إنجليزية بسيطة. العيوب: غالباً ما تكون غامضة ومسهبة.

Pseudocode Pseudocode (الكود الزائف)

Preferred مفضل

Mix of English and code structures. Implementation independent but precise. مزيج من الإنجليزية وهياكل الكود. مستقل عن التنفيذ ولكنه دقيق.

Flowchart Flowchart (المخطط الانسيابي)

Graphical representation. Drawback: Hard to modify and space-consuming for complex logic. تمثيل رسومي. العيوب: صعب التعديل ويستهلك مساحة للمنطق المعقد.

Case Study: Euclid's GCD دراسة حالة: Euclid's GCD

Pseudocode
ALGORITHM Euclid_gcd(m, n)
// Computes gcd(m, n) via Euclid's Method
// Input: Two non-negative integers

while n ≠ 0 do
r m mod n
m n
n r
return m

Logic: If $n=0$, the GCD is $m$. Otherwise, replace $m$ with $n$ and $n$ with the remainder of $m/n$. المنطق: إذا كان $n=0$، فإن الـ GCD هو $m$. وإلا، استبدل $m$ بـ $n$ واستبدل $n$ بباقي قسمة $m/n$.

The Algorithm Design Life Cycle دورة حياة تصميم الخوارزمية

1

Understand Understand

Clarify inputs & problem statement. توضيح المدخلات وبيان المشكلة.

2

Decide Decide

Exact vs Approx? Sequential vs Parallel? دقيق أم تقريبي؟ متسلسل أم متوازي؟

3

Design Design

Select technique (Brute Force, Divide & Conquer). اختيار التقنية (Brute Force, Divide & Conquer).

4

Prove Prove

Use Mathematical Induction. استخدم الاستقراء الرياضي (Induction).

5

Analyze Analyze

Time & Space efficiency. كفاءة الوقت والمساحة (Time & Space).

6

Code Code

Implement in C++, Java, etc. التنفيذ بلغة C++، Java، إلخ.

The Problem Landscape مشهد المشكلات (Problem Landscape)

Sorting Sorting (الفرز)

Rearranging items in ascending/descending order. (e.g., QuickSort). إعادة ترتيب العناصر بترتيب تصاعدي/تنازلي. (مثل QuickSort).

Searching Searching (البحث)

Finding a specific value (search key) in a dataset. (e.g., Binary Search). إيجاد قيمة معينة (مفتاح البحث) في مجموعة بيانات. (مثل Binary Search).

String Processing String Processing (معالجة النصوص)

Handling text data, vital for Bioinformatics (DNA sequences) and search engines. التعامل مع البيانات النصية، حيوي للمعلوماتية الحيوية (سلاسل DNA) ومحركات البحث.

Graph Problems Graph Problems (مشاكل الرسوم البيانية)

Modeling connections (cities, networks). Examples: Shortest Path, Traveling Salesman. نمذجة الاتصالات (المدن، الشبكات). أمثلة: أقصر مسار (Shortest Path)، البائع المتجول (Traveling Salesman).

Combinatorial Combinatorial (التوافقيات)

Finding permutations or subsets satisfying constraints. Often the hardest problems. إيجاد التباديل أو المجموعات الفرعية التي تحقق قيوداً معينة. غالباً ما تكون أصعب المشاكل.

Geometric Geometric (الهندسية)

Dealing with points, lines, polygons. Used in Computer Graphics and Robotics. التعامل مع النقاط، الخطوط، المضلعات. تستخدم في رسومات الحاسوب (Graphics) والروبوتات.

The Exam Vault خزنة الاختبار

Professor's Secrets & Trap Avoidance أسرار البروفيسور وتجنب الفخاخ

TRAP: Efficiency vs. Correctness فخ: Efficiency مقابل Correctness

A super-fast algorithm (High Efficiency) is worthless if it gives the wrong answer (Zero Correctness). Correctness is the prerequisite; Efficiency is the goal. Never optimize before proving correctness. الخوارزمية فائقة السرعة (عالية الـ Efficiency) عديمة الفائدة إذا أعطت إجابة خاطئة (Zero Correctness). الصحة (Correctness) هي الشرط المسبق؛ الكفاءة (Efficiency) هي الهدف. لا تقم بالتحسين أبداً قبل إثبات الصحة.

TRAP: Algorithm vs. Program فخ: Algorithm مقابل Program

An operating system is a Program (it runs indefinitely/infinite loop). An Algorithm MUST terminate (Finiteness). If asked "Is Windows an algorithm?", the answer is strictly No. نظام التشغيل هو Program (يعمل بشكل غير محدد/حلقة لا نهائية). الـ Algorithm يجب أن تتوقف (Finiteness). إذا سُئلت "هل Windows خوارزمية؟"، الإجابة هي قطعاً لا.

SECRET: Why Pseudocode? سر: لماذا Pseudocode؟

Professors prefer pseudocode because it ignores syntax errors (semicolons, types) and focuses purely on logic. In exams, never write C++/Java syntax unless explicitly asked. Stick to standard pseudocode (`←` for assignment). يفضل الأساتذة الـ Pseudocode لأنه يتجاهل أخطاء النحو (syntax errors) ويركز فقط على المنطق. في الاختبارات، لا تكتب كود C++/Java ما لم يُطلب منك صراحة. التزم بالـ Pseudocode القياسي (`←` للإسناد).

KEY CONCEPT: "Legitimate Input" مفهوم مفتاحي: "Legitimate Input"

If an algorithm fails on $n = -1$ but the input was defined as "Positive Integers", the algorithm is still correct. Correctness is only judged against the defined valid range of inputs. إذا فشلت الخوارزمية عند $n = -1$ ولكن تم تعريف الإدخال على أنه "أعداد صحيحة موجبة"، فإن الخوارزمية تظل صحيحة. يتم الحكم على الصحة (Correctness) فقط بناءً على النطاق الصالح المحدد للمدخلات.