An Introduction to Artificial Intelligence مقدمة في الذكاء الاصطناعي
Intelligence is the capacity to adapt, solve problems, and learn. Alfred Binet described it as "good sense, practical sense, initiative, and the faculty of adapting to circumstances."
Three core abilities:
- Solve novel problems
- Act rationally
- Act like humans
الذكاء هو القدرة على التكيف وحل المشكلات والتعلم. وصفه ألفريد بينيه بأنه "الحكمة، والتكيف مع الظروف، ونقد الذات."
ثلاث قدرات جوهرية:
- حل المشكلات الجديدة
- التصرف بعقلانية
- التصرف مثل البشر
AI was born at the Dartmouth Workshop (1956). Key definitions:
Alan Turing (1950)
"Can machines think?" — founding question.
John McCarthy (1956)
CS branch making computers behave like humans. Invented LISP.
Marvin Minsky
Making machines do things requiring intelligence if done by humans.
4 Main Approaches
Acting/Thinking Humanly or Rationally.
وُلد الذكاء الاصطناعي في ورشة دارتموث 1956. أبرز التعريفات:
آلان تورينج (1950)
"هل تستطيع الآلات التفكير؟" — السؤال التأسيسي.
جون مكارثي (1956)
فرع علوم الحاسوب لجعل الحاسبات تتصرف كالبشر. اخترع LISP.
مارفن مينسكي
علم جعل الآلات تؤدي مهاماً تتطلب ذكاءً لو أدّاها الإنسان.
٤ مناهج رئيسية
التصرف/التفكير كالإنسان أو بعقلانية.
A computer passes if a human interrogator can't distinguish it from a human after written questions. Requirements: NLP, Knowledge Representation, Automated Reasoning, Machine Learning.
Total Turing Test adds: Computer Vision + Robotics.
Chinese Room (Searle): a system can produce correct output without truly understanding → debate: Weak AI vs Strong AI.
يجتاز الحاسوب الاختبار إذا لم يستطع المحقق البشري التمييز بين ردوده وردود الإنسان. المتطلبات: معالجة اللغة، تمثيل المعرفة، الاستدلال الآلي، التعلم الآلي.
الاختبار الكلي يضيف: رؤية الحاسوب + الروبوتيات.
الغرفة الصينية (سيرل): الآلة تُنتج ردوداً صحيحة دون فهم حقيقي ← جدل الذكاء الضعيف مقابل الذكاء القوي.
ANI — Narrow AI
Current stage. Specialized in one task (chess, voice assistants).
AGI — General AI
Future. Performs any intellectual task a human can.
ASI — Super AI
Hypothetical. Surpasses humans in every domain.
Supporting fields
Philosophy, Maths, Neuroscience, Psychology, Economics, CS, Linguistics, Control Theory.
ANI — الذكاء الضيق
المرحلة الحالية. متخصص في مهمة واحدة (شطرنج، مساعد صوتي).
AGI — الذكاء العام
مستقبلي. قادر على أداء أي مهمة فكرية بشرية.
ASI — الذكاء الخارق
افتراضي. يتفوق على الإنسان في كل المجالات.
المجالات الداعمة
الفلسفة، الرياضيات، علم الأعصاب، النفس، الاقتصاد، الحاسوب، اللغويات، التحكم.
ملخص المحاضرة الأولى — للمذاكرة الجادة
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
🧠 ١. الذكاء: Alfred Binet = حكم عملي + تكيف + نقد ذات | Webster = التعلم وحل المشكلات
🏛️ ٢. ورشة دارتموث 1956: ميلاد مصطلح AI | Turing 1950: "هل تفكر الآلات؟" | McCarthy: اخترع AI + LISP
🔲 ٣. الأربع مناهج: Acting Humanly (تورينج) | Thinking Humanly (معرفي) | Thinking Rationally (منطق) | Acting Rationally (وكيل عقلاني)
🎮 ٤. اختبار تورينج: محقق لا يفرق بين الإنسان والآلة = نجاح | يحتاج: NLP + KR + AR + ML | الكلي يضيف: CV + Robotics
🚪 ٥. الغرفة الصينية (Searle): ردود صحيحة بدون فهم = نقد لتورينج | Weak AI = محاكاة | Strong AI = وعي حقيقي
📈 ٦. المراحل: ANI (الآن) ← متخصص | AGI (مستقبل) ← عام | ASI (افتراضي) ← خارق
Intelligent Agents & Task Environments الوكلاء الأذكياء وبيئات المهام
An agent perceives its environment through sensors and acts through actuators. Agent function: f: P* → A
Human Agent
Sensors: eyes, ears, nose, skin
Actuators: hands, legs, mouth
Robotic Agent
Sensors: cameras, infrared
Actuators: motors, wheels
Software Agent
Sensors: keystrokes, files
Actuators: screen, file writes
Cycle
Sense → Reason → Act → Feedback → Learn
الوكيل يُدرك بيئته عبر المستشعرات ويتصرف فيها عبر المشغّلات. دالة الوكيل: f: P* → A
الوكيل البشري
المستشعرات: عيون، آذان، أنف، جلد
المشغّلات: يدان، رجلان، فم
الوكيل الروبوتي
المستشعرات: كاميرات، أشعة تحت حمراء
المشغّلات: محركات، عجلات
الوكيل البرمجي
المستشعرات: ضغطات، ملفات
المشغّلات: عرض الشاشة، كتابة الملفات
دورة الوكيل
الإحساس ← الاستدلال ← التصرف ← التغذية الراجعة ← التعلم
Performance · Environment · Actuators · Sensors
Autonomous Taxi example:
- P: Safe, fast, legal, comfortable trip; maximize profits
- E: Roads, traffic, pedestrians, customers
- A: Steering wheel, accelerator, brake, horn
- S: Cameras, LIDAR, GPS, speedometer
P = الأداء · E = البيئة · A = المشغّلات · S = المستشعرات
مثال سيارة الأجرة الذاتية:
- P: رحلة آمنة وسريعة وقانونية ومريحة، تعظيم الأرباح
- E: الطرق، المركبات الأخرى، المشاة، الركاب
- A: عجلة قيادة، مسرّع، فرامل، بوق
- S: كاميرات، LiDAR، GPS، مقياس السرعة
Observable ↔ Partial
Can the agent see the full state?
Deterministic ↔ Stochastic
Is next state fully determined by current state + action?
Episodic ↔ Sequential
Are decisions independent of each other?
Static ↔ Dynamic
Does the world change while agent thinks?
Discrete ↔ Continuous
Are percepts/actions countable?
Single ↔ Multi-Agent
Is the agent operating alone?
مرئية ↔ جزئية
هل يرى الوكيل الحالة الكاملة دائماً؟
حتمية ↔ عشوائية
هل الحالة التالية تتحدد بالكامل؟
متقطعة ↔ متسلسلة
هل القرارات مستقلة عن بعضها؟
ثابتة ↔ ديناميكية
هل يتغير العالم بينما يفكر الوكيل؟
منفصلة ↔ مستمرة
هل الإدراكات قابلة للعدّ؟
وكيل ↔ متعدد
هل يعمل الوكيل بمفرده؟
ملخص المحاضرة الثانية
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
🤖 الوكيل: يُدرك (sensors) → يستدل → يتصرف (actuators) | دالة الوكيل: f: P* → A
📋 PEAS: P=الأداء | E=البيئة | A=المشغّلات | S=المستشعرات | مثال: سيارة الأجرة الذاتية
🌍 خصائص البيئة ODESDA: Observable / Deterministic / Episodic / Static / Discrete / Single-agent
📊 أمثلة: Chess = Fully+Strategic+Sequential+Semi-dyn+Discrete+Multi | Driving = Partially+Stochastic+Sequential+Dynamic+Continuous+Multi
⚡ الخلاصة: نوع البيئة هو الذي يحدد تصميم الوكيل بشكل كبير
Agent Types & Problem Solving أنواع الوكلاء وحل المشكلات
1. Simple Reflex
Current percept only. Condition-action rules. No memory. Fully observable. May loop.
2. Model-Based Reflex
Maintains internal world model. Handles partial observability. = Simple + Memory + Model.
3. Goal-Based
Has goal info. Plans actions. Considers future consequences. More flexible.
4. Utility-Based
Uses utility function. Maximizes expected happiness. Handles conflicting goals.
Learning Agent: improves over time via feedback. Components: Performance Element + Critic + Learning Element + Problem Generator.
١. المنعكس البسيط
الإدراك الحالي فقط. قواعد شرط-فعل. لا ذاكرة. Fully Observable فقط. قد يقع في حلقات.
٢. القائم على النموذج
يحتفظ بنموذج داخلي للعالم. يتعامل مع Partially Observable. = منعكس + ذاكرة + نموذج.
٣. القائم على الهدف
لديه معلومات عن الهدف. يخطط. يأخذ عواقب الأفعال المستقبلية. أكثر مرونة.
٤. القائم على المنفعة
يستخدم دالة منفعة. يعظّم السعادة المتوقعة. يحل تعارض الأهداف.
الوكيل التعلمي: يحسّن أداءه بمرور الوقت. مكوناته: عنصر الأداء + الناقد + عنصر التعلم + مولد المشكلات.
- Simple Reflex: Limited — some moves need memory (castling)
- Model-Based: Better — but needs future reasoning
- Goal-Based: Good — but conflicting goals (attack vs defense)?
- Utility-Based: Best — balances all goals simultaneously
Hierarchy (least → most capable):
Simple Reflex → Model-Based → Goal-Based → Utility-Based → Learning Agent
- المنعكس البسيط: محدود — بعض الحركات تحتاج ذاكرة (القلعة)
- القائم على النموذج: أفضل — لكنه يحتاج الاستدلال عن المستقبل
- القائم على الهدف: جيد — لكن ماذا عن تعارض الهجوم والدفاع؟
- القائم على المنفعة: الأفضل — يوازن بين كل الأهداف في آنٍ واحد
التسلسل من الأبسط للأذكى:
منعكس بسيط → نموذج → هدف → منفعة → تعلّمي
ملخص المحاضرة الثالثة
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
🤖 Simple Reflex: لا ذاكرة | الحاضر فقط | Fully Observable | قد يدور في حلقات | الحل: Randomization
🧠 Model-Based: ذاكرة + نموذج داخلي | Partially Observable | لكن بدون هدف واضح
🎯 Goal-Based: يخطط + يبحث | يفكر في المستقبل | مرن | الأهداف المتضاربة مشكلة
⚖️ Utility-Based: يوازن الأهداف | يختار الأعلى قيمة | الأكثر قدرة | يحتاج دالة منفعة جيدة
📚 Learning Agent: Performance + Critic + Learning Element + Problem Generator
📈 التسلسل: Simple → Model → Goal → Utility → Learning (كل مرحلة تضيف قدرة جديدة)
Problem Formulation & Search Strategies صياغة المشاكل واستراتيجيات البحث
We design goal-based agents in environments that are fully observable, deterministic, discrete, known.
A problem is defined by 5 items:
1. Initial State
The starting state of the agent.
2. Actions
Set of possible actions available.
3. Transition Model
What each action does (Successor function).
4. Goal Test
Is the current state the goal?
5. Path Cost
Numeric cost of each path (usually 1/step).
Examples
Vacuum: L/R/Suck | N-Puzzle: move blank | Hanoi: move disk
نصمم وكلاء قائمة على الهدف في بيئات مرئية بالكامل + حتمية + منفصلة + معروفة.
المشكلة تُعرَّف بـ ٥ عناصر:
١. الحالة الابتدائية
نقطة البداية للوكيل.
٢. الأفعال
مجموعة الأفعال المتاحة.
٣. نموذج الانتقال
نتيجة كل فعل (دالة الخلف).
٤. اختبار الهدف
هل الحالة الحالية هي الهدف؟
٥. تكلفة المسار
تكلفة رقمية لكل مسار (عادةً ١/خطوة).
أمثلة
مكنسة: يسار/يمين/امتصاص | N-Puzzle: تحريك الفراغ | هانوي: نقل قرص
State = physical configuration of world | Node = data structure (state + parent + action + g(n) + depth)
Strategies evaluated by: Completeness, Time Complexity, Space Complexity, Optimality
Key parameters: b = branching factor · d = depth of shallowest solution · m = max depth
Uninformed (Blind): BFS, UCS, DFS, DLS, IDS, BS — no extra info.
Informed (Heuristic): A*, Greedy — uses domain knowledge.
الحالة (State) = تكوين جسدي للعالم | العقدة (Node) = هيكل بيانات (حالة + أم + فعل + g(n) + عمق)
معايير التقييم: الاكتمال، التعقيد الزمني، التعقيد المكاني، الأمثلية
المعاملات: b = معامل التفرع · d = عمق أضحل حل · m = الحد الأقصى للعمق
أعمى (Uninformed): BFS, UCS, DFS, DLS, IDS, BS — بدون معلومات إضافية.
إرشادي (Informed): A*، Greedy — يستغل معرفة بالمجال.
ملخص المحاضرة الرابعة
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
🔍 البحث = إيجاد تسلسل أفعال من الحالة الابتدائية للهدف في بيئة: Fully + Deterministic + Discrete + Known
📋 ٥ مكونات: Initial State | Actions | Transition Model | Goal Test | Path Cost
⚖️ ٤ معايير تقييم: Completeness | Time Complexity | Space Complexity | Optimality
📐 المعاملات: b = معامل التفرع | d = عمق أضحل حل | m = الحد الأقصى | g(n) = تكلفة المسار
🔀 نوعا البحث: Blind (بدون معرفة) ← BFS/UCS/DFS/DLS/IDS | Informed (بمعرفة) ← A*/Greedy
Uninformed Search Algorithms خوارزميات البحث غير الموجّه — BFS · UCS · DFS · DLS · IDS · BS
Strategy: Always expand the shallowest unexpanded node.
Data structure: FIFO Queue.
| Property | BFS | Notes |
|---|---|---|
| Complete? | Yes | If b is finite |
| Time? | O(bd) | Exponential in d |
| Space? | O(bd) | Keeps ALL nodes in memory! |
| Optimal? | Yes | Only if all step costs = 1 |
الاستراتيجية: دايماً يوسِّع أضحل عقدة غير موسَّعة.
هيكل البيانات: FIFO Queue.
| الخاصية | BFS | ملاحظة |
|---|---|---|
| اكتمال؟ | نعم | لو b محدودة |
| الزمن؟ | O(bd) | أسي في d |
| المساحة؟ | O(bd) | بيحتفظ بكل العقد في الذاكرة! |
| أمثل؟ | نعم | بس لو تكلفة كل خطوة = ١ |
Strategy: Expand least-cost unexpanded node.
Data structure: Priority Queue ordered by g(n).
C* = optimal cost · ε = min step cost.
| Property | UCS | Notes |
|---|---|---|
| Complete? | Yes | If g > ε |
| Time? | O(bC*/ε) | All nodes with cost ≤ C* |
| Space? | O(bC*/ε) | Same as time |
| Optimal? | Yes | Always finds cheapest path |
الاستراتيجية: يوسِّع أقل تكلفة عقدة غير موسَّعة.
هيكل البيانات: Priority Queue مرتَّب بـ g(n).
C* = تكلفة الحل الأمثل · ε = الحد الأدنى لتكلفة خطوة.
| الخاصية | UCS | ملاحظة |
|---|---|---|
| اكتمال؟ | نعم | لو g > ε |
| الزمن؟ | O(bC*/ε) | كل العقد بتكلفة ≤ C* |
| المساحة؟ | O(bC*/ε) | نفس الزمن |
| أمثل؟ | نعم | دايماً يلاقي أرخص مسار |
BFS searches level by level — visits all nodes at depth 0 first, then depth 1, then depth 2, etc. Uses a FIFO Queue: new nodes added at the back, processed from the front.
⚙ How it works:
- Add root node to the Queue
- Remove front node, check if it's the goal
- If not — add all its children to the Queue
- Repeat until goal found or Queue is empty
✅ Pros / ❌ Cons:
- ✅ Complete: always finds solution if it exists
- ✅ Optimal: finds shortest path (equal costs)
- ❌ Huge memory: keeps all nodes of current level
- ❌ Very slow in deep trees
تبحث BFS عن الحل مستوى بمستوى — تزور جميع عقد العمق 0 أولاً، ثم عمق 1، ثم عمق 2، وهكذا. تعتمد على Queue (طابور FIFO): تُضاف العقد الجديدة في النهاية وتُعالج من البداية.
⚙ آلية العمل:
- ابدأ بإضافة العقدة الجذر للـ Queue
- أخرج العقدة الأمامية (Front)، تحقق هل هي الهدف؟
- إذا لا — أضف جميع أبنائها للـ Queue
- كرر حتى تجد الهدف أو ينتهي الـ Queue
✅ المزايا / ❌ العيوب:
- ✅ Complete: تجد الحل دائماً إذا وُجد
- ✅ Optimal: تجد أقصر مسار (بالتكاليف المتساوية)
- ❌ ذاكرة ضخمة: تحتفظ بجميع عقد المستوى الحالي
- ❌ بطيئة جداً في الأشجار العميقة
ترتيب الزيارة: A①→ B②→ C③→ D④→ E⑤→ F⑥→ G⑦ | Queue (FIFO)
Modified BFS that considers edge weights (costs). Always processes the node with lowest cumulative cost g(n). Uses a Priority Queue (Min-Heap).
⚙ How it works:
- Insert root with cost 0 in Priority Queue
- Extract node with lowest g(n)
- Check if goal — if yes, done
- Add children with g(n) = g(parent) + cost(edge)
- If a node has two paths, keep the cheaper one
✅ Pros / ❌ Cons:
- ✅ Always Optimal: always finds cheapest path
- ✅ Complete: as long as costs are positive
- ❌ Large memory (like BFS)
- ❌ Slow if step costs are very small
نسخة معدّلة من BFS تأخذ أوزان الحواف (Edge Costs) بعين الاعتبار. تعالج دائماً العقدة ذات أقل تكلفة تراكمية g(n). تستخدم Priority Queue (Min-Heap).
⚙ آلية العمل:
- أدخل الجذر بتكلفة 0 في الـ Priority Queue
- أخرج العقدة ذات أقل g(n)
- تحقق هل هي الهدف؟ إذا نعم — انتهى
- أضف أبناءها مع تحديث g(n) = g(parent) + cost(edge)
- إذا وُجدت عقدة بمسارين، احتفظ بالأرخص
✅ المزايا / ❌ العيوب:
- ✅ Optimal تماماً: تجد أقل تكلفة دائماً
- ✅ Complete: طالما التكاليف موجبة
- ❌ ذاكرة كبيرة (مثل BFS)
- ❌ بطيئة إذا كانت التكاليف صغيرة جداً
ترتيب المعالجة: A(0) → B(2) → D(3) → C(5) ≡ E(5) | الأرقام = g(n) التكلفة التراكمية
ملخص المحاضرة ٥–٦ (BFS & UCS)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
🌊 BFS: FIFO Queue | دايماً أضحل عقدة | Complete ✅ | Time O(b^d) | Space O(b^d) ❌ | Optimal ✅ (تكاليف متساوية)
💰 UCS: Priority Queue بـ g(n) | دايماً أرخص عقدة | Complete ✅ | Time O(b^(C*/ε)) | Optimal ✅ دائماً
📊 مقارنة: UCS = BFS لو تكاليف متساوية | BFS أبسط | UCS أدق مع تكاليف مختلفة
📱 تطبيقات BFS: GPS Navigation | شبكات P2P | الشبكات الاجتماعية | MIMO
⚠️ المشكلة الكبرى لـ BFS: المساحة! b=10, d=12 ← 111 تيرابايت
DFS: Always expand deepest node. LIFO Stack.
| DFS | DLS (limit L) | |
|---|---|---|
| Complete? | No | No (if d>L) |
| Time? | O(bm) | O(bL) |
| Space? | O(b·m) ✅ | O(b·L) ✅ |
| Optimal? | No | No |
DLS = DFS with depth limit L. Solves infinite-depth problem of DFS but fails if d > L.
DFS: دايماً يوسِّع أعمق عقدة. LIFO Stack.
| DFS | DLS (حد L) | |
|---|---|---|
| اكتمال؟ | لا | لا (لو d>L) |
| الزمن؟ | O(bm) | O(bL) |
| المساحة؟ | O(b·m) ✅ | O(b·L) ✅ |
| أمثل؟ | لا | لا |
DLS = DFS مع حد عمق L. يحل مشكلة العمق اللانهائي لكن يفشل لو d > L.
IDS: Repeatedly apply DLS with increasing L = 0, 1, 2, … until goal found. Combines BFS completeness + DFS space efficiency.
Bidirectional (BS): Run BFS forward from START and backward from GOAL simultaneously. Meet in the middle!
| IDS | BS | |
|---|---|---|
| Complete? | Yes | Yes* |
| Time? | O(bd) | O(bd/2) ✅✅ |
| Space? | O(b·d) ✅ | O(bd/2) |
| Optimal? | Yes* | Yes* |
IDS: يكرر DLS مع حد متزايد L = 0, 1, 2, … لحد ما يلاقي الهدف. يجمع اكتمال BFS + كفاءة مساحة DFS.
الثنائي (BS): يشغِّل BFS للأمام من البداية + BFS للخلف من الهدف في آنٍ واحد. الحل لما يلتقوا في المنتصف!
| IDS | BS | |
|---|---|---|
| اكتمال؟ | نعم | نعم* |
| الزمن؟ | O(bd) | O(bd/2) ✅✅ |
| المساحة؟ | O(b·d) ✅ | O(bd/2) |
| أمثل؟ | نعم* | نعم* |
| Algorithm | Complete? | Time | Space | Optimal? |
|---|---|---|---|---|
| BFS | Yes* | O(b^d) | O(b^d) | Yes* |
| UCS | Yes* | O(b^(C*/ε)) | O(b^(C*/ε)) | Yes |
| DFS | No | O(b^m) | O(b·m) | No |
| DLS | No | O(b^L) | O(b·L) | No |
| IDS | Yes* | O(b^d) | O(b·d) | Yes* |
| BS | Yes* | O(b^(d/2)) | O(b^(d/2)) | Yes* |
* = if b finite and step costs equal
| الخوارزمية | اكتمال؟ | الزمن | المساحة | أمثل؟ |
|---|---|---|---|---|
| BFS | نعم* | O(b^d) | O(b^d) | نعم* |
| UCS | نعم* | O(b^(C*/ε)) | O(b^(C*/ε)) | نعم |
| DFS | لا | O(b^m) | O(b·m) | لا |
| DLS | لا | O(b^L) | O(b·L) | لا |
| IDS | نعم* | O(b^d) | O(b·d) | نعم* |
| BS | نعم* | O(b^(d/2)) | O(b^(d/2)) | نعم* |
* = لو b محدودة وتكاليف متساوية
DFS dives as deep as possible into one branch before backtracking. Uses a Stack (LIFO) or recursive calls.
⚙ How it works:
- Insert root into the Stack
- Pop node from top
- Check if goal — if not, push children to Stack
- Continue until goal found or Stack empty
- If loop detected — may run forever (incomplete!)
✅ Pros / ❌ Cons:
- ✅ Low memory: O(b·m) — much better than BFS
- ✅ Fast if solution is deep/left in the tree
- ❌ Incomplete: may loop forever
- ❌ Not optimal: first solution found may not be best
تغوص DFS لأعمق نقطة ممكنة في فرع واحد قبل الرجوع (Backtracking). تعتمد على Stack (LIFO) أو الاستدعاء التكراري (Recursion).
⚙ آلية العمل:
- أدخل العقدة الجذر في الـ Stack
- أخرج العقدة من القمة (Top)
- تحقق هل هي الهدف؟ إذا لا — أضف أبناءها للـ Stack
- يستمر حتى يجد الهدف أو ينتهي الـ Stack
- إذا وجد حلقة — قد يدور إلى الأبد (غير مكتملة)
✅ المزايا / ❌ العيوب:
- ✅ ذاكرة قليلة: O(b·m) فقط — أفضل بكثير من BFS
- ✅ سريعة إذا كان الحل في اليسار/الأعمق
- ❌ غير مكتملة: قد تدور في حلقات
- ❌ غير مثلى: أول حل تجده ليس بالضرورة الأفضل
ترتيب DFS: A①→B②→D③→H④→I⑤→E⑥→C⑦→F⑧→G⑨ | ينزل لأقصى عمق ثم يرجع (Backtrack)
Modified DFS that adds a maximum depth limit L. Stops expanding at depth L even if goal not found. Solves DFS's infinite-loop problem.
⚙ How it works:
- Works exactly like DFS
- If node depth = L, don't expand it
- Returns: solution or "Cutoff" (not found within L)
- If goal is deeper than L → won't find it
✅ Pros / ❌ Cons:
- ✅ Solves infinite-loop problem
- ✅ Linear memory O(b·L)
- ❌ Incomplete: if solution is deeper than L
- ❌ Not optimal
- ❌ Wrong choice of L loses the solution
نسخة معدّلة من DFS تضيف حداً أقصى للعمق L. عند الوصول للعمق L، تتوقف عن التوسع حتى لو لم تجد الهدف. تحل مشكلة الحلقات اللانهائية في DFS.
⚙ آلية العمل:
- تعمل مثل DFS تماماً
- إذا وصلت لعقدة عمقها = L، لا توسّعها
- ترجع نتيجتين: الحل أو "Cutoff" (لم يوجد ضمن L)
- إذا كان الهدف أعمق من L → لن تجده
✅ المزايا / ❌ العيوب:
- ✅ تحل مشكلة الحلقات اللانهائية
- ✅ ذاكرة خطية O(b·L)
- ❌ غير مكتملة: إذا كان الحل أعمق من L
- ❌ غير مثلى
- ❌ اختيار L الخاطئ يُضيّع الحل
عند L=2: العقد D,F,G = STOP (Cutoff) — لا تُوسَّع | E = GOAL Found ✓
Combines best of BFS and DFS: repeatedly applies DLS with increasing limit L = 0, 1, 2, 3… until goal found. Guarantees BFS completeness + DFS memory efficiency.
⚙ How it works:
- Start with L = 0: apply DLS(L=0)
- If Cutoff → increment L by 1
- Apply DLS(L=1) → if Cutoff → L=2
- Repeat until solution is found
- Yes, revisits previous nodes — but that's acceptable!
✅ Pros / ❌ Cons:
- ✅ Complete like BFS
- ✅ Low memory like DFS: O(b·d)
- ✅ Optimal (equal costs)
- ❌ Revisits upper-level nodes (acceptable overhead)
- ❌ Slightly slower than BFS due to revisits
تجمع IDS أفضل ما في BFS وDFS: تُطبّق DLS بشكل متكرر مع زيادة الحد L = 0, 1, 2, 3, … حتى تجد الحل. تضمن اكتمال BFS وذاكرة DFS.
⚙ آلية العمل:
- ابدأ بـ L = 0: طبّق DLS(L=0)
- إذا Cutoff → زد L بمقدار 1
- طبّق DLS(L=1) → إذا Cutoff → L=2
- كرر حتى تجد الحل
- نعم، تُعيد زيارة عقد سابقة — لكن هذا مقبول!
✅ المزايا / ❌ العيوب:
- ✅ Complete مثل BFS
- ✅ ذاكرة قليلة مثل DFS: O(b·d)
- ✅ Optimal (تكاليف متساوية)
- ❌ تُعيد زيارة عقد المستويات السابقة (overhead مقبول)
- ❌ أبطأ قليلاً من BFS (بسبب الإعادة)
Runs two BFS searches simultaneously: one forward from Start, one backward from Goal. Ends when the two searches meet in the middle.
⚙ How it works:
- Create forward Queue (from Start) + backward Queue (from Goal)
- Each step: expand one forward + one backward
- After each expansion: check if frontiers meet
- When they meet: merge the two paths for complete solution
✅ Pros / ❌ Cons:
- ✅ Very efficient: O(bd/2) instead of O(bd)
- ✅ Massive savings: b=10, d=6 → BFS: 1M nodes, BS: 2000 only!
- ❌ Requires knowing the goal in advance
- ❌ Complex to implement (managing two searches)
- ❌ Needs reverse operators
تُشغّل بحثين BFS في نفس الوقت: الأول من البداية (Start) للأمام، والثاني من الهدف (Goal) للخلف. تنتهي عندما يلتقي البحثان في منتصف الطريق.
⚙ آلية العمل:
- أنشئ Queue أمامية (من Start) وQueue خلفية (من Goal)
- في كل خطوة: وسّع بحثاً من الأمام + بحثاً من الخلف
- بعد كل توسع: تحقق هل التقت الحدود؟
- عند الالتقاء: ادمج المسارين للحصول على الحل الكامل
✅ المزايا / ❌ العيوب:
- ✅ فعّال جداً: O(bd/2) بدلاً من O(bd)
- ✅ توفير هائل: b=10, d=6 → BFS: مليون عقدة, BS: 2000 فقط!
- ❌ يتطلب معرفة الهدف مسبقاً
- ❌ معقد في التطبيق (إدارة بحثين)
- ❌ يحتاج عمليات عكسية (Reverse Operators)
Forward BFS (من A) + Backward BFS (من Z) يلتقيان في المنتصف | توفير: O(bd/2) بدلاً من O(bd)
ملخص المحاضرة ٧–٨ (DFS, DLS, IDS, BS)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⬇️ DFS: LIFO Stack | أعمق عقدة | Complete ❌ | Time O(b^m) | Space O(b·m) ✅ | Optimal ❌
🔢 DLS: DFS + حد L | Complete ❌ (لو d>L) | Time O(b^L) | Space O(b·L) ✅ | Optimal ❌
🔄 IDS: يكرر DLS مع L متزايد | Complete ✅ | Time O(b^d) | Space O(b·d) ✅ | Optimal ✅
↔️ BS: BFS من البداية + BFS من الهدف | Complete ✅ | Time O(b^(d/2)) ✅✅ | Optimal ✅
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
🏆 IDS = الأفضل شاملاً (كامل + أمثل + مساحة جيدة) | BS = الأسرع زمنياً
🔵 BFS: أقصر مسار + تكاليف متساوية + ذاكرة كافية
🟢 UCS: أرخص مسار + تكاليف مختلفة
🔴 DFS: ذاكرة محدودة جداً + لا يهمك الأمثلية
🟠 DLS: تعرف الحد الأقصى للعمق مسبقاً
🟣 IDS: أفضل خوارزمية عامة في معظم الحالات ✨
🟡 BS: تعرف الهدف + d كبير + تريد السرعة
| Algorithm | Strategy / الاستراتيجية | Data Structure | Complete? | Time | Space | Optimal? |
|---|---|---|---|---|---|---|
|
BFS Breadth-First Search |
مستوى بمستوى Level by Level |
Queue (FIFO) | ✓ Complete | O(bd) | O(bd) | ✓ Optimal* |
|
UCS Uniform Cost Search |
أقل تكلفة تراكمية Min Cumulative Cost |
Priority Queue (Min-Heap) |
✓ Complete | O(b1+⌊C*/ε⌋) | O(b1+⌊C*/ε⌋) | ✓ Optimal |
|
DFS Depth-First Search |
أعمق أولاً Deepest First |
Stack (LIFO) | ✗ Incomplete | O(bm) | O(b·m) ✅ | ✗ Not Optimal |
|
DLS Depth-Limited Search |
DFS مع حد عمق L DFS with Depth Limit |
Stack (LIFO) | ~ Incomplete* | O(bL) | O(b·L) ✅ | ✗ Not Optimal |
|
IDS Iterative Deepening Search |
DLS متكررة بزيادة العمق Repeated DLS L=0,1,2… |
Stack (LIFO) | ✓ Complete | O(bd) | O(b·d) ✅ | ✓ Optimal* ✨ |
|
BS Bidirectional Search |
BFS من البداية والهدف Simultaneous BFS |
Queue × 2 (FIFO) |
~ Conditional | O(bd/2) ✅✅ | O(bd/2) | ~ Conditional |
Informed Search — البحث الموجّه Heuristics · Greedy Search · A* Search · Admissibility · Dominance
A heuristic h(n) is extra information used by the search algorithm beside the problem definition. It estimates the cost from node n to the goal — it is not the actual cost, just an optimistic expectation.
Key idea: Uninformed search is blind — it expands nodes without knowing which direction the goal is. Informed search uses h(n) to guide the search toward the goal more efficiently.
الـ heuristic h(n) هي معلومة إضافية بيستخدمها خوارزمية البحث جنب تعريف المشكلة. بتقدّر التكلفة من العقدة n للهدف — مش التكلفة الحقيقية، بس توقع متفائل.
الفكرة الجوهرية: البحث الأعمى (uninformed) مش بيعرف الهدف فين. البحث الموجّه بيستخدم h(n) يوجّه البحث ناحية الهدف بكفاءة أعلى.
h(n) = Straight-Line Distance (SLD) from node n to goal.
Even if roads are winding, the straight-line distance is always ≤ actual road distance — so it never overestimates. ✅
h(n) = المسافة بالخط المستقيم (SLD) من العقدة n للهدف.
حتى لو الطرق ملتوية، المسافة المستقيمة دائماً ≤ المسافة الفعلية — فلا تتجاوز الحقيقية أبداً. ✅
h₁ = Number of misplaced tiles — count tiles not in their goal position. Simple but less accurate.
h₂ = Manhattan Distance — sum of |row_diff| + |col_diff| for each tile. More informed than h₁.
h₁ = عدد القطع في غير مكانها — عد القطع اللي مش في مكانها الصحيح. بسيطة لكن أقل دقة.
h₂ = مسافة Manhattan — مجموع |فرق الصف| + |فرق العمود| لكل قطعة. أكثر معلومات من h₁.
Evaluation function: f(n) = h(n)
Greedy search expands the node that appears closest to goal. It chooses the minimum h(n) — ignoring how much it cost to reach that node (g(n) is ignored).
Think of it as: "Go to the city that looks closest to the destination, regardless of how far you've already traveled."
| Property | Greedy | Why? |
|---|---|---|
| Complete? | ❌ No | Can get stuck in loops (Iași → Neamț → Iași...) |
| Time? | O(bm) | Exponential; good heuristic helps a lot |
| Space? | O(bm) | Keeps all nodes in memory |
| Optimal? | ❌ No | Finds A solution, not the BEST one |
دالة التقييم: f(n) = h(n)
البحث الجشع يوسّع العقدة اللي تبدو أقرب للهدف. يختار أقل h(n) — بيتجاهل تكلفة الوصول لتلك العقدة (بيتجاهل g(n)).
تخيّل: "اذهب للمدينة اللي تبدو أقرب للوجهة، بصرف النظر عن المسافة اللي قطعتها فعلاً."
| الخاصية | Greedy | السبب |
|---|---|---|
| اكتمال؟ | ❌ لا | يمكن يعلق في حلقات (Iași ← Neamț ← Iași...) |
| الزمن؟ | O(bm) | أسي؛ الـ heuristic الجيدة تحسّن كثيراً |
| المساحة؟ | O(bm) | بيحتفظ بكل العقد في الذاكرة |
| أمثل؟ | ❌ لا | يلاقي حل، مش الأفضل |
Evaluation function: f(n) = g(n) + h(n)
g(n)= actual cost from start to n (how far we've come)h(n)= estimated cost from n to goal (how far we expect to go)f(n)= estimated total cost of cheapest path through n
A* avoids expanding paths that are already expensive. It balances "how much did we pay?" with "how much do we expect to pay?" — giving the optimal solution when h(n) is admissible.
دالة التقييم: f(n) = g(n) + h(n)
g(n)= التكلفة الفعلية من البداية لـ n (كده قطعنا كام)h(n)= التكلفة المقدَّرة من n للهدف (كام باقي متوقع)f(n)= إجمالي التكلفة المقدَّرة للمسار عبر n
A* بتتجنب توسيع المسارات الغالية فعلاً. بتوازن "كده دفعنا كام؟" مع "متوقع ندفع كام؟" — وبتدي الحل الأمثل لو h(n) مقبولة.
| Property | A* | Notes |
|---|---|---|
| Complete? | ✅ Yes | If h is finite everywhere |
| Time? | Exponential | Depends on heuristic quality |
| Space? | O(bd) | Keeps all nodes in memory |
| Optimal? | ✅ Yes* | *Only if h(n) is admissible |
| الخاصية | A* | ملاحظة |
|---|---|---|
| اكتمال؟ | ✅ نعم | لو h محدودة في كل مكان |
| الزمن؟ | أسي | يعتمد على جودة الـ heuristic |
| المساحة؟ | O(bd) | بيحتفظ بكل العقد في الذاكرة |
| أمثل؟ | ✅ نعم* | *بشرط أن h(n) تكون مقبولة |
A heuristic h(n) is admissible if for every node n:
where h*(n) is the true actual cost to the goal. An admissible heuristic is always optimistic — it never overestimates. This guarantees A* finds the optimal solution.
If h₂(n) ≥ h₁(n) for all n (and both are admissible), then h₂ dominates h₁. A dominating heuristic always leads to fewer node expansions — it is strictly better.
Example: h₂ (manhattan distance = 18) dominates h₁ (misplaced tiles = 8) → prefer h₂.
الـ heuristic h(n) تكون مقبولة لو لكل عقدة n:
حيث h*(n) هي التكلفة الحقيقية الفعلية للهدف. الـ heuristic المقبولة دائماً متفائلة — لا تبالغ في التقدير أبداً. ده بيضمن إن A* تلاقي الحل الأمثل.
لو h₂(n) ≥ h₁(n) لكل n (والاتنين مقبولتين)، فـ h₂ تسود على h₁. الـ heuristic المسيطرة دايماً بتوسّع عقداً أقل — أفضل بكل المقاييس.
مثال: h₂ (مسافة Manhattan = 18) تسود h₁ (عدد القطع المشتتة = 8) ← يُفضَّل استخدام h₂.
| Algorithm | f(n) | Complete? | Optimal? | Time | Space |
|---|---|---|---|---|---|
| BFS | depth(n) |
✅ | ✅ (equal costs) | O(bd) |
O(bd) |
| UCS | g(n) |
✅ | ✅ Always | O(bC*/ε) |
O(bC*/ε) |
| Greedy | h(n) |
❌ No | ❌ No | O(bm) |
O(bm) |
| A* | g(n)+h(n) |
✅ Yes | ✅ If h admissible | Exponential |
O(bd) |
ملخص المحاضرة السابعة — Lecture 7 Summary
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
🧠 Heuristic h(n): معلومة إضافية تُقدِّر التكلفة لـ Goal — متفائلة دائماً (لا تتجاوز h*) | Extra info estimating cost to goal — always optimistic
📌 أمثلة: SLD للمسارات | h₁=misplaced tiles | h₂=Manhattan distance | h(queens)=attacking pairs
🔴 Greedy: f(n)=h(n) — سريع لكن ❌ غير مكتمل ❌ غير أمثل — قد يعلق في loops
⭐ A*: f(n)=g(n)+h(n) — ✅ مكتمل ✅ أمثل (بشرط h مقبولة) — يوازن التكلفة المقطوعة والمتوقعة
✅ Admissibility: h(n) ≤ h*(n) — شرط لأمثلية A* | لا تبالغ في التقدير أبداً
🏆 Dominance: h₂ تسود h₁ لو h₂(n) ≥ h₁(n) لكل n — نفس الضمان لكن أداء أفضل
🗺 مثال Bucharest: Greedy = 450km ❌ | A* = 418km ✅ — فرق واضح في الجودة