Lecture 3: Applications of graph algorithms, Topological sort, Strongly connected components Henry Xia, Brandon Zhang based on CPSC 490 slides from 2014-2018 2019-01-10 University of British Columbia CPSC 490: Problem Solving in Computer Science
• Assignment 1 is due Tuesday at noon. Start now! • We’ve already covered all the material for this assignment. • Assignment 2 will be released tonight. • Offjce hours Friday 1-2 pm and Monday 3-4 pm. 1 Announcements
As we saw last class... • Problems where you just need to run a classic algorithm on a graph are very rare. • Oħten need to model the problem as a graph, or transform the given graph. • Always ask: • What do the nodes represent? • What do the edges represent? • How can the restrictions in the problem be encoded in the graph? • Can we adapt a classic algorithm to the problem? 2 How to solve graph problems?
3 6 8 5 6 2 7 3 1 4 8 5 2 7 3 1 4 determine it is impossible. cell. cell. A move consists of sliding one of the cells adjacent to the blank cell into the blank Problem 1 – Eight Puzzle Input : two configurations of an “Eight Puzzle”. An “Eight Puzzle” is a 3 × 3 grid with the numbers 1 to 8 in eight cells, and a blank ninth Output : the minimum number of moves to turn the first configuration into the second, or → Figure : A possible move in an Eight Puzzle.
Construct a graph! • Nodes represent board configurations. • There is an edge between two nodes if there is a move which turns one board into the other. Run BFS on this graph to find the shortest path. To consider: how large is the graph? Is it feasible to solve a “Fiħteen Puzzle” with this approach? 4 Problem 1 – Solution
Construct a graph! • Nodes represent board configurations. • There is an edge between two nodes if there is a move which turns one board into the other. Run BFS on this graph to find the shortest path. To consider: how large is the graph? Is it feasible to solve a “Fiħteen Puzzle” with this approach? 4 Problem 1 – Solution
In a normal graph, we can run DFS/BFS to count connected components. 5 Problem 2 – Complement Graph Input : a graph G with N ≤ 10 5 nodes and M ≤ 10 5 edges. Output : the number of connected components in the complement graph of G . (The complement graph G c of G has the same vertices of G , and there is an edge ( u , v ) in G c ifg ( u , v ) is not in G .) But G c has lots of edges...
for which u v is in G c (i.e. u v is not in G ); Run a modified DFS! • Instead of a visited array, keep track of an unvisited set. • Initially, the unvisited set contains all vertices. • When we visit a node u : • remove it from the unvisited set; • instead of iterating through u ’s neighbours, iterate through the unvisited set to find a v • recurse on v . We need to be a little careful when iterating to not make our runtime quadratic. Time complexity: O N M N , using a balanced binary search tree to implement the set and adjacency list. 6 Problem 2 – Solution
Run a modified DFS! • Instead of a visited array, keep track of an unvisited set. • Initially, the unvisited set contains all vertices. • When we visit a node u : • remove it from the unvisited set; • instead of iterating through u ’s neighbours, iterate through the unvisited set to find a v • recurse on v . We need to be a little careful when iterating to not make our runtime quadratic. Time complexity: O N M N , using a balanced binary search tree to implement the set and adjacency list. 6 Problem 2 – Solution for which ( u , v ) is in G c (i.e. ( u , v ) is not in G );
Run a modified DFS! • Instead of a visited array, keep track of an unvisited set. • Initially, the unvisited set contains all vertices. • When we visit a node u : • remove it from the unvisited set; • instead of iterating through u ’s neighbours, iterate through the unvisited set to find a v • recurse on v . We need to be a little careful when iterating to not make our runtime quadratic. set and adjacency list. 6 Problem 2 – Solution for which ( u , v ) is in G c (i.e. ( u , v ) is not in G ); Time complexity: O (( N + M ) log N ) , using a balanced binary search tree to implement the
constants c k . 7 Problem 3 – Inequality Feasibility Input : A set of N ≤ 1000 inequalities of the form x j − x i ≤ c k , for variables x j , x i and Output : A solution to the inequalities, or determine if it is impossible.
Use Bellman-Ford! • For each variable x i , create a node v i . • Run Bellman-Ford from S . • If there is a negative weight cycle, the inequalities are unsatisfiable. Why does this work? 8 Problem 3 – Solution • For each inequality x j − x i ≤ c k , create a directed edge v i → v j with weight c k . • Create a node S and directed edges S → v i with weight 0. • Otherwise, setting x i to dist ( v i ) gives a solution.
9 6 1 (Converse leħt as exercise.) 2 exactly the inequality we wanted to satisfy! 3 8 4 7 5 Recall Bellman-Ford: Problem 3 – Solution dist = array of V elements, initialized to ∞ dist[s] = 0 repeat V - 1 times: for each edge u → v with cost c: dist[v] = min(dist[v], dist[u] + c) for each edge u → v with cost c: if dist[v] > dist[u] + c: output negative weight cycle If there is no negative weight cycle, then dist[v] <= dist[u] + c for each edge. This is
10 . Source: ACM-ICPC Pacific Northwest Regional 2016 E . . . X . . R command string by deleting characters or inserting characters at arbitrary positions. The robot will move according to the characters in the command string. We may edit the Problem 4 – Buggy Robot Input : An R × C grid ( R , C ≤ 50), containing obstacles, a robot, and an exit, and a command string s of length at most 50, containing the characters U , D , L , R . Output : the minimum number of edits necessary to make the robot reach the exit. DRRDD Figure : A grid and command string. The robot can reach the exit if we add a D to the beginning.
Idea: edit the command string as we go. Construct a graph as follows: command string. • use the i th character in the string to move to the cell v , costing 0 edits and ending up at node v i 1 ; • delete the character at position i , costing 1 edit and ending up at node v i 1 ; or • add a character at position i and use it to move to v , costing 1 edit and ending up at node v i . • Run 0-1 BFS on this graph to get the shortest path. Time complexity: O RC s . 11 Problem 4 – Solution • The nodes are pairs ( v , i ) , where v is the grid cell and i is the position in the • At a node ( v , i ) , we can:
Idea: edit the command string as we go. Construct a graph as follows: command string. • delete the character at position i , costing 1 edit and ending up at node v i 1 ; or • add a character at position i and use it to move to v , costing 1 edit and ending up at node v i . • Run 0-1 BFS on this graph to get the shortest path. Time complexity: O RC s . 11 Problem 4 – Solution • The nodes are pairs ( v , i ) , where v is the grid cell and i is the position in the • At a node ( v , i ) , we can: • use the i th character in the string to move to the cell v ′ , costing 0 edits and ending up at node ( v ′ , i + 1 ) ;
Idea: edit the command string as we go. Construct a graph as follows: command string. • add a character at position i and use it to move to v , costing 1 edit and ending up at node v i . • Run 0-1 BFS on this graph to get the shortest path. Time complexity: O RC s . 11 Problem 4 – Solution • The nodes are pairs ( v , i ) , where v is the grid cell and i is the position in the • At a node ( v , i ) , we can: • use the i th character in the string to move to the cell v ′ , costing 0 edits and ending up at node ( v ′ , i + 1 ) ; • delete the character at position i , costing 1 edit and ending up at node ( v , i + 1 ) ; or
Idea: edit the command string as we go. Construct a graph as follows: command string. • Run 0-1 BFS on this graph to get the shortest path. Time complexity: O RC s . 11 Problem 4 – Solution • The nodes are pairs ( v , i ) , where v is the grid cell and i is the position in the • At a node ( v , i ) , we can: • use the i th character in the string to move to the cell v ′ , costing 0 edits and ending up at node ( v ′ , i + 1 ) ; • delete the character at position i , costing 1 edit and ending up at node ( v , i + 1 ) ; or • add a character at position i and use it to move to v ′ , costing 1 edit and ending up at node ( v ′ , i ) .
Idea: edit the command string as we go. Construct a graph as follows: command string. • Run 0-1 BFS on this graph to get the shortest path. 11 Problem 4 – Solution • The nodes are pairs ( v , i ) , where v is the grid cell and i is the position in the • At a node ( v , i ) , we can: • use the i th character in the string to move to the cell v ′ , costing 0 edits and ending up at node ( v ′ , i + 1 ) ; • delete the character at position i , costing 1 edit and ending up at node ( v , i + 1 ) ; or • add a character at position i and use it to move to v ′ , costing 1 edit and ending up at node ( v ′ , i ) . Time complexity: O ( RC | s | ) .
Recommend
More recommend