2019-05-09 Graph Review

Exam

1 hr 50 mins

Test-part:

4 problems
10 points total

Nontest-part:

3 problems
30 points total

Graphs

Shortest path algorithms

name edges complexity
BFS length 1
Dijkstra with RB-tree (heap lengths > 0
Dijkstra with array lengths > 0
Bellman-Ford any
Floyd-Warshall any
  • Firsr four: single source
  • Last: all-total paths

Graph Transformations

  1. reverse edges
  2. change edge weights
  3. add nodes
    • dummy nodes
    • split existing nodes
    • add common source

Reverse edges

Input

Graph G

Output

True if G is strongly connected

Steps

  1. pick node v
  2. do DFS/BFS from v
  3. reverse edges
  4. do DFS/BDS from v

Change edge weights

cities problem

A -> C = 60
A -> B = B -> C = 20

extra condition: every transfer takes 1 hour

complexity:

transform the graph, add wait time on the destination

A -> C = 120
A -> B = B -> C = 80

length of a path = travel + 60

Add nodes

Input

  • Graph G with edge length being 1 or 2
  • vertex v

Output

dist[1..n] where dist[u] is the length of the shortest path from v to u

Complexity

Steps

The complexity indicates BFS but BFS doesn't accept weights

  1. introduce dummy node for every edge of length 2
  2. run BFS
  3. discard distances to dummy nodes

new graph

Examples

same problem, edge length are integers from 1 to l, and complexity

Split existing nodes

Train and bus in the last homework

create nodes A_bus, A_train.. and connect

  1. create new graph
  2. run Dijkstra
  3. for each city, choose the min of dist to bus and train stations (O(n))
    • vertices: 2n
    • edges: m + 2n

Add common source

friend infection problem

Idea

  1. Add one more person who is a friend of infected
  2. run BFS from this person
  3. take max(dist) - 1
    • vertices:
    • edges:

你可能感兴趣的:(2019-05-09 Graph Review)