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

cpsc 490 problem solving in computer science
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 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

slide-2
SLIDE 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

slide-3
SLIDE 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

slide-4
SLIDE 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

slide-5
SLIDE 5

Dijkstra’s Algorithm

Graph traversal algorithm which starts at a given node, and explores the other nodes by order

  • f 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

slide-6
SLIDE 6

The Shortest Path Problem for Weighted Graphs: Example (1)

12 9 2 22 3 4 5 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

slide-7
SLIDE 7

The Shortest Path Problem for Weighted Graphs: Example (2)

12 9 2 22 3 4 5 3 4 ∞ ∞ ∞ ∞ ∞ ∞ ∞

Initialize the distance of the source vertex to be 0, and all non-source distances to be ∞.

6

slide-8
SLIDE 8

The Shortest Path Problem for Weighted Graphs: Example (3)

12 9 2 22 3 4 5 3 4 ∞ ∞ ∞ ∞ ∞ ∞ 12

Find the closest node to the source.

7

slide-9
SLIDE 9

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

Find the second-closest node to the source.

8

slide-10
SLIDE 10

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

9

slide-11
SLIDE 11

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

10

slide-12
SLIDE 12

The Shortest Path Problem for Weighted Graphs: Example (7)

12 9 2 22 3 4 5 3 4 14 22 ∞ 19 ∞ 23 12

11

slide-13
SLIDE 13

The Shortest Path Problem for Weighted Graphs: Example (8)

12 9 2 22 3 4 5 3 4 14 22 26 19 26 23 12

12

slide-14
SLIDE 14

Graph Traversals: Dijkstra’s Algorithm Implementation

1

vector<int> dijkstra(vector<vector<int>> &adj,

2

vector<vector<int>> &w, int s) {

3

vector<bool> visited(adj.size());

4

vector<int> dist(n); // shortest distance from s

5

// pair: first is distance (sorted by), second is node

6

priority_queue<pair<int,int>, vector<pair<int,int>>,

7

greater<pair<int,int>>> q; q.push({0, s});

8

while (!q.empty()) {

9

int cur_dist, cur_node;

10

tie(cur_dist,cur_node) = q.top(); q.pop();

11

if (visited[cur_node]) continue; visited[cur_node] = true;

12

dist[cur_node] = cur_dist;

13

for (int i = 0; i < adj[cur_node].size(); ++i)

14

q.emplace(dist[cur_node] + w[i], adj[i]);

15

}

16

}

13

slide-15
SLIDE 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

slide-16
SLIDE 16

Discussion Problem 1 - Insight

Duplicate the graph

  • Every node u becomes nodes u1 and u2
  • Every edge u → v becomes edges u1 → v2 and u2 → v1

Paths from u1 to v1 and from u2 to v2 contain an even number of edges. Paths from u1 to v2 and from u2 to v1 contain an odd number of edges. Run Dijkstra to find the shortest path from s1 to e2.

15

slide-17
SLIDE 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

slide-18
SLIDE 18

All pairs Shortest Paths: Floyd-Warshall

1

initialize a matrix dist [][] to infinity

2

set dist[u][u] to 0 for all nodes u

3

set dist[u][v] to w(u,v) for all edges e = (u,v)

4

for k = 1 to |V|:

5

for i = 1 to |V|:

6

for j = 1 to |V|:

7

dist[i][j] = min(dist[i][j],

8

dist[i][k] + dist[k][j]) 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

slide-19
SLIDE 19

Discussion Problem 2 - Maximin

Lucca is in a country with N cities and M roads connecting them. The country is full of

  • verhead bridges, so each road connecting cities ui and vi has a maximum height hi 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

slide-20
SLIDE 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:

1

dist[i][j] = max(dist[i][j], min(dist[i][k], dist[k][j]))

19

slide-21
SLIDE 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

slide-22
SLIDE 22

Union-Find Example: Initial state

1 F=1 2 F=2 3 F=3 4 F=4 5 F=5 6 F=6

21

