DFS, BFS, SPs, MSTs (Discussion 10) (Thuật toán duyệt đồ thị) - Christine Zhou

UN
Uni24h
0 lượt tải
68
0

Nội dung tài liệu

Trang 2

Announcements ● Welcome back from spring break! Hope you had a restful week :) ● Reach out to your mentor GSI if you feel yourself falling behind, or are feeling overwhelmed -- we’re here for you! We want to help you finish as strong as you can. ● We will be running the full autograder for project 2AB on ~4/2 ● Midterm 2 this Friday, April 5th from 8-10pm! ○ Check out @3764 for important information about the midterm & review sessions! ● No lab due this week! We’ll be doing exam review in lab. ● 61B Imposter Syndrome Panel ○ Sunday, 4/7 from 5-6:30pm in Soda 310 ● Remember to complete the anonymous spring break survey for 8 points of EC! ● Discussion survey: tinyurl.com/cz-disc10-sp19

Trang 4

Graph Traversals ● Level-Order/Breadth First Search (BFS): ○ Visit top to bottom, left to right, just like how you read! ● Depth-order/Depth First Search (DFS): ○ Traverse “deeper” nodes before shallow ones

Trang 5

Preorder vs. Postorder vs. Inorder (all types of DFS) ● Preorder: “Visit” a node, then traverse its children ● Inorder: Traverse left child, “visit” node, then traverse right child ● Postorder: Traverse children, then “visit” node. ● Note: The prefix to order (pre, in, post) refers to when a node is visited with respect to its children ○ ex) That’s why postorder means that we visit the node AFTER we visit the children (post is a prefix, meaning “behind,” “after,”) ● Note: Node and vertex are used interchangeably.

Trang 6

Q1.1: Graphs Give the DFS preorder, DFS postorder, and BFS order of the graph traversals starting from vertex A. Break ties alphabetically.

Trang 7

Q1.1: Implementing Post/Preorder - Maintain a stack of nodes and a marked set. - As soon as we add something to our stack, we note it for preorder. - The top node in our stack represents the node we are currently on, and the marked set represents nodes that have been visited. - After we add a node to the stack, we visit its lexicographically next unmarked child. - If there is none, we pop the topmost node from the stack and note it down for postorder.

Trang 8

Q1.1: DFS Pre/postorder Solutionremember: the node on the top of the stack is the node we’re currently on C Stack: A B F G Marked Set: A D Preorder: A A E - A has two unmarked children (B, D) (unmarked means the child is - Going to add B (alphabetical) to our stack, not in the Marked Set) preorder, and marked set.

Trang 9

Q1.1: DFS Pre/postorder Solution C Stack: A B B F G Marked Set: A B D Preorder: A B A E - B has an unmarked child (C) - Going to add C to our stack, preorder, and marked set.

Trang 10

Q1.1: DFS Pre/postorder Solution C Stack: A B C B F G Marked Set: A B C D Preorder: A B C A E - C has an unmarked child (F) - Going to add F to our stack, preorder, and marked set.

Trang 12

Q1.1: DFS Pre/postorder Solution C Stack: A B C F B F G Marked Set: A B C F D Preorder: A B C F A E - F has no unmarked children - Pop from the stack (F) and note it down for postorder.

Trang 13

Q1.1: DFS Pre/postorder Solution C Stack: A B C B F G Marked Set: A B C F D Preorder: A B C F Postorder: F A E - C has no unmarked children - Pop from the stack (C) and note it down for postorder.

Trang 14

Q1.1: DFS Pre/postorder Solution C Stack: A B B F G Marked Set: A B C F D Preorder: A B C F Postorder: F C A E - B has no unmarked children - Pop from the stack (B) and note it down for postorder.

Trang 15

Q1.1: DFS Pre/postorder Solution C Stack: A B F G Marked Set: A B C F D Preorder: A B C F Postorder: F C B A E - A has an unmarked child (D) - Going to add D to our stack, preorder, and marked set.

Trang 16

Q1.1: DFS Pre/postorder Solution C Stack: A D B F G Marked Set: A B C F D D Preorder: A B C F D Postorder: F C B A E - D has an unmarked child (E) - Going to add E to our stack, preorder, and marked set.

Trang 17

Q1.1: DFS Pre/postorder Solution C Stack: A D E B F G Marked Set: A B C F D E D Preorder: A B C F D E Postorder: F C B A E - E has no unmarked children - Pop from the stack (E) and note it down for postorder.

Trang 18

Q1.1: DFS Pre/postorder Solution C Stack: A D B F G Marked Set: A B C F D E D Preorder: A B C F D E Postorder: F C B E A E - D has no unmarked children - Pop from the stack (D) and note it down for postorder.

Trang 19

Q1.1: DFS Pre/postorder Solution C Stack: A B F G Marked Set: A B C F D E D Preorder: A B C F D E Postorder: F C B E D A E - A has no unmarked children - Pop from the stack (A) and note it down for postorder.

Trang 20

Q1.1: DFS Pre/postorder Solution C Stack: empty B F G Marked Set: A B C F D E (G) D Preorder: A B C F D E (G) Postorder: F C B E D A (G) A E

Hiển thị 20 trang đầu. Tải tài liệu để xem đầy đủ.

Bình luận (0)

Đăng nhập để tham gia bình luận

Chưa có bình luận nào. Hãy là người đầu tiên!