39
CPSC 490 – Problem Solving in Computer Science Lecture 4: Bellman-Ford, Floyd-Warshall, DFS Jason Chiu and Raunak Kumar Based on slides by Paul Liu (2014) 2017/01/11 University of British Columbia

CPSC 490 – Problem Solving in Computer Sciencecs-490/2016W2/lectures/...Bellman-Ford Algorithm Calculatestheshortestpathfromstartnodestoeveryothernode. 1 initialize a vector dist[]

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CPSC 490 – Problem Solving in Computer Sciencecs-490/2016W2/lectures/...Bellman-Ford Algorithm Calculatestheshortestpathfromstartnodestoeveryothernode. 1 initialize a vector dist[]

CPSC 490 – Problem Solving inComputer ScienceLecture 4: Bellman-Ford, Floyd-Warshall, DFS

Jason Chiu and Raunak KumarBased on slides by Paul Liu (2014)2017/01/11

University of British Columbia

Page 2: CPSC 490 – Problem Solving in Computer Sciencecs-490/2016W2/lectures/...Bellman-Ford Algorithm Calculatestheshortestpathfromstartnodestoeveryothernode. 1 initialize a vector dist[]

Shortest Path

What is the shortest path between A and C? What is output ofDijkstra’s algorithm in the following graph?

1

Page 3: CPSC 490 – Problem Solving in Computer Sciencecs-490/2016W2/lectures/...Bellman-Ford Algorithm Calculatestheshortestpathfromstartnodestoeveryothernode. 1 initialize a vector dist[]

Shortest Path

Shortest path length is 2 but Dijkstra outputs 3.

Dijkstra doesn’t work on graph with negative edge weights!

2

Page 4: CPSC 490 – Problem Solving in Computer Sciencecs-490/2016W2/lectures/...Bellman-Ford Algorithm Calculatestheshortestpathfromstartnodestoeveryothernode. 1 initialize a vector dist[]

Bellman-Ford Algorithm

Calculates the shortest path from start node s to every other node.

1 initialize a vector dist[] to infinity2 set dist[s] = 03 for |V|-1 iterations:4 for each edge u->v in E:5 dist[v] = min(dist[v], dist[u] + w(u,v))

Time complexity: O(VE)

This doesn’t work if the graph has a negative weight cycle. But it’seasy to modify the algorithm to detect negative weight cycles.

3

Page 5: CPSC 490 – Problem Solving in Computer Sciencecs-490/2016W2/lectures/...Bellman-Ford Algorithm Calculatestheshortestpathfromstartnodestoeveryothernode. 1 initialize a vector dist[]

Who wants to get rich?

3

Page 6: CPSC 490 – Problem Solving in Computer Sciencecs-490/2016W2/lectures/...Bellman-Ford Algorithm Calculatestheshortestpathfromstartnodestoeveryothernode. 1 initialize a vector dist[]

Bellman-Ford Application – Arbitrage

Suppose we had currencies in CAD, USD and EUR, and the exchangerates are as follows:

• 1 CAD = 0.85 USD• 1 USD = 0.95 EUR• 1 EUR = 1.45 CAD

How can we become rich?

0.85 · 0.95 · 1.45 = 1.17 > 1. So 1 CADconverts to more than 1 CAD!

4

Page 7: CPSC 490 – Problem Solving in Computer Sciencecs-490/2016W2/lectures/...Bellman-Ford Algorithm Calculatestheshortestpathfromstartnodestoeveryothernode. 1 initialize a vector dist[]

Bellman-Ford Application – Arbitrage

Suppose we had currencies in CAD, USD and EUR, and the exchangerates are as follows:

• 1 CAD = 0.85 USD• 1 USD = 0.95 EUR• 1 EUR = 1.45 CAD

How can we become rich? 0.85 · 0.95 · 1.45 = 1.17 > 1. So 1 CADconverts to more than 1 CAD!

4

Page 8: CPSC 490 – Problem Solving in Computer Sciencecs-490/2016W2/lectures/...Bellman-Ford Algorithm Calculatestheshortestpathfromstartnodestoeveryothernode. 1 initialize a vector dist[]

Bellman-Ford Problem 1

Input: You are a bank, and you have a set of currencies and theirconversion rates.