slide-23
SLIDE 23

Union-Find Example: Union(1, 2)

1 F=1 2 F=1 3 F=3 4 F=4 5 F=5 6 F=6

22

slide-24
SLIDE 24

Union-Find Example: Union(4, 5)

1 F=1 2 F=1 3 F=3 4 F=4 5 F=5 6 F=6

23

slide-25
SLIDE 25

Union-Find Example: Union(3, 6)

1 F=1 2 F=1 3 F=6 4 F=4 5 F=4 6 F=6

24

slide-26
SLIDE 26

Union-Find Example: Union(2, 3)

1 F=1 2 F=1 3 F=1 4 F=4 5 F=4 6 F=1

25

slide-27
SLIDE 27

Union-Find: Find Function

1

vector<int> parent;

2

// in an initial state, parent[i] = i for every i

3 4

int find(int x) {

5

if (parent[x] != x) {

6

parent[x] = find(parent[x]); // path-compression

7

}

8

return parent[x];

9

}

26

slide-28
SLIDE 28

Union-Find: Union by Rank (Join) Function

1

vector<int> parent;

2

vector<int> rnk; // rank -- initialized to all 0s

3 4

void join(int a, int b) {

5

// we are only interested in the representatives

6

a = find(a); b = find(b);

7

if (a == b) return; // already joined

8 9

if (rank[a] > rank[b]) parent[b] = a;

10

else if (rank[b] > rank[a]) parent[a] = b;

11

else {

12

parent[b] = a;

13

rank[a]++;

14

}

15

}

27

slide-29
SLIDE 29

Topological Sort

Remember this graph?

CPSC 110 CPSC 310 CPSC 213 CPSC 221 CPSC 210 CPSC 121 CPSC 320 CPSC 420 CPSC 313 CPSC 415 CPSC 410

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

slide-30
SLIDE 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

slide-31
SLIDE 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

slide-32
SLIDE 32

Topological Sort - Intuition

Can you start with a task with in-degree1 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.

1in-degree of a node is the number of edges that arrive at that node.

31

slide-33
SLIDE 33

Topological Sort - Possible Starting Nodes

32

slide-34
SLIDE 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

slide-35
SLIDE 35

Topological Sort - Completing a Task

34

slide-36
SLIDE 36

Topological Sort - Completing a Task

35

slide-37
SLIDE 37

Topological Sort - Intuition

What do we do now? Repeat until all tasks are completed!

36

slide-38
SLIDE 38

Topological Sort - Possible Starting Nodes

37

slide-39
SLIDE 39

Topological Sort - Algorithm Idea

While there are still nodes in the graph:

  • Pick a node with in-degree 0.
  • Add it to the sequence of tasks completed.
  • Remove it from the graph.
  • More practically, this means marking the node as “visited”.
  • Instead of deleting the edges in memory, you can just decrease the count of in-degree of all

its successors.

38

slide-40
SLIDE 40

Topological Sort - Pseudocode

1

indegree = array of length V, initialized to 0

2

for each edge u to 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 a BFS!

39

slide-41
SLIDE 41

Discussion Problem 3 - All Aboard

Input: There are n different classes of seats on a train. Each class has ai seats, which are assigned on a first-come, first-server basis. m groups of passengers show up, one after another. The jth group, with aj people, will only purchase seats for class ≥ cj, but they don’t mind being seated in multiple classes. Output: Assuming seat assignment is always optimal (everyone is assigned to the smallest class available within their contraint), what is the first group of people that the train cannot accommodate? Constraint: 1 < n, m < 107, 1 ≤ cj ≤ n.

40

slide-42
SLIDE 42

Discussion Problem 3 - Insight

The naive solution here is: For each group, linearly try every class starting from cj until we either run out, or find enough seats. However, this is worst-case linear time per group. In order to do better, we can use Union-Find. Starting with a disjoint set for each class, we will merge sets whenever we run out of seats. We can also store two pieces of additional information in every root:

  • The highest class in each set
  • The number of remaining seats.

41