cpsc 490 problem solving in computer science
play

CPSC 490 Problem Solving in Computer Science Dijkstra, - PowerPoint PPT Presentation

CPSC 490 Problem Solving in Computer Science Dijkstra, Floyd-Warshall, Union-Find, and Toposort Lucca Siaudzionis and Jack Spalding-Jamieson 2020/01/14 University of British Columbia Announcements Assignment 1 has been released. After


  1. CPSC 490 Problem Solving in Computer Science Dijkstra, Floyd-Warshall, Union-Find, and Toposort Lucca Siaudzionis and Jack Spalding-Jamieson 2020/01/14 University of British Columbia

  2. Announcements • Assignment 1 has been released. • After today’s lecture, you will be able to solve most of the problems. • If you haven’t started yet, please try to do so today. • If anyone still does not have an account, please request one on piazza ASAP. 1

  3. The Shortest Path Problem for Weighted Graphs: Given Pair Input : A graph with non-negative edge-weights, and labelled nodes u and v . Output : The shortest distance from u to v . What if we can solve a more general problem? 2

  4. The Shortest Path Problem for Weighted Graphs: Single Source Input : A graph with non-negative edge-weights, and a labelled node u . Output : The distance from u to each other node in the graph. To solve the original problem: Output the distance from u to v after running the solution here. Ideas for solving this problem: This looks very similar to our use-case for BFS, but now with edge-weights. Maybe we can do something similar? 3

  5. Dijkstra’s Algorithm Graph traversal algorithm which starts at a given node, and explores the other nodes by order of their distance to the given node. • Time complexity: O ( m + n log m ) Things to consider for a Dijkstra problem: • What needs to be done when ... • Visiting a new node? • Visiting an already-visited node? • Visiting the target node? • Do we need to modify the graph in any way? • Do we need to create and connect copies of the graph? 4

  6. The Shortest Path Problem for Weighted Graphs: Example (1) 12 2 9 4 5 22 3 3 4 Our source node is the purple node in the top-left. We can use Dijkstra’s algorithm to solve the single-source shortest path problem on this graph. 5

  7. The Shortest Path Problem for Weighted Graphs: Example (2) ∞ 0 12 2 9 ∞ ∞ 4 ∞ 5 22 3 ∞ ∞ 3 4 ∞ Initialize the distance of the source vertex to be 0, and all non-source distances to be ∞ . 6

  8. The Shortest Path Problem for Weighted Graphs: Example (3) ∞ 0 12 2 9 ∞ ∞ 4 12 5 22 3 ∞ ∞ 3 4 ∞ Find the closest node to the source. 7

  9. The Shortest Path Problem for Weighted Graphs: Example (4) 14 0 12 2 9 ∞ ∞ 4 12 5 22 3 ∞ ∞ 3 4 ∞ Find the second-closest node to the source. 8

  10. The Shortest Path Problem for Weighted Graphs: Example (5) 14 0 12 2 9 ∞ ∞ 4 12 5 22 3 ∞ 19 3 4 ∞ 9

  11. The Shortest Path Problem for Weighted Graphs: Example (6) 14 0 12 2 9 22 ∞ 4 12 5 22 3 ∞ 19 3 4 ∞ 10

  12. The Shortest Path Problem for Weighted Graphs: Example (7) 14 0 12 2 9 22 ∞ 4 12 5 22 3 ∞ 19 3 4 23 11

  13. The Shortest Path Problem for Weighted Graphs: Example (8) 14 0 12 2 9 22 26 4 12 5 22 3 26 19 3 4 23 12

  14. Graph Traversals: Dijkstra’s Algorithm Implementation vector<int> dijkstra(vector<vector<int>> &adj, 1 vector<vector<int>> &w, int s) { 2 vector<bool> visited(adj.size()); 3 vector<int> dist(n); // shortest distance from s 4 // pair: first is distance (sorted by), second is node 5 priority_queue<pair<int,int>, vector<pair<int,int>>, 6 greater<pair<int,int>>> q; q.push({0, s}); 7 while (!q.empty()) { 8 int cur_dist, cur_node; 9 tie(cur_dist,cur_node) = q.top(); q.pop(); 10 if (visited[cur_node]) continue; visited[cur_node] = true; 11 dist[cur_node] = cur_dist; 12 for (int i = 0; i < adj[cur_node].size(); ++i) 13 q.emplace(dist[cur_node] + w[i], adj[i]); 14 } 15 13 } 16

  15. Discussion Problem 1 - Odd Path Input: A graph G with non-negative weighted directed edges, a node s , and a node e . Output: The shortest path from s to e that contains an odd number of edges. 14

  16. Discussion Problem 1 - Insight Duplicate the graph • Every node u becomes nodes u 1 and u 2 • Every edge u → v becomes edges u 1 → v 2 and u 2 → v 1 Paths from u 1 to v 1 and from u 2 to v 2 contain an even number of edges. Paths from u 1 to v 2 and from u 2 to v 1 contain an odd number of edges. Run Dijkstra to find the shortest path from s 1 to e 2 . 15

  17. All pairs Shortest Paths: Problem Statement Input : A graph with edge-weights. The edge-weights might be negative, but there will be no negative cycles. Output : For each pair u , v of vertices, the length of the smallest path between u and v (possibly ∞ ). Observation 1: If the edge-weights were non-negative, we could use Dijkstra to solve this in O ( nm log n ) time. Observation 2: Similarly to BFS and Dijkstra, any shortest path between u and v using more than two edges uses a shortest path of some w ∈ N ( u ) and v . 16

  18. All pairs Shortest Paths: Floyd-Warshall initialize a matrix dist [][] to infinity 1 set dist[u][u] to 0 for all nodes u 2 set dist[u][v] to w(u,v) for all edges e = (u,v) 3 for k = 1 to |V|: 4 for i = 1 to |V|: 5 for j = 1 to |V|: 6 dist[i][j] = min(dist[i][j], 7 dist[i][k] + dist[k][j]) 8 Time Complexity: O ( V 3 ). Runs very fast in practice because of tightly nested loops. This is one of the cases where you do want to use an adjacency matrix! 17

  19. Discussion Problem 2 - Maximin Lucca is in a country with N cities and M roads connecting them. The country is full of overhead bridges, so each road connecting cities u i and v i has a maximum height h i allowed for all vehicles in it. Lucca likes to rent very tall trucks, and was wondering what is the tallest truck that he can rent to go from city S to city E . 18

  20. Discussion Problem 2 - Insight Change the step that joins distances to use a min instead of a sum . Change the step that compares distances to use a max instead of a min . Example in Floyd-Warshall: dist[i][j] = max(dist[i][j], min(dist[i][k], dist[k][j])) 1 19

  21. Union-Find (Disjoint Set Union) Union-Find is a data structure used to keep track of disjoint sets. It allows for two operations: • Union : Join two sets. • Find : Find the ”representative” of an element’s set. Two elements are in the same set ⇒ they have the same representative (i.e. find(a) == find(b) ). ⇐ Time Complexity: O ( α ( n )) ← inverse Ackermann function Essentially constant time ( O (1)) for any practical purposes! 20

  22. Union-Find Example: Initial state 1 2 3 F=2 F=3 F=1 5 4 6 F=4 F=5 F=6 21

  23. Union-Find Example: Union(1, 2) 1 2 3 F=1 F=3 F=1 4 5 6 F=5 F=4 F=6 22

  24. Union-Find Example: Union(4, 5) 1 2 3 F=1 F=3 F=1 4 5 6 F=5 F=4 F=6 23

  25. Union-Find Example: Union(3, 6) 1 2 3 F=6 F=1 F=1 5 4 6 F=4 F=4 F=6 24

  26. Union-Find Example: Union(2, 3) 1 2 3 F=1 F=1 F=1 4 5 6 F=4 F=4 F=1 25

  27. Union-Find: Find Function vector<int> parent; 1 // in an initial state, parent[i] = i for every i 2 3 int find(int x) { 4 if (parent[x] != x) { 5 parent[x] = find(parent[x]); // path-compression 6 } 7 return parent[x]; 8 } 9 26

  28. Union-Find: Union by Rank (Join) Function vector<int> parent; 1 vector<int> rnk; // rank -- initialized to all 0s 2 3 void join(int a, int b) { 4 // we are only interested in the representatives 5 a = find(a); b = find(b); 6 if (a == b) return; // already joined 7 8 if (rank[a] > rank[b]) parent[b] = a; 9 else if (rank[b] > rank[a]) parent[a] = b; 10 else { 11 parent[b] = a; 12 rank[a]++; 13 } 14 } 15 27

  29. Topological Sort Remember this graph? CPSC 110 CPSC 121 CPSC 210 CPSC 213 CPSC 221 CPSC 310 CPSC 313 CPSC 320 CPSC 410 CPSC 415 CPSC 420 Pictured: Graph of the UBC Computer Science pre-requisite chain. If someone asked you for a correct sequence to take all these courses, how would you compute such sequence? 28

  30. Topological Sort Problem formulation: • You have N tasks (numbered 1 to N ). • You have M requirements ( u , v ), meaning that task u must be completed before task v . Output: A valid sequence to complete all tasks. 29

  31. Topological Sort Problem graph: • Every task can be represented by a node in the graph. • Every requirement ( u , v ) can be represented by a directed edge from u to v . Note that the resulting graph is a DAG iff the problem has a solution. 30

  32. Topological Sort - Intuition Can you start with a task with in-degree 1 greater than 0? • No. If an edge arrives at a task, then the task on the other side must be completed first. • That means that we can only start with tasks with in-degree equal to 0. 1 in-degree of a node is the number of edges that arrive at that node. 31

  33. Topological Sort - Possible Starting Nodes 32

  34. Topological Sort - Intuition What happens after we complete a task? • That means that all the tasks that depended on it no longer need to await its completion. • Which means that we can essentially delete that node and all its edges from the graph. • Any problem with this feature is called self-reducible . Later in this course, we will discuss another important self-reducible problem. 33

  35. Topological Sort - Completing a Task 34

  36. Topological Sort - Completing a Task 35

  37. Topological Sort - Intuition What do we do now? Repeat until all tasks are completed! 36

  38. Topological Sort - Possible Starting Nodes 37

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend