Coping with Limitations التعامل مع القيود (Limitations)

When a problem is NP-Hard, we have two choices: find an exact solution intelligently (Backtracking/Branch-and-Bound) or settle for a "good enough" approximation. عندما تكون المشكلة NP-Hard، لدينا خياران: إيجاد حل دقيق بذكاء (Backtracking/Branch-and-Bound) أو القبول بتقريب "جيد بما فيه الكفاية".

Backtracking التراجع (Backtracking) State-Space Trees أشجار فضاء الحالة Branch-and-Bound التفرع والحد (Branch-and-Bound) Approximation Algorithms خوارزميات التقريب

Two Approaches to NP-Hard Problems نهجان لمشاكل NP-Hard

1. Exact Solutions 1. الحلول الدقيقة

We want the optimal answer, even if it takes a long time. We try to be smarter than Brute Force by eliminating bad options early. نريد الإجابة المثلى (Optimal)، حتى لو استغرقت وقتاً طويلاً. نحاول أن نكون أذكى من القوة الغاشمة (Brute Force) عن طريق استبعاد الخيارات السيئة مبكراً.

  • Backtracking:التراجع (Backtracking): Depth-First Search with pruning. بحث العمق أولاً (DFS) مع التقليم.
  • Branch-and-Bound:التفرع والحد: Optimization with bounding functions. التحسين باستخدام دوال الحد (Bounding Functions).

2. Approximations 2. التقريبات (Approximations)

We accept a sub-optimal answer if we can find it in Polynomial Time. نقبل بإجابة دون المستوى الأمثل (Sub-optimal) إذا تمكنا من العثور عليها في وقت كثير الحدود.

  • Accuracy Ratio:نسبة الدقة: How close are we to optimal? كم نحن قريبون من الحل الأمثل؟
  • Heuristics:الاستدلال (Heuristics): Greedy strategies (Nearest Neighbor). استراتيجيات الجشع (أقرب جار).

Backtracking التراجع (Backtracking)

Builds the solution component by component (e.g., $x_1, x_2, \dots, x_n$). If a partial solution $(x_1, \dots, x_i)$ cannot lead to a complete valid solution, we Prune that node (terminate that branch) and backtrack to try a different choice for $x_i$. يبني الحل مكوناً تلو الآخر (مثل $x_1, x_2, \dots, x_n$). إذا كان الحل الجزئي $(x_1, \dots, x_i)$ لا يمكن أن يؤدي إلى حل صالح كامل، نقوم بـ تقليم (Prune) تلك العقدة (إنهاء ذلك الفرع) والتراجع لتجربة خيار مختلف لـ $x_i$.

Example: 3-Queens Problem مثال: مشكلة الملكات الثلاث

Root
Q1 at (1,1)
Q2 at (2,1)? No
Q2 at (2,2)? No
Q2 at (2,3)
Promising

N-Queens Problem مشكلة الملكات الـ N (N-Queens)

Place $n$ queens on an $n \times n$ board so no two attack each other.
Constraint: No two queens share a row, column, or diagonal.
ضع $n$ ملكات على لوحة $n \times n$ بحيث لا تهاجم أي اثنتين بعضهما البعض.
القيد: لا تشترك أي ملكتين في صف أو عمود أو قطر.

Hamiltonian Circuit دائرة هاميلتون (Hamiltonian Circuit)

Find a cycle visiting every vertex exactly once. Backtracking explores paths and prunes if a vertex is visited twice or no edge exists. العثور على دورة تزور كل رأس مرة واحدة بالضبط. يستكشف Backtracking المسارات ويقوم بالتقليم إذا تمت زيارة الرأس مرتين أو لم تكن هناك حافة.

Branch-and-Bound التفرع والحد (Branch-and-Bound)

Similar to Backtracking but used for Optimization Problems (finding the best solution, not just any solution). مشابه لـ Backtracking ولكنه يستخدم لـ مشاكل التحسين (إيجاد الحل الأفضل، وليس مجرد أي حل).

Key Components المكونات الرئيسية

  • Bounding Function:دالة الحد (Bounding Function): Calculates a lower (for minimization) or upper (for maximization) bound on the optimal solution obtainable from a node. تحسب حداً أدنى (للتقليل) أو أعلى (للتعظيم) للحل الأمثل الذي يمكن الحصول عليه من عقدة.
  • Pruning Rule:قاعدة التقليم: If a node's bound is worse than the best solution found so far, kill the node. إذا كان حد العقدة أسوأ من أفضل حل تم العثور عليه حتى الآن، اقتل العقدة.

Knapsack Example (Max Value) مثال الحقيبة (قيمة قصوى)

