cpsc 490 – problem solving in computer sciencecs-490/2016w2/lectures/...bellman-ford algorithm...

Post on 16-Dec-2020

4 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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

Shortest Path

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

1

Shortest Path

Shortest path length is 2 but Dijkstra outputs 3.

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

2

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

Who wants to get rich?

3

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

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

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

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

Bellman-Ford – Extensions

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

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

7

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

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

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

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

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

Moving on from Shortest Paths...

12

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

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

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

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

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

Preorder and Postorder Traversals

Think about what it means when

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

18

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

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

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

Cycle Detection – Directed

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

21

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

Black Grey White DFS

23

Black Grey White DFS

23

Black Grey White DFS

23

Black Grey White DFS

23

Black Grey White DFS

23

Black Grey White DFS

23

Black Grey White DFS

23

Other Uses of DFS

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

24

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

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

End of material for Assignment 1

26

Next Class

Dynamic Programming

27

top related