CPSC 490: Problem Solving in Computer Science Assignment 1 is due - - PowerPoint PPT Presentation

cpsc 490 problem solving in computer science
SMART_READER_LITE
LIVE PREVIEW

CPSC 490: Problem Solving in Computer Science Assignment 1 is due - - PowerPoint PPT Presentation

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


slide-1
SLIDE 1

CPSC 490: Problem Solving in Computer Science

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

slide-2
SLIDE 2

Announcements

  • 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

slide-3
SLIDE 3

How to solve graph problems?

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

slide-4
SLIDE 4

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

  • cell. A move consists of sliding one of the cells adjacent to the blank cell into the blank

cell. Output: the minimum number of moves to turn the first configuration into the second, or determine it is impossible. 4 1 3 7 2 6 5 8 → 4 1 3 7 2 6 5 8 Figure: A possible move in an Eight Puzzle.

3

slide-5
SLIDE 5

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

slide-6
SLIDE 6

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

slide-7
SLIDE 7

Problem 2 – Complement Graph

Input: a graph G with N ≤ 105 nodes and M ≤ 105 edges. Output: the number of connected components in the complement graph of G. (The complement graph Gc of G has the same vertices of G, and there is an edge (u, v) in Gc ifg (u, v) is not in G.) In a normal graph, we can run DFS/BFS to count connected components. But Gc has lots of edges...

5

slide-8
SLIDE 8

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

for which u v is in Gc (i.e. u v is not in G);

  • 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

slide-9
SLIDE 9

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

for which (u, v) is in Gc (i.e. (u, v) is not in G);

  • 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

slide-10
SLIDE 10

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

for which (u, v) is in Gc (i.e. (u, v) is not in G);

  • recurse on v.

We need to be a little careful when iterating to not make our runtime quadratic. Time complexity: O((N + M) log N), using a balanced binary search tree to implement the set and adjacency list.

6

slide-11
SLIDE 11

Problem 3 – Inequality Feasibility

Input: A set of N ≤ 1000 inequalities of the form xj − xi ≤ ck, for variables xj, xi and constants ck. Output: A solution to the inequalities, or determine if it is impossible.

7

slide-12
SLIDE 12

Problem 3 – Solution

Use Bellman-Ford!

  • For each variable xi, create a node vi.
  • For each inequality xj − xi ≤ ck, create a directed edge vi → vj with weight ck.
  • Create a node S and directed edges S → vi with weight 0.
  • Run Bellman-Ford from S.
  • If there is a negative weight cycle, the inequalities are unsatisfiable.
  • Otherwise, setting xi to dist(vi) gives a solution.

Why does this work?

8

slide-13
SLIDE 13

Problem 3 – Solution

Recall Bellman-Ford:

1

dist = array of V elements, initialized to ∞

2

dist[s] = 0

3

repeat V - 1 times:

4

for each edge u → v with cost c:

5

dist[v] = min(dist[v], dist[u] + c)

6

for each edge u → v with cost c:

7

if dist[v] > dist[u] + c:

8

  • utput negative weight cycle

If there is no negative weight cycle, then dist[v] <= dist[u] + c for each edge. This is exactly the inequality we wanted to satisfy! (Converse leħt as exercise.)

9

slide-14
SLIDE 14

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. The robot will move according to the characters in the command string. We may edit the command string by deleting characters or inserting characters at arbitrary positions. Output: the minimum number of edits necessary to make the robot reach the exit. R . . . X . . . E DRRDD Figure: A grid and command string. The robot can reach the exit if we add a D to the beginning.

Source: ACM-ICPC Pacific Northwest Regional 2016

10

slide-15
SLIDE 15

Problem 4 – Solution

Idea: edit the command string as we go. Construct a graph as follows:

  • The nodes are pairs (v, i), where v is the grid cell and i is the position in the

command string.

  • At a node (v, i), we can:
  • use the ith 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

slide-16
SLIDE 16

Problem 4 – Solution

Idea: edit the command string as we go. Construct a graph as follows:

  • The nodes are pairs (v, i), where v is the grid cell and i is the position in the

command string.

  • At a node (v, i), we can:
  • use the ith 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

slide-17
SLIDE 17

Problem 4 – Solution

