Python Code Snippet: Shortest Path Algorithm - كتاب الذكاء الإصطناعي - الصف 12 - الفصل 1 - المملكة العربية السعودية

الكتاب: كتاب الذكاء الإصطناعي - الصف 12 - الفصل 1 | المادة: الذكاء الإصطناعي | المرحلة: الصف 12 | الفصل الدراسي: 1

الدولة: المملكة العربية السعودية | المنهج: المنهج السعودي - وزارة التعليم

الدرس: خوارزمية إيجاد أقصر مسار في المتاهة

📚 معلومات الصفحة

الكتاب: كتاب الذكاء الإصطناعي - الصف 12 - الفصل 1 | المادة: الذكاء الإصطناعي | المرحلة: الصف 12 | الفصل الدراسي: 1

الدولة: المملكة العربية السعودية | المنهج: المنهج السعودي - وزارة التعليم

نوع المحتوى: درس تعليمي

مستوى الصعوبة: متوسط

📝 ملخص الصفحة

تقدم هذه الصفحة مقتطف كود بلغة Python يوضح خوارزمية إيجاد أقصر مسار في متاهة باستخدام مفاهيم نظرية المخططات. يبدأ الكود بتهيئة متغيرات مثل `shortest_distance` و `parent` لتتبع المسافات والعلاقات بين الخلايا، مع تعيين المسافة صفر للخلية البداية. يستخدم حلقة `while` لتوسيع الخلايا من قائمة `to_expand`، حيث يتم استخراج الخلية التالية وزيارة جيرانها عبر دالة `get_neighbors`. إذا كانت الخلية المجاورة غير مزارة، تتم إضافتها إلى القائمة وتحديث المسافة والوالد، مع التحقق من الوصول إلى الهدف وإعادة بناء المسار عبر `reconstruct_shortest_path` عند الوصول. إذا كانت الخلية المجاورة مزارة مسبقاً، يتم تحديث المسافة إذا وجد مسار أقصر. في حالة عدم العثور على مسار، تُرجع الدالة `None`. يُظهر الكود تطبيقاً عملياً لخوارزميات البحث في الذكاء الاصطناعي أو علوم الحاسب، مع إدراج تعليقات توضيحية لتحسين الفهم. الصفحة تشمل أيضاً شعار وزارة التعليم السعودية، مما يشير إلى أنها جزء من منهج تعليمي معتمد.

📄 النص الكامل للصفحة

shortest_distance[start_cell] = 0 # remembers the direct parent of each cell on the shortest path from the start_cell to the cell parent = {} #the parent of the start cell is itself parent[start_cell] = start_cell while len(to_expand)>0: next_cell = to_expand.pop(0) # get the next cell and remove it from the expansion list if verbose: print('\nExpanding cell', next_cell) # for each neighbor of this cell for neighbor,cost in get_neighbors(maze, next_cell): if verbose: print('\tVisiting neighbor cell',neighbor) cell_visits+=1 if neighbor not in visited: # if this is the first time this neighbor is visited visited.add(neighbor) to_expand.append(neighbor) parent[neighbor] = next_cell shortest_distance[neighbor]=shortest_distance[next_cell]+cost # target reached if neighbor==target_cell: # get the shortest path to the target cell, reconstructed in reverse. shortest_path = reconstruct_shortest_path(parent, start_cell, target_cell) return shortest_path, shortest_distance[target_cell],cell_visits else: # this neighbor has been visited before # if the current shortest distance to the neighbor is longer than the shortest # distance to next_cell plus the cost of transitioning from next_cell to this neighbor if shortest_distance[neighbor]>shortest_distance[next_cell] +cost: parent[neighbor]=next_cell shortest_distance[neighbor]=shortest_distance[next_cell]+cost # search complete but the target was never reached, no path exists return None,None,None وزارة التعليم Ministry of Education 2023 - 1447 113 --- VISUAL CONTEXT --- **IMAGE**: Ministry of Education Logo Description: A logo consisting of several green dots arranged in a pattern, with Arabic and English text below it. Context: Indicates the source or publisher of the educational material.