Graphs
- A graph is a set of nodes (also called vertices) joined by edges (connections).
- Graphs model networks: friends, roads between cities, links between web pages.
- A tree is really a special graph; a general graph can have cycles and many links per node.
الرسوم البيانية
- الرسم البياني هو مجموعة من العقد (تسمى أيضاً الرؤوس) متصلة بواسطة حواف (روابط).
- تحاكي الرسوم البيانية الشبكات: الأصدقاء، الطرق بين المدن، الروابط بين صفحات الويب.
- الشجرة هي حقاً رسم بياني خاص؛ الرسم البياني العام يمكن أن يحتوي على حلقات وعديد من الروابط لكل عقدة.
Representing a graph: adjacency list
- One common way is an adjacency list: a dictionary mapping each node to its list of neighbours.
graph["A"]is the list of nodes directly connected toA.- This is compact when each node has only a few connections.
تمثيل الرسم البياني: القائمة المجاورة
- إحدى الطرق الشائعة هي القائمة المجاورة: قاموس يربط كل عقدة بـقائمة جيرانها.
graph["A"]هي قائمة العقد المتصلة مباشرة بـA.- هذا تمثيل مدمج عندما يكون لكل عقدة عدد قليل من الاتصالات فقط.
# A graph as a dict: each node maps to its list of neighbours
graph = {
"A": ["B", "C"],
"B": ["A"],
"C": ["A"],
}
print(graph["A"]) # ['B', 'C'] -- A connects to B and C
print(graph["B"]) # ['A']
Adding an edge
- In an undirected graph, an edge
A—Bgoes both ways. - So adding it means appending
BtoA's list andAtoB's list. - If a node is new, start it with an empty list first.
إضافة حافة
- في الرسم البياني غير الموجه، الحافة
A—Bتسير في الاتجاهين. - لذا فإن إضافتها تعني إضافة
Bإلى قائمةAوAإلى قائمةB. - إذا كانت العقدة جديدة، ابدأ بقائمة فارغة أولاً.
def add_edge(graph, a, b):
if a not in graph:
graph[a] = []
if b not in graph:
graph[b] = []
graph[a].append(b)
graph[b].append(a)
graph = {}
add_edge(graph, "A", "B")
add_edge(graph, "A", "C")
print(graph) # {'A': ['B', 'C'], 'B': ['A'], 'C': ['A']}
Walking the neighbours
- To explore from a node, you read its neighbour list and visit each one.
- A node that is not in the graph has no neighbours — return an empty list, not an error.
- This neighbour lookup is the first step of bigger jobs like searching the whole graph.
التجول عبر الجيران
- لاستكشاف نقطة ما، تقرأ قائمة جيرانها تزور واحداً تلو الآخر.
- العقدة التي لا توجد في الرسم البياني ليس لها جيران — أعد قائمة فارغة، وليس خطأ.
- هذا الاستعلام عن الجيران هو الخطوة الأولى لمهام أكبر مثل البحث في الرسم البياني بالكامل.
Directed vs undirected
- In an undirected graph an edge works both ways (a friendship).
- In a directed graph an edge points one way only (a one-way street, a "follows" link).
- For directed edges you would append in one direction only.
موجه مقابل غير موجه
- في رسم بياني غير موجه تعمل الحافة في كلا الاتجاهين (صداقة).
- في رسم بياني موجه تشير الحافة في اتجاه واحد فقط (شارع ذو اتجاه واحد، رابط "يتبع").
- بالنسبة للحواف الموجهة، ستضيف في اتجاه واحد فقط.
Common mistakes
- A graph is nodes joined by edges; an edge can be one-way or two-way.
- A tree is a graph with no cycles — do not confuse the two.
أخطاء شائعة
- الرسم البياني هو عقد متصلة بحواف؛ يمكن أن تكون الحافة ذات اتجاه واحد أو اتجاهين.
- الشجرة هي رسم بياني بدون حلقات — لا تخلط بينهما.
Now you try
- Build the adjacency-list operations:
add_edge,neighbours, andcount_edges. - Each task checks your function on a small graph.
- Press Check answer to test it.
الآن جرب بنفسك
- بنِ عمليات القائمة المجاورة:
add_edge،neighbours، وcount_edges. - كل مهمة تتحقق من دالة على رسم بياني صغير.
- اضغط على تحقق من الإجابة لاختبارها.
Write add_edge(graph, a, b) for an undirected graph stored as a dict of neighbour lists. Append b to graph[a] and a to graph[b]. If a node is not in the graph yet, give it an empty list [] first. Change the dict in place.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Write neighbours(graph, node) that returns the list of nodes directly connected to node. If node is not in the graph, return an empty list [] (do not raise an error).
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Write count_edges(graph) for an undirected graph: count how many edges it has. Add up the lengths of every neighbour list, then divide by 2 (each edge is stored in both nodes' lists). Example: a triangle A–B–C has 3 edges.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.