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

cpsc 490 problem solving in computer science
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

CPSC 490: Problem Solving in Computer Science

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

slide-2
SLIDE 2

Last time...

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

1

slide-3
SLIDE 3

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 city, and pass through cities and roads multiple times.

2

slide-4
SLIDE 4

Problem 1 – Solution

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

3

slide-5
SLIDE 5

2-SAT

Input: An expression of the form (a ∨ b) ∧ (c ∨ d) ∧ (e ∨ f) ∧ . . . , where a, b, c, . . . are of the form xi or ¬xi. Output: An assignment to the boolean variables x1, x2 . . . which makes the expression true, or determine it is impossible. We can solve this with SCC!

4

slide-6
SLIDE 6

2-SAT – Solution

Build a graph.

  • There is a node representing each variable xi and a node for the negation ¬xi.
  • For each clause (a ∨ b):
  • (a ∨ b) is logically equivalent to (¬a ⇒ b) ∧ (¬b ⇒ a).
  • Convert each clause into these two implications.
  • 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 xi such that xi and ¬xi lie in the same SCC. If there is a solution, we can find a satisfying assignment to the xi by looking at the component graph.

5

slide-7
SLIDE 7

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 2k + 1. The center of the plank is placed on the peg. Planks may be placed vertically or horizontally. Output: whether it is possible to place a plank on each peg without overlaps. Constraints: n, r, c ≤ 1000.

6

slide-8
SLIDE 8

Problem 2 – Solution

Reduce the problem to 2-SAT.

  • Variable xi is true if the plank placed on the ith peg is vertical.
  • For each pair of pegs and each orientation the planks can be in, determine if they
  • verlap. Each of these turns into a clause.
  • For example, if a vertical plank placed on the ith peg overlaps with a horizontal plank

placed on the jth peg, we should add the clause ¬xi ∨ xj.

  • 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

slide-9
SLIDE 9

Problem 3 – Team Selection

Input: N ≤ 100 people, each with two specializations and a list of people they would like 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. Output: whether it’s possible to assign everyone to a team while keeping everyone happy.

8

slide-10
SLIDE 10

Problem 3 – Solution

Use 2-SAT!

  • The variables xi represent the team that the ith person is on.
  • 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

slide-11
SLIDE 11

Trees

A tree is:

  • A connected graph with |V| − 1 edges.
  • 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. A leaf is a node in a tree with degree 1.

10

slide-12
SLIDE 12

Problem 4 – Center of a tree

Input: A tree with N ≤ 105 vertices. Output: the center of the tree. 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

slide-13
SLIDE 13

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! Keep removing leaves until we only have one or two nodes leħt. How do we implement this?

12

slide-14
SLIDE 14

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! Keep removing leaves until we only have one or two nodes leħt. How do we implement this?

12

slide-15
SLIDE 15

Problem 4 – Solution

Looks like topological sort!

1

degree = array of length N, storing the degree of each node

2

q = new queue

3

for u = 1 .. N:

4

if degree[u] == 1:

5

push(q, u)

6 7

while q is not empty:

8

u = pop(q)

9

for v in neighbours(u):

10

decrement degree[v]

11

if degree[v] == 1:

12

push(q, v)

The center is the last node (or last two nodes) processed.

13

slide-16
SLIDE 16

Minimum spanning tree

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

14

slide-17
SLIDE 17

Dijkstra’s algorithm

Recall Dijkstra:

1

q = new priority queue, sorted by distance, containing (s, 0)

2

while q is not empty:

3

(u, cost) = pop(q)

4

if u is visited:

5

continue

6

mark u as visited

7

dist[u] = cost

8

for each edge u → v with cost c:

9

enqueue(q, (v, cost + c))

15

slide-18
SLIDE 18

Prim’s algorithm

Uses the same greedy approach as Dijkstra.

1

q = new priority queue, sorted by weight, containing (s, 0)

2

while q is not empty:

3

(u, cost) = pop(q)

4

if u is visited:

5

continue

6

mark u as visited

7

add this edge to the MST

8

for each edge u → v with cost c:

9

enqueue(q, (v, c))

16

slide-19
SLIDE 19

Kruskal’s algorithm

Alternative greedy: add smallest (resp. largest) edges that don’t make a cycle.

1

dsu = new union-find data structure

2

for each edge (u, v), in order of weight:

3

if u, v are in different sets:

4

add this edge to the MST

5

merge(u, v)

Complexity: O(|E| log |E|) (assuming an effjcient implementation of union-find). (A union-find data structure lets us quickly check whether two nodes are in difgerent sets, and merge two sets together.)

17

slide-20
SLIDE 20

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. The bandwidth of a path is the minimum edge weight on the path.

18

slide-21
SLIDE 21

Problem 4 – Solution

Build a maximum spanning tree!

19

slide-22
SLIDE 22

Problem 4 – Solution

Why does this work? Because of the greedy selection of edges!

  • Suppose there’s an edge (u, v) with weight wuv that we don’t pick for the MST.
  • 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

with bandwidth ≥ wuv.

20

slide-23
SLIDE 23

Problem 5 – Building Roads

Input: N cities, and a set of contract proposals for roads from two companies. 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. Output: The minimum cost to build roads between the cities.

21

slide-24
SLIDE 24

Problem 5 – Solution

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

slide-25
SLIDE 25

Watch out!

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