Output: Determine if arbitrage is possible.

Constraints: At most 100 currencies.

5

Page 9: CPSC 490 – Problem Solving in Computer Sciencecs-490/2016W2/lectures/...Bellman-Ford Algorithm Calculatestheshortestpathfromstartnodestoeveryothernode. 1 initialize a vector dist[]

Bellman-Ford Problem 1 – Solution

• Make node for each currency.• Add an edge between each pair of nodes with weight− log(conversion rate).

• Determine if there is a negative weight cycle in this graph.

6

Page 10: CPSC 490 – Problem Solving in Computer Sciencecs-490/2016W2/lectures/...Bellman-Ford Algorithm Calculatestheshortestpathfromstartnodestoeveryothernode. 1 initialize a vector dist[]

Bellman-Ford – Extensions

Solve a system of inequalities with Bellman-Ford (see notes).

Shortest Path Faster Algorithm: Bellman-Ford implemented like BFS!

7

Page 11: CPSC 490 – Problem Solving in Computer Sciencecs-490/2016W2/lectures/...Bellman-Ford Algorithm Calculatestheshortestpathfromstartnodestoeveryothernode. 1 initialize a vector dist[]

BFS

1 initialize a queue Q2 initialize a vector dist to infinity3 set dist[s] = 04 push s onto Q5 while Q is not empty:6 pop the topmost element u from Q7 for each edge u->v:8 if dist[v] > dist[u] + 19 dist[v] = dist[u] + 110 push v onto Q

8

Page 12: CPSC 490 – Problem Solving in Computer Sciencecs-490/2016W2/lectures/...Bellman-Ford Algorithm Calculatestheshortestpathfromstartnodestoeveryothernode. 1 initialize a vector dist[]

Shortest Path Faster Algorithm (SPFA)

1 initialize a queue Q2 initialize a vector dist to infinity3 set dist[s] = 04 push s onto Q5 while Q is not empty:6 pop the topmost element u from Q7 for each edge u->v:8 if dist[v] > dist[u] + wt(u, v)9 dist[v] = dist[u] + wt(u, v)10 push v onto Q

9

Page 13: CPSC 490 – Problem Solving in Computer Sciencecs-490/2016W2/lectures/...Bellman-Ford Algorithm Calculatestheshortestpathfromstartnodestoeveryothernode. 1 initialize a vector dist[]

Shortest Paths Again

Given a weighted graph G, we know how to find the shortest pathbetween s and e.

• Non-negative edge weights: Dijkstra• Negative edge weights: Bellman-Ford

What if we want the shortest path between all pairs of vertices?

10

Page 14: CPSC 490 – Problem Solving in Computer Sciencecs-490/2016W2/lectures/...Bellman-Ford Algorithm Calculatestheshortestpathfromstartnodestoeveryothernode. 1 initialize a vector dist[]

Shortest Paths Again

We could just run Dijkstra or Bellman-Ford multiple times. What’sthe time complexity?

• Dijkstra: O(V2 + VE log V)• Bellman-Ford: O(V2E)

11

Page 15: CPSC 490 – Problem Solving in Computer Sciencecs-490/2016W2/lectures/...Bellman-Ford Algorithm Calculatestheshortestpathfromstartnodestoeveryothernode. 1 initialize a vector dist[]

Floyd-Warshall Algorithm – All Pairs Shortest Paths

1 initialize a matrix dist[][] to infinity2 set dist[u][u] to 0 for all nodes u3 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(V3).

Runs very fast in practice because of tightly nested loops.

12

Page 16: CPSC 490 – Problem Solving in Computer Sciencecs-490/2016W2/lectures/...Bellman-Ford Algorithm Calculatestheshortestpathfromstartnodestoeveryothernode. 1 initialize a vector dist[]

Moving on from Shortest Paths...

12

Page 17: CPSC 490 – Problem Solving in Computer Sciencecs-490/2016W2/lectures/...Bellman-Ford Algorithm Calculatestheshortestpathfromstartnodestoeveryothernode. 1 initialize a vector dist[]

Depth First Search (DFS)

Another graph traversal algorithm like BFS but traverses the nodes ina different order.

• Easier to code than BFS• Particularly useful while traversing trees due to recursion (intrees and DFS)

13

