Graphs (Discussion 12) (Biểu diễn đồ thị và thuật toán Dijkstra) - Christine Zhou
Generating preview...
Tài liệu thảo luận về đồ thị: biểu diễn đồ thị, các thuật toán duyệt (DFS, BFS), sắp xếp tô-pô và thuật toán Dijkstra, kèm bài tập thực hành.
Description
Discussion 12: Graphs Christine Zhou Agenda Announcements Let’s try to get through all the worksheet! Announcements Congratulations on finishing the project and getting through these past few weeks! Stay strong, keep pushing through :) HW 7 has been released Threads for each section on Piazza Want to chat? Email me! Graphs Can represent relationships! Family trees, cities and roads, etc. Lots of terminology: Vertices/nodes: graphs are made up of a set of these Edges: connect two vertices together (can be (un)directed) Adjacent: vertices with an edge between them Labels/Weights: the value of a vertex or edge Path: vertices connected by edges Cycle: path whose first and last vertices are the same Connectivity: vertices are connected if there is a path in between them; graphs are connected if all vertices are connected How do we represent a graph? Adjacency matrix Edge set Adjacency list 1 Graph Representation Represent the graph above with an adjacency list and an adjacency matrix representation. General Graph Traversal Algorithm Stack fringe = new Stack(); Set visited = new Set(); fringe.push (startVertex); while (!fringe.isEmpty()) { Vertex v = fringe.pop(); if (!visited.contains(v)) { process(v); //do something with v for (Vertex neighbor: v.neighbors) { fringe.push(neighbor); } visited.add(v); } } Depth First Search (DFS) Explore as far as possible before going back Search down entire subgraph of a child before moving onto the next child If there are multiple children that you could explore, break ties alphabetically Preorder: mark the current vertex, then visit the children Postorder: visit the children, then mark the current vertex Fringe is a stack Runtime: O(V + E) Visualization: here RECURSIVE IMPLEMENTATION OF DFS private void dfs(Graph G, int v) { marked[v] = true; for (int w : G.adj(v)) { if (!marked[w]) { edgeTo[w] = v; dfs(G, w); } } } Breadth First Search (BFS) ITERATIVE IMPLEMENTATION OF BFS
AI summary
- Document name
- Graphs (Discussion 12) (Biểu diễn đồ thị và thuật toán Dijkstra) - Christine Zhou
- School / Course
- University of California, Berkeley · Lập trình Java
- Content
- Tài liệu giới thiệu về đồ thị, các cách biểu diễn, thuật toán duyệt (DFS, BFS), sắp xếp tôpô và thuật toán Dijkstra tìm đường đi ngắn nhất. Nó cung cấp cả lý thuyết và bài tập thực hành.
- Table of contents
- Agenda
- Announcements
- Graphs
- 1 Graph Representation
- General Graph Traversal Algorithm
- Depth First Search (DFS)
- RECURSIVE IMPLEMENTATION OF DFS
- Breadth First Search (BFS) ITERATIVE IMPLEMENTATION OF BFS
- 2 Searches and Traversals
- Topological Sorting
- 3 Topological Sorting
- Dijkstra’s Algorithm
- 4 Dijkstra’s Algorithm
- 5 Dijkstra’s Correctness
- Pages
- 15 pages
- Uploaded by
- Uni24h
Frequently asked questions
Is this document free?
Yes. “Graphs (Discussion 12) (Biểu diễn đồ thị và thuật toán Dijkstra) - Christine Zhou” is free — just sign in and click Download to get the original file.
How many pages is this document?
The document has 15 pages, for the course Lập trình Java. You can preview it online before downloading.
Can I preview before downloading?
Yes. You can preview this document right on this page with the online reader, then decide whether to download.
Graphs (Discussion 12) (Biểu diễn đồ thị và thuật toán Dijkstra) - Christine Zhou
Generating preview...
Discussion 12: Graphs Christine Zhou Agenda Announcements Let’s try to get through all the worksheet! Announcements Congratulations on finishing the project and getting through these past few weeks! Stay strong, keep pushing through :) HW 7 has been released Threads for each section on Piazza Want to chat? Email me! Graphs Can represent relationships! Family trees, cities and roads, etc. Lots of terminology: Vertices/nodes: graphs are made up of a set of these Edges: connect two vertices together (can be (un)directed) Adjacent: vertices with an edge between them Labels/Weights: the value of a vertex or edge Path: vertices connected by edges Cycle: path whose first and last vertices are the same Connectivity: vertices are connected if there is a path in between them; graphs are connected if all vertices are connected How do we represent a graph? Adjacency matrix Edge set Adjacency list 1 Graph Representation Represent the graph above with an adjacency list and an adjacency matrix representation. General Graph Traversal Algorithm Stack fringe = new Stack(); Set visited = new Set(); fringe.push (startVertex); while (!fringe.isEmpty()) { Vertex v = fringe.pop(); if (!visited.contains(v)) { process(v); //do something with v for (Vertex neighbor: v.neighbors) { fringe.push(neighbor); } visited.add(v); } } Depth First Search (DFS) Explore as far as possible before going back Search down entire subgraph of a child before moving onto the next child If there are multiple children that you could explore, break ties alphabetically Preorder: mark the current vertex, then visit the children Postorder: visit the children, then mark the current vertex Fringe is a stack Runtime: O(V + E) Visualization: here RECURSIVE IMPLEMENTATION OF DFS private void dfs(Graph G, int v) { marked[v] = true; for (int w : G.adj(v)) { if (!marked[w]) { edgeTo[w] = v; dfs(G, w); } } } Breadth First Search (BFS) ITERATIVE IMPLEMENTATION OF BFS
Read full document
- Document name
- Graphs (Discussion 12) (Biểu diễn đồ thị và thuật toán Dijkstra) - Christine Zhou
- School / Course
- University of California, Berkeley · Lập trình Java
- Content
- Tài liệu giới thiệu về đồ thị, các cách biểu diễn, thuật toán duyệt (DFS, BFS), sắp xếp tôpô và thuật toán Dijkstra tìm đường đi ngắn nhất. Nó cung cấp cả lý thuyết và bài tập thực hành.
- Table of contents
- Agenda
- Announcements
- Graphs
- 1 Graph Representation
- General Graph Traversal Algorithm
- Depth First Search (DFS)
- RECURSIVE IMPLEMENTATION OF DFS
- Breadth First Search (BFS) ITERATIVE IMPLEMENTATION OF BFS
- 2 Searches and Traversals
- Topological Sorting
- 3 Topological Sorting
- Dijkstra’s Algorithm
- 4 Dijkstra’s Algorithm
- 5 Dijkstra’s Correctness
- Pages
- 15 pages
- Uploaded by
- Uni24h
Comments (0)
No comments yet. Be the first!
Misc.onclusion (Discussion 14-END) (Ôn tập cấu trúc dữ liệu) - Christine Zhou
Final Review Solutions (Cấu trúc dữ liệu heap, hàng đợi ưu tiên và duyệt đồ thị) - Ching and Christines
Giáo trình Lập trình Java
Inheritance (Discussion 4) (Kế thừa trong Java) - Christine Zhou
Asymptotic Analysis (Discussion 7) (Phân tích tiệm cận) - Christine Zhou
Chương 7.Cơ học lượng tử - Vật lý đại cương 3 - TS.Nguyễn Thị Trang
Chương 6.Quang học lượng tử - Vật lý đại cương 3 - TS.Nguyễn Thị Trang
Chương 5.Thuyết tương đối - Vật lý đại cương 3 - TS.Nguyễn Thị Trang
Chương 4. Tán xạ ánh sáng - Vật lý đại cương 3 - TS.Nguyễn Thị Trang
Chương 3.Phân cực ánh sáng - Vật lý đại cương 3 - TS.Nguyễn Thị Trang
Comments (0)
No comments yet. Be the first!