cpsc 490 problem solving in computer science
play

CPSC 490: Problem Solving in Computer Science Considering SCCs is - PowerPoint PPT Presentation

Lecture 4: 2-SAT, Trees, Minimum spanning tree Henry Xia, Brandon Zhang based on CPSC 490 slides from 2014-2018 2019-01-15 University of British Columbia CPSC 490: Problem Solving in Computer Science Considering SCCs is useful when


  1. Lecture 4: 2-SAT, Trees, Minimum spanning tree Henry Xia, Brandon Zhang based on CPSC 490 slides from 2014-2018 2019-01-15 University of British Columbia CPSC 490: Problem Solving in Computer Science

  2. • Considering SCCs is useful when answering questions about reachability. • We can partition directed graphs into strongly connected components. • We can compute strongly connected components in linear time with DFS! 1 Last time...

  3. city, and pass through cities and roads multiple times. 2 Problem 1 – Tourist Input : A list of cities, connected by one-way roads. Output : whether it is possible to visit all the cities. It is allowed to start and end at any

  4. • Consider the component graph (where we compress each SCC into a node). • Find the strongly connected components of the graph. • Check if there is one path that passes through all the nodes (with topological sort!) 3 Problem 1 – Solution

  5. true, or determine it is impossible. We can solve this with SCC! 4 2-SAT Input : An expression of the form ( a ∨ b ) ∧ ( c ∨ d ) ∧ ( e ∨ f ) ∧ . . . , where a , b , c , . . . are of the form x i or ¬ x i . Output : An assignment to the boolean variables x 1 , x 2 . . . which makes the expression

  6. Build a graph. • Convert each clause into these two implications. component graph. 5 2-SAT – Solution • There is a node representing each variable x i and a node for the negation ¬ x i . • For each clause ( a ∨ b ) : • ( a ∨ b ) is logically equivalent to ( ¬ a ⇒ b ) ∧ ( ¬ b ⇒ a ) . • For each implication u ⇒ v , make an edge u → v . Find the SCCs of this graph. There is a solution if and only if there is no x i such that x i and ¬ x i lie in the same SCC. If there is a solution, we can find a satisfying assignment to the x i by looking at the

  7. 6 Planks may be placed vertically or horizontally. Problem 2 – Wooden Beams Input : A r × c rectangular grid with n pegs, where wooden planks may be placed. Each plank has the same length 2 k + 1. The center of the plank is placed on the peg. Output : whether it is possible to place a plank on each peg without overlaps. Constraints : n , r , c ≤ 1000.

  8. Reduce the problem to 2-SAT. • For each pair of pegs and each orientation the planks can be in, determine if they overlap. Each of these turns into a clause. • For example, if a vertical plank placed on the i th peg overlaps with a horizontal plank • We can place a plank on each peg with no overlaps if and only if there is a solution to this 2-SAT problem. 7 Problem 2 – Solution • Variable x i is true if the plank placed on the i th peg is vertical. placed on the j th peg, we should add the clause ¬ x i ∨ x j .

  9. to work with. We wish to assign these people to K teams, each of which corresponds to a specialization. Each person must be assigned to a team corresponding to their specialization, and their team should only consist of people with whom they like to work with. 8 Problem 3 – Team Selection Input : N ≤ 100 people, each with two specializations and a list of people they would like Output : whether it’s possible to assign everyone to a team while keeping everyone happy.

  10. Use 2-SAT! • If person i doesn’t like person j and they share a specialization, add a clause ensuring they won’t end up on the same team. 9 Problem 3 – Solution • The variables x i represent the team that the i th person is on.

  11. A tree is: • A connected graph with no cycles. (These definitions are equivalent.) Some nice properties: • Unique simple path between any two nodes. • Adding any edge forms exactly one cycle. • Removing any edge splits the tree into two trees. Oħten, we consider rooted trees, where we assign one node to be the root. We can then talk about a node’s parent and children. 10 Trees • A connected graph with | V | − 1 edges. A leaf is a node in a tree with degree 1.

  12. The center of a tree is the node which minimizes the distance to the farthest node from it. (There can be one or two centers.) 11 Problem 4 – Center of a tree Input : A tree with N ≤ 10 5 vertices. Output : the center of the tree.

  13. Keep removing leaves until we only have one or two nodes leħt. How do we implement this? 12 Problem 4 – Solution Given the tree T , consider T ′ = T with all its leaves removed. Claim: the center of T ′ is the same as the center of T !

  14. Keep removing leaves until we only have one or two nodes leħt. How do we implement this? 12 Problem 4 – Solution Given the tree T , consider T ′ = T with all its leaves removed. Claim: the center of T ′ is the same as the center of T !

  15. 13 6 The center is the last node (or last two nodes) processed. 12 11 10 9 8 Looks like topological sort! 7 5 3 1 4 2 Problem 4 – Solution degree = array of length N, storing the degree of each node q = new queue for u = 1 .. N: if degree[u] == 1: push(q, u) while q is not empty: u = pop(q) for v in neighbours(u): decrement degree[v] if degree[v] == 1: push(q, v)

  16. contained in G where the sum of the edge weights is minimized/maximized. Given a (connected) graph G , a minimum/maximum spanning tree is a spanning tree How to find MSTs? It turns out we can be greedy! 14 Minimum spanning tree

  17. 15 4 9 8 7 6 Recall Dijkstra: 5 2 3 1 Dijkstra’s algorithm q = new priority queue, sorted by distance, containing (s, 0) while q is not empty: (u, cost) = pop(q) if u is visited: continue mark u as visited dist[u] = cost for each edge u → v with cost c: enqueue(q, (v, cost + c))

  18. 16 4 9 8 7 6 Uses the same greedy approach as Dijkstra. 5 2 3 1 Prim’s algorithm q = new priority queue, sorted by weight, containing (s, 0) while q is not empty: (u, cost) = pop(q) if u is visited: continue mark u as visited add this edge to the MST for each edge u → v with cost c: enqueue(q, (v, c))

  19. Alternative greedy: add smallest (resp. largest) edges that don’t make a cycle. 1 2 3 4 5 (A union-find data structure lets us quickly check whether two nodes are in difgerent sets, and merge two sets together.) 17 Kruskal’s algorithm dsu = new union-find data structure for each edge (u, v), in order of weight: if u, v are in different sets: add this edge to the MST merge(u, v) Complexity: O ( | E | log | E | ) (assuming an effjcient implementation of union-find).

  20. The bandwidth of a path is the minimum edge weight on the path. 18 Problem 4 – Bandwidth Input : A weighted graph with N ≤ 1000 nodes, and non-negative edge weights. Output : For every pair of nodes, the maximum-bandwidth path between them.

  21. 19 Build a maximum spanning tree! Problem 4 – Solution

  22. Why does this work? Because of the greedy selection of edges! • Instead of picking this edge, we previously picked edges that form a u - v path. • These edges must have had greater weight, so there’s already a u - v path in the MST 20 Problem 4 – Solution • Suppose there’s an edge ( u , v ) with weight w uv that we don’t pick for the MST. with bandwidth ≥ w uv .

  23. The cities must be connected by roads. Each contract proposes to build one road for some cost. You were bribed to give exactly K contracts to company A. 21 Problem 5 – Building Roads Input : N cities, and a set of contract proposals for roads from two companies. Output : The minimum cost to build roads between the cities.

  24. Connect the graph with minimum total cost – MST! But, we can’t enforce that we use K of company A’s roads. • Suppose we choose more that K roads from company A. • Apply a constant penalty P to each of company A’s proposals. • Run MST on the new set of proposals. • Similarly, if we choose fewer than K roads then apply an incentive P . • Still don’t use K roads? Change P and try again! • Binary search on P . 22 Problem 5 – Solution

  25. Some edge cases to watch for: • Tree with zero or one node • Tree which is a stick • Tree with very large breadth • “Broomstick” tree: large number of sticks glued together 23 Watch out!

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