Idea: edit the command string as we go. Construct a graph as follows:

  • The nodes are pairs (v, i), where v is the grid cell and i is the position in the

command string.

  • At a node (v, i), we can:
  • use the ith 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

slide-18
SLIDE 18

Problem 4 – Solution

Idea: edit the command string as we go. Construct a graph as follows:

  • The nodes are pairs (v, i), where v is the grid cell and i is the position in the

command string.

  • At a node (v, i), we can:
  • use the ith 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

slide-19
SLIDE 19

Problem 4 – Solution

Idea: edit the command string as we go. Construct a graph as follows:

  • The nodes are pairs (v, i), where v is the grid cell and i is the position in the

command string.

  • At a node (v, i), we can:
  • use the ith 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

slide-20
SLIDE 20

Directed graphs

Some definitions:

  • A directed cycle in a directed graph is a path v1 → v2 → · · · → vk → v1.
  • A directed acyclic graph (DAG) is a directed graph with no directed cycles.
  • A directed graph is strongly connected if every vertex is reachable from every other

vertex.

  • We can partition every directed graph into strongly connected components.
  • The strongly connected components in a DAG are trivial: they only have one node.
  • The in-degree of a node is the number of edges directed into that node (and likewise

for out-degree).

12

slide-21
SLIDE 21

Problem 5 – Compilation

Input: A program, consisting of N ≤ 105 modules and M ≤ 106 pairs (ui, vi) indicating that module ui depends on module vi. Output: A possible order to compile the modules, or detect if there is a circular dependency.

13

slide-22
SLIDE 22

Problem 5 – Solution

  • While there are still modules that need to be compiled:
  • pick a module with no remaining dependencies;
  • compile it;
  • remove any dependencies to it.
  • If there are modules we haven’t compiled but all of them have dependencies, then

we found a circular dependency.

14

slide-23
SLIDE 23

Problem 5 – Solution

In terms of graphs:

  • The nodes are modules.
  • If ui depends on vi, make an edge vi → ui.
  • While there are still unvisited nodes:
  • pick a node with 0 in-degree;
  • visit it;
  • remove it from the graph (decrementing its successors’ in-degrees).
  • If there are unvisited nodes remaining, there is a directed cycle.

This is called topological sort, and the order in which we visit nodes is called the topological order. Important property of the topological order: if there is an edge u → v, then u comes before v in the topological order.

15

slide-24
SLIDE 24

Topological sort

Given a directed graph, produce a topological order if the graph is a DAG, or report there is a directed cycle.

1

indegree = array of length V, initialized to 0

2

for each edge u → v:

3

increment indegree[v]

4

q = new queue containing all nodes with indegree 0

5

while q is not empty:

6

u = pop(q)

7

  • utput u

8

for v in neighbours of u:

9

decrement indegree[v]

10

if indegree[v] == 0:

11

push(q, v)

12

for u = 1 .. V:

13

if indegree[u] > 0:

14

  • utput "directed cycle"

Looks like BFS!

16

slide-25
SLIDE 25

Problem 6 – Shortest Paths, Again

Input: A DAG with N ≤ 105 nodes and M ≤ 105 edges, with arbitrary edge weights. Output: The length of the shortest path from node s to all other nodes.

17

slide-26
SLIDE 26

Problem 6 – Solution

Process the nodes in topological order.

1

dist = array of length V, initialized to ∞

2

dist[s] = 0

3

indegree = array of length V, initialized to 0

4

for each edge u → v:

5

increment indegree[v]

6

q = new queue containing all nodes with indegree 0

7

while q is not empty:

8

u = pop(q)

9

for each edge u → v with cost c:

10

dist[v] = min(dist[v], dist[u] + c)

11

decrement indegree[v]

12

if indegree[v] == 0:

13

push(q, v)

Before we visit u, we visited all its predecessors, so we’re guaranteed to find the shortest path.

18

slide-27
SLIDE 27

Problem 7 – Intervals

Input: An integer N ≤ 105, and M ≤ 105 restrictions of 2 types. We wish to construct N intervals [li, ri] on the real line. The restrictions are of the following form:

  • 1. The ith interval needs to lie entirely to the leħt of the jth interval.
  • 2. The ith and jth interval need to overlap (in more than one point).

