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.
Graphs
- A graphคือชุดของ nodes (หรือเรียกว่า vertices) ที่เชื่อมต่อกันด้วย edges (connections)
- Graphs model networks: เพื่อน, ถนนระหว่างเมือง, links ระหว่างหน้าเว็บ
- Tree จริงๆ แล้วคือ graph เฉพาะ; general graph สามารถมี cycles และหลาย links ต่อ 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: adjacency list
- One common way คือ adjacency list: dictionary ที่ map แต่ละ node ไปยัง list of neighbours ของมัน
graph["A"]คือ list ของ nodes ที่เชื่อมต่อกันโดยตรงกับA- สิ่งนี้มีความหนาแน่นเมื่อแต่ละ node มีเพียง few connections
# 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.
การเพิ่ม edge
- ใน undirected graph, edge
A—Bทำงานได้ทั้งสองทาง - ดังนั้นการเพิ่มมันหมายถึงการ append
BไปยังA's list และAไปยังB's list - หาก node เป็นใหม่ ให้เริ่มต้นด้วย empty list ก่อน
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.
การเดินผ่านเพื่อนบ้าน
- เพื่อ explore จาก node, คุณอ่าน neighbour list ของมันและ visit แต่ละตัว
- Node ที่ไม่อยู่ใน graph มี no neighbours — return empty list, ไม่ใช่ error
- การดูข้อมูลเพื่อนบ้านนี้เป็นขั้นตอนแรกของการทำงานที่ใหญ่ขึ้น เช่น การค้นหาทั้ง 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.
Directed vs undirected
- ใน undirected graph edge ทำงานได้ทั้งสองทาง (a friendship)
- ใน directed graph edge ชี้ไปในทิศทางเดียวเท่านั้น (one-way street, a "follows" link)
- สำหรับ directed edges คุณควร append ในทิศทาง หนึ่ง เท่านั้น
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.
ข้อผิดพลาดที่พบบ่อย
- Graph คือ nodes ที่เชื่อมต่อกันด้วย edges; edge อาจเป็น one-way หรือ two-way
- Tree คือ graphที่ไม่มี cycles —อย่าสับสนระหว่างสองสิ่งนี้
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.
ลองดูเลย
- สร้าง_operations ของ adjacency-list:
add_edge,neighbours, และcount_edges - Each task ตรวจสอบฟังก์ชันของคุณบน graph เล็ก
- กด Check answer เพื่อทดสอบ
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. · เขียน add_edge(graph, a, b) สำหรับ undirected graph ที่เก็บเป็น dict ของ neighbor lists. Append b ลงใน graph[a] และ a ลงใน graph[b]. ถ้า node ยังไม่มีอยู่ใน graph,给它 empty list [] ก่อน. เปลี่ยน dict in place
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
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). · เขียน neighbours(graph, node) ที่ return รายชื่อของ nodes ที่เชื่อมต่อกับ node โดยตรง. ถ้า node ไม่มีอยู่ใน graph, return list ว่าง [] (อย่า raise error)
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
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. · เขียน count_edges(graph) สำหรับ undirected graph: นับว่ามี edges กี่เส้น. บวกความยาวของ neighbor list ทุกอัน แล้วหารด้วย 2 (แต่ละ edge ถูกเก็บในรายการของทั้งสอง node) ตัวอย่าง: สามเหลี่ยม A–B–C มี 3 edges
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่