Page 18: CPSC 490 – Problem Solving in Computer Sciencecs-490/2016W2/lectures/...Bellman-Ford Algorithm Calculatestheshortestpathfromstartnodestoeveryothernode. 1 initialize a vector dist[]

DFS Algorithm

1 DFS(s):2 mark s as visited:3 for each edge e = (s,t):4 if t is not visited, call DFS(t)

Time complexity: O(V+ E)

14

Page 19: CPSC 490 – Problem Solving in Computer Sciencecs-490/2016W2/lectures/...Bellman-Ford Algorithm Calculatestheshortestpathfromstartnodestoeveryothernode. 1 initialize a vector dist[]

Preorder and Postorder Traversals

If the underlying graph is a tree, DFS can compute useful statistics.

• Preorder numbers• Postorder numbers

See notes on web page for bridge detection and strongly connectedcomponents which make use of these statistics.

15

Page 20: CPSC 490 – Problem Solving in Computer Sciencecs-490/2016W2/lectures/...Bellman-Ford Algorithm Calculatestheshortestpathfromstartnodestoeveryothernode. 1 initialize a vector dist[]

Preorder Traversals

1 initialize a counter c = 02 DFS(s):3 mark s as visited4 set preorder[s] = c and increment c5 for each edge e = (s,t);6 if t is not visited, call DFS(t)

16

Page 21: CPSC 490 – Problem Solving in Computer Sciencecs-490/2016W2/lectures/...Bellman-Ford Algorithm Calculatestheshortestpathfromstartnodestoeveryothernode. 1 initialize a vector dist[]

Postorder Traversals

1 initialize a counter c = 02 DFS(s):3 mark s as visited4 for each edge e = (s,t);5 if t is not visited, call DFS(t)6 set postorder[s] = c and increment c

17

Page 22: CPSC 490 – Problem Solving in Computer Sciencecs-490/2016W2/lectures/...Bellman-Ford Algorithm Calculatestheshortestpathfromstartnodestoeveryothernode. 1 initialize a vector dist[]

Preorder and Postorder Traversals

Think about what it means when

• When preorder(a) < preorder(b)• When postorder(a) > postorder(b)

18

Page 23: CPSC 490 – Problem Solving in Computer Sciencecs-490/2016W2/lectures/...Bellman-Ford Algorithm Calculatestheshortestpathfromstartnodestoeveryothernode. 1 initialize a vector dist[]

Cycle Detection – Undirected

We can just run DFS.

1 DFS(s):2 mark s as visited3 for each edge e = (s,t):4 if t is not visited, call DFS(t)5 if t is visited and t is not the parent of s,6 output HAS_CYCLE7 output NO_CYCLE

19

Page 24: CPSC 490 – Problem Solving in Computer Sciencecs-490/2016W2/lectures/...Bellman-Ford Algorithm Calculatestheshortestpathfromstartnodestoeveryothernode. 1 initialize a vector dist[]

Cycle Detection – Directed

Can we just use normal DFS?

Normal DFS finds A to C to D path, marks D as visited, finds A to B toD path and outputs HAS_CYCLE. But there is no directed cycle in thisgraph!

20

Page 25: CPSC 490 – Problem Solving in Computer Sciencecs-490/2016W2/lectures/...Bellman-Ford Algorithm Calculatestheshortestpathfromstartnodestoeveryothernode. 1 initialize a vector dist[]

Cycle Detection – Directed

Can we just use normal DFS?

Normal DFS finds A to C to D path, marks D as visited, finds A to B toD path and outputs HAS_CYCLE. But there is no directed cycle in thisgraph!

20

Page 26: CPSC 490 – Problem Solving in Computer Sciencecs-490/2016W2/lectures/...Bellman-Ford Algorithm Calculatestheshortestpathfromstartnodestoeveryothernode. 1 initialize a vector dist[]

Cycle Detection – Directed

How do we modify DFS to detect cycles in directed graphs?

21

Page 27: CPSC 490 – Problem Solving in Computer Sciencecs-490/2016W2/lectures/...Bellman-Ford Algorithm Calculatestheshortestpathfromstartnodestoeveryothernode. 1 initialize a vector dist[]

Black Grey White DFS