Output: A possible set of intervals, or determine if it is impossible.

19

slide-28
SLIDE 28

Problem 7 – Solution

Convert the restrictions into inequalities.

  • The ith interval lies entirely to the leħt of the jth interval ⇐

⇒ ri < lj.

  • The ith and jth interval overlap ⇐

⇒ li < rj and lj < ri (check this!)

  • In addition, the intervals should actually be intervals (i.e. li < ri for every i).

Construct a graph!

  • There is a node for each li and for each ri.
  • For every inequality x

y we need as described above, construct an edge x y. A topological ordering of this graph (if it exists) satisfies the inequalities! If variable xi comes kth in the topological order, give it the value k.

20

slide-29
SLIDE 29

Problem 7 – Solution

Convert the restrictions into inequalities.

  • The ith interval lies entirely to the leħt of the jth interval ⇐

⇒ ri < lj.

  • The ith and jth interval overlap ⇐

⇒ li < rj and lj < ri (check this!)

  • In addition, the intervals should actually be intervals (i.e. li < ri for every i).

Construct a graph!

  • There is a node for each li and for each ri.
  • For every inequality x < y we need as described above, construct an edge x → y.

A topological ordering of this graph (if it exists) satisfies the inequalities! If variable xi comes kth in the topological order, give it the value k.

20

slide-30
SLIDE 30

Problem 7 – Solution

Convert the restrictions into inequalities.

  • The ith interval lies entirely to the leħt of the jth interval ⇐

⇒ ri < lj.

  • The ith and jth interval overlap ⇐

⇒ li < rj and lj < ri (check this!)

  • In addition, the intervals should actually be intervals (i.e. li < ri for every i).

Construct a graph!

  • There is a node for each li and for each ri.
  • For every inequality x < y we need as described above, construct an edge x → y.

A topological ordering of this graph (if it exists) satisfies the inequalities! If variable xi comes kth in the topological order, give it the value k.

20

slide-31
SLIDE 31

Strongly connected components

Problem: given a directed graph, partition it into strongly connected components. Figure: A graph divided into its SCCs. (Wikipedia) Why do this?

  • We can easily answer questions about reachability.
  • The component graph forms a DAG! Lots of tools to work with DAGs.
  • Various applications (e.g. 2-SAT).

21

slide-32
SLIDE 32

Strongly connected components

We can compute SCCs in O(|V| + |E|) time using DFS! Tarjan’s SCC algorithm:

  • 1. Initialize a stack S
  • 2. Pick any unvisited node and start a DFS from it
  • 3. For every newly visited node n in the DFS:
  • assign n an index in order of discovery, and initialize n’s low value to its index
  • push n onto S
  • recurse on n’s unvisited neighbours
  • update n’s low value to the minimum of its neighbours’ low values, but only considering

neighbours that are not yet assigned to an SCC

  • if n.low = n.index, pop nodes from the stack until n is popped. These nodes form an SCC
  • 4. If there are any unvisited nodes leħt, repeat from step 2

22

slide-33
SLIDE 33

Strongly connected components

23

slide-34
SLIDE 34

Strongly connected components

23

slide-35
SLIDE 35

Strongly connected components

23

slide-36
SLIDE 36

Strongly connected components

23

slide-37
SLIDE 37

Strongly connected components

23

slide-38
SLIDE 38

Strongly connected components

23

slide-39
SLIDE 39

Strongly connected components

23

slide-40
SLIDE 40

Strongly connected components

23

slide-41
SLIDE 41

Strongly connected components

23

slide-42
SLIDE 42

Strongly connected components

23

slide-43
SLIDE 43

Strongly connected components

23

slide-44
SLIDE 44

Strongly connected components

23

slide-45
SLIDE 45

Strongly connected components

23

slide-46
SLIDE 46

Strongly connected components

23

slide-47
SLIDE 47

Strongly connected components

23

slide-48
SLIDE 48

Strongly connected components

23

slide-49
SLIDE 49

Strongly connected components

23

slide-50
SLIDE 50

Strongly connected components

23

slide-51
SLIDE 51

Strongly connected components

23

slide-52
SLIDE 52

Strongly connected components

23

slide-53
SLIDE 53

Strongly connected components

23

slide-54
SLIDE 54

Strongly connected components

23