To find the Upper Bound for a node (partial selection), we relax the integer constraint: imagine we can take fractions of the remaining items (Greedy approach). للعثور على الحد الأعلى (Upper Bound) لعقدة (اختيار جزئي)، نخفف قيد العدد الصحيح: تخيل أننا يمكن أن نأخذ كسوراً من العناصر المتبقية (نهج Greedy).

If $UB < Best\_Value\_So\_Far$, prune! إذا كان $UB < Best\_Value\_So\_Far$، قلم!

Approximation Algorithms خوارزميات التقريب

Performance Ratio ($\rho$) نسبة الأداء ($\rho$)

We measure quality by the ratio of the approximate solution cost ($f(s_a)$) to the optimal cost ($f(s^*)$). نقيس الجودة بنسبة تكلفة الحل التقريبي ($f(s_a)$) إلى التكلفة المثلى ($f(s^*)$).

$\rho(n) = \frac{f(s_a)}{f(s^*)}$ (for minimization) (للتقليل)

TSP: Nearest Neighbor TSP: أقرب جار

Greedy

Strategy: Always go to the nearest unvisited city.
Accuracy: For general graphs, the error is unbounded (can be $\infty$). For Euclidean graphs (triangle inequality holds), it satisfies $\rho(n) \le \frac{1}{2}(\lceil \log_2 n \rceil + 1)$.
الاستراتيجية: اذهب دائماً إلى أقرب مدينة لم تتم زيارتها.
الدقة: للرسوم العامة، الخطأ غير محدود (قد يكون $\infty$). للرسوم الإقليدية (تنطبق متباينة المثلث)، تحقق $\rho(n) \le \frac{1}{2}(\lceil \log_2 n \rceil + 1)$.

Knapsack: Greedy Ratio الحقيبة: نسبة الجشع

Greedy

Strategy: Sort items by value-to-weight ratio ($v_i/w_i$) and pick the best ones.
Accuracy: The performance ratio is bounded (usually $\le 2$).
الاستراتيجية: رتب العناصر حسب نسبة القيمة إلى الوزن ($v_i/w_i$) واختر الأفضل.
الدقة: نسبة الأداء محدودة (عادة $\le 2$).

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

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

TRAP: Worst Case Backtracking فخ: أسوأ حالة للتراجع

"Is Backtracking efficient?"
In practice, yes (it prunes many branches). But in the Worst Case, it visits every node, so it is still Exponential ($O(2^n)$ or $O(n!)$). Do not call it "Polynomial Time".
"هل التراجع فعال؟"
عملياً، نعم (يقلم العديد من الفروع). لكن في الحالة الأسوأ، يزور كل عقدة، لذا فهو لا يزال أسياً ($O(2^n)$ أو $O(n!)$). لا تسمه "وقت كثير الحدود".

TRAP: Approximation Ratio فخ: نسبة التقريب

For maximization problems (Knapsack), the ratio is usually defined as $f(s^*)/f(s_a) \ge 1$. For minimization (TSP), it is $f(s_a)/f(s^*) \ge 1$. Always check the direction of the inequality so your ratio is $\ge 1$. لمشاكل التعظيم (الحقيبة)، تعرف النسبة عادة كـ $f(s^*)/f(s_a) \ge 1$. للتقليل (TSP)، هي $f(s_a)/f(s^*) \ge 1$. تحقق دائماً من اتجاه المتباينة لتكون النسبة $\ge 1$.

SECRET: B&B vs Backtracking سر: B&B مقابل التراجع

Backtracking is for non-optimization (decision) problems or finding all solutions.
Branch-and-Bound is for optimization problems (Min/Max). It uses the numeric bound to prune. This distinction is crucial for definitions.
التراجع (Backtracking) لمشاكل غير التحسين (القرار) أو إيجاد كل الحلول.
التفرع والحد (Branch-and-Bound) لمشاكل التحسين (Min/Max). يستخدم الحد الرقمي للتقليم. هذا التمييز حاسم للتعريفات.

KEY CONCEPT: Multifragment Heuristic مفهوم مفتاحي: Multifragment Heuristic

For TSP, another approximation is the Multifragment Heuristic. It sorts edges by weight (like Kruskal's) and adds them if they don't create a loop (unless size < n) and don't create a degree-3 vertex. It often outperforms Nearest Neighbor. لـ TSP، تقريب آخر هو Multifragment Heuristic. يرتب الأضلاع حسب الوزن (مثل Kruskal) ويضيفها إذا لم تنشئ حلقة (إلا إذا كان الحجم < n) ولم تنشئ رأساً من الدرجة 3. غالباً ما يتفوق على أقرب جار.