1 initialize the color of all nodes as WHITE2 DFS(s):3 mark s as GREY4 for each edge e = (s,t):5 if t is WHITE, call DFS(t)6 if t is GREY, output HAS_CYCLE7 if t is BLACK, do nothing8 mark s as BLACK9 output NO_CYCLE

22

Page 28: CPSC 490 – Problem Solving in Computer Sciencecs-490/2016W2/lectures/...Bellman-Ford Algorithm Calculatestheshortestpathfromstartnodestoeveryothernode. 1 initialize a vector dist[]

Black Grey White DFS

23

Page 29: CPSC 490 – Problem Solving in Computer Sciencecs-490/2016W2/lectures/...Bellman-Ford Algorithm Calculatestheshortestpathfromstartnodestoeveryothernode. 1 initialize a vector dist[]

Black Grey White DFS

23

Page 30: CPSC 490 – Problem Solving in Computer Sciencecs-490/2016W2/lectures/...Bellman-Ford Algorithm Calculatestheshortestpathfromstartnodestoeveryothernode. 1 initialize a vector dist[]

Black Grey White DFS

23

Page 31: CPSC 490 – Problem Solving in Computer Sciencecs-490/2016W2/lectures/...Bellman-Ford Algorithm Calculatestheshortestpathfromstartnodestoeveryothernode. 1 initialize a vector dist[]

Black Grey White DFS

23

Page 32: CPSC 490 – Problem Solving in Computer Sciencecs-490/2016W2/lectures/...Bellman-Ford Algorithm Calculatestheshortestpathfromstartnodestoeveryothernode. 1 initialize a vector dist[]

Black Grey White DFS

23

Page 33: CPSC 490 – Problem Solving in Computer Sciencecs-490/2016W2/lectures/...Bellman-Ford Algorithm Calculatestheshortestpathfromstartnodestoeveryothernode. 1 initialize a vector dist[]

Black Grey White DFS

23

Page 34: CPSC 490 – Problem Solving in Computer Sciencecs-490/2016W2/lectures/...Bellman-Ford Algorithm Calculatestheshortestpathfromstartnodestoeveryothernode. 1 initialize a vector dist[]

Black Grey White DFS

23

Page 35: CPSC 490 – Problem Solving in Computer Sciencecs-490/2016W2/lectures/...Bellman-Ford Algorithm Calculatestheshortestpathfromstartnodestoeveryothernode. 1 initialize a vector dist[]

Other Uses of DFS

• Strongly-connected component, 2SAT• Bi-connected component, bridge detection, articulation points

24

Page 36: CPSC 490 – Problem Solving in Computer Sciencecs-490/2016W2/lectures/...Bellman-Ford Algorithm Calculatestheshortestpathfromstartnodestoeveryothernode. 1 initialize a vector dist[]

Strongly Connected Component (SCC)

Given a directed graph...

• u and v are strongly connected if there are paths u→ v, v→ u• Graph is strongly connected if every pair of nodes are.

We can divide up a graph into SCCs in O(V+ E) time with DFS!

Applications: 2-SAT, etc.

Figure 1: A graph divided into its SCC’s (Wikipedia)

25

Page 37: CPSC 490 – Problem Solving in Computer Sciencecs-490/2016W2/lectures/...Bellman-Ford Algorithm Calculatestheshortestpathfromstartnodestoeveryothernode. 1 initialize a vector dist[]

Biconnected Component (BCC)

A (usually undirected) graph is biconnected if removing a vertexdoes not disconnect the graph.

We can divide up a graph into BCCs in O(V+ E) time with DFS!The same DFS algorithms allows us to find

• Articulation point: vertex that disconnects graph if removed• Bridge: edge that disconnects graph if removed

Figure 2: A graph divided into its BCC’s (Wikipedia)26

Page 38: CPSC 490 – Problem Solving in Computer Sciencecs-490/2016W2/lectures/...Bellman-Ford Algorithm Calculatestheshortestpathfromstartnodestoeveryothernode. 1 initialize a vector dist[]

End of material for Assignment 1

26

Page 39: CPSC 490 – Problem Solving in Computer Sciencecs-490/2016W2/lectures/...Bellman-Ford Algorithm Calculatestheshortestpathfromstartnodestoeveryothernode. 1 initialize a vector dist[]

Next Class

Dynamic